3 * Drupal's Settings Tray library.
9 const blockConfigureSelector = '[data-settings-tray-edit]';
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';
12 const contextualItemsSelector = '[data-contextual-id] a, [data-contextual-id] button';
13 const quickEditItemSelector = '[data-quickedit-entity-id]';
16 * Prevent default click events except contextual links.
18 * In edit mode the default action of click events is suppressed.
20 * @param {jQuery.Event} event
23 function preventClick(event) {
24 // Do not prevent contextual links.
25 if ($(event.target).closest('.contextual-links').length) {
28 event.preventDefault();
32 * Close any active toolbar tray before entering edit mode.
34 function closeToolbarTrays() {
35 $(Drupal.toolbar.models.toolbarModel.get('activeTab')).trigger('click');
39 * Disables the QuickEdit module editor if open.
41 function disableQuickEdit() {
42 $('.quickedit-toolbar button.action-cancel').trigger('click');
46 * Closes/removes off-canvas.
48 function closeOffCanvas() {
49 $('.ui-dialog-off-canvas .ui-dialog-titlebar-close').trigger('click');
53 * Gets all items that should be toggled with class during edit mode.
56 * Items that should be toggled.
58 function getItemsToToggle() {
59 return $(itemsToToggleSelector).not(contextualItemsSelector);
63 * Helper to switch edit mode state.
65 * @param {boolean} editMode
66 * True enable edit mode, false disable edit mode.
68 function setEditModeState(editMode) {
69 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 editMode = !!editMode;
73 const $editButton = $(toggleEditSelector);
77 $editButton.text(Drupal.t('Editing'));
80 $editables = $('[data-drupal-settingstray="editable"]').once('settingstray');
81 if ($editables.length) {
82 // Use event capture to prevent clicks on links.
83 document.querySelector('[data-off-canvas-main-canvas]').addEventListener('click', preventClick, true);
85 * When a click occurs try and find the settings-tray edit link
89 .not(contextualItemsSelector)
90 .on('click.settingstray', (e) => {
91 // Contextual links are allowed to function in Edit mode.
92 if ($(e.target).closest('.contextual').length || !localStorage.getItem('Drupal.contextualToolbar.isViewing')) {
95 $(e.currentTarget).find(blockConfigureSelector).trigger('click');
98 $(quickEditItemSelector)
99 .not(contextualItemsSelector)
100 .on('click.settingstray', (e) => {
102 * For all non-contextual links or the contextual QuickEdit link
103 * close the off-canvas dialog.
105 if (!$(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
108 // Do not trigger if target is quick edit link to avoid loop.
109 if ($(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
112 $(e.currentTarget).find('li.quickedit a').trigger('click');
116 // Disable edit mode.
118 $editables = $('[data-drupal-settingstray="editable"]').removeOnce('settingstray');
119 if ($editables.length) {
120 document.querySelector('[data-off-canvas-main-canvas]').removeEventListener('click', preventClick, true);
121 $editables.off('.settingstray');
122 $(quickEditItemSelector).off('.settingstray');
125 $editButton.text(Drupal.t('Edit'));
129 getItemsToToggle().toggleClass('js-settings-tray-edit-mode', editMode);
130 $('.edit-mode-inactive').toggleClass('visually-hidden', editMode);
134 * Helper to check the state of the settings-tray mode.
136 * @todo don't use a class for this.
139 * State of the settings-tray edit mode.
141 function isInEditMode() {
142 return $('#toolbar-bar').hasClass('js-settings-tray-edit-mode');
146 * Helper to toggle Edit mode.
148 function toggleEditMode() {
149 setEditModeState(!isInEditMode());
153 * Prepares Ajax links to work with off-canvas and Settings Tray module.
155 function prepareAjaxLinks() {
156 // Find all Ajax instances that use the 'off_canvas' renderer.
157 Drupal.ajax.instances
159 * If there is an element and the renderer is 'off_canvas' then we want
160 * to add our changes.
162 .filter(instance => instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas')
164 * Loop through all Ajax instances that use the 'off_canvas' renderer to
165 * set active editable ID.
167 .forEach((instance) => {
168 // Check to make sure existing dialogOptions aren't overridden.
169 if (!instance.options.data.hasOwnProperty('dialogOptions')) {
170 instance.options.data.dialogOptions = {};
172 instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id');
173 instance.progress = { type: 'fullscreen' };
178 * Reacts to contextual links being added.
180 * @param {jQuery.Event} event
181 * The `drupalContextualLinkAdded` event.
182 * @param {object} data
183 * An object containing the data relevant to the event.
185 * @listens event:drupalContextualLinkAdded
187 $(document).on('drupalContextualLinkAdded', (event, data) => {
189 * When contextual links are add we need to set extra properties on the
190 * instances in Drupal.ajax.instances for them to work with Edit Mode.
194 // When the first contextual link is added to the page set Edit Mode.
195 $('body').once('settings_tray.edit_mode_init').each(() => {
196 const editMode = localStorage.getItem('Drupal.contextualToolbar.isViewing') === 'false';
198 setEditModeState(true);
203 * Bind a listener to all 'Quick edit' links for blocks. Click "Edit"
204 * button in toolbar to force Contextual Edit which starts Settings Tray
207 data.$el.find(blockConfigureSelector)
208 .on('click.settingstray', () => {
209 if (!isInEditMode()) {
210 $(toggleEditSelector).trigger('click').trigger('click.settings_tray');
213 * Always disable QuickEdit regardless of whether "EditMode" was just
220 $(document).on('keyup.settingstray', (e) => {
221 if (isInEditMode() && e.keyCode === 27) {
223 Drupal.t('Exited edit mode.'),
230 * Toggle the js-settings-tray-edit-mode class on items that we want to
231 * disable while in edit mode.
233 * @type {Drupal~behavior}
235 * @prop {Drupal~behaviorAttach} attach
236 * Toggle the js-settings-tray-edit-mode class.
238 Drupal.behaviors.toggleEditMode = {
240 $(toggleEditSelector).once('settingstray').on('click.settingstray', toggleEditMode);
244 // Manage Active editable class on opening and closing of the dialog.
246 'dialog:beforecreate': (event, dialog, $element, settings) => {
247 if ($element.is('#drupal-off-canvas')) {
248 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');
249 const $activeElement = $(`#${settings.settingsTrayActiveEditableId}`);
250 if ($activeElement.length) {
251 $activeElement.addClass('settings-tray-active-editable');
255 'dialog:beforeclose': (event, dialog, $element) => {
256 if ($element.is('#drupal-off-canvas')) {
257 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');