comparison core/modules/contextual/js/contextual.js @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
24 }); 24 });
25 } 25 }
26 storage.setItem('Drupal.contextual.permissionsHash', permissionsHash); 26 storage.setItem('Drupal.contextual.permissionsHash', permissionsHash);
27 } 27 }
28 28
29 function adjustIfNestedAndOverlapping($contextual) {
30 var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual');
31
32 if ($contextuals.length <= 1) {
33 return;
34 }
35
36 var firstTop = $contextuals.eq(0).offset().top;
37 var secondTop = $contextuals.eq(1).offset().top;
38 if (firstTop === secondTop) {
39 var $nestedContextual = $contextuals.eq(1);
40
41 var height = 0;
42 var $trigger = $nestedContextual.find('.trigger');
43
44 $trigger.removeClass('visually-hidden');
45 height = $nestedContextual.height();
46 $trigger.addClass('visually-hidden');
47
48 $nestedContextual.css({ top: $nestedContextual.position().top + height });
49 }
50 }
51
29 function initContextual($contextual, html) { 52 function initContextual($contextual, html) {
30 var $region = $contextual.closest('.contextual-region'); 53 var $region = $contextual.closest('.contextual-region');
31 var contextual = Drupal.contextual; 54 var contextual = Drupal.contextual;
32 55
33 $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger')); 56 $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger'));
34 57
35 var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath); 58 var destination = 'destination=' + Drupal.encodePath(Drupal.url(drupalSettings.path.currentPath));
36 $contextual.find('.contextual-links a').each(function () { 59 $contextual.find('.contextual-links a').each(function () {
37 var url = this.getAttribute('href'); 60 var url = this.getAttribute('href');
38 var glue = url.indexOf('?') === -1 ? '?' : '&'; 61 var glue = url.indexOf('?') === -1 ? '?' : '&';
39 this.setAttribute('href', url + glue + destination); 62 this.setAttribute('href', url + glue + destination);
40 }); 63 });
59 }); 82 });
60 83
61 adjustIfNestedAndOverlapping($contextual); 84 adjustIfNestedAndOverlapping($contextual);
62 } 85 }
63 86
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 = { 87 Drupal.behaviors.contextual = {
88 attach: function attach(context) { 88 attach: function attach(context) {
89 var $context = $(context); 89 var $context = $(context);
90 90
91 var $placeholders = $context.find('[data-contextual-id]').once('contextual-render'); 91 var $placeholders = $context.find('[data-contextual-id]').once('contextual-render');
93 return; 93 return;
94 } 94 }
95 95
96 var ids = []; 96 var ids = [];
97 $placeholders.each(function () { 97 $placeholders.each(function () {
98 ids.push($(this).attr('data-contextual-id')); 98 ids.push({
99 id: $(this).attr('data-contextual-id'),
100 token: $(this).attr('data-contextual-token')
101 });
99 }); 102 });
100 103
101 var uncachedIDs = _.filter(ids, function (contextualID) { 104 var uncachedIDs = [];
102 var html = storage.getItem('Drupal.contextual.' + contextualID); 105 var uncachedTokens = [];
106 ids.forEach(function (contextualID) {
107 var html = storage.getItem('Drupal.contextual.' + contextualID.id);
103 if (html && html.length) { 108 if (html && html.length) {
104 window.setTimeout(function () { 109 window.setTimeout(function () {
105 initContextual($context.find('[data-contextual-id="' + contextualID + '"]'), html); 110 initContextual($context.find('[data-contextual-id="' + contextualID.id + '"]'), html);
106 }); 111 });
107 return false; 112 return;
108 } 113 }
109 return true; 114 uncachedIDs.push(contextualID.id);
115 uncachedTokens.push(contextualID.token);
110 }); 116 });
111 117
112 if (uncachedIDs.length > 0) { 118 if (uncachedIDs.length > 0) {
113 $.ajax({ 119 $.ajax({
114 url: Drupal.url('contextual/render'), 120 url: Drupal.url('contextual/render'),
115 type: 'POST', 121 type: 'POST',
116 data: { 'ids[]': uncachedIDs }, 122 data: { 'ids[]': uncachedIDs, 'tokens[]': uncachedTokens },
117 dataType: 'json', 123 dataType: 'json',
118 success: function success(results) { 124 success: function success(results) {
119 _.each(results, function (html, contextualID) { 125 _.each(results, function (html, contextualID) {
120 storage.setItem('Drupal.contextual.' + contextualID, html); 126 storage.setItem('Drupal.contextual.' + contextualID, html);
121 127
137 views: [], 143 views: [],
138 144
139 regionViews: [] 145 regionViews: []
140 }; 146 };
141 147
142 Drupal.contextual.collection = new Backbone.Collection([], { model: Drupal.contextual.StateModel }); 148 Drupal.contextual.collection = new Backbone.Collection([], {
149 model: Drupal.contextual.StateModel
150 });
143 151
144 Drupal.theme.contextualTrigger = function () { 152 Drupal.theme.contextualTrigger = function () {
145 return '<button class="trigger visually-hidden focusable" type="button"></button>'; 153 return '<button class="trigger visually-hidden focusable" type="button"></button>';
146 }; 154 };
147 155