diff --git a/core/modules/media_library/js/media_library.click_to_select.es6.js b/core/modules/media_library/js/media_library.click_to_select.es6.js index 90c58b665b..db42fe4790 100644 --- a/core/modules/media_library/js/media_library.click_to_select.es6.js +++ b/core/modules/media_library/js/media_library.click_to_select.es6.js @@ -5,6 +5,11 @@ (($, Drupal) => { /** * Allows users to select an element which checks a hidden checkbox. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior for selecting media library item. */ Drupal.behaviors.ClickToSelect = { attach(context) { diff --git a/core/modules/media_library/js/media_library.ui.es6.js b/core/modules/media_library/js/media_library.ui.es6.js index df22ff0676..ad457aee12 100644 --- a/core/modules/media_library/js/media_library.ui.es6.js +++ b/core/modules/media_library/js/media_library.ui.es6.js @@ -17,6 +17,11 @@ /** * Warn users when clicking outgoing links from the library or widget. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to links in the media library. */ Drupal.behaviors.MediaLibraryWidgetWarn = { attach(context) { @@ -42,13 +47,18 @@ * want to load the changed library content. This is not only more efficient, * but also provides a more accessible user experience for screen readers. * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to vertical tabs in the media library. + * * @todo Remove when the AJAX system adds support for replacing a specific * selector via a link. * https://www.drupal.org/project/drupal/issues/3026636 */ Drupal.behaviors.MediaLibraryTabs = { attach(context) { - const $menu = $('.media-library-menu'); + const $menu = $('.js-media-library-menu'); $menu .find('a', context) .once('media-library-menu-item') @@ -110,35 +120,17 @@ }, }; - /** - * Update the number of selected items in the button pane. - */ - function updateSelectionInfo(remaining) { - const $buttonPane = $('.media-library-widget-modal .ui-dialog-buttonpane'); - if (!$buttonPane.length) { - return; - } - - // Add the selection count. - const latestCount = Drupal.theme( - 'mediaLibrarySelectionCount', - Drupal.MediaLibrary.currentSelection, - remaining, - ); - const $existingCount = $buttonPane.find('.media-library-selected-count'); - if ($existingCount.length) { - $existingCount.replaceWith(latestCount); - } else { - $buttonPane.append(latestCount); - } - } - /** * Update the media library selection when loaded or media items are selected. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to select media items. */ Drupal.behaviors.MediaLibraryItemSelection = { attach(context, settings) { - const $form = $('.media-library-views-form', context); + const $form = $('.js-media-library-views-form', context); const currentSelection = Drupal.MediaLibrary.currentSelection; if (!$form.length) { @@ -167,13 +159,19 @@ currentSelection.splice(position, 1); } - // Set the selection in the hidden form element. + // Set the selection in the hidden form element. $form - .find('input#media-library-modal-selection') + .find('#media-library-modal-selection') .val(currentSelection.join()) .trigger('change'); }); + /** + * Disable media items. + * + * @param {jQuery} $items + * A jQuery object representing the media items that should be disabled. + */ function disableItems($items) { $items .prop('disabled', true) @@ -181,6 +179,12 @@ .addClass('media-library-item--disabled'); } + /** + * Enable media items. + * + * @param {jQuery} $items + * A jQuery object representing the media items that should be enabled. + */ function enableItems($items) { $items .prop('disabled', false) @@ -188,8 +192,34 @@ .removeClass('media-library-item--disabled'); } + /** + * Update the number of selected items in the button pane. + * + * @param {number} remaining + * The number of remaining slots. + */ + function updateSelectionInfo(remaining) { + const $buttonPane = $('.media-library-widget-modal .ui-dialog-buttonpane'); + if (!$buttonPane.length) { + return; + } + + // Add the selection count. + const latestCount = Drupal.theme( + 'mediaLibrarySelectionCount', + Drupal.MediaLibrary.currentSelection, + remaining, + ); + const $existingCount = $buttonPane.find('.media-library-selected-count'); + if ($existingCount.length) { + $existingCount.replaceWith(latestCount); + } else { + $buttonPane.append(latestCount); + } + } + // The hidden selection form field changes when the selection is updated. - $('input#media-library-modal-selection', $form) + $('#media-library-modal-selection', $form) .once('media-library-selection-change') .on('change', e => { updateSelectionInfo(settings.media_library.selection_remaining); @@ -228,6 +258,11 @@ /** * Clear the current selection. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to clear the selection when the library modal closes. */ Drupal.behaviors.MediaLibraryModalClearSelection = { attach() { diff --git a/core/modules/media_library/js/media_library.ui.js b/core/modules/media_library/js/media_library.ui.js index af324857bf..3b1a8a2ee1 100644 --- a/core/modules/media_library/js/media_library.ui.js +++ b/core/modules/media_library/js/media_library.ui.js @@ -24,7 +24,7 @@ Drupal.behaviors.MediaLibraryTabs = { attach: function attach(context) { - var $menu = $('.media-library-menu'); + var $menu = $('.js-media-library-menu'); $menu.find('a', context).once('media-library-menu-item').on('click', function (e) { e.preventDefault(); e.stopPropagation(); @@ -69,24 +69,9 @@ } }; - function updateSelectionInfo(remaining) { - var $buttonPane = $('.media-library-widget-modal .ui-dialog-buttonpane'); - if (!$buttonPane.length) { - return; - } - - var latestCount = Drupal.theme('mediaLibrarySelectionCount', Drupal.MediaLibrary.currentSelection, remaining); - var $existingCount = $buttonPane.find('.media-library-selected-count'); - if ($existingCount.length) { - $existingCount.replaceWith(latestCount); - } else { - $buttonPane.append(latestCount); - } - } - Drupal.behaviors.MediaLibraryItemSelection = { attach: function attach(context, settings) { - var $form = $('.media-library-views-form', context); + var $form = $('.js-media-library-views-form', context); var currentSelection = Drupal.MediaLibrary.currentSelection; if (!$form.length) { @@ -107,7 +92,7 @@ currentSelection.splice(position, 1); } - $form.find('input#media-library-modal-selection').val(currentSelection.join()).trigger('change'); + $form.find('#media-library-modal-selection').val(currentSelection.join()).trigger('change'); }); function disableItems($items) { @@ -118,7 +103,22 @@ $items.prop('disabled', false).closest('.js-media-library-item').removeClass('media-library-item--disabled'); } - $('input#media-library-modal-selection', $form).once('media-library-selection-change').on('change', function (e) { + function updateSelectionInfo(remaining) { + var $buttonPane = $('.media-library-widget-modal .ui-dialog-buttonpane'); + if (!$buttonPane.length) { + return; + } + + var latestCount = Drupal.theme('mediaLibrarySelectionCount', Drupal.MediaLibrary.currentSelection, remaining); + var $existingCount = $buttonPane.find('.media-library-selected-count'); + if ($existingCount.length) { + $existingCount.replaceWith(latestCount); + } else { + $buttonPane.append(latestCount); + } + } + + $('#media-library-modal-selection', $form).once('media-library-selection-change').on('change', function (e) { updateSelectionInfo(settings.media_library.selection_remaining); if (currentSelection.length === settings.media_library.selection_remaining) { diff --git a/core/modules/media_library/js/media_library.view.es6.js b/core/modules/media_library/js/media_library.view.es6.js index dcda58d62e..be2ddf390a 100644 --- a/core/modules/media_library/js/media_library.view.es6.js +++ b/core/modules/media_library/js/media_library.view.es6.js @@ -4,11 +4,16 @@ (($, Drupal) => { /** * Adds hover effect to media items. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to add a class when hovering over media items. */ Drupal.behaviors.MediaLibraryHover = { attach(context) { $( - '.media-library-item .js-click-to-select-trigger,.media-library-item .js-click-to-select-checkbox', + '.js-click-to-select-trigger, .js-click-to-select-checkbox', context, ) .once('media-library-item-hover') @@ -22,10 +27,15 @@ /** * Adds focus effect to media items. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to add a focus effect to media items. */ Drupal.behaviors.MediaLibraryFocus = { attach(context) { - $('.media-library-item .js-click-to-select-checkbox input', context) + $('.js-click-to-select-checkbox input', context) .once('media-library-item-focus') .on('focus blur', ({ currentTarget, type }) => { $(currentTarget) @@ -37,10 +47,15 @@ /** * Adds checkbox to select all items in the library. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to select all media items. */ Drupal.behaviors.MediaLibrarySelectAll = { attach(context) { - const $view = $('.media-library-view', context).once( + const $view = $('.js-media-library-view', context).once( 'media-library-select-all', ); if ($view.length && $view.find('.media-library-item').length) { diff --git a/core/modules/media_library/js/media_library.view.js b/core/modules/media_library/js/media_library.view.js index 18facca93a..73028f6af8 100644 --- a/core/modules/media_library/js/media_library.view.js +++ b/core/modules/media_library/js/media_library.view.js @@ -8,7 +8,7 @@ (function ($, Drupal) { Drupal.behaviors.MediaLibraryHover = { attach: function attach(context) { - $('.media-library-item .js-click-to-select-trigger,.media-library-item .js-click-to-select-checkbox', context).once('media-library-item-hover').on('mouseover mouseout', function (_ref) { + $('.js-click-to-select-trigger, .js-click-to-select-checkbox', context).once('media-library-item-hover').on('mouseover mouseout', function (_ref) { var currentTarget = _ref.currentTarget, type = _ref.type; @@ -19,7 +19,7 @@ Drupal.behaviors.MediaLibraryFocus = { attach: function attach(context) { - $('.media-library-item .js-click-to-select-checkbox input', context).once('media-library-item-focus').on('focus blur', function (_ref2) { + $('.js-click-to-select-checkbox input', context).once('media-library-item-focus').on('focus blur', function (_ref2) { var currentTarget = _ref2.currentTarget, type = _ref2.type; @@ -30,7 +30,7 @@ Drupal.behaviors.MediaLibrarySelectAll = { attach: function attach(context) { - var $view = $('.media-library-view', context).once('media-library-select-all'); + var $view = $('.js-media-library-view', context).once('media-library-select-all'); if ($view.length && $view.find('.media-library-item').length) { var $checkbox = $('').on('click', function (_ref3) { var currentTarget = _ref3.currentTarget; diff --git a/core/modules/media_library/js/media_library.widget.es6.js b/core/modules/media_library/js/media_library.widget.es6.js index 63de589b7d..93878a0f6a 100644 --- a/core/modules/media_library/js/media_library.widget.es6.js +++ b/core/modules/media_library/js/media_library.widget.es6.js @@ -4,6 +4,11 @@ (($, Drupal) => { /** * Allows users to re-order their selection with drag+drop. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to re-order selected media items. */ Drupal.behaviors.MediaLibraryWidgetSortable = { attach(context) { @@ -30,6 +35,11 @@ /** * Allows selection order to be set without drag+drop for accessibility. + * + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Attaches behavior to toggle the weight field for media items. */ Drupal.behaviors.MediaLibraryWidgetToggleWeight = { attach(context) { diff --git a/core/modules/media_library/src/MediaLibraryUiBuilder.php b/core/modules/media_library/src/MediaLibraryUiBuilder.php index b981602fbb..f4b889bbeb 100644 --- a/core/modules/media_library/src/MediaLibraryUiBuilder.php +++ b/core/modules/media_library/src/MediaLibraryUiBuilder.php @@ -151,7 +151,7 @@ protected function buildMediaTypeMenu(MediaLibraryState $state) { '#theme' => 'links', '#links' => [], '#attributes' => [ - 'class' => ['media-library-menu'], + 'class' => ['media-library-menu', 'js-media-library-menu'], ], ]; diff --git a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php index 155eaa8401..6ef0f57c16 100644 --- a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php +++ b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php @@ -46,6 +46,10 @@ public function render(ResultRow $values) { * The current state of the form. */ public function viewsForm(array &$form, FormStateInterface $form_state) { + $form['#attributes'] = [ + 'class' => ['media-library-views-form', 'js-media-library-views-form'], + ]; + // Render checkboxes for all rows. $form[$this->options['id']]['#tree'] = TRUE; foreach ($this->view->result as $row_index => $row) {