collapse.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 ($, Modernizr, Drupal) {
  8. function CollapsibleDetails(node) {
  9. this.$node = $(node);
  10. this.$node.data('details', this);
  11. var anchor = window.location.hash && window.location.hash !== '#' ? ', ' + window.location.hash : '';
  12. if (this.$node.find('.error' + anchor).length) {
  13. this.$node.attr('open', true);
  14. }
  15. this.setupSummary();
  16. this.setupLegend();
  17. }
  18. $.extend(CollapsibleDetails, {
  19. instances: []
  20. });
  21. $.extend(CollapsibleDetails.prototype, {
  22. setupSummary: function setupSummary() {
  23. this.$summary = $('<span class="summary"></span>');
  24. this.$node.on('summaryUpdated', $.proxy(this.onSummaryUpdated, this)).trigger('summaryUpdated');
  25. },
  26. setupLegend: function setupLegend() {
  27. var $legend = this.$node.find('> summary');
  28. $('<span class="details-summary-prefix visually-hidden"></span>').append(this.$node.attr('open') ? Drupal.t('Hide') : Drupal.t('Show')).prependTo($legend).after(document.createTextNode(' '));
  29. $('<a class="details-title"></a>').attr('href', '#' + this.$node.attr('id')).prepend($legend.contents()).appendTo($legend);
  30. $legend.append(this.$summary).on('click', $.proxy(this.onLegendClick, this));
  31. },
  32. onLegendClick: function onLegendClick(e) {
  33. this.toggle();
  34. e.preventDefault();
  35. },
  36. onSummaryUpdated: function onSummaryUpdated() {
  37. var text = $.trim(this.$node.drupalGetSummary());
  38. this.$summary.html(text ? ' (' + text + ')' : '');
  39. },
  40. toggle: function toggle() {
  41. var _this = this;
  42. var isOpen = !!this.$node.attr('open');
  43. var $summaryPrefix = this.$node.find('> summary span.details-summary-prefix');
  44. if (isOpen) {
  45. $summaryPrefix.html(Drupal.t('Show'));
  46. } else {
  47. $summaryPrefix.html(Drupal.t('Hide'));
  48. }
  49. setTimeout(function () {
  50. _this.$node.attr('open', !isOpen);
  51. }, 0);
  52. }
  53. });
  54. Drupal.behaviors.collapse = {
  55. attach: function attach(context) {
  56. if (Modernizr.details) {
  57. return;
  58. }
  59. var $collapsibleDetails = $(context).find('details').once('collapse').addClass('collapse-processed');
  60. if ($collapsibleDetails.length) {
  61. for (var i = 0; i < $collapsibleDetails.length; i++) {
  62. CollapsibleDetails.instances.push(new CollapsibleDetails($collapsibleDetails[i]));
  63. }
  64. }
  65. }
  66. };
  67. var handleFragmentLinkClickOrHashChange = function handleFragmentLinkClickOrHashChange(e, $target) {
  68. $target.parents('details').not('[open]').find('> summary').trigger('click');
  69. };
  70. $('body').on('formFragmentLinkClickOrHashChange.details', handleFragmentLinkClickOrHashChange);
  71. Drupal.CollapsibleDetails = CollapsibleDetails;
  72. })(jQuery, Modernizr, Drupal);