3 * Defines the behavior of the Drupal administration toolbar.
6 (function ($, Drupal, drupalSettings) {
7 // Merge run-time settings with the defaults.
8 const options = $.extend(
12 'toolbar.standard': '',
16 drupalSettings.toolbar,
17 // Merge strings on top of drupalSettings so that they are not mutable.
20 horizontal: Drupal.t('Horizontal orientation'),
21 vertical: Drupal.t('Vertical orientation'),
27 * Registers tabs with the toolbar.
29 * The Drupal toolbar allows modules to register top-level tabs. These may
30 * point directly to a resource or toggle the visibility of a tray.
32 * Modules register tabs with hook_toolbar().
34 * @type {Drupal~behavior}
36 * @prop {Drupal~behaviorAttach} attach
37 * Attaches the toolbar rendering functionality to the toolbar element.
39 Drupal.behaviors.toolbar = {
41 // Verify that the user agent understands media queries. Complex admin
42 // toolbar layouts require media query support.
43 if (!window.matchMedia('only screen').matches) {
46 // Process the administrative toolbar.
47 $(context).find('#toolbar-administration').once('toolbar').each(function () {
48 // Establish the toolbar models and views.
49 const model = new Drupal.toolbar.ToolbarModel({
50 locked: JSON.parse(localStorage.getItem('Drupal.toolbar.trayVerticalLocked')),
51 activeTab: document.getElementById(JSON.parse(localStorage.getItem('Drupal.toolbar.activeTabID'))),
52 height: $('#toolbar-administration').outerHeight(),
55 Drupal.toolbar.models.toolbarModel = model;
57 // Attach a listener to the configured media query breakpoints.
58 // Executes it before Drupal.toolbar.views to avoid extra rendering.
59 Object.keys(options.breakpoints).forEach((label) => {
60 const mq = options.breakpoints[label];
61 const mql = window.matchMedia(mq);
62 Drupal.toolbar.mql[label] = mql;
63 // Curry the model and the label of the media query breakpoint to
64 // the mediaQueryChangeHandler function.
65 mql.addListener(Drupal.toolbar.mediaQueryChangeHandler.bind(null, model, label));
66 // Fire the mediaQueryChangeHandler for each configured breakpoint
67 // so that they process once.
68 Drupal.toolbar.mediaQueryChangeHandler.call(null, model, label, mql);
71 Drupal.toolbar.views.toolbarVisualView = new Drupal.toolbar.ToolbarVisualView({
74 strings: options.strings,
76 Drupal.toolbar.views.toolbarAuralView = new Drupal.toolbar.ToolbarAuralView({
79 strings: options.strings,
81 Drupal.toolbar.views.bodyVisualView = new Drupal.toolbar.BodyVisualView({
86 // Force layout render to fix mobile view. Only needed on load, not
87 // for every media query match.
88 model.trigger('change:isFixed', model, model.get('isFixed'));
89 model.trigger('change:activeTray', model, model.get('activeTray'));
91 // Render collapsible menus.
92 const menuModel = new Drupal.toolbar.MenuModel();
93 Drupal.toolbar.models.menuModel = menuModel;
94 Drupal.toolbar.views.menuVisualView = new Drupal.toolbar.MenuVisualView({
95 el: $(this).find('.toolbar-menu-administration').get(0),
97 strings: options.strings,
100 // Handle the resolution of Drupal.toolbar.setSubtrees.
101 // This is handled with a deferred so that the function may be invoked
103 Drupal.toolbar.setSubtrees.done((subtrees) => {
104 menuModel.set('subtrees', subtrees);
105 const theme = drupalSettings.ajaxPageState.theme;
106 localStorage.setItem(`Drupal.toolbar.subtrees.${theme}`, JSON.stringify(subtrees));
107 // Indicate on the toolbarModel that subtrees are now loaded.
108 model.set('areSubtreesLoaded', true);
111 // Trigger an initial attempt to load menu subitems. This first attempt
112 // is made after the media query handlers have had an opportunity to
113 // process. The toolbar starts in the vertical orientation by default,
114 // unless the viewport is wide enough to accommodate a horizontal
115 // orientation. Thus we give the Toolbar a chance to determine if it
116 // should be set to horizontal orientation before attempting to load
118 Drupal.toolbar.views.toolbarVisualView.loadSubtrees();
121 // Update the model when the viewport offset changes.
122 .on('drupalViewportOffsetChange.toolbar', (event, offsets) => {
123 model.set('offsets', offsets);
126 // Broadcast model changes to other modules.
128 .on('change:orientation', (model, orientation) => {
129 $(document).trigger('drupalToolbarOrientationChange', orientation);
131 .on('change:activeTab', (model, tab) => {
132 $(document).trigger('drupalToolbarTabChange', tab);
134 .on('change:activeTray', (model, tray) => {
135 $(document).trigger('drupalToolbarTrayChange', tray);
138 // If the toolbar's orientation is horizontal and no active tab is
139 // defined then show the tray of the first toolbar tab by default (but
140 // not the first 'Home' toolbar tab).
141 if (Drupal.toolbar.models.toolbarModel.get('orientation') === 'horizontal' && Drupal.toolbar.models.toolbarModel.get('activeTab') === null) {
142 Drupal.toolbar.models.toolbarModel.set({
143 activeTab: $('.toolbar-bar .toolbar-tab:not(.home-toolbar-tab) a').get(0),
151 * Toolbar methods of Backbone objects.
158 * A hash of View instances.
160 * @type {object.<string, Backbone.View>}
165 * A hash of Model instances.
167 * @type {object.<string, Backbone.Model>}
172 * A hash of MediaQueryList objects tracked by the toolbar.
174 * @type {object.<string, object>}
179 * Accepts a list of subtree menu elements.
181 * A deferred object that is resolved by an inlined JavaScript callback.
183 * @type {jQuery.Deferred}
185 * @see toolbar_subtrees_jsonp().
187 setSubtrees: new $.Deferred(),
190 * Respond to configured narrow media query changes.
192 * @param {Drupal.toolbar.ToolbarModel} model
194 * @param {string} label
196 * @param {object} mql
197 * A MediaQueryList object.
199 mediaQueryChangeHandler(model, label, mql) {
201 case 'toolbar.narrow':
203 isOriented: mql.matches,
204 isTrayToggleVisible: false,
206 // If the toolbar doesn't have an explicit orientation yet, or if the
207 // narrow media query doesn't match then set the orientation to
209 if (!mql.matches || !model.get('orientation')) {
210 model.set({ orientation: 'vertical' }, { validate: true });
214 case 'toolbar.standard':
216 isFixed: mql.matches,
222 orientation: ((mql.matches && !model.get('locked')) ? 'horizontal' : 'vertical'),
223 }, { validate: true });
224 // The tray orientation toggle visibility does not need to be
227 isTrayToggleVisible: mql.matches,
238 * A toggle is an interactive element often bound to a click handler.
241 * A string representing a DOM fragment.
243 Drupal.theme.toolbarOrientationToggle = function () {
244 return '<div class="toolbar-toggle-orientation"><div class="toolbar-lining">' +
245 '<button class="toolbar-icon" type="button"></button>' +
250 * Ajax command to set the toolbar subtrees.
252 * @param {Drupal.Ajax} ajax
253 * {@link Drupal.Ajax} object created by {@link Drupal.ajax}.
254 * @param {object} response
255 * JSON response from the Ajax request.
256 * @param {number} [status]
257 * XMLHttpRequest status.
259 Drupal.AjaxCommands.prototype.setToolbarSubtrees = function (ajax, response, status) {
260 Drupal.toolbar.setSubtrees.resolve(response.subtrees);
262 }(jQuery, Drupal, drupalSettings));