3 * A Backbone view for the toolbar element. Listens to mouse & touch.
6 (function($, Drupal, drupalSettings, Backbone) {
7 Drupal.toolbar.ToolbarVisualView = Backbone.View.extend(
8 /** @lends Drupal.toolbar.ToolbarVisualView# */ {
10 * Event map for the `ToolbarVisualView`.
16 // Prevents delay and simulated mouse events.
17 const touchEndToClick = function(event) {
18 event.preventDefault();
23 'click .toolbar-bar .toolbar-tab .trigger': 'onTabClick',
24 'click .toolbar-toggle-orientation button':
25 'onOrientationToggleClick',
26 'touchend .toolbar-bar .toolbar-tab .trigger': touchEndToClick,
27 'touchend .toolbar-toggle-orientation button': touchEndToClick,
32 * Backbone view for the toolbar element. Listens to mouse & touch.
36 * @augments Backbone.View
38 * @param {object} options
39 * Options for the view object.
40 * @param {object} options.strings
41 * Various strings to use in the view.
44 this.strings = options.strings;
48 'change:activeTab change:orientation change:isOriented change:isTrayToggleVisible',
51 this.listenTo(this.model, 'change:mqMatches', this.onMediaQueryChange);
52 this.listenTo(this.model, 'change:offsets', this.adjustPlacement);
55 'change:activeTab change:orientation change:isOriented',
56 this.updateToolbarHeight,
59 // Add the tray orientation toggles.
61 .find('.toolbar-tray .toolbar-lining')
62 .append(Drupal.theme('toolbarOrientationToggle'));
64 // Trigger an activeTab change so that listening scripts can respond on
65 // page load. This will call render.
66 this.model.trigger('change:activeTab');
70 * Update the toolbar element height.
74 * @augments Backbone.View
76 updateToolbarHeight() {
77 const toolbarTabOuterHeight =
81 const toolbarTrayHorizontalOuterHeight =
82 $('.is-active.toolbar-tray-horizontal').outerHeight() || 0;
85 toolbarTabOuterHeight + toolbarTrayHorizontalOuterHeight,
89 'padding-top': this.model.get('height'),
92 this.triggerDisplace();
95 // Trigger a recalculation of viewport displacing elements. Use setTimeout
96 // to ensure this recalculation happens after changes to visual elements
100 Drupal.displace(true);
107 * @return {Drupal.toolbar.ToolbarVisualView}
108 * The `ToolbarVisualView` instance.
112 this.updateTrayOrientation();
113 this.updateBarAttributes();
115 $('body').removeClass('toolbar-loading');
117 // Load the subtrees if the orientation of the toolbar is changed to
118 // vertical. This condition responds to the case that the toolbar switches
119 // from horizontal to vertical orientation. The toolbar starts in a
120 // vertical orientation by default and then switches to horizontal during
121 // initialization if the media query conditions are met. Simply checking
122 // that the orientation is vertical here would result in the subtrees
123 // always being loaded, even when the toolbar initialization ultimately
124 // results in a horizontal orientation.
126 // @see Drupal.behaviors.toolbar.attach() where admin menu subtrees
127 // loading is invoked during initialization after media query conditions
128 // have been processed.
130 this.model.changed.orientation === 'vertical' ||
131 this.model.changed.activeTab
140 * Responds to a toolbar tab click.
142 * @param {jQuery.Event} event
143 * The event triggered.
146 // If this tab has a tray associated with it, it is considered an
148 if (event.target.hasAttribute('data-toolbar-tray')) {
149 const activeTab = this.model.get('activeTab');
150 const clickedTab = event.target;
152 // Set the event target as the active item if it is not already.
155 !activeTab || clickedTab !== activeTab ? clickedTab : null,
158 event.preventDefault();
159 event.stopPropagation();
164 * Toggles the orientation of a toolbar tray.
166 * @param {jQuery.Event} event
167 * The event triggered.
169 onOrientationToggleClick(event) {
170 const orientation = this.model.get('orientation');
171 // Determine the toggle-to orientation.
172 const antiOrientation =
173 orientation === 'vertical' ? 'horizontal' : 'vertical';
174 const locked = antiOrientation === 'vertical';
175 // Remember the locked state.
177 localStorage.setItem('Drupal.toolbar.trayVerticalLocked', 'true');
179 localStorage.removeItem('Drupal.toolbar.trayVerticalLocked');
185 orientation: antiOrientation,
193 event.preventDefault();
194 event.stopPropagation();
198 * Updates the display of the tabs: toggles a tab and the associated tray.
201 const $tab = $(this.model.get('activeTab'));
202 // Deactivate the previous tab.
203 $(this.model.previous('activeTab'))
204 .removeClass('is-active')
205 .prop('aria-pressed', false);
206 // Deactivate the previous tray.
207 $(this.model.previous('activeTray')).removeClass('is-active');
209 // Activate the selected tab.
210 if ($tab.length > 0) {
212 .addClass('is-active')
213 // Mark the tab as pressed.
214 .prop('aria-pressed', true);
215 const name = $tab.attr('data-toolbar-tray');
216 // Store the active tab name or remove the setting.
217 const id = $tab.get(0).id;
219 localStorage.setItem(
220 'Drupal.toolbar.activeTabID',
224 // Activate the associated tray.
225 const $tray = this.$el.find(
226 `[data-toolbar-tray="${name}"].toolbar-tray`,
229 $tray.addClass('is-active');
230 this.model.set('activeTray', $tray.get(0));
232 // There is no active tray.
233 this.model.set('activeTray', null);
236 // There is no active tray.
237 this.model.set('activeTray', null);
238 localStorage.removeItem('Drupal.toolbar.activeTabID');
243 * Update the attributes of the toolbar bar element.
245 updateBarAttributes() {
246 const isOriented = this.model.get('isOriented');
248 this.$el.find('.toolbar-bar').attr('data-offset-top', '');
250 this.$el.find('.toolbar-bar').removeAttr('data-offset-top');
252 // Toggle between a basic vertical view and a more sophisticated
253 // horizontal and vertical display of the toolbar bar and trays.
254 this.$el.toggleClass('toolbar-oriented', isOriented);
258 * Updates the orientation of the active tray if necessary.
260 updateTrayOrientation() {
261 const orientation = this.model.get('orientation');
263 // The antiOrientation is used to render the view of action buttons like
264 // the tray orientation toggle.
265 const antiOrientation =
266 orientation === 'vertical' ? 'horizontal' : 'vertical';
268 // Toggle toolbar's parent classes before other toolbar classes to avoid
269 // potential flicker and re-rendering.
271 .toggleClass('toolbar-vertical', orientation === 'vertical')
272 .toggleClass('toolbar-horizontal', orientation === 'horizontal');
275 antiOrientation === 'horizontal'
276 ? 'toolbar-tray-horizontal'
277 : 'toolbar-tray-vertical';
278 const $trays = this.$el
279 .find('.toolbar-tray')
280 .removeClass(removeClass)
281 .addClass(`toolbar-tray-${orientation}`);
283 // Update the tray orientation toggle button.
284 const iconClass = `toolbar-icon-toggle-${orientation}`;
285 const iconAntiClass = `toolbar-icon-toggle-${antiOrientation}`;
286 const $orientationToggle = this.$el
287 .find('.toolbar-toggle-orientation')
288 .toggle(this.model.get('isTrayToggleVisible'));
291 .val(antiOrientation)
292 .attr('title', this.strings[antiOrientation])
293 .text(this.strings[antiOrientation])
294 .removeClass(iconClass)
295 .addClass(iconAntiClass);
297 // Update data offset attributes for the trays.
298 const dir = document.documentElement.dir;
299 const edge = dir === 'rtl' ? 'right' : 'left';
300 // Remove data-offset attributes from the trays so they can be refreshed.
301 $trays.removeAttr('data-offset-left data-offset-right data-offset-top');
302 // If an active vertical tray exists, mark it as an offset element.
304 .filter('.toolbar-tray-vertical.is-active')
305 .attr(`data-offset-${edge}`, '');
306 // If an active horizontal tray exists, mark it as an offset element.
308 .filter('.toolbar-tray-horizontal.is-active')
309 .attr('data-offset-top', '');
313 * Sets the tops of the trays so that they align with the bottom of the bar.
316 const $trays = this.$el.find('.toolbar-tray');
317 if (!this.model.get('isOriented')) {
319 .removeClass('toolbar-tray-horizontal')
320 .addClass('toolbar-tray-vertical');
325 * Calls the endpoint URI that builds an AJAX command with the rendered
328 * The rendered admin menu subtrees HTML is cached on the client in
329 * localStorage until the cache of the admin menu subtrees on the server-
330 * side is invalidated. The subtreesHash is stored in localStorage as well
331 * and compared to the subtreesHash in drupalSettings to determine when the
332 * admin menu subtrees cache has been invalidated.
335 const $activeTab = $(this.model.get('activeTab'));
336 const orientation = this.model.get('orientation');
337 // Only load and render the admin menu subtrees if:
338 // (1) They have not been loaded yet.
339 // (2) The active tab is the administration menu tab, indicated by the
340 // presence of the data-drupal-subtrees attribute.
341 // (3) The orientation of the tray is vertical.
343 !this.model.get('areSubtreesLoaded') &&
344 typeof $activeTab.data('drupal-subtrees') !== 'undefined' &&
345 orientation === 'vertical'
347 const subtreesHash = drupalSettings.toolbar.subtreesHash;
348 const theme = drupalSettings.ajaxPageState.theme;
349 const endpoint = Drupal.url(`toolbar/subtrees/${subtreesHash}`);
350 const cachedSubtreesHash = localStorage.getItem(
351 `Drupal.toolbar.subtreesHash.${theme}`,
353 const cachedSubtrees = JSON.parse(
354 localStorage.getItem(`Drupal.toolbar.subtrees.${theme}`),
356 const isVertical = this.model.get('orientation') === 'vertical';
357 // If we have the subtrees in localStorage and the subtree hash has not
358 // changed, then use the cached data.
361 subtreesHash === cachedSubtreesHash &&
364 Drupal.toolbar.setSubtrees.resolve(cachedSubtrees);
366 // Only make the call to get the subtrees if the orientation of the
367 // toolbar is vertical.
368 else if (isVertical) {
369 // Remove the cached menu information.
370 localStorage.removeItem(`Drupal.toolbar.subtreesHash.${theme}`);
371 localStorage.removeItem(`Drupal.toolbar.subtrees.${theme}`);
372 // The AJAX response's command will trigger the resolve method of the
373 // Drupal.toolbar.setSubtrees Promise.
374 Drupal.ajax({ url: endpoint }).execute();
375 // Cache the hash for the subtrees locally.
376 localStorage.setItem(
377 `Drupal.toolbar.subtreesHash.${theme}`,
385 })(jQuery, Drupal, drupalSettings, Backbone);