1 /* global plugininstallL10n, tb_click, tb_remove */
4 * Functionality for the plugin install screens.
7 jQuery( document ).ready( function( $ ) {
15 $uploadViewToggle = $( '.upload-view-toggle' ),
16 $wrap = $ ( '.wrap' ),
17 $body = $( document.body );
19 tb_position = function() {
20 var width = $( window ).width(),
21 H = $( window ).height() - ( ( 792 < width ) ? 60 : 20 ),
22 W = ( 792 < width ) ? 772 : width - 20;
24 tbWindow = $( '#TB_window' );
26 if ( tbWindow.length ) {
27 tbWindow.width( W ).height( H );
28 $( '#TB_iframeContent' ).width( W ).height( H );
30 'margin-left': '-' + parseInt( ( W / 2 ), 10 ) + 'px'
32 if ( typeof document.body.style.maxWidth !== 'undefined' ) {
40 return $( 'a.thickbox' ).each( function() {
41 var href = $( this ).attr( 'href' );
45 href = href.replace( /&width=[0-9]+/g, '' );
46 href = href.replace( /&height=[0-9]+/g, '' );
47 $(this).attr( 'href', href + '&width=' + W + '&height=' + ( H ) );
51 $( window ).resize( function() {
56 * Custom events: when a Thickbox iframe has loaded and when the Thickbox
57 * modal gets removed from the DOM.
60 .on( 'thickbox:iframe:loaded', tbWindow, function() {
63 .on( 'thickbox:removed', function() {
64 // Set focus back to the element that opened the modal dialog.
65 // Note: IE 8 would need this wrapped in a fake setTimeout `0`.
66 $focusedBefore.focus();
69 function iframeLoaded() {
70 var $iframe = tbWindow.find( '#TB_iframeContent' );
72 // Get the iframe body.
73 $iframeBody = $iframe.contents().find( 'body' );
75 // Get the tabbable elements and handle the keydown event on first load.
78 // Set initial focus on the "Close" button.
79 $firstTabbable.focus();
82 * When the "Install" button is disabled (e.g. the Plugin is already installed)
83 * then we can't predict where the last focusable element is. We need to get
84 * the tabbable elements and handle the keydown event again and again,
85 * each time the active tab panel changes.
87 $( '#plugin-information-tabs a', $iframeBody ).on( 'click', function() {
91 // Close the modal when pressing Escape.
92 $iframeBody.on( 'keydown', function( event ) {
93 if ( 27 !== event.which ) {
101 * Get the tabbable elements and detach/attach the keydown event.
102 * Called after the iframe has fully loaded so we have all the elements we need.
103 * Called again each time a Tab gets clicked.
104 * @todo Consider to implement a WordPress general utility for this and don't use jQuery UI.
106 function handleTabbables() {
108 // Get all the tabbable elements.
109 $tabbables = $( ':tabbable', $iframeBody );
110 // Our first tabbable element is always the "Close" button.
111 $firstTabbable = tbWindow.find( '#TB_closeWindowButton' );
112 // Get the last tabbable element.
113 $lastTabbable = $tabbables.last();
114 // Make a jQuery collection.
115 $firstAndLast = $firstTabbable.add( $lastTabbable );
116 // Detach any previously attached keydown event.
117 $firstAndLast.off( 'keydown.wp-plugin-details' );
118 // Attach again the keydown event on the first and last focusable elements.
119 $firstAndLast.on( 'keydown.wp-plugin-details', function( event ) {
120 constrainTabbing( event );
124 // Constrain tabbing within the plugin modal dialog.
125 function constrainTabbing( event ) {
126 if ( 9 !== event.which ) {
130 if ( $lastTabbable[0] === event.target && ! event.shiftKey ) {
131 event.preventDefault();
132 $firstTabbable.focus();
133 } else if ( $firstTabbable[0] === event.target && event.shiftKey ) {
134 event.preventDefault();
135 $lastTabbable.focus();
139 // Open the Plugin details modal.
140 $( '.thickbox.open-plugin-details-modal' ).on( 'click', function( e ) {
141 // The `data-title` attribute is used only in the Plugin screens.
142 var title = $( this ).data( 'title' ) ? plugininstallL10n.plugin_information + ' ' + $( this ).data( 'title' ) : plugininstallL10n.plugin_modal_label;
147 // Store the element that has focus before opening the modal dialog, i.e. the control which opens it.
148 $focusedBefore = $( this );
152 // Set ARIA role and ARIA label.
155 'aria-label': plugininstallL10n.plugin_modal_label
158 // Set title attribute on the iframe.
159 tbWindow.find( '#TB_iframeContent' ).attr( 'title', title );
162 /* Plugin install related JS */
163 $( '#plugin-information-tabs a' ).click( function( event ) {
164 var tab = $( this ).attr( 'name' );
165 event.preventDefault();
168 $( '#plugin-information-tabs a.current' ).removeClass( 'current' );
169 $( this ).addClass( 'current' );
171 // Only show the fyi box in the description section, on smaller screen, where it's otherwise always displayed at the top.
172 if ( 'description' !== tab && $( window ).width() < 772 ) {
173 $( '#plugin-information-content' ).find( '.fyi' ).hide();
175 $( '#plugin-information-content' ).find( '.fyi' ).show();
179 $( '#section-holder div.section' ).hide(); // Hide 'em all.
180 $( '#section-' + tab ).show();
184 * When a user presses the "Upload Plugin" button, show the upload form in place
185 * rather than sending them to the devoted upload plugin page.
186 * The `?tab=upload` page still exists for no-js support and for plugins that
187 * might access it directly. When we're in this page, let the link behave
188 * like a link. Otherwise we're in the normal plugin installer pages and the
189 * link should behave like a toggle button.
191 if ( ! $wrap.hasClass( 'plugin-install-tab-upload' ) ) {
195 'aria-expanded': 'false'
197 .on( 'click', function( event ) {
198 event.preventDefault();
199 $body.toggleClass( 'show-upload-view' );
200 $uploadViewToggle.attr( 'aria-expanded', $body.hasClass( 'show-upload-view' ) );