comparison 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
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
2 * @file media_library.widget.js 2 * @file media_library.widget.js
3 */ 3 */
4 (($, Drupal) => { 4 (($, Drupal) => {
5 /** 5 /**
6 * Allows users to re-order their selection with drag+drop. 6 * Allows users to re-order their selection with drag+drop.
7 *
8 * @type {Drupal~behavior}
9 *
10 * @prop {Drupal~behaviorAttach} attach
11 * Attaches behavior to re-order selected media items.
7 */ 12 */
8 Drupal.behaviors.MediaLibraryWidgetSortable = { 13 Drupal.behaviors.MediaLibraryWidgetSortable = {
9 attach(context) { 14 attach(context) {
10 // Allow media items to be re-sorted with drag+drop in the widget. 15 // Allow media items to be re-sorted with drag+drop in the widget.
11 $('.js-media-library-selection', context) 16 $('.js-media-library-selection', context)
28 }, 33 },
29 }; 34 };
30 35
31 /** 36 /**
32 * Allows selection order to be set without drag+drop for accessibility. 37 * Allows selection order to be set without drag+drop for accessibility.
38 *
39 * @type {Drupal~behavior}
40 *
41 * @prop {Drupal~behaviorAttach} attach
42 * Attaches behavior to toggle the weight field for media items.
33 */ 43 */
34 Drupal.behaviors.MediaLibraryWidgetToggleWeight = { 44 Drupal.behaviors.MediaLibraryWidgetToggleWeight = {
35 attach(context) { 45 attach(context) {
36 const strings = { 46 const strings = {
37 show: Drupal.t('Show media item weights'), 47 show: Drupal.t('Show media item weights'),
60 .hide(); 70 .hide();
61 }, 71 },
62 }; 72 };
63 73
64 /** 74 /**
65 * Warn users when clicking outgoing links from the library or widget. 75 * Disable the open button when the user is not allowed to add more items.
76 *
77 * @type {Drupal~behavior}
78 *
79 * @prop {Drupal~behaviorAttach} attach
80 * Attaches behavior to disable the media library open button.
66 */ 81 */
67 Drupal.behaviors.MediaLibraryWidgetWarn = { 82 Drupal.behaviors.MediaLibraryWidgetDisableButton = {
68 attach(context) { 83 attach(context) {
69 $('.js-media-library-item a[href]', context) 84 // When the user returns from the modal to the widget, we want to shift
70 .once('media-library-warn-link') 85 // the focus back to the open button. If the user is not allowed to add
71 .on('click', e => { 86 // more items, the button needs to be disabled. Since we can't shift the
72 const message = Drupal.t( 87 // focus to disabled elements, the focus is set back to the open button
73 'Unsaved changes to the form will be lost. Are you sure you want to leave?', 88 // via JavaScript by adding the 'data-disabled-focus' attribute.
74 ); 89 $('.js-media-library-open-button[data-disabled-focus="true"]', context)
75 const confirmation = window.confirm(message); 90 .once('media-library-disable')
76 if (!confirmation) { 91 .each(function() {
77 e.preventDefault(); 92 $(this).focus();
78 }
79 });
80 },
81 };
82 93
83 /** 94 // There is a small delay between the focus set by the browser and the
84 * Prevent users from selecting more items than allowed in the view. 95 // focus of screen readers. We need to give screen readers time to
85 */ 96 // shift the focus as well before the button is disabled.
86 Drupal.behaviors.MediaLibraryWidgetRemaining = { 97 setTimeout(() => {
87 attach(context, settings) { 98 $(this).attr('disabled', 'disabled');
88 const $view = $('.js-media-library-view', context).once( 99 }, 50);
89 'media-library-remaining',
90 );
91 $view
92 .find('.js-media-library-item input[type="checkbox"]')
93 .on('change', () => {
94 if (
95 settings.media_library &&
96 settings.media_library.selection_remaining
97 ) {
98 const $checkboxes = $view.find(
99 '.js-media-library-item input[type="checkbox"]',
100 );
101 if (
102 $checkboxes.filter(':checked').length ===
103 settings.media_library.selection_remaining
104 ) {
105 $checkboxes
106 .not(':checked')
107 .prop('disabled', true)
108 .closest('.js-media-library-item')
109 .addClass('media-library-item--disabled');
110 } else {
111 $checkboxes
112 .prop('disabled', false)
113 .closest('.js-media-library-item')
114 .removeClass('media-library-item--disabled');
115 }
116 }
117 }); 100 });
118 }, 101 },
119 }; 102 };
120 })(jQuery, Drupal); 103 })(jQuery, Drupal);