annotate core/modules/media_library/js/media_library.widget.es6.js @ 19:fa3358dc1485 tip

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