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