1 /* global plugininstallL10n, tb_click, tb_remove */
3 /* Plugin Browser Thickbox related JS*/
5 jQuery( document ).ready( function( $ ) {
14 tb_position = function() {
15 var width = $( window ).width(),
16 H = $( window ).height() - ( ( 792 < width ) ? 60 : 20 ),
17 W = ( 792 < width ) ? 772 : width - 20;
19 tbWindow = $( '#TB_window' );
21 if ( tbWindow.length ) {
22 tbWindow.width( W ).height( H );
23 $( '#TB_iframeContent' ).width( W ).height( H );
25 'margin-left': '-' + parseInt( ( W / 2 ), 10 ) + 'px'
27 if ( typeof document.body.style.maxWidth !== 'undefined' ) {
35 return $( 'a.thickbox' ).each( function() {
36 var href = $( this ).attr( 'href' );
40 href = href.replace( /&width=[0-9]+/g, '' );
41 href = href.replace( /&height=[0-9]+/g, '' );
42 $(this).attr( 'href', href + '&width=' + W + '&height=' + ( H ) );
46 $( window ).resize( function() {
51 * Custom events: when a Thickbox iframe has loaded and when the Thickbox
52 * modal gets removed from the DOM.
55 .on( 'thickbox:iframe:loaded', tbWindow, function() {
58 .on( 'thickbox:removed', function() {
59 // Set focus back to the element that opened the modal dialog.
60 // Note: IE 8 would need this wrapped in a fake setTimeout `0`.
61 $focusedBefore.focus();
64 function iframeLoaded() {
65 var $iframe = tbWindow.find( '#TB_iframeContent' );
67 // Get the iframe body.
68 $iframeBody = $iframe.contents().find( 'body' );
70 // Get the tabbable elements and handle the keydown event on first load.
73 // Set initial focus on the "Close" button.
74 $firstTabbable.focus();
77 * When the "Install" button is disabled (e.g. the Plugin is already installed)
78 * then we can't predict where the last focusable element is. We need to get
79 * the tabbable elements and handle the keydown event again and again,
80 * each time the active tab panel changes.
82 $( '#plugin-information-tabs a', $iframeBody ).on( 'click', function() {
86 // Close the modal when pressing Escape.
87 $iframeBody.on( 'keydown', function( event ) {
88 if ( 27 !== event.which ) {
96 * Get the tabbable elements and detach/attach the keydown event.
97 * Called after the iframe has fully loaded so we have all the elements we need.
98 * Called again each time a Tab gets clicked.
99 * @todo Consider to implement a WordPress general utility for this and don't use jQuery UI.
101 function handleTabbables() {
103 // Get all the tabbable elements.
104 $tabbables = $( ':tabbable', $iframeBody );
105 // Our first tabbable element is always the "Close" button.
106 $firstTabbable = tbWindow.find( '#TB_closeWindowButton' );
107 // Get the last tabbable element.
108 $lastTabbable = $tabbables.last();
109 // Make a jQuery collection.
110 $firstAndLast = $firstTabbable.add( $lastTabbable );
111 // Detach any previously attached keydown event.
112 $firstAndLast.off( 'keydown.wp-plugin-details' );
113 // Attach again the keydown event on the first and last focusable elements.
114 $firstAndLast.on( 'keydown.wp-plugin-details', function( event ) {
115 constrainTabbing( event );
119 // Constrain tabbing within the plugin modal dialog.
120 function constrainTabbing( event ) {
121 if ( 9 !== event.which ) {
125 if ( $lastTabbable[0] === event.target && ! event.shiftKey ) {
126 event.preventDefault();
127 $firstTabbable.focus();
128 } else if ( $firstTabbable[0] === event.target && event.shiftKey ) {
129 event.preventDefault();
130 $lastTabbable.focus();
134 // Open the Plugin details modal.
135 $( '.thickbox.open-plugin-details-modal' ).on( 'click', function( e ) {
136 // The `data-title` attribute is used only in the Plugin screens.
137 var title = $( this ).data( 'title' ) ? plugininstallL10n.plugin_information + ' ' + $( this ).data( 'title' ) : plugininstallL10n.plugin_modal_label;
142 // Store the element that has focus before opening the modal dialog, i.e. the control which opens it.
143 $focusedBefore = $( this );
147 // Set ARIA role and ARIA label.
150 'aria-label': plugininstallL10n.plugin_modal_label
153 // Set title attribute on the iframe.
154 tbWindow.find( '#TB_iframeContent' ).attr( 'title', title );
157 /* Plugin install related JS */
158 $( '#plugin-information-tabs a' ).click( function( event ) {
159 var tab = $( this ).attr( 'name' );
160 event.preventDefault();
163 $( '#plugin-information-tabs a.current' ).removeClass( 'current' );
164 $( this ).addClass( 'current' );
166 // Only show the fyi box in the description section, on smaller screen, where it's otherwise always displayed at the top.
167 if ( 'description' !== tab && $( window ).width() < 772 ) {
168 $( '#plugin-information-content' ).find( '.fyi' ).hide();
170 $( '#plugin-information-content' ).find( '.fyi' ).show();
174 $( '#section-holder div.section' ).hide(); // Hide 'em all.
175 $( '#section-' + tab ).show();