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' ),
32 for ( i = 0; i < share_input.length; i++ ) {
33 share_input[ i ].addEventListener( 'click', function ( e ) {
39 function openSharingDialog() {
40 share_dialog.className = share_dialog.className.replace( 'hidden', '' );
41 // Initial focus should go on the currently selected tab in the dialog.
42 document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' ).focus();
45 function closeSharingDialog() {
46 share_dialog.className += ' hidden';
47 document.querySelector( '.wp-embed-share-dialog-open' ).focus();
50 if ( share_dialog_open ) {
51 share_dialog_open.addEventListener( 'click', function () {
56 if ( share_dialog_close ) {
57 share_dialog_close.addEventListener( 'click', function () {
62 function shareClickHandler( e ) {
63 var currentTab = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
64 currentTab.setAttribute( 'aria-selected', 'false' );
65 document.querySelector( '#' + currentTab.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
67 e.target.setAttribute( 'aria-selected', 'true' );
68 document.querySelector( '#' + e.target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
71 function shareKeyHandler( e ) {
72 var target = e.target,
73 previousSibling = target.parentElement.previousElementSibling,
74 nextSibling = target.parentElement.nextElementSibling,
77 if ( 37 === e.keyCode ) {
78 newTab = previousSibling;
79 } else if ( 39 === e.keyCode ) {
85 if ( 'rtl' === document.documentElement.getAttribute( 'dir' ) ) {
86 newTab = ( newTab === previousSibling ) ? nextSibling : previousSibling;
90 newTabChild = newTab.firstElementChild;
92 target.setAttribute( 'tabindex', '-1' );
93 target.setAttribute( 'aria-selected', false );
94 document.querySelector( '#' + target.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'true' );
96 newTabChild.setAttribute( 'tabindex', '0' );
97 newTabChild.setAttribute( 'aria-selected', 'true' );
99 document.querySelector( '#' + newTabChild.getAttribute( 'aria-controls' ) ).setAttribute( 'aria-hidden', 'false' );
103 if ( share_dialog_tabs ) {
104 for ( i = 0; i < share_dialog_tabs.length; i++ ) {
105 share_dialog_tabs[ i ].addEventListener( 'click', shareClickHandler );
107 share_dialog_tabs[ i ].addEventListener( 'keydown', shareKeyHandler );
111 document.addEventListener( 'keydown', function ( e ) {
112 if ( 27 === e.keyCode && -1 === share_dialog.className.indexOf( 'hidden' ) ) {
113 closeSharingDialog();
114 } else if ( 9 === e.keyCode ) {
115 constrainTabbing( e );
119 function constrainTabbing( e ) {
120 // Need to re-get the selected tab each time.
121 var firstFocusable = document.querySelector( '.wp-embed-share-tab-button [aria-selected="true"]' );
123 if ( share_dialog_close === e.target && ! e.shiftKey ) {
124 firstFocusable.focus();
126 } else if ( firstFocusable === e.target && e.shiftKey ) {
127 share_dialog_close.focus();
132 if ( window.self === window.top ) {
137 * Send this document's height to the parent (embedding) site.
139 sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
142 * Detect clicks to external (_top) links.
144 function linkClickHandler( e ) {
145 var target = e.target,
147 if ( target.hasAttribute( 'href' ) ) {
148 href = target.getAttribute( 'href' );
150 href = target.parentElement.getAttribute( 'href' );
154 * Send link target to the parent (embedding) site.
157 sendEmbedMessage( 'link', href );
162 document.addEventListener( 'click', linkClickHandler );
166 * Iframe resize handler.
168 function onResize() {
169 if ( window.self === window.top ) {
173 clearTimeout( resizing );
175 resizing = setTimeout( function () {
176 sendEmbedMessage( 'height', Math.ceil( document.body.getBoundingClientRect().height ) );
181 * Re-get the secret when it was added later on.
183 function getSecret() {
184 if ( window.self === window.top || !!secret ) {
188 secret = window.location.hash.replace( /.*secret=([\d\w]{10}).*/, '$1' );
190 clearTimeout( secretTimeout );
192 secretTimeout = setTimeout( function () {
197 if ( supportedBrowser ) {
199 document.documentElement.className = document.documentElement.className.replace( /\bno-js\b/, '' ) + ' js';
200 document.addEventListener( 'DOMContentLoaded', onLoad, false );
201 window.addEventListener( 'load', onLoad, false );
202 window.addEventListener( 'resize', onResize, false );
204 })( window, document );