contextual.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 initContextual($contextual, html) {
  27. var $region = $contextual.closest('.contextual-region');
  28. var contextual = Drupal.contextual;
  29. $contextual.html(html).addClass('contextual').prepend(Drupal.theme('contextualTrigger'));
  30. var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath);
  31. $contextual.find('.contextual-links a').each(function () {
  32. var url = this.getAttribute('href');
  33. var glue = url.indexOf('?') === -1 ? '?' : '&';
  34. this.setAttribute('href', url + glue + destination);
  35. });
  36. var model = new contextual.StateModel({
  37. title: $region.find('h2').eq(0).text().trim()
  38. });
  39. var viewOptions = $.extend({ el: $contextual, model: model }, options);
  40. contextual.views.push({
  41. visual: new contextual.VisualView(viewOptions),
  42. aural: new contextual.AuralView(viewOptions),
  43. keyboard: new contextual.KeyboardView(viewOptions)
  44. });
  45. contextual.regionViews.push(new contextual.RegionView($.extend({ el: $region, model: model }, options)));
  46. contextual.collection.add(model);
  47. $(document).trigger('drupalContextualLinkAdded', {
  48. $el: $contextual,
  49. $region: $region,
  50. model: model
  51. });
  52. adjustIfNestedAndOverlapping($contextual);
  53. }
  54. function adjustIfNestedAndOverlapping($contextual) {
  55. var $contextuals = $contextual.parents('.contextual-region').eq(-1).find('.contextual');
  56. if ($contextuals.length <= 1) {
  57. return;
  58. }
  59. var firstTop = $contextuals.eq(0).offset().top;
  60. var secondTop = $contextuals.eq(1).offset().top;
  61. if (firstTop === secondTop) {
  62. var $nestedContextual = $contextuals.eq(1);
  63. var height = 0;
  64. var $trigger = $nestedContextual.find('.trigger');
  65. $trigger.removeClass('visually-hidden');
  66. height = $nestedContextual.height();
  67. $trigger.addClass('visually-hidden');
  68. $nestedContextual.css({ top: $nestedContextual.position().top + height });
  69. }
  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($(this).attr('data-contextual-id'));
  81. });
  82. var uncachedIDs = _.filter(ids, function (contextualID) {
  83. var html = storage.getItem('Drupal.contextual.' + contextualID);
  84. if (html && html.length) {
  85. window.setTimeout(function () {
  86. initContextual($context.find('[data-contextual-id="' + contextualID + '"]'), html);
  87. });
  88. return false;
  89. }
  90. return true;
  91. });
  92. if (uncachedIDs.length > 0) {
  93. $.ajax({
  94. url: Drupal.url('contextual/render'),
  95. type: 'POST',
  96. data: { 'ids[]': uncachedIDs },
  97. dataType: 'json',
  98. success: function success(results) {
  99. _.each(results, function (html, contextualID) {
  100. storage.setItem('Drupal.contextual.' + contextualID, html);
  101. if (html.length > 0) {
  102. $placeholders = $context.find('[data-contextual-id="' + contextualID + '"]');
  103. for (var i = 0; i < $placeholders.length; i++) {
  104. initContextual($placeholders.eq(i), html);
  105. }
  106. }
  107. });
  108. }
  109. });
  110. }
  111. }
  112. };
  113. Drupal.contextual = {
  114. views: [],
  115. regionViews: []
  116. };
  117. Drupal.contextual.collection = new Backbone.Collection([], { model: Drupal.contextual.StateModel });
  118. Drupal.theme.contextualTrigger = function () {
  119. return '<button class="trigger visually-hidden focusable" type="button"></button>';
  120. };
  121. $(document).on('drupalContextualLinkAdded', function (event, data) {
  122. Drupal.ajax.bindAjaxLinks(data.$el[0]);
  123. });
  124. })(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage);