annotate core/modules/toolbar/js/views/ToolbarVisualView.js @ 19:fa3358dc1485 tip

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