contextual.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. (function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) {
  8. var options = $.extend(drupalSettings.contextual, {
  9. strings: {
  10. open: Drupal.t('Open'),
  11. close: Drupal.t('Close')
  12. }
  13. });
  14. var cachedPermissionsHash = storage.getItem('Drupal.contextual.permissionsHash');
  15. var permissionsHash = drupalSettings.user.permissionsHash;
  16. if (cachedPermissionsHash !== permissionsHash) {
  17. if (typeof permissionsHash === 'string') {
  18. _.chain(storage).keys().each(function (key) {
  19. if (key.substring(0, 18) === 'Drupal.contextual.') {
  20. storage.removeItem(key);
  21. }
  22. });
  23. }
  24. storage.setItem('Drupal.contextual.permissionsHash', permissionsHash);
  25. }
  26. function adjustIfNestedAndOverlapping($contextual) {
  27. var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual');
  28. if ($contextuals.length <= 1) {
  29. return;
  30. }
  31. var firstTop = $contextuals.eq(0).offset().top;
  32. var secondTop = $contextuals.eq(1).offset().top;
  33. if (firstTop === secondTop) {
  34. var $nestedContextual = $contextuals.eq(1);
  35. var height = 0;
  36. var $trigger = $nestedContextual.find('.trigger');
  37. $trigger.removeClass('visually-hidden');
  38. height = $nestedContextual.height();
  39. $trigger.addClass('visually-hidden');
  40. $nestedContextual.css({ top: $nestedContextual.position().top + height });
  41. }
  42. }
  43. function initContextual($contextual, html) {
  44. var $region = $contextual.closest('.contextual-region');
  45. var contextual = Drupal.contextual;
  46. $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger'));
  47. var destination = 'destination=' + Drupal.encodePath(Drupal.url(drupalSettings.path.currentPath));
  48. $contextual.find('.contextual-links a').each(function () {
  49. var url = this.getAttribute('href');
  50. var glue = url.indexOf('?') === -1 ? '?' : '&';
  51. this.setAttribute('href', url + glue + destination);
  52. });
  53. var model = new contextual.StateModel({
  54. title: $region.find('h2').eq(0).text().trim()
  55. });
  56. var viewOptions = $.extend({ el: $contextual, model: model }, options);
  57. contextual.views.push({
  58. visual: new contextual.VisualView(viewOptions),
  59. aural: new contextual.AuralView(viewOptions),
  60. keyboard: new contextual.KeyboardView(viewOptions)
  61. });
  62. contextual.regionViews.push(new contextual.RegionView($.extend({ el: $region, model: model }, options)));
  63. contextual.collection.add(model);
  64. $(document).trigger('drupalContextualLinkAdded', {
  65. $el: $contextual,
  66. $region: $region,
  67. model: model
  68. });
  69. adjustIfNestedAndOverlapping($contextual);
  70. }
  71. Drupal.behaviors.contextual = {
  72. attach: function attach(context) {
  73. var $context = $(context);
  74. var $placeholders = $context.find('[data-contextual-id]').once('contextual-render');
  75. if ($placeholders.length === 0) {
  76. return;
  77. }
  78. var ids = [];
  79. $placeholders.each(function () {
  80. ids.push({
  81. id: $(this).attr('data-contextual-id'),
  82. token: $(this).attr('data-contextual-token')
  83. });
  84. });
  85. var uncachedIDs = [];
  86. var uncachedTokens = [];
  87. ids.forEach(function (contextualID) {
  88. var html = storage.getItem('Drupal.contextual.' + contextualID.id);
  89. if (html && html.length) {
  90. window.setTimeout(function () {
  91. initContextual($context.find('[data-contextual-id="' + contextualID.id + '"]:empty').eq(0), html);
  92. });
  93. return;
  94. }
  95. uncachedIDs.push(contextualID.id);
  96. uncachedTokens.push(contextualID.token);
  97. });
  98. if (uncachedIDs.length > 0) {
  99. $.ajax({
  100. url: Drupal.url('contextual/render'),
  101. type: 'POST',
  102. data: { 'ids[]': uncachedIDs, 'tokens[]': uncachedTokens },
  103. dataType: 'json',
  104. success: function success(results) {
  105. _.each(results, function (html, contextualID) {
  106. storage.setItem('Drupal.contextual.' + contextualID, html);
  107. if (html.length > 0) {
  108. $placeholders = $context.find('[data-contextual-id="' + contextualID + '"]');
  109. for (var i = 0; i < $placeholders.length; i++) {
  110. initContextual($placeholders.eq(i), html);
  111. }
  112. }
  113. });
  114. }
  115. });
  116. }
  117. }
  118. };
  119. Drupal.contextual = {
  120. views: [],
  121. regionViews: []
  122. };
  123. Drupal.contextual.collection = new Backbone.Collection([], {
  124. model: Drupal.contextual.StateModel
  125. });
  126. Drupal.theme.contextualTrigger = function () {
  127. return '<button class="trigger visually-hidden focusable" type="button"></button>';
  128. };
  129. $(document).on('drupalContextualLinkAdded', function (event, data) {
  130. Drupal.ajax.bindAjaxLinks(data.$el[0]);
  131. });
  132. })(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage);