toolbar_menu.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @file
  3. * Defines the behavior of the toolbar_menu module.
  4. *
  5. * Rewrite a piece of toolbar.js
  6. * to render all toolbar element in collapsible menu.
  7. */
  8. (function ($, Drupal, drupalSettings) {
  9. 'use strict';
  10. // Merge run-time settings with the defaults.
  11. var options = $.extend(
  12. {
  13. breakpoints: {
  14. 'toolbar.narrow': '',
  15. 'toolbar.standard': '',
  16. 'toolbar.wide': ''
  17. }
  18. },
  19. drupalSettings.toolbar,
  20. // Merge strings on top of drupalSettings so that they are not mutable.
  21. {
  22. strings: {
  23. horizontal: Drupal.t('Horizontal orientation'),
  24. vertical: Drupal.t('Vertical orientation')
  25. }
  26. }
  27. );
  28. Drupal.behaviors.toolbar_menu = {
  29. attach: function (context) {
  30. // Process the administrative toolbar.
  31. $(context).find('#toolbar-administration').once('toolbar-menu').each(function () {
  32. // Establish the toolbar models and views.
  33. var model = Drupal.toolbar.models.toolbarModel = new Drupal.toolbar.ToolbarModel({
  34. locked: JSON.parse(localStorage.getItem('Drupal.toolbar.trayVerticalLocked')) || false,
  35. activeTab: document.getElementById(JSON.parse(localStorage.getItem('Drupal.toolbar.activeTabID')))
  36. });
  37. // Render collapsible menus.
  38. var menuModel = Drupal.toolbar.models.menuModel = new Drupal.toolbar.MenuModel();
  39. Drupal.toolbar.views.menuVisualView = new Drupal.toolbar.MenuVisualView({
  40. el: $(this).find('.toolbar-menu-administration'),
  41. model: menuModel,
  42. strings: options.strings
  43. });
  44. // Handle the resolution of Drupal.toolbar.setSubtrees.
  45. // This is handled with a deferred so that the function may be invoked
  46. // asynchronously.
  47. Drupal.toolbar.setSubtrees.done(function (subtrees) {
  48. menuModel.set('subtrees', subtrees);
  49. var theme = drupalSettings.ajaxPageState.theme;
  50. localStorage.setItem('Drupal.toolbar.subtrees.' + theme, JSON.stringify(subtrees));
  51. // Indicate on the toolbarModel that subtrees are now loaded.
  52. model.set('areSubtreesLoaded', true);
  53. });
  54. });
  55. }
  56. };
  57. }(jQuery, Drupal, drupalSettings));