PageSourceSearch

https://ndi.org/modules/contrib/webform/js/webform.element.details.save.js?v=10.6.15

js ndi.org collected 2026-09-24 08:28:39 UTC 3,345 bytes, 126 lines download raw bytes

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