comparison core/modules/settings_tray/js/settings_tray.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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) {
9 var blockConfigureSelector = '[data-settings-tray-edit]';
10 var toggleEditSelector = '[data-drupal-settingstray="toggle"]';
11 var itemsToToggleSelector = '[data-off-canvas-main-canvas], #toolbar-bar, [data-drupal-settingstray="editable"] a, [data-drupal-settingstray="editable"] button';
12 var contextualItemsSelector = '[data-contextual-id] a, [data-contextual-id] button';
13 var quickEditItemSelector = '[data-quickedit-entity-id]';
14
15 function preventClick(event) {
16 if ($(event.target).closest('.contextual-links').length) {
17 return;
18 }
19 event.preventDefault();
20 }
21
22 function closeToolbarTrays() {
23 $(Drupal.toolbar.models.toolbarModel.get('activeTab')).trigger('click');
24 }
25
26 function disableQuickEdit() {
27 $('.quickedit-toolbar button.action-cancel').trigger('click');
28 }
29
30 function closeOffCanvas() {
31 $('.ui-dialog-off-canvas .ui-dialog-titlebar-close').trigger('click');
32 }
33
34 function getItemsToToggle() {
35 return $(itemsToToggleSelector).not(contextualItemsSelector);
36 }
37
38 function setEditModeState(editMode) {
39 if (!document.querySelector('[data-off-canvas-main-canvas]')) {
40 throw new Error('data-off-canvas-main-canvas is missing from settings-tray-page-wrapper.html.twig');
41 }
42 editMode = !!editMode;
43 var $editButton = $(toggleEditSelector);
44 var $editables = void 0;
45
46 if (editMode) {
47 $editButton.text(Drupal.t('Editing'));
48 closeToolbarTrays();
49
50 $editables = $('[data-drupal-settingstray="editable"]').once('settingstray');
51 if ($editables.length) {
52 document.querySelector('[data-off-canvas-main-canvas]').addEventListener('click', preventClick, true);
53
54 $editables.not(contextualItemsSelector).on('click.settingstray', function (e) {
55 if ($(e.target).closest('.contextual').length || !localStorage.getItem('Drupal.contextualToolbar.isViewing')) {
56 return;
57 }
58 $(e.currentTarget).find(blockConfigureSelector).trigger('click');
59 disableQuickEdit();
60 });
61 $(quickEditItemSelector).not(contextualItemsSelector).on('click.settingstray', function (e) {
62 if (!$(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
63 closeOffCanvas();
64 }
65
66 if ($(e.target).parent().hasClass('contextual') || $(e.target).parent().hasClass('quickedit')) {
67 return;
68 }
69 $(e.currentTarget).find('li.quickedit a').trigger('click');
70 });
71 }
72 } else {
73 $editables = $('[data-drupal-settingstray="editable"]').removeOnce('settingstray');
74 if ($editables.length) {
75 document.querySelector('[data-off-canvas-main-canvas]').removeEventListener('click', preventClick, true);
76 $editables.off('.settingstray');
77 $(quickEditItemSelector).off('.settingstray');
78 }
79
80 $editButton.text(Drupal.t('Edit'));
81 closeOffCanvas();
82 disableQuickEdit();
83 }
84 getItemsToToggle().toggleClass('js-settings-tray-edit-mode', editMode);
85 $('.edit-mode-inactive').toggleClass('visually-hidden', editMode);
86 }
87
88 function isInEditMode() {
89 return $('#toolbar-bar').hasClass('js-settings-tray-edit-mode');
90 }
91
92 function toggleEditMode() {
93 setEditModeState(!isInEditMode());
94 }
95
96 function prepareAjaxLinks() {
97 Drupal.ajax.instances.filter(function (instance) {
98 return instance && $(instance.element).attr('data-dialog-renderer') === 'off_canvas';
99 }).forEach(function (instance) {
100 if (!instance.options.data.hasOwnProperty('dialogOptions')) {
101 instance.options.data.dialogOptions = {};
102 }
103 instance.options.data.dialogOptions.settingsTrayActiveEditableId = $(instance.element).parents('.settings-tray-editable').attr('id');
104 instance.progress = { type: 'fullscreen' };
105 });
106 }
107
108 $(document).on('drupalContextualLinkAdded', function (event, data) {
109 prepareAjaxLinks();
110
111 $('body').once('settings_tray.edit_mode_init').each(function () {
112 var editMode = localStorage.getItem('Drupal.contextualToolbar.isViewing') === 'false';
113 if (editMode) {
114 setEditModeState(true);
115 }
116 });
117
118 data.$el.find(blockConfigureSelector).on('click.settingstray', function () {
119 if (!isInEditMode()) {
120 $(toggleEditSelector).trigger('click').trigger('click.settings_tray');
121 }
122
123 disableQuickEdit();
124 });
125 });
126
127 $(document).on('keyup.settingstray', function (e) {
128 if (isInEditMode() && e.keyCode === 27) {
129 Drupal.announce(Drupal.t('Exited edit mode.'));
130 toggleEditMode();
131 }
132 });
133
134 Drupal.behaviors.toggleEditMode = {
135 attach: function attach() {
136 $(toggleEditSelector).once('settingstray').on('click.settingstray', toggleEditMode);
137 }
138 };
139
140 $(window).on({
141 'dialog:beforecreate': function dialogBeforecreate(event, dialog, $element, settings) {
142 if ($element.is('#drupal-off-canvas')) {
143 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');
144 var $activeElement = $('#' + settings.settingsTrayActiveEditableId);
145 if ($activeElement.length) {
146 $activeElement.addClass('settings-tray-active-editable');
147 }
148 }
149 },
150 'dialog:beforeclose': function dialogBeforeclose(event, dialog, $element) {
151 if ($element.is('#drupal-off-canvas')) {
152 $('body .settings-tray-active-editable').removeClass('settings-tray-active-editable');
153 }
154 }
155 });
156 })(jQuery, Drupal);