2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
8 (function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) {
9 var options = $.extend(drupalSettings.contextual, {
11 open: Drupal.t('Open'),
12 close: Drupal.t('Close')
16 var cachedPermissionsHash = storage.getItem('Drupal.contextual.permissionsHash');
17 var permissionsHash = drupalSettings.user.permissionsHash;
18 if (cachedPermissionsHash !== permissionsHash) {
19 if (typeof permissionsHash === 'string') {
20 _.chain(storage).keys().each(function (key) {
21 if (key.substring(0, 18) === 'Drupal.contextual.') {
22 storage.removeItem(key);
26 storage.setItem('Drupal.contextual.permissionsHash', permissionsHash);
29 function initContextual($contextual, html) {
30 var $region = $contextual.closest('.contextual-region');
31 var contextual = Drupal.contextual;
33 $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger'));
35 var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath);
36 $contextual.find('.contextual-links a').each(function () {
37 var url = this.getAttribute('href');
38 var glue = url.indexOf('?') === -1 ? '?' : '&';
39 this.setAttribute('href', url + glue + destination);
42 var model = new contextual.StateModel({
43 title: $region.find('h2').eq(0).text().trim()
45 var viewOptions = $.extend({ el: $contextual, model: model }, options);
46 contextual.views.push({
47 visual: new contextual.VisualView(viewOptions),
48 aural: new contextual.AuralView(viewOptions),
49 keyboard: new contextual.KeyboardView(viewOptions)
51 contextual.regionViews.push(new contextual.RegionView($.extend({ el: $region, model: model }, options)));
53 contextual.collection.add(model);
55 $(document).trigger('drupalContextualLinkAdded', {
61 adjustIfNestedAndOverlapping($contextual);
64 function adjustIfNestedAndOverlapping($contextual) {
65 var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual');
67 if ($contextuals.length <= 1) {
71 var firstTop = $contextuals.eq(0).offset().top;
72 var secondTop = $contextuals.eq(1).offset().top;
73 if (firstTop === secondTop) {
74 var $nestedContextual = $contextuals.eq(1);
77 var $trigger = $nestedContextual.find('.trigger');
79 $trigger.removeClass('visually-hidden');
80 height = $nestedContextual.height();
81 $trigger.addClass('visually-hidden');
83 $nestedContextual.css({ top: $nestedContextual.position().top + height });
87 Drupal.behaviors.contextual = {
88 attach: function attach(context) {
89 var $context = $(context);
91 var $placeholders = $context.find('[data-contextual-id]').once('contextual-render');
92 if ($placeholders.length === 0) {
97 $placeholders.each(function () {
98 ids.push($(this).attr('data-contextual-id'));
101 var uncachedIDs = _.filter(ids, function (contextualID) {
102 var html = storage.getItem('Drupal.contextual.' + contextualID);
103 if (html && html.length) {
104 window.setTimeout(function () {
105 initContextual($context.find('[data-contextual-id="' + contextualID + '"]'), html);
112 if (uncachedIDs.length > 0) {
114 url: Drupal.url('contextual/render'),
116 data: { 'ids[]': uncachedIDs },
118 success: function success(results) {
119 _.each(results, function (html, contextualID) {
120 storage.setItem('Drupal.contextual.' + contextualID, html);
122 if (html.length > 0) {
123 $placeholders = $context.find('[data-contextual-id="' + contextualID + '"]');
125 for (var i = 0; i < $placeholders.length; i++) {
126 initContextual($placeholders.eq(i), html);
136 Drupal.contextual = {
142 Drupal.contextual.collection = new Backbone.Collection([], { model: Drupal.contextual.StateModel });
144 Drupal.theme.contextualTrigger = function () {
145 return '<button class="trigger visually-hidden focusable" type="button"></button>';
147 })(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage);