plugin-install.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* global plugininstallL10n, tb_click, tb_remove */
  2. /**
  3. * Functionality for the plugin install screens.
  4. */
  5. var tb_position;
  6. jQuery( document ).ready( function( $ ) {
  7. var tbWindow,
  8. $iframeBody,
  9. $tabbables,
  10. $firstTabbable,
  11. $lastTabbable,
  12. $focusedBefore = $(),
  13. $uploadViewToggle = $( '.upload-view-toggle' ),
  14. $wrap = $ ( '.wrap' ),
  15. $body = $( document.body );
  16. tb_position = function() {
  17. var width = $( window ).width(),
  18. H = $( window ).height() - ( ( 792 < width ) ? 60 : 20 ),
  19. W = ( 792 < width ) ? 772 : width - 20;
  20. tbWindow = $( '#TB_window' );
  21. if ( tbWindow.length ) {
  22. tbWindow.width( W ).height( H );
  23. $( '#TB_iframeContent' ).width( W ).height( H );
  24. tbWindow.css({
  25. 'margin-left': '-' + parseInt( ( W / 2 ), 10 ) + 'px'
  26. });
  27. if ( typeof document.body.style.maxWidth !== 'undefined' ) {
  28. tbWindow.css({
  29. 'top': '30px',
  30. 'margin-top': '0'
  31. });
  32. }
  33. }
  34. return $( 'a.thickbox' ).each( function() {
  35. var href = $( this ).attr( 'href' );
  36. if ( ! href ) {
  37. return;
  38. }
  39. href = href.replace( /&width=[0-9]+/g, '' );
  40. href = href.replace( /&height=[0-9]+/g, '' );
  41. $(this).attr( 'href', href + '&width=' + W + '&height=' + ( H ) );
  42. });
  43. };
  44. $( window ).resize( function() {
  45. tb_position();
  46. });
  47. /*
  48. * Custom events: when a Thickbox iframe has loaded and when the Thickbox
  49. * modal gets removed from the DOM.
  50. */
  51. $body
  52. .on( 'thickbox:iframe:loaded', tbWindow, function() {
  53. /*
  54. * Return if it's not the modal with the plugin details iframe. Other
  55. * thickbox instances might want to load an iframe with content from
  56. * an external domain. Avoid to access the iframe contents when we're
  57. * not sure the iframe loads from the same domain.
  58. */
  59. if ( ! tbWindow.hasClass( 'plugin-details-modal' ) ) {
  60. return;
  61. }
  62. iframeLoaded();
  63. })
  64. .on( 'thickbox:removed', function() {
  65. // Set focus back to the element that opened the modal dialog.
  66. // Note: IE 8 would need this wrapped in a fake setTimeout `0`.
  67. $focusedBefore.focus();
  68. });
  69. function iframeLoaded() {
  70. var $iframe = tbWindow.find( '#TB_iframeContent' );
  71. // Get the iframe body.
  72. $iframeBody = $iframe.contents().find( 'body' );
  73. // Get the tabbable elements and handle the keydown event on first load.
  74. handleTabbables();
  75. // Set initial focus on the "Close" button.
  76. $firstTabbable.focus();
  77. /*
  78. * When the "Install" button is disabled (e.g. the Plugin is already installed)
  79. * then we can't predict where the last focusable element is. We need to get
  80. * the tabbable elements and handle the keydown event again and again,
  81. * each time the active tab panel changes.
  82. */
  83. $( '#plugin-information-tabs a', $iframeBody ).on( 'click', function() {
  84. handleTabbables();
  85. });
  86. // Close the modal when pressing Escape.
  87. $iframeBody.on( 'keydown', function( event ) {
  88. if ( 27 !== event.which ) {
  89. return;
  90. }
  91. tb_remove();
  92. });
  93. }
  94. /*
  95. * Get the tabbable elements and detach/attach the keydown event.
  96. * Called after the iframe has fully loaded so we have all the elements we need.
  97. * Called again each time a Tab gets clicked.
  98. * @todo Consider to implement a WordPress general utility for this and don't use jQuery UI.
  99. */
  100. function handleTabbables() {
  101. var $firstAndLast;
  102. // Get all the tabbable elements.
  103. $tabbables = $( ':tabbable', $iframeBody );
  104. // Our first tabbable element is always the "Close" button.
  105. $firstTabbable = tbWindow.find( '#TB_closeWindowButton' );
  106. // Get the last tabbable element.
  107. $lastTabbable = $tabbables.last();
  108. // Make a jQuery collection.
  109. $firstAndLast = $firstTabbable.add( $lastTabbable );
  110. // Detach any previously attached keydown event.
  111. $firstAndLast.off( 'keydown.wp-plugin-details' );
  112. // Attach again the keydown event on the first and last focusable elements.
  113. $firstAndLast.on( 'keydown.wp-plugin-details', function( event ) {
  114. constrainTabbing( event );
  115. });
  116. }
  117. // Constrain tabbing within the plugin modal dialog.
  118. function constrainTabbing( event ) {
  119. if ( 9 !== event.which ) {
  120. return;
  121. }
  122. if ( $lastTabbable[0] === event.target && ! event.shiftKey ) {
  123. event.preventDefault();
  124. $firstTabbable.focus();
  125. } else if ( $firstTabbable[0] === event.target && event.shiftKey ) {
  126. event.preventDefault();
  127. $lastTabbable.focus();
  128. }
  129. }
  130. /*
  131. * Open the Plugin details modal. The event is delegated to get also the links
  132. * in the plugins search tab, after the AJAX search rebuilds the HTML. It's
  133. * delegated on the closest ancestor and not on the body to avoid conflicts
  134. * with other handlers, see Trac ticket #43082.
  135. */
  136. $( '.wrap' ).on( 'click', '.thickbox.open-plugin-details-modal', function( e ) {
  137. // The `data-title` attribute is used only in the Plugin screens.
  138. var title = $( this ).data( 'title' ) ? plugininstallL10n.plugin_information + ' ' + $( this ).data( 'title' ) : plugininstallL10n.plugin_modal_label;
  139. e.preventDefault();
  140. e.stopPropagation();
  141. // Store the element that has focus before opening the modal dialog, i.e. the control which opens it.
  142. $focusedBefore = $( this );
  143. tb_click.call(this);
  144. // Set ARIA role, ARIA label, and add a CSS class.
  145. tbWindow
  146. .attr({
  147. 'role': 'dialog',
  148. 'aria-label': plugininstallL10n.plugin_modal_label
  149. })
  150. .addClass( 'plugin-details-modal' );
  151. // Set title attribute on the iframe.
  152. tbWindow.find( '#TB_iframeContent' ).attr( 'title', title );
  153. });
  154. /* Plugin install related JS */
  155. $( '#plugin-information-tabs a' ).click( function( event ) {
  156. var tab = $( this ).attr( 'name' );
  157. event.preventDefault();
  158. // Flip the tab
  159. $( '#plugin-information-tabs a.current' ).removeClass( 'current' );
  160. $( this ).addClass( 'current' );
  161. // Only show the fyi box in the description section, on smaller screen, where it's otherwise always displayed at the top.
  162. if ( 'description' !== tab && $( window ).width() < 772 ) {
  163. $( '#plugin-information-content' ).find( '.fyi' ).hide();
  164. } else {
  165. $( '#plugin-information-content' ).find( '.fyi' ).show();
  166. }
  167. // Flip the content.
  168. $( '#section-holder div.section' ).hide(); // Hide 'em all.
  169. $( '#section-' + tab ).show();
  170. });
  171. /*
  172. * When a user presses the "Upload Plugin" button, show the upload form in place
  173. * rather than sending them to the devoted upload plugin page.
  174. * The `?tab=upload` page still exists for no-js support and for plugins that
  175. * might access it directly. When we're in this page, let the link behave
  176. * like a link. Otherwise we're in the normal plugin installer pages and the
  177. * link should behave like a toggle button.
  178. */
  179. if ( ! $wrap.hasClass( 'plugin-install-tab-upload' ) ) {
  180. $uploadViewToggle
  181. .attr({
  182. role: 'button',
  183. 'aria-expanded': 'false'
  184. })
  185. .on( 'click', function( event ) {
  186. event.preventDefault();
  187. $body.toggleClass( 'show-upload-view' );
  188. $uploadViewToggle.attr( 'aria-expanded', $body.hasClass( 'show-upload-view' ) );
  189. });
  190. }
  191. });