comparison core/modules/settings_tray/js/settings_tray.es6.js @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
6 */ 6 */
7 7
8 (($, Drupal) => { 8 (($, Drupal) => {
9 const blockConfigureSelector = '[data-settings-tray-edit]'; 9 const blockConfigureSelector = '[data-settings-tray-edit]';
10 const toggleEditSelector = '[data-drupal-settingstray="toggle"]'; 10 const toggleEditSelector = '[data-drupal-settingstray="toggle"]';
11 const itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button'; 11 const itemsToToggleSelector =
12 const contextualItemsSelector = '[data-contextual-id] a, [data-contextual-id] button'; 12 '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button';
13 const contextualItemsSelector =
14 '[data-contextual-id] a, [data-contextual-id] button';
13 const quickEditItemSelector = '[data-quickedit-entity-id]'; 15 const quickEditItemSelector = '[data-quickedit-entity-id]';
14 16
15 /** 17 /**
16 * Prevent default click events except contextual links. 18 * Prevent default click events except contextual links.
17 * 19 *
65 * @param {boolean} editMode 67 * @param {boolean} editMode
66 * True enable edit mode, false disable edit mode. 68 * True enable edit mode, false disable edit mode.
67 */ 69 */
68 function setEditModeState(editMode) { 70 function setEditModeState(editMode) {
69 if (!document.querySelector('[data-off-canvas-main-canvas]')) { 71 if (!document.querySelector('[data-off-canvas-main-canvas]')) {
70 throw new Error('data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig'); 72 throw new Error(
73 'data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig',
74 );
71 } 75 }
72 editMode = !!editMode; 76 editMode = !!editMode;
73 const $editButton = $(toggleEditSelector); 77 const $editButton = $(toggleEditSelector);
74 let $editables; 78 let $editables;
75 // Turn on edit mode. 79 // Turn on edit mode.
76 if (editMode) { 80 if (editMode) {
77 $editButton.text(Drupal.t('Editing')); 81 $editButton.text(Drupal.t('Editing'));
78 closeToolbarTrays(); 82 closeToolbarTrays();
79 83
80 $editables = $('[data-drupal-settingstray="editable"]').once('settingstray'); 84 $editables = $('[data-drupal-settingstray="editable"]').once(
85 'settingstray',
86 );
81 if ($editables.length) { 87 if ($editables.length) {
82 // Use event capture to prevent clicks on links. 88 // Use event capture to prevent clicks on links.
83 document.querySelector('[data-off-canvas-main-canvas]').addEventListener('click', preventClick, true); 89 document
90 .querySelector('[data-off-canvas-main-canvas]')
91 .addEventListener('click', preventClick, true);
84 /** 92 /**
85 * When a click occurs try and find the settings-tray edit link 93 * When a click occurs try and find the settings-tray edit link
86 * and click it. 94 * and click it.
87 */ 95 */
88 $editables 96 $editables.not(contextualItemsSelector).on('click.settingstray', e => {
89 .not(contextualItemsSelector) 97 // Contextual links are allowed to function in Edit mode.
90 .on('click.settingstray', (e) => { 98 if (
91 // Contextual links are allowed to function in Edit mode. 99 $(e.target).closest('.contextual').length ||
92 if ($(e.target).closest('.contextual').length || !localStorage.getItem('Drupal.contextualToolbar.isViewing')) { 100 !localStorage.getItem('Drupal.contextualToolbar.isViewing')
93 return; 101 ) {
94 } 102 return;
95 $(e.currentTarget).find(blockConfigureSelector).trigger('click'); 103 }
96 disableQuickEdit(); 104 $(e.currentTarget)
97 }); 105 .find(blockConfigureSelector)
106 .trigger('click');
107 disableQuickEdit();
108 });
98 $(quickEditItemSelector) 109 $(quickEditItemSelector)
99 .not(contextualItemsSelector) 110 .not(contextualItemsSelector)
100 .on('click.settingstray', (e) => { 111 .on('click.settingstray', e => {
101 /** 112 /**
102 * For all non-contextual links or the contextual QuickEdit link 113 * For all non-contextual links or the contextual QuickEdit link
103 * close the off-canvas dialog. 114 * close the off-canvas dialog.
104 */ 115 */
105 if (!$(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) { 116 if (
117 !$(e.target)
118 .parent()
119 .hasClass('contextual') ||
120 $(e.target)
121 .parent()
122 .hasClass('quickedit')
123 ) {
106 closeOffCanvas(); 124 closeOffCanvas();
107 } 125 }
108 // Do not trigger if target is quick edit link to avoid loop. 126 // Do not trigger if target is quick edit link to avoid loop.
109 if ($(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) { 127 if (
128 $(e.target)
129 .parent()
130 .hasClass('contextual') ||
131 $(e.target)
132 .parent()
133 .hasClass('quickedit')
134 ) {
110 return; 135 return;
111 } 136 }
112 $(e.currentTarget).find('li.quickedit a').trigger('click'); 137 $(e.currentTarget)
138 .find('li.quickedit a')
139 .trigger('click');
113 }); 140 });
114 } 141 }
115 } 142 }
116 // Disable edit mode. 143 // Disable edit mode.
117 else { 144 else {
118 $editables = $('[data-drupal-settingstray="editable"]').removeOnce('settingstray'); 145 $editables = $('[data-drupal-settingstray="editable"]').removeOnce(
146 'settingstray',
147 );
119 if ($editables.length) { 148 if ($editables.length) {
120 document.querySelector('[data-off-canvas-main-canvas]').removeEventListener('click', preventClick, true); 149 document
150 .querySelector('[data-off-canvas-main-canvas]')
151 .removeEventListener('click', preventClick, true);
121 $editables.off('.settingstray'); 152 $editables.off('.settingstray');
122 $(quickEditItemSelector).off('.settingstray'); 153 $(quickEditItemSelector).off('.settingstray');
123 } 154 }
124 155
125 $editButton.text(Drupal.t('Edit')); 156 $editButton.text(Drupal.t('Edit'));
157 Drupal.ajax.instances 188 Drupal.ajax.instances
158 /** 189 /**
159 * If there is an element and the renderer is 'off_canvas' then we want 190 * If there is an element and the renderer is 'off_canvas' then we want
160 * to add our changes. 191 * to add our changes.
161 */ 192 */
162 .filter(instance => instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas') 193 .filter(
194 instance =>
195 instance &&
196 $(instance.element).attr('data-dialog-renderer') === 'off_canvas',
197 )
163 /** 198 /**
164 * Loop through all Ajax instances that use the 'off_canvas' renderer to 199 * Loop through all Ajax instances that use the 'off_canvas' renderer to
165 * set active editable ID. 200 * set active editable ID.
166 */ 201 */
167 .forEach((instance) => { 202 .forEach(instance => {
168 // Check to make sure existing dialogOptions aren't overridden. 203 // Check to make sure existing dialogOptions aren't overridden.
169 if (!instance.options.data.hasOwnProperty('dialogOptions')) { 204 if (!instance.options.data.hasOwnProperty('dialogOptions')) {
170 instance.options.data.dialogOptions = {}; 205 instance.options.data.dialogOptions = {};
171 } 206 }
172 instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id'); 207 instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(
208 instance.element,
209 )
210 .parents('.settings-tray-editable')
211 .attr('id');
173 instance.progress = { type: 'fullscreen' }; 212 instance.progress = { type: 'fullscreen' };
174 }); 213 });
175 } 214 }
176 215
177 /** 216 /**
190 * instances in Drupal.ajax.instances for them to work with Edit Mode. 229 * instances in Drupal.ajax.instances for them to work with Edit Mode.
191 */ 230 */
192 prepareAjaxLinks(); 231 prepareAjaxLinks();
193 232
194 // When the first contextual link is added to the page set Edit Mode. 233 // When the first contextual link is added to the page set Edit Mode.
195 $('body').once('settings_tray.edit_mode_init').each(() => { 234 $('body')
196 const editMode = localStorage.getItem('Drupal.contextualToolbar.isViewing') === 'false'; 235 .once('settings_tray.edit_mode_init')
197 if (editMode) { 236 .each(() => {
198 setEditModeState(true); 237 const editMode =
199 } 238 localStorage.getItem('Drupal.contextualToolbar.isViewing') ===
200 }); 239 'false';
240 if (editMode) {
241 setEditModeState(true);
242 }
243 });
201 244
202 /** 245 /**
203 * Bind a listener to all 'Quick edit' links for blocks. Click "Edit" 246 * Bind a listener to all 'Quick edit' links for blocks. Click "Edit"
204 * button in toolbar to force Contextual Edit which starts Settings Tray 247 * button in toolbar to force Contextual Edit which starts Settings Tray
205 * edit mode also. 248 * edit mode also.
206 */ 249 */
207 data.$el.find(blockConfigureSelector) 250 data.$el.find(blockConfigureSelector).on('click.settingstray', () => {
208 .on('click.settingstray', () => { 251 if (!isInEditMode()) {
209 if (!isInEditMode()) { 252 $(toggleEditSelector)
210 $(toggleEditSelector).trigger('click').trigger('click.settings_tray'); 253 .trigger('click')
211 } 254 .trigger('click.settings_tray');
212 /** 255 }
213 * Always disable QuickEdit regardless of whether "EditMode" was just 256 /**
214 * enabled. 257 * Always disable QuickEdit regardless of whether "EditMode" was just
215 */ 258 * enabled.
216 disableQuickEdit(); 259 */
217 }); 260 disableQuickEdit();
261 });
218 }); 262 });
219 263
220 $(document).on('keyup.settingstray', (e) => { 264 $(document).on('keyup.settingstray', e => {
221 if (isInEditMode() && e.keyCode === 27) { 265 if (isInEditMode() && e.keyCode === 27) {
222 Drupal.announce( 266 Drupal.announce(Drupal.t('Exited edit mode.'));
223 Drupal.t('Exited edit mode.'),
224 );
225 toggleEditMode(); 267 toggleEditMode();
226 } 268 }
227 }); 269 });
228 270
229 /** 271 /**
235 * @prop {Drupal~behaviorAttach} attach 277 * @prop {Drupal~behaviorAttach} attach
236 * Toggle the js-settings-tray-edit-mode class. 278 * Toggle the js-settings-tray-edit-mode class.
237 */ 279 */
238 Drupal.behaviors.toggleEditMode = { 280 Drupal.behaviors.toggleEditMode = {
239 attach() { 281 attach() {
240 $(toggleEditSelector).once('settingstray').on('click.settingstray', toggleEditMode); 282 $(toggleEditSelector)
283 .once('settingstray')
284 .on('click.settingstray', toggleEditMode);
241 }, 285 },
242 }; 286 };
243 287
244 // Manage Active editable class on opening and closing of the dialog. 288 // Manage Active editable class on opening and closing of the dialog.
245 $(window).on({ 289 $(window).on({
246 'dialog:beforecreate': (event, dialog, $element, settings) => { 290 'dialog:beforecreate': (event, dialog, $element, settings) => {
247 if ($element.is('#drupal-off-canvas')) { 291 if ($element.is('#drupal-off-canvas')) {
248 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable'); 292 $('body .settings-tray-active-editable').removeClass(
293 'settings-tray-active-editable',
294 );
249 const $activeElement = $(`#${settings.settingsTrayActiveEditableId}`); 295 const $activeElement = $(`#${settings.settingsTrayActiveEditableId}`);
250 if ($activeElement.length) { 296 if ($activeElement.length) {
251 $activeElement.addClass('settings-tray-active-editable'); 297 $activeElement.addClass('settings-tray-active-editable');
252 } 298 }
253 } 299 }
254 }, 300 },
255 'dialog:beforeclose': (event, dialog, $element) => { 301 'dialog:beforeclose': (event, dialog, $element) => {
256 if ($element.is('#drupal-off-canvas')) { 302 if ($element.is('#drupal-off-canvas')) {
257 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable'); 303 $('body .settings-tray-active-editable').removeClass(
304 'settings-tray-active-editable',
305 );
258 } 306 }
259 }, 307 },
260 }); 308 });
261 })(jQuery, Drupal); 309 })(jQuery, Drupal);