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