annotate core/modules/media_library/js/media_library.widget.es6.js @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@4 1 /**
Chris@4 2 * @file media_library.widget.js
Chris@4 3 */
Chris@4 4 (($, Drupal) => {
Chris@4 5 /**
Chris@4 6 * Allows users to re-order their selection with drag+drop.
Chris@5 7 *
Chris@5 8 * @type {Drupal~behavior}
Chris@5 9 *
Chris@5 10 * @prop {Drupal~behaviorAttach} attach
Chris@5 11 * Attaches behavior to re-order selected media items.
Chris@4 12 */
Chris@4 13 Drupal.behaviors.MediaLibraryWidgetSortable = {
Chris@4 14 attach(context) {
Chris@4 15 // Allow media items to be re-sorted with drag+drop in the widget.
Chris@4 16 $('.js-media-library-selection', context)
Chris@4 17 .once('media-library-sortable')
Chris@4 18 .sortable({
Chris@4 19 tolerance: 'pointer',
Chris@4 20 helper: 'clone',
Chris@4 21 handle: '.js-media-library-item-preview',
Chris@4 22 stop: ({ target }) => {
Chris@4 23 // Update all the hidden "weight" fields.
Chris@4 24 $(target)
Chris@4 25 .children()
Chris@4 26 .each((index, child) => {
Chris@4 27 $(child)
Chris@4 28 .find('.js-media-library-item-weight')
Chris@4 29 .val(index);
Chris@4 30 });
Chris@4 31 },
Chris@4 32 });
Chris@4 33 },
Chris@4 34 };
Chris@4 35
Chris@4 36 /**
Chris@4 37 * Allows selection order to be set without drag+drop for accessibility.
Chris@5 38 *
Chris@5 39 * @type {Drupal~behavior}
Chris@5 40 *
Chris@5 41 * @prop {Drupal~behaviorAttach} attach
Chris@5 42 * Attaches behavior to toggle the weight field for media items.
Chris@4 43 */
Chris@4 44 Drupal.behaviors.MediaLibraryWidgetToggleWeight = {
Chris@4 45 attach(context) {
Chris@4 46 const strings = {
Chris@4 47 show: Drupal.t('Show media item weights'),
Chris@4 48 hide: Drupal.t('Hide media item weights'),
Chris@4 49 };
Chris@4 50 $('.js-media-library-widget-toggle-weight', context)
Chris@4 51 .once('media-library-toggle')
Chris@4 52 .on('click', e => {
Chris@4 53 e.preventDefault();
Chris@4 54 $(e.currentTarget)
Chris@4 55 .toggleClass('active')
Chris@4 56 .text(
Chris@4 57 $(e.currentTarget).hasClass('active')
Chris@4 58 ? strings.hide
Chris@4 59 : strings.show,
Chris@4 60 )
Chris@4 61 .parent()
Chris@4 62 .find('.js-media-library-item-weight')
Chris@4 63 .parent()
Chris@4 64 .toggle();
Chris@4 65 })
Chris@4 66 .text(strings.show);
Chris@4 67 $('.js-media-library-item-weight', context)
Chris@4 68 .once('media-library-toggle')
Chris@4 69 .parent()
Chris@4 70 .hide();
Chris@4 71 },
Chris@4 72 };
Chris@4 73
Chris@4 74 /**
Chris@5 75 * Disable the open button when the user is not allowed to add more items.
Chris@5 76 *
Chris@5 77 * @type {Drupal~behavior}
Chris@5 78 *
Chris@5 79 * @prop {Drupal~behaviorAttach} attach
Chris@5 80 * Attaches behavior to disable the media library open button.
Chris@4 81 */
Chris@5 82 Drupal.behaviors.MediaLibraryWidgetDisableButton = {
Chris@4 83 attach(context) {
Chris@5 84 // When the user returns from the modal to the widget, we want to shift
Chris@5 85 // the focus back to the open button. If the user is not allowed to add
Chris@5 86 // more items, the button needs to be disabled. Since we can't shift the
Chris@5 87 // focus to disabled elements, the focus is set back to the open button
Chris@5 88 // via JavaScript by adding the 'data-disabled-focus' attribute.
Chris@5 89 $('.js-media-library-open-button[data-disabled-focus="true"]', context)
Chris@5 90 .once('media-library-disable')
Chris@5 91 .each(function() {
Chris@5 92 $(this).focus();
Chris@4 93
Chris@5 94 // There is a small delay between the focus set by the browser and the
Chris@5 95 // focus of screen readers. We need to give screen readers time to
Chris@5 96 // shift the focus as well before the button is disabled.
Chris@5 97 setTimeout(() => {
Chris@5 98 $(this).attr('disabled', 'disabled');
Chris@5 99 }, 50);
Chris@4 100 });
Chris@4 101 },
Chris@4 102 };
Chris@4 103 })(jQuery, Drupal);