Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Defines the behavior of the Drupal administration toolbar.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
6 (function ($, Drupal, drupalSettings) {
|
Chris@0
|
7 // Merge run-time settings with the defaults.
|
Chris@0
|
8 const options = $.extend(
|
Chris@0
|
9 {
|
Chris@0
|
10 breakpoints: {
|
Chris@0
|
11 'toolbar.narrow': '',
|
Chris@0
|
12 'toolbar.standard': '',
|
Chris@0
|
13 'toolbar.wide': '',
|
Chris@0
|
14 },
|
Chris@0
|
15 },
|
Chris@0
|
16 drupalSettings.toolbar,
|
Chris@0
|
17 // Merge strings on top of drupalSettings so that they are not mutable.
|
Chris@0
|
18 {
|
Chris@0
|
19 strings: {
|
Chris@0
|
20 horizontal: Drupal.t('Horizontal orientation'),
|
Chris@0
|
21 vertical: Drupal.t('Vertical orientation'),
|
Chris@0
|
22 },
|
Chris@0
|
23 },
|
Chris@0
|
24 );
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Registers tabs with the toolbar.
|
Chris@0
|
28 *
|
Chris@0
|
29 * The Drupal toolbar allows modules to register top-level tabs. These may
|
Chris@0
|
30 * point directly to a resource or toggle the visibility of a tray.
|
Chris@0
|
31 *
|
Chris@0
|
32 * Modules register tabs with hook_toolbar().
|
Chris@0
|
33 *
|
Chris@0
|
34 * @type {Drupal~behavior}
|
Chris@0
|
35 *
|
Chris@0
|
36 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
37 * Attaches the toolbar rendering functionality to the toolbar element.
|
Chris@0
|
38 */
|
Chris@0
|
39 Drupal.behaviors.toolbar = {
|
Chris@0
|
40 attach(context) {
|
Chris@0
|
41 // Verify that the user agent understands media queries. Complex admin
|
Chris@0
|
42 // toolbar layouts require media query support.
|
Chris@0
|
43 if (!window.matchMedia('only screen').matches) {
|
Chris@0
|
44 return;
|
Chris@0
|
45 }
|
Chris@0
|
46 // Process the administrative toolbar.
|
Chris@0
|
47 $(context).find('#toolbar-administration').once('toolbar').each(function () {
|
Chris@0
|
48 // Establish the toolbar models and views.
|
Chris@0
|
49 const model = Drupal.toolbar.models.toolbarModel = new Drupal.toolbar.ToolbarModel({
|
Chris@0
|
50 locked: JSON.parse(localStorage.getItem('Drupal.toolbar.trayVerticalLocked')),
|
Chris@0
|
51 activeTab: document.getElementById(JSON.parse(localStorage.getItem('Drupal.toolbar.activeTabID'))),
|
Chris@0
|
52 height: $('#toolbar-administration').outerHeight(),
|
Chris@0
|
53 });
|
Chris@0
|
54
|
Chris@0
|
55 // Attach a listener to the configured media query breakpoints.
|
Chris@0
|
56 // Executes it before Drupal.toolbar.views to avoid extra rendering.
|
Chris@0
|
57 for (const label in options.breakpoints) {
|
Chris@0
|
58 if (options.breakpoints.hasOwnProperty(label)) {
|
Chris@0
|
59 const mq = options.breakpoints[label];
|
Chris@0
|
60 const mql = Drupal.toolbar.mql[label] = window.matchMedia(mq);
|
Chris@0
|
61 // Curry the model and the label of the media query breakpoint to
|
Chris@0
|
62 // the mediaQueryChangeHandler function.
|
Chris@0
|
63 mql.addListener(Drupal.toolbar.mediaQueryChangeHandler.bind(null, model, label));
|
Chris@0
|
64 // Fire the mediaQueryChangeHandler for each configured breakpoint
|
Chris@0
|
65 // so that they process once.
|
Chris@0
|
66 Drupal.toolbar.mediaQueryChangeHandler.call(null, model, label, mql);
|
Chris@0
|
67 }
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 Drupal.toolbar.views.toolbarVisualView = new Drupal.toolbar.ToolbarVisualView({
|
Chris@0
|
71 el: this,
|
Chris@0
|
72 model,
|
Chris@0
|
73 strings: options.strings,
|
Chris@0
|
74 });
|
Chris@0
|
75 Drupal.toolbar.views.toolbarAuralView = new Drupal.toolbar.ToolbarAuralView({
|
Chris@0
|
76 el: this,
|
Chris@0
|
77 model,
|
Chris@0
|
78 strings: options.strings,
|
Chris@0
|
79 });
|
Chris@0
|
80 Drupal.toolbar.views.bodyVisualView = new Drupal.toolbar.BodyVisualView({
|
Chris@0
|
81 el: this,
|
Chris@0
|
82 model,
|
Chris@0
|
83 });
|
Chris@0
|
84
|
Chris@0
|
85 // Force layout render to fix mobile view. Only needed on load, not
|
Chris@0
|
86 // for every media query match.
|
Chris@0
|
87 model.trigger('change:isFixed', model, model.get('isFixed'));
|
Chris@0
|
88 model.trigger('change:activeTray', model, model.get('activeTray'));
|
Chris@0
|
89
|
Chris@0
|
90 // Render collapsible menus.
|
Chris@0
|
91 const menuModel = Drupal.toolbar.models.menuModel = new Drupal.toolbar.MenuModel();
|
Chris@0
|
92 Drupal.toolbar.views.menuVisualView = new Drupal.toolbar.MenuVisualView({
|
Chris@0
|
93 el: $(this).find('.toolbar-menu-administration').get(0),
|
Chris@0
|
94 model: menuModel,
|
Chris@0
|
95 strings: options.strings,
|
Chris@0
|
96 });
|
Chris@0
|
97
|
Chris@0
|
98 // Handle the resolution of Drupal.toolbar.setSubtrees.
|
Chris@0
|
99 // This is handled with a deferred so that the function may be invoked
|
Chris@0
|
100 // asynchronously.
|
Chris@0
|
101 Drupal.toolbar.setSubtrees.done((subtrees) => {
|
Chris@0
|
102 menuModel.set('subtrees', subtrees);
|
Chris@0
|
103 const theme = drupalSettings.ajaxPageState.theme;
|
Chris@0
|
104 localStorage.setItem(`Drupal.toolbar.subtrees.${theme}`, JSON.stringify(subtrees));
|
Chris@0
|
105 // Indicate on the toolbarModel that subtrees are now loaded.
|
Chris@0
|
106 model.set('areSubtreesLoaded', true);
|
Chris@0
|
107 });
|
Chris@0
|
108
|
Chris@0
|
109 // Trigger an initial attempt to load menu subitems. This first attempt
|
Chris@0
|
110 // is made after the media query handlers have had an opportunity to
|
Chris@0
|
111 // process. The toolbar starts in the vertical orientation by default,
|
Chris@0
|
112 // unless the viewport is wide enough to accommodate a horizontal
|
Chris@0
|
113 // orientation. Thus we give the Toolbar a chance to determine if it
|
Chris@0
|
114 // should be set to horizontal orientation before attempting to load
|
Chris@0
|
115 // menu subtrees.
|
Chris@0
|
116 Drupal.toolbar.views.toolbarVisualView.loadSubtrees();
|
Chris@0
|
117
|
Chris@0
|
118 $(document)
|
Chris@0
|
119 // Update the model when the viewport offset changes.
|
Chris@0
|
120 .on('drupalViewportOffsetChange.toolbar', (event, offsets) => {
|
Chris@0
|
121 model.set('offsets', offsets);
|
Chris@0
|
122 });
|
Chris@0
|
123
|
Chris@0
|
124 // Broadcast model changes to other modules.
|
Chris@0
|
125 model
|
Chris@0
|
126 .on('change:orientation', (model, orientation) => {
|
Chris@0
|
127 $(document).trigger('drupalToolbarOrientationChange', orientation);
|
Chris@0
|
128 })
|
Chris@0
|
129 .on('change:activeTab', (model, tab) => {
|
Chris@0
|
130 $(document).trigger('drupalToolbarTabChange', tab);
|
Chris@0
|
131 })
|
Chris@0
|
132 .on('change:activeTray', (model, tray) => {
|
Chris@0
|
133 $(document).trigger('drupalToolbarTrayChange', tray);
|
Chris@0
|
134 });
|
Chris@0
|
135
|
Chris@0
|
136 // If the toolbar's orientation is horizontal and no active tab is
|
Chris@0
|
137 // defined then show the tray of the first toolbar tab by default (but
|
Chris@0
|
138 // not the first 'Home' toolbar tab).
|
Chris@0
|
139 if (Drupal.toolbar.models.toolbarModel.get('orientation') === 'horizontal' && Drupal.toolbar.models.toolbarModel.get('activeTab') === null) {
|
Chris@0
|
140 Drupal.toolbar.models.toolbarModel.set({
|
Chris@0
|
141 activeTab: $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a').get(0),
|
Chris@0
|
142 });
|
Chris@0
|
143 }
|
Chris@0
|
144 });
|
Chris@0
|
145 },
|
Chris@0
|
146 };
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * Toolbar methods of Backbone objects.
|
Chris@0
|
150 *
|
Chris@0
|
151 * @namespace
|
Chris@0
|
152 */
|
Chris@0
|
153 Drupal.toolbar = {
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * A hash of View instances.
|
Chris@0
|
157 *
|
Chris@0
|
158 * @type {object.<string, Backbone.View>}
|
Chris@0
|
159 */
|
Chris@0
|
160 views: {},
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * A hash of Model instances.
|
Chris@0
|
164 *
|
Chris@0
|
165 * @type {object.<string, Backbone.Model>}
|
Chris@0
|
166 */
|
Chris@0
|
167 models: {},
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * A hash of MediaQueryList objects tracked by the toolbar.
|
Chris@0
|
171 *
|
Chris@0
|
172 * @type {object.<string, object>}
|
Chris@0
|
173 */
|
Chris@0
|
174 mql: {},
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * Accepts a list of subtree menu elements.
|
Chris@0
|
178 *
|
Chris@0
|
179 * A deferred object that is resolved by an inlined JavaScript callback.
|
Chris@0
|
180 *
|
Chris@0
|
181 * @type {jQuery.Deferred}
|
Chris@0
|
182 *
|
Chris@0
|
183 * @see toolbar_subtrees_jsonp().
|
Chris@0
|
184 */
|
Chris@0
|
185 setSubtrees: new $.Deferred(),
|
Chris@0
|
186
|
Chris@0
|
187 /**
|
Chris@0
|
188 * Respond to configured narrow media query changes.
|
Chris@0
|
189 *
|
Chris@0
|
190 * @param {Drupal.toolbar.ToolbarModel} model
|
Chris@0
|
191 * A toolbar model
|
Chris@0
|
192 * @param {string} label
|
Chris@0
|
193 * Media query label.
|
Chris@0
|
194 * @param {object} mql
|
Chris@0
|
195 * A MediaQueryList object.
|
Chris@0
|
196 */
|
Chris@0
|
197 mediaQueryChangeHandler(model, label, mql) {
|
Chris@0
|
198 switch (label) {
|
Chris@0
|
199 case 'toolbar.narrow':
|
Chris@0
|
200 model.set({
|
Chris@0
|
201 isOriented: mql.matches,
|
Chris@0
|
202 isTrayToggleVisible: false,
|
Chris@0
|
203 });
|
Chris@0
|
204 // If the toolbar doesn't have an explicit orientation yet, or if the
|
Chris@0
|
205 // narrow media query doesn't match then set the orientation to
|
Chris@0
|
206 // vertical.
|
Chris@0
|
207 if (!mql.matches || !model.get('orientation')) {
|
Chris@0
|
208 model.set({ orientation: 'vertical' }, { validate: true });
|
Chris@0
|
209 }
|
Chris@0
|
210 break;
|
Chris@0
|
211
|
Chris@0
|
212 case 'toolbar.standard':
|
Chris@0
|
213 model.set({
|
Chris@0
|
214 isFixed: mql.matches,
|
Chris@0
|
215 });
|
Chris@0
|
216 break;
|
Chris@0
|
217
|
Chris@0
|
218 case 'toolbar.wide':
|
Chris@0
|
219 model.set({
|
Chris@0
|
220 orientation: ((mql.matches && !model.get('locked')) ? 'horizontal' : 'vertical'),
|
Chris@0
|
221 }, { validate: true });
|
Chris@0
|
222 // The tray orientation toggle visibility does not need to be
|
Chris@0
|
223 // validated.
|
Chris@0
|
224 model.set({
|
Chris@0
|
225 isTrayToggleVisible: mql.matches,
|
Chris@0
|
226 });
|
Chris@0
|
227 break;
|
Chris@0
|
228
|
Chris@0
|
229 default:
|
Chris@0
|
230 break;
|
Chris@0
|
231 }
|
Chris@0
|
232 },
|
Chris@0
|
233 };
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * A toggle is an interactive element often bound to a click handler.
|
Chris@0
|
237 *
|
Chris@0
|
238 * @return {string}
|
Chris@0
|
239 * A string representing a DOM fragment.
|
Chris@0
|
240 */
|
Chris@0
|
241 Drupal.theme.toolbarOrientationToggle = function () {
|
Chris@0
|
242 return '<div class="toolbar-toggle-orientation"><div class="toolbar-lining">' +
|
Chris@0
|
243 '<button class="toolbar-icon" type="button"></button>' +
|
Chris@0
|
244 '</div></div>';
|
Chris@0
|
245 };
|
Chris@0
|
246
|
Chris@0
|
247 /**
|
Chris@0
|
248 * Ajax command to set the toolbar subtrees.
|
Chris@0
|
249 *
|
Chris@0
|
250 * @param {Drupal.Ajax} ajax
|
Chris@0
|
251 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}.
|
Chris@0
|
252 * @param {object} response
|
Chris@0
|
253 * JSON response from the Ajax request.
|
Chris@0
|
254 * @param {number} [status]
|
Chris@0
|
255 * XMLHttpRequest status.
|
Chris@0
|
256 */
|
Chris@0
|
257 Drupal.AjaxCommands.prototype.setToolbarSubtrees = function (ajax, response, status) {
|
Chris@0
|
258 Drupal.toolbar.setSubtrees.resolve(response.subtrees);
|
Chris@0
|
259 };
|
Chris@0
|
260 }(jQuery, Drupal, drupalSettings));
|