toolbar.es6.js 8.8 KB

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