PageSourceSearch

https://dcgary.org/modules/contrib/webform/js/webform.element.details.save.js?tjxjwj

js dcgary.org collected 2026-09-24 09:38:08 UTC 3,316 bytes, 123 lines download raw bytes

1/**
2 * @file
3 * JavaScript behaviors for details element.
4 */
5
6(function ($, Drupal, once) {
7  // Determine if local storage exists and is enabled.
8  // This approach is copied from Modernizr.
9  // @see https://github.com/Modernizr/Modernizr/blob/c56fb8b09515f629806ca44742932902ac145302/modernizr.js#L696-731
10  var hasLocalStorage = (function () {
11    try {
12      localStorage.setItem('webform', 'webform');
13      localStorage.removeItem('webform');
14      return true;
15    }
16    catch (e) {
17      return false;
18    }
19  }());
20
21  /**
22   * Attach handler to save details open/close state.
23   *
24   * @type {Drupal~behavior}
25   */
26  Drupal.behaviors.webformDetailsSave = {
27    attach(context) {
28      if (!hasLocalStorage) {
29        return;
30      }
31
32      // Summary click event handler.
33      $(once('webform-details-summary-save', 'details > summary', context)).on('click', function () {
34        var $details = $(this).parent();
35
36        // @see https://css-tricks.com/snippets/jquery/make-an-jquery-hasattr/
37        if ($details[
370].hasAttribute('data-webform-details-nosave')) {
38          return;
39        }
40
41        var name = Drupal.webformDetailsSaveGetName($details);
42        if (!name) {
43          return;
44        }
45
46        var open = ($details.attr('open') !== 'open') ? '1' : '0';
47        localStorage.setItem(name, open);
48      });
49
50      // Initialize details open state via local storage.
51      $(once('webform-details-save', 'details', context)).each(function () {
52        var $details = $(this);
53
54        var name = Drupal.webformDetailsSaveGetName($details);
55        if (!name) {
56          return;
57        }
58
59        var open = localStorage.getItem(name);
60        if (open === null) {
61          return;
62        }
63
64        if (open === '1') {
65          $details.attr('open', 'open');
66        }
67        else {
68          $details.removeAttr('open');
69        }
70      });
71    }
72
73  };
74
75  /**
76   * Get the name used to store the state of details element.
77   *
78   * @param {jQuery} $details
79   *   A details element.
80   *
81   * @return {string}
82   *   The name used to store the state of details element.
83   */
84  Drupal.webformDetailsSaveGetName = function ($details) {
85    if (!hasLocalStorage) {
86      return '';
87    }
88
89    // Ignore details that are vertical tabs pane.
90    if ($details.hasClass('vertical-tabs__pane')) {
91      return '';
92    }
93
94    // Any details element not included a webform must have define its own id.
95    var webformId = $details.attr('data-webform-element-id');
96    if (webformId) {
97      return 'Drupal.webform.' + webformId.replace('--', '.');
98    }
99
100    var detailsId = $details.attr('id');
101    if (!detailsId) {
102      return '';
103    }
104
105    var $form = $details.parents('form');
106    if (!$form.length || !$form.attr('id')) {
107      return '';
108    }
109
110    var formId = $form.attr('id');
111    if (!formId) {
112      return '';
113    }
114
115    // ISSUE: When Drupal renders a webform in a modal dialog it appends a unique
116    // identifier to webform ids and details ids. (i.e. my-form--FeSFISegTUI)
117    // WORKAROUND: Remove the unique id that delimited using double dashes.
118    formId = formId.replace(/--.+?$/, '').replace(/-/g, '_');
119    detailsId = detailsId.replace(/--.+?$/, '').replace(/-/g, '_');
120    return 'Drupal.webform.' + formId + '.' + detailsId;
121  };
122
123})(jQuery, Drupal, once);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.