a23ac66ab3c796c2861f24b7ac709f2dd72dc61e
[yaffs-website] / web / core / modules / contextual / js / contextual.js
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
8 (function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) {
9   var options = $.extend(drupalSettings.contextual, {
10     strings: {
11       open: Drupal.t('Open'),
12       close: Drupal.t('Close')
13     }
14   });
15
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);
23         }
24       });
25     }
26     storage.setItem('Drupal.contextual.permissionsHash', permissionsHash);
27   }
28
29   function initContextual($contextual, html) {
30     var $region = $contextual.closest('.contextual-region');
31     var contextual = Drupal.contextual;
32
33     $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger'));
34
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);
40     });
41
42     var model = new contextual.StateModel({
43       title: $region.find('h2').eq(0).text().trim()
44     });
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)
50     });
51     contextual.regionViews.push(new contextual.RegionView($.extend({ el: $region, model: model }, options)));
52
53     contextual.collection.add(model);
54
55     $(document).trigger('drupalContextualLinkAdded', {
56       $el: $contextual,
57       $region: $region,
58       model: model
59     });
60
61     adjustIfNestedAndOverlapping($contextual);
62   }
63
64   function adjustIfNestedAndOverlapping($contextual) {
65     var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual');
66
67     if ($contextuals.length <= 1) {
68       return;
69     }
70
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);
75
76       var height = 0;
77       var $trigger = $nestedContextual.find('.trigger');
78
79       $trigger.removeClass('visually-hidden');
80       height = $nestedContextual.height();
81       $trigger.addClass('visually-hidden');
82
83       $nestedContextual.css({ top: $nestedContextual.position().top + height });
84     }
85   }
86
87   Drupal.behaviors.contextual = {
88     attach: function attach(context) {
89       var $context = $(context);
90
91       var $placeholders = $context.find('[data-contextual-id]').once('contextual-render');
92       if ($placeholders.length === 0) {
93         return;
94       }
95
96       var ids = [];
97       $placeholders.each(function () {
98         ids.push($(this).attr('data-contextual-id'));
99       });
100
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);
106           });
107           return false;
108         }
109         return true;
110       });
111
112       if (uncachedIDs.length > 0) {
113         $.ajax({
114           url: Drupal.url('contextual/render'),
115           type: 'POST',
116           data: { 'ids[]': uncachedIDs },
117           dataType: 'json',
118           success: function success(results) {
119             _.each(results, function (html, contextualID) {
120               storage.setItem('Drupal.contextual.' + contextualID, html);
121
122               if (html.length > 0) {
123                 $placeholders = $context.find('[data-contextual-id="' + contextualID + '"]');
124
125                 for (var i = 0; i < $placeholders.length; i++) {
126                   initContextual($placeholders.eq(i), html);
127                 }
128               }
129             });
130           }
131         });
132       }
133     }
134   };
135
136   Drupal.contextual = {
137     views: [],
138
139     regionViews: []
140   };
141
142   Drupal.contextual.collection = new Backbone.Collection([], { model: Drupal.contextual.StateModel });
143
144   Drupal.theme.contextualTrigger = function () {
145     return '<button class="trigger visually-hidden focusable" type="button"></button>';
146   };
147 })(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage);