wp-embed-template.js
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(function ( window, document ) {
'use strict';
var supportedBrowser = ( document.querySelector && window.addEventListener ),
loaded = false,
secret,
secretTimeout,
resizing;
function sendEmbedMessage( message, value ) {
window.parent.postMessage( {
message: message,
value: value,
secret: secret
}, '*' );
}
function onLoad() {
if ( loaded ) {
return;
}
loaded = true;
var share_dialog = document.querySelector( '.wp-embed-share-dialog' ),
share_dialog_open = document.querySelector( '.wp-embed-share-dialog-open' ),
share_dialog_close = document.querySelector( '.wp-embed-share-dialog-close' ),
share_input = document.querySelectorAll( '.wp-embed-share-input' ),
share_dialog_tabs = document.querySelectorAll( '.wp-embed-share-tab-button button' ),
featured_image = document.querySelector( '.wp-embed-featured-image img' ),
i;
if ( share_input ) {
for ( i = 0; i < share_input.length; i++ ) {
share_input[ i ].addEventListener( 'click', function ( e ) {
e.target.select();
} );
}
}
function openSharingDialog() {
share_dialog.className = share_dialog.className.replace( 'hidden', '' );
// Initial focus should go on the currently selected tab in the dialog.
document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' ).focus();
}
function closeSharingDialog() {
share_dialog.className += ' hidden';
document.querySelector( '.wp-embed-share-dialog-open' ).focus();
}
if ( share_dialog_open ) {
share_dialog_open.addEventListener( 'click', function () {
openSharingDialog();
} );
}
if ( share_dialog_close ) {
share_dialog_close.addEventListener( 'click', function () {
closeSharingDialog();
} );
}
function shareClickHandler( e ) {
var currentTab = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
currentTab.setAttribute( 'aria-selected', 'false' );
document.querySelector( '#' + currentTab.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
e.target.setAttribute( 'aria-selected', 'true' );
document.querySelector( '#' + e.target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
}
function shareKeyHandler( e ) {
var target = e.target,
previousSibling = target.parentElement.previousElementSibling,
nextSibling = target.parentElement.nextElementSibling,
newTab, newTabChild;
if ( 37 === e.keyCode ) {
newTab = previousSibling;
} else if ( 39 === e.keyCode ) {
newTab = nextSibling;
} else {
return false;
}
if ( 'rtl' === document.documentElement.getAttribute( 'dir' ) ) {
newTab = ( newTab === previousSibling ) ? nextSibling : previousSibling;
}
if ( newTab ) {
newTabChild = newTab.firstElementChild;
target.setAttribute( 'tabindex', '-1' );
target.setAttribute( 'aria-selected', false );
document.querySelector( '#' + target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
newTabChild.setAttribute( 'tabindex', '0' );
newTabChild.setAttribute( 'aria-selected', 'true' );
newTabChild.focus();
document.querySelector( '#' + newTabChild.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
}
}
if ( share_dialog_tabs ) {
for ( i = 0; i < share_dialog_tabs.length; i++ ) {
share_dialog_tabs[ i ].addEventListener( 'click', shareClickHandler );
share_dialog_tabs[ i ].addEventListener( 'keydown', shareKeyHandler );
}
}
document.addEventListener( 'keydown', function ( e ) {
if ( 27 === e.keyCode && -1 === share_dialog.className.indexOf( 'hidden' ) ) {
closeSharingDialog();
} else if ( 9 === e.keyCode ) {
constrainTabbing( e );
}
}, false );
function constrainTabbing( e ) {
// Need to re-get the selected tab each time.
var firstFocusable = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
if ( share_dialog_close === e.target && ! e.shiftKey ) {
firstFocusable.focus();
e.preventDefault();
} else if ( firstFocusable === e.target && e.shiftKey ) {
share_dialog_close.focus();
e.preventDefault();
}
}
if ( window.self === window.top ) {
return;
}
/**
* Send this document's height to the parent (embedding) site.
*/
sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
// Send the document's height again after the featured image has been loaded.
if ( featured_image ) {
featured_image.addEventListener( 'load', function() {
sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
} );
}
/**
* Detect clicks to external (_top) links.
*/
function linkClickHandler( e ) {
var target = e.target,
href;
if ( target.hasAttribute( 'href' ) ) {
href = target.getAttribute( 'href' );
} else {
href = target.parentElement.getAttribute( 'href' );
}
/**
* Send link target to the parent (embedding) site.
*/
if ( href ) {
sendEmbedMessage( 'link', href );
e.preventDefault();
}
}
document.addEventListener( 'click', linkClickHandler );
}
/**
* Iframe resize handler.
*/
function onResize() {
if ( window.self === window.top ) {
return;
}
clearTimeout( resizing );
resizing = setTimeout( function () {
sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
}, 100 );
}
/**
* Re-get the secret when it was added later on.
*/
function getSecret() {
if ( window.self === window.top || !!secret ) {
return;
}
secret = window.location.hash.replace( /.*secret=([\d\w]{10}).*/, '$1' );
clearTimeout( secretTimeout );
secretTimeout = setTimeout( function () {
getSecret();
}, 100 );
}
if ( supportedBrowser ) {
getSecret();
document.documentElement.className = document.documentElement.className.replace( /\bno-js\b/, '' ) + ' js';
document.addEventListener( 'DOMContentLoaded', onLoad, false );
window.addEventListener( 'load', onLoad, false );
window.addEventListener( 'resize', onResize, false );
}
})( window, document );