diff --git a/core/modules/toolbar/js/toolbar.js b/core/modules/toolbar/js/toolbar.js index 47d0899..51b1b96 100644 --- a/core/modules/toolbar/js/toolbar.js +++ b/core/modules/toolbar/js/toolbar.js @@ -73,6 +73,8 @@ Drupal.behaviors.toolbar = { Drupal.toolbar.setSubtrees.done(function (subtrees) { menuModel.set('subtrees', subtrees); localStorage.setItem('Drupal.toolbar.subtrees', JSON.stringify(subtrees)); + // Indicate on the toolbarModel that subtrees are now loaded. + model.set('areSubtreesLoaded', true); }); // Attach a listener to the configured media query breakpoints. @@ -333,7 +335,18 @@ Drupal.toolbar = { this.updateTabs(); this.updateTrayOrientation(); this.updateBarAttributes(); - // Load the subtrees if the toolbar tray is in a vertical orientation. + // Load the subtrees if the orientation of the toolbar is changed to + // vertical. This condition responds to the case that the toolbar switches + // from horizontal to vertical orientation. The toolbar starts in a + // vertical orientation by default and then switches to horizontal during + // initialization if the media query conditions are met. Simply checking + // that the orientation is vertical here would result in the subtrees + // always being loaded, even when the toolbar initialization ultimately + // results in a horizontal orientation. + // + // @see Drupal.behaviors.toolbar.attach() where admin menu subtrees + // loading is invoked during initialization after media query conditions + // have been processed. if (this.model.changed.orientation === 'vertical' || this.model.changed.activeTab) { this.loadSubtrees(); } @@ -506,15 +519,22 @@ Drupal.toolbar = { /** * Calls the endpoint URI that will return rendered subtrees with JSONP. + * + * The rendered admin menu subtrees HTML is cached on the client in + * localStorage until the cache of the admin menu subtrees on the server- + * side is invalidated. The subtreesHash is stored in localStorage as well + * and compared to the subtreesHash in drupalSettings to determine when the + * admin menu subtrees cache has been invalidated. */ loadSubtrees: function () { var $activeTab = $(this.model.get('activeTab')); var orientation = this.model.get('orientation'); // Only load and render the admin menu subtrees if: // (1) They have not been loaded yet. - // (2) The active tab is the administration menu tab. + // (2) The active tab is the administration menu tab, indicated by the + // presence of the data-drupal-subtrees attribute. // (3) The orientation of the tray is vertical. - if (!this.model.get('areSubtreesLoaded') && $activeTab.data('drupal-defer-subtrees') !== undefined && orientation === 'vertical') { + if (!this.model.get('areSubtreesLoaded') && $activeTab.data('drupal-subtrees') !== undefined && orientation === 'vertical') { var subtreesHash = drupalSettings.toolbar.subtreesHash; var endpoint = Drupal.url('toolbar/subtrees/' + subtreesHash); var cachedSubtreesHash = localStorage.getItem('Drupal.toolbar.subtreesHash'); @@ -524,7 +544,6 @@ Drupal.toolbar = { // changed, then use the cached data. if (isVertical && subtreesHash === cachedSubtreesHash && cachedSubtrees) { Drupal.toolbar.setSubtrees.resolve(cachedSubtrees); - this.model.set('areSubtreesLoaded', true); } // Only make the call to get the subtrees if the orientation of the // toolbar is vertical. @@ -537,7 +556,6 @@ Drupal.toolbar = { $.ajax(endpoint); // Cache the hash for the subtrees locally. localStorage.setItem('Drupal.toolbar.subtreesHash', subtreesHash); - this.model.set('areSubtreesLoaded', true); } } } diff --git a/core/modules/toolbar/toolbar.module b/core/modules/toolbar/toolbar.module index 8a34936..9ef5d54 100644 --- a/core/modules/toolbar/toolbar.module +++ b/core/modules/toolbar/toolbar.module @@ -463,9 +463,12 @@ function toolbar_toolbar() { 'attributes' => array( 'title' => t('Admin menu'), 'class' => array('toolbar-icon', 'toolbar-icon-menu'), - // A marker that indicates to the client to defer loading of the admin - // menu subtrees until this tab is activated. - 'data-drupal-defer-subtrees' => '', + // A data marker that indicates to the client to defer loading of the + // admin menu subtrees until this tab is activated. Admin menu + // subtrees will not render to the DOM if this attribute is removed. + // The value of the attribute is intentionally left blank. Only the + // presence of the attribute is necessary. + 'data-drupal-subtrees' => '', ), ), ), @@ -628,7 +631,7 @@ function _toolbar_get_subtrees_hash() { // Cache using a tag 'user' so that we can invalidate all user-specific // caches later, based on the user's ID regardless of language. // Clear the cache when the 'locale' tag is deleted. This ensures a fresh - // subtrees rendering if when string translations are made. + // subtrees rendering when string translations are made. cache('toolbar')->set($cid, $hash, CacheBackendInterface::CACHE_PERMANENT, array('user' => array($uid), 'locale' => TRUE,)); } return $hash;