comparison core/modules/media_library/js/media_library.ui.js @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, window) {
9 Drupal.MediaLibrary = {
10 currentSelection: []
11 };
12
13 Drupal.AjaxCommands.prototype.updateMediaLibrarySelection = function (ajax, response, status) {
14 Object.values(response.mediaIds).forEach(function (value) {
15 Drupal.MediaLibrary.currentSelection.push(value);
16 });
17 };
18
19 Drupal.behaviors.MediaLibraryWidgetWarn = {
20 attach: function attach(context) {
21 $('.js-media-library-item a[href]', context).once('media-library-warn-link').on('click', function (e) {
22 var message = Drupal.t('Unsaved changes to the form will be lost. Are you sure you want to leave?');
23 var confirmation = window.confirm(message);
24 if (!confirmation) {
25 e.preventDefault();
26 }
27 });
28 }
29 };
30
31 Drupal.behaviors.MediaLibraryTabs = {
32 attach: function attach(context) {
33 var $menu = $('.js-media-library-menu');
34 $menu.find('a', context).once('media-library-menu-item').on('click', function (e) {
35 e.preventDefault();
36 e.stopPropagation();
37
38 var ajaxObject = Drupal.ajax({
39 wrapper: 'media-library-content',
40 url: e.currentTarget.href,
41 dialogType: 'ajax',
42 progress: {
43 type: 'fullscreen',
44 message: Drupal.t('Please wait...')
45 }
46 });
47
48 ajaxObject.success = function (response, status) {
49 var _this = this;
50
51 if (this.progress.element) {
52 $(this.progress.element).remove();
53 }
54 if (this.progress.object) {
55 this.progress.object.stopMonitoring();
56 }
57 $(this.element).prop('disabled', false);
58
59 Object.keys(response || {}).forEach(function (i) {
60 if (response[i].command && _this.commands[response[i].command]) {
61 _this.commands[response[i].command](_this, response[i], status);
62 }
63 });
64
65 document.getElementById('media-library-content').focus();
66
67 this.settings = null;
68 };
69 ajaxObject.execute();
70
71 $menu.find('.active-tab').remove();
72 $menu.find('a').removeClass('active');
73 $(e.currentTarget).addClass('active').html(Drupal.t('@title<span class="active-tab visually-hidden"> (active tab)</span>', { '@title': $(e.currentTarget).html() }));
74 });
75 }
76 };
77
78 Drupal.behaviors.MediaLibraryViewsDisplay = {
79 attach: function attach(context) {
80 var $view = $(context).hasClass('.js-media-library-view') ? $(context) : $('.js-media-library-view', context);
81
82 $view.closest('.views-element-container').attr('id', 'media-library-view');
83
84 $('.views-display-link-widget, .views-display-link-widget_table', context).once('media-library-views-display-link').on('click', function (e) {
85 e.preventDefault();
86 e.stopPropagation();
87
88 var $link = $(e.currentTarget);
89
90 var loadingAnnouncement = '';
91 var displayAnnouncement = '';
92 var focusSelector = '';
93 if ($link.hasClass('views-display-link-widget')) {
94 loadingAnnouncement = Drupal.t('Loading grid view.');
95 displayAnnouncement = Drupal.t('Changed to grid view.');
96 focusSelector = '.views-display-link-widget';
97 } else if ($link.hasClass('views-display-link-widget_table')) {
98 loadingAnnouncement = Drupal.t('Loading table view.');
99 displayAnnouncement = Drupal.t('Changed to table view.');
100 focusSelector = '.views-display-link-widget_table';
101 }
102
103 var ajaxObject = Drupal.ajax({
104 wrapper: 'media-library-view',
105 url: e.currentTarget.href,
106 dialogType: 'ajax',
107 progress: {
108 type: 'fullscreen',
109 message: loadingAnnouncement || Drupal.t('Please wait...')
110 }
111 });
112
113 if (displayAnnouncement || focusSelector) {
114 var success = ajaxObject.success;
115 ajaxObject.success = function (response, status) {
116 success.bind(this)(response, status);
117
118 if (focusSelector) {
119 $(focusSelector).focus();
120 }
121
122 if (displayAnnouncement) {
123 Drupal.announce(displayAnnouncement);
124 }
125 };
126 }
127
128 ajaxObject.execute();
129
130 if (loadingAnnouncement) {
131 Drupal.announce(loadingAnnouncement);
132 }
133 });
134 }
135 };
136
137 Drupal.behaviors.MediaLibraryItemSelection = {
138 attach: function attach(context, settings) {
139 var $form = $('.js-media-library-views-form, .js-media-library-add-form', context);
140 var currentSelection = Drupal.MediaLibrary.currentSelection;
141
142 if (!$form.length) {
143 return;
144 }
145
146 var $mediaItems = $('.js-media-library-item input[type="checkbox"]', $form);
147
148 function disableItems($items) {
149 $items.prop('disabled', true).closest('.js-media-library-item').addClass('media-library-item--disabled');
150 }
151
152 function enableItems($items) {
153 $items.prop('disabled', false).closest('.js-media-library-item').removeClass('media-library-item--disabled');
154 }
155
156 function updateSelectionCount(remaining) {
157 var selectItemsText = remaining < 0 ? Drupal.formatPlural(currentSelection.length, '1 item selected', '@count items selected') : Drupal.formatPlural(remaining, '@selected of @count item selected', '@selected of @count items selected', {
158 '@selected': currentSelection.length
159 });
160
161 $('.js-media-library-selected-count').html(selectItemsText);
162 }
163
164 $mediaItems.once('media-item-change').on('change', function (e) {
165 var id = e.currentTarget.value;
166
167 var position = currentSelection.indexOf(id);
168 if (e.currentTarget.checked) {
169 if (position === -1) {
170 currentSelection.push(id);
171 }
172 } else if (position !== -1) {
173 currentSelection.splice(position, 1);
174 }
175
176 $form.find('#media-library-modal-selection').val(currentSelection.join()).trigger('change');
177
178 $('.js-media-library-add-form-current-selection').val(currentSelection.join());
179 });
180
181 $('#media-library-modal-selection', $form).once('media-library-selection-change').on('change', function (e) {
182 updateSelectionCount(settings.media_library.selection_remaining);
183
184 if (currentSelection.length === settings.media_library.selection_remaining) {
185 disableItems($mediaItems.not(':checked'));
186 enableItems($mediaItems.filter(':checked'));
187 } else {
188 enableItems($mediaItems);
189 }
190 });
191
192 currentSelection.forEach(function (value) {
193 $form.find('input[type="checkbox"][value="' + value + '"]').prop('checked', true).trigger('change');
194 });
195
196 $(window).once('media-library-selection-info').on('dialog:aftercreate', function () {
197 var $buttonPane = $('.media-library-widget-modal .ui-dialog-buttonpane');
198 if (!$buttonPane.length) {
199 return;
200 }
201 $buttonPane.append(Drupal.theme('mediaLibrarySelectionCount'));
202 updateSelectionCount(settings.media_library.selection_remaining);
203 });
204 }
205 };
206
207 Drupal.behaviors.MediaLibraryModalClearSelection = {
208 attach: function attach() {
209 $(window).once('media-library-clear-selection').on('dialog:afterclose', function () {
210 Drupal.MediaLibrary.currentSelection = [];
211 });
212 }
213 };
214
215 Drupal.theme.mediaLibrarySelectionCount = function () {
216 return '<div class="media-library-selected-count js-media-library-selected-count" role="status" aria-live="polite" aria-atomic="true"></div>';
217 };
218 })(jQuery, Drupal, window);