ToolbarVisualView.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, drupalSettings, Backbone) {
  8. Drupal.toolbar.ToolbarVisualView = Backbone.View.extend({
  9. events: function events() {
  10. var touchEndToClick = function touchEndToClick(event) {
  11. event.preventDefault();
  12. event.target.click();
  13. };
  14. return {
  15. 'click .toolbar-bar .toolbar-tab .trigger': 'onTabClick',
  16. 'click .toolbar-toggle-orientation button': 'onOrientationToggleClick',
  17. 'touchend .toolbar-bar .toolbar-tab .trigger': touchEndToClick,
  18. 'touchend .toolbar-toggle-orientation button': touchEndToClick
  19. };
  20. },
  21. initialize: function initialize(options) {
  22. this.strings = options.strings;
  23. this.listenTo(this.model, 'change:activeTab change:orientation change:isOriented change:isTrayToggleVisible', this.render);
  24. this.listenTo(this.model, 'change:mqMatches', this.onMediaQueryChange);
  25. this.listenTo(this.model, 'change:offsets', this.adjustPlacement);
  26. this.listenTo(this.model, 'change:activeTab change:orientation change:isOriented', this.updateToolbarHeight);
  27. this.$el.find('.toolbar-tray .toolbar-lining').append(Drupal.theme('toolbarOrientationToggle'));
  28. this.model.trigger('change:activeTab');
  29. },
  30. updateToolbarHeight: function updateToolbarHeight() {
  31. var toolbarTabOuterHeight = $('#toolbar-bar').find('.toolbar-tab').outerHeight() || 0;
  32. var toolbarTrayHorizontalOuterHeight = $('.is-active.toolbar-tray-horizontal').outerHeight() || 0;
  33. this.model.set('height', toolbarTabOuterHeight + toolbarTrayHorizontalOuterHeight);
  34. $('body').css({
  35. 'padding-top': this.model.get('height')
  36. });
  37. this.triggerDisplace();
  38. },
  39. triggerDisplace: function triggerDisplace() {
  40. _.defer(function () {
  41. Drupal.displace(true);
  42. });
  43. },
  44. render: function render() {
  45. this.updateTabs();
  46. this.updateTrayOrientation();
  47. this.updateBarAttributes();
  48. $('body').removeClass('toolbar-loading');
  49. if (this.model.changed.orientation === 'vertical' || this.model.changed.activeTab) {
  50. this.loadSubtrees();
  51. }
  52. return this;
  53. },
  54. onTabClick: function onTabClick(event) {
  55. if (event.target.hasAttribute('data-toolbar-tray')) {
  56. var activeTab = this.model.get('activeTab');
  57. var clickedTab = event.target;
  58. this.model.set('activeTab', !activeTab || clickedTab !== activeTab ? clickedTab : null);
  59. event.preventDefault();
  60. event.stopPropagation();
  61. }
  62. },
  63. onOrientationToggleClick: function onOrientationToggleClick(event) {
  64. var orientation = this.model.get('orientation');
  65. var antiOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
  66. var locked = antiOrientation === 'vertical';
  67. if (locked) {
  68. localStorage.setItem('Drupal.toolbar.trayVerticalLocked', 'true');
  69. } else {
  70. localStorage.removeItem('Drupal.toolbar.trayVerticalLocked');
  71. }
  72. this.model.set({
  73. locked: locked,
  74. orientation: antiOrientation
  75. }, {
  76. validate: true,
  77. override: true
  78. });
  79. event.preventDefault();
  80. event.stopPropagation();
  81. },
  82. updateTabs: function updateTabs() {
  83. var $tab = $(this.model.get('activeTab'));
  84. $(this.model.previous('activeTab')).removeClass('is-active').prop('aria-pressed', false);
  85. $(this.model.previous('activeTray')).removeClass('is-active');
  86. if ($tab.length > 0) {
  87. $tab.addClass('is-active').prop('aria-pressed', true);
  88. var name = $tab.attr('data-toolbar-tray');
  89. var id = $tab.get(0).id;
  90. if (id) {
  91. localStorage.setItem('Drupal.toolbar.activeTabID', JSON.stringify(id));
  92. }
  93. var $tray = this.$el.find('[data-toolbar-tray="' + name + '"].toolbar-tray');
  94. if ($tray.length) {
  95. $tray.addClass('is-active');
  96. this.model.set('activeTray', $tray.get(0));
  97. } else {
  98. this.model.set('activeTray', null);
  99. }
  100. } else {
  101. this.model.set('activeTray', null);
  102. localStorage.removeItem('Drupal.toolbar.activeTabID');
  103. }
  104. },
  105. updateBarAttributes: function updateBarAttributes() {
  106. var isOriented = this.model.get('isOriented');
  107. if (isOriented) {
  108. this.$el.find('.toolbar-bar').attr('data-offset-top', '');
  109. } else {
  110. this.$el.find('.toolbar-bar').removeAttr('data-offset-top');
  111. }
  112. this.$el.toggleClass('toolbar-oriented', isOriented);
  113. },
  114. updateTrayOrientation: function updateTrayOrientation() {
  115. var orientation = this.model.get('orientation');
  116. var antiOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
  117. $('body').toggleClass('toolbar-vertical', orientation === 'vertical').toggleClass('toolbar-horizontal', orientation === 'horizontal');
  118. var removeClass = antiOrientation === 'horizontal' ? 'toolbar-tray-horizontal' : 'toolbar-tray-vertical';
  119. var $trays = this.$el.find('.toolbar-tray').removeClass(removeClass).addClass('toolbar-tray-' + orientation);
  120. var iconClass = 'toolbar-icon-toggle-' + orientation;
  121. var iconAntiClass = 'toolbar-icon-toggle-' + antiOrientation;
  122. var $orientationToggle = this.$el.find('.toolbar-toggle-orientation').toggle(this.model.get('isTrayToggleVisible'));
  123. $orientationToggle.find('button').val(antiOrientation).attr('title', this.strings[antiOrientation]).text(this.strings[antiOrientation]).removeClass(iconClass).addClass(iconAntiClass);
  124. var dir = document.documentElement.dir;
  125. var edge = dir === 'rtl' ? 'right' : 'left';
  126. $trays.removeAttr('data-offset-left data-offset-right data-offset-top');
  127. $trays.filter('.toolbar-tray-vertical.is-active').attr('data-offset-' + edge, '');
  128. $trays.filter('.toolbar-tray-horizontal.is-active').attr('data-offset-top', '');
  129. },
  130. adjustPlacement: function adjustPlacement() {
  131. var $trays = this.$el.find('.toolbar-tray');
  132. if (!this.model.get('isOriented')) {
  133. $trays.removeClass('toolbar-tray-horizontal').addClass('toolbar-tray-vertical');
  134. }
  135. },
  136. loadSubtrees: function loadSubtrees() {
  137. var $activeTab = $(this.model.get('activeTab'));
  138. var orientation = this.model.get('orientation');
  139. if (!this.model.get('areSubtreesLoaded') && typeof $activeTab.data('drupal-subtrees') !== 'undefined' && orientation === 'vertical') {
  140. var subtreesHash = drupalSettings.toolbar.subtreesHash;
  141. var theme = drupalSettings.ajaxPageState.theme;
  142. var endpoint = Drupal.url('toolbar/subtrees/' + subtreesHash);
  143. var cachedSubtreesHash = localStorage.getItem('Drupal.toolbar.subtreesHash.' + theme);
  144. var cachedSubtrees = JSON.parse(localStorage.getItem('Drupal.toolbar.subtrees.' + theme));
  145. var isVertical = this.model.get('orientation') === 'vertical';
  146. if (isVertical && subtreesHash === cachedSubtreesHash && cachedSubtrees) {
  147. Drupal.toolbar.setSubtrees.resolve(cachedSubtrees);
  148. } else if (isVertical) {
  149. localStorage.removeItem('Drupal.toolbar.subtreesHash.' + theme);
  150. localStorage.removeItem('Drupal.toolbar.subtrees.' + theme);
  151. Drupal.ajax({ url: endpoint }).execute();
  152. localStorage.setItem('Drupal.toolbar.subtreesHash.' + theme, subtreesHash);
  153. }
  154. }
  155. }
  156. });
  157. })(jQuery, Drupal, drupalSettings, Backbone);