3 * Drupal's Settings Tray library.
9 const blockConfigureSelector = '[data-settings-tray-edit]';
10 const toggleEditSelector = '[data-drupal-settingstray="toggle"]';
11 const itemsToToggleSelector =
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';
15 const quickEditItemSelector = '[data-quickedit-entity-id]';
18 * Prevent default click events except contextual links.
20 * In edit mode the default action of click events is suppressed.
22 * @param {jQuery.Event} event
25 function preventClick(event) {
26 // Do not prevent contextual links.
27 if ($(event.target).closest('.contextual-links').length) {
30 event.preventDefault();
34 * Close any active toolbar tray before entering edit mode.
36 function closeToolbarTrays() {
37 $(Drupal.toolbar.models.toolbarModel.get('activeTab')).trigger('click');
41 * Disables the QuickEdit module editor if open.
43 function disableQuickEdit() {
44 $('.quickedit-toolbar button.action-cancel').trigger('click');
48 * Closes/removes off-canvas.
50 function closeOffCanvas() {
51 $('.ui-dialog-off-canvas .ui-dialog-titlebar-close').trigger('click');
55 * Gets all items that should be toggled with class during edit mode.
58 * Items that should be toggled.
60 function getItemsToToggle() {
61 return $(itemsToToggleSelector).not(contextualItemsSelector);
65 * Helper to switch edit mode state.
67 * @param {boolean} editMode
68 * True enable edit mode, false disable edit mode.
70 function setEditModeState(editMode) {
71 if (!document.querySelector('[data-off-canvas-main-canvas]')) {
73 'data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig',
76 editMode = !!editMode;
77 const $editButton = $(toggleEditSelector);
81 $editButton.text(Drupal.t('Editing'));
84 $editables = $('[data-drupal-settingstray="editable"]').once(
87 if ($editables.length) {
88 // Use event capture to prevent clicks on links.
90 .querySelector('[data-off-canvas-main-canvas]')
91 .addEventListener('click', preventClick, true);
93 * When a click occurs try and find the settings-tray edit link
96 $editables.not(contextualItemsSelector).on('click.settingstray', e => {
97 // Contextual links are allowed to function in Edit mode.
99 $(e.target).closest('.contextual').length ||
100 !localStorage.getItem('Drupal.contextualToolbar.isViewing')
105 .find(blockConfigureSelector)
109 $(quickEditItemSelector)
110 .not(contextualItemsSelector)
111 .on('click.settingstray', e => {
113 * For all non-contextual links or the contextual QuickEdit link
114 * close the off-canvas dialog.
119 .hasClass('contextual') ||
122 .hasClass('quickedit')
126 // Do not trigger if target is quick edit link to avoid loop.
130 .hasClass('contextual') ||
133 .hasClass('quickedit')
138 .find('li.quickedit a')
143 // Disable edit mode.
145 $editables = $('[data-drupal-settingstray="editable"]').removeOnce(
148 if ($editables.length) {
150 .querySelector('[data-off-canvas-main-canvas]')
151 .removeEventListener('click', preventClick, true);
152 $editables.off('.settingstray');
153 $(quickEditItemSelector).off('.settingstray');
156 $editButton.text(Drupal.t('Edit'));
160 getItemsToToggle().toggleClass('js-settings-tray-edit-mode', editMode);
161 $('.edit-mode-inactive').toggleClass('visually-hidden', editMode);
165 * Helper to check the state of the settings-tray mode.
167 * @todo don't use a class for this.
170 * State of the settings-tray edit mode.
172 function isInEditMode() {
173 return $('#toolbar-bar').hasClass('js-settings-tray-edit-mode');
177 * Helper to toggle Edit mode.
179 function toggleEditMode() {
180 setEditModeState(!isInEditMode());
184 * Prepares Ajax links to work with off-canvas and Settings Tray module.
186 function prepareAjaxLinks() {
187 // Find all Ajax instances that use the 'off_canvas' renderer.
188 Drupal.ajax.instances
190 * If there is an element and the renderer is 'off_canvas' then we want
191 * to add our changes.
196 $(instance.element).attr('data-dialog-renderer') === 'off_canvas',
199 * Loop through all Ajax instances that use the 'off_canvas' renderer to
200 * set active editable ID.
202 .forEach(instance => {
203 // Check to make sure existing dialogOptions aren't overridden.
204 if (!instance.options.data.hasOwnProperty('dialogOptions')) {
205 instance.options.data.dialogOptions = {};
207 instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(
210 .parents('.settings-tray-editable')
212 instance.progress = { type: 'fullscreen' };
217 * Reacts to contextual links being added.
219 * @param {jQuery.Event} event
220 * The `drupalContextualLinkAdded` event.
221 * @param {object} data
222 * An object containing the data relevant to the event.
224 * @listens event:drupalContextualLinkAdded
226 $(document).on('drupalContextualLinkAdded', (event, data) => {
228 * When contextual links are add we need to set extra properties on the
229 * instances in Drupal.ajax.instances for them to work with Edit Mode.
233 // When the first contextual link is added to the page set Edit Mode.
235 .once('settings_tray.edit_mode_init')
238 localStorage.getItem('Drupal.contextualToolbar.isViewing') ===
241 setEditModeState(true);
246 * Bind a listener to all 'Quick edit' links for blocks. Click "Edit"
247 * button in toolbar to force Contextual Edit which starts Settings Tray
250 data.$el.find(blockConfigureSelector).on('click.settingstray', () => {
251 if (!isInEditMode()) {
252 $(toggleEditSelector)
254 .trigger('click.settings_tray');
257 * Always disable QuickEdit regardless of whether "EditMode" was just
264 $(document).on('keyup.settingstray', e => {
265 if (isInEditMode() && e.keyCode === 27) {
266 Drupal.announce(Drupal.t('Exited edit mode.'));
272 * Toggle the js-settings-tray-edit-mode class on items that we want to
273 * disable while in edit mode.
275 * @type {Drupal~behavior}
277 * @prop {Drupal~behaviorAttach} attach
278 * Toggle the js-settings-tray-edit-mode class.
280 Drupal.behaviors.toggleEditMode = {
282 $(toggleEditSelector)
283 .once('settingstray')
284 .on('click.settingstray', toggleEditMode);
288 // Manage Active editable class on opening and closing of the dialog.
290 'dialog:beforecreate': (event, dialog, $element, settings) => {
291 if ($element.is('#drupal-off-canvas')) {
292 $('body .settings-tray-active-editable').removeClass(
293 'settings-tray-active-editable',
295 const $activeElement = $(`#${settings.settingsTrayActiveEditableId}`);
296 if ($activeElement.length) {
297 $activeElement.addClass('settings-tray-active-editable');
301 'dialog:beforeclose': (event, dialog, $element) => {
302 if ($element.is('#drupal-off-canvas')) {
303 $('body .settings-tray-active-editable').removeClass(
304 'settings-tray-active-editable',