1 (function ( window, document ) {
4 var supportedBrowser = ( document.querySelector && window.addEventListener ),
10 function sendEmbedMessage( message, value ) {
11 window.parent.postMessage( {
24 var share_dialog = document.querySelector( '.wp-embed-share-dialog' ),
25 share_dialog_open = document.querySelector( '.wp-embed-share-dialog-open' ),
26 share_dialog_close = document.querySelector( '.wp-embed-share-dialog-close' ),
27 share_input = document.querySelectorAll( '.wp-embed-share-input' ),
28 share_dialog_tabs = document.querySelectorAll( '.wp-embed-share-tab-button button' ),
29 links = document.getElementsByTagName( 'a' ),
33 for ( i = 0; i < share_input.length; i++ ) {
34 share_input[ i ].addEventListener( 'click', function ( e ) {
40 function openSharingDialog() {
41 share_dialog.className = share_dialog.className.replace( 'hidden', '' );
42 // Initial focus should go on the currently selected tab in the dialog.
43 document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' ).focus();
46 function closeSharingDialog() {
47 share_dialog.className += ' hidden';
48 document.querySelector( '.wp-embed-share-dialog-open' ).focus();
51 if ( share_dialog_open ) {
52 share_dialog_open.addEventListener( 'click', function () {
57 if ( share_dialog_close ) {
58 share_dialog_close.addEventListener( 'click', function () {
63 function shareClickHandler( e ) {
64 var currentTab = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
65 currentTab.setAttribute( 'aria-selected', 'false' );
66 document.querySelector( '#' + currentTab.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
68 e.target.setAttribute( 'aria-selected', 'true' );
69 document.querySelector( '#' + e.target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
72 function shareKeyHandler( e ) {
73 var target = e.target,
74 previousSibling = target.parentElement.previousElementSibling,
75 nextSibling = target.parentElement.nextElementSibling,
78 if ( 37 === e.keyCode ) {
79 newTab = previousSibling;
80 } else if ( 39 === e.keyCode ) {
86 if ( 'rtl' === document.documentElement.getAttribute( 'dir' ) ) {
87 newTab = ( newTab === previousSibling ) ? nextSibling : previousSibling;
91 newTabChild = newTab.firstElementChild;
93 target.setAttribute( 'tabindex', '-1' );
94 target.setAttribute( 'aria-selected', false );
95 document.querySelector( '#' + target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
97 newTabChild.setAttribute( 'tabindex', '0' );
98 newTabChild.setAttribute( 'aria-selected', 'true' );
100 document.querySelector( '#' + newTabChild.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
104 if ( share_dialog_tabs ) {
105 for ( i = 0; i < share_dialog_tabs.length; i++ ) {
106 share_dialog_tabs[ i ].addEventListener( 'click', shareClickHandler );
108 share_dialog_tabs[ i ].addEventListener( 'keydown', shareKeyHandler );
112 document.addEventListener( 'keydown', function ( e ) {
113 if ( 27 === e.keyCode && -1 === share_dialog.className.indexOf( 'hidden' ) ) {
114 closeSharingDialog();
115 } else if ( 9 === e.keyCode ) {
116 constrainTabbing( e );
120 function constrainTabbing( e ) {
121 // Need to re-get the selected tab each time.
122 var firstFocusable = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
124 if ( share_dialog_close === e.target && ! e.shiftKey ) {
125 firstFocusable.focus();
127 } else if ( firstFocusable === e.target && e.shiftKey ) {
128 share_dialog_close.focus();
133 if ( window.self === window.top ) {
138 * Send this document's height to the parent (embedding) site.
140 sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
143 * Detect clicks to external (_top) links.
145 function linkClickHandler( e ) {
146 var target = e.target,
148 if ( target.hasAttribute( 'href' ) ) {
149 href = target.getAttribute( 'href' );
151 href = target.parentElement.getAttribute( 'href' );
155 * Send link target to the parent (embedding) site.
157 sendEmbedMessage( 'link', href );
161 for ( i = 0; i < links.length; i++ ) {
162 links[ i ].addEventListener( 'click', linkClickHandler );
167 * Iframe resize handler.
169 function onResize() {
170 if ( window.self === window.top ) {
174 clearTimeout( resizing );
176 resizing = setTimeout( function () {
177 sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
182 * Re-get the secret when it was added later on.
184 function getSecret() {
185 if ( window.self === window.top || !!secret ) {
189 secret = window.location.hash.replace( /.*secret=([\d\w]{10}).*/, '$1' );
191 clearTimeout( secretTimeout );
193 secretTimeout = setTimeout( function () {
198 if ( supportedBrowser ) {
200 document.documentElement.className = document.documentElement.className.replace( /\bno-js\b/, '' ) + ' js';
201 document.addEventListener( 'DOMContentLoaded', onLoad, false );
202 window.addEventListener( 'load', onLoad, false );
203 window.addEventListener( 'resize', onResize, false );
205 })( window, document );