1/** 2 * @file 3 * Select2 integration for PTHV PDF times form selects. 4 */ 5(function (Drupal, once, $) { 6 'use strict'; 7 8 const selector = '[data-pthv-select2]'; 9 10 /** 11 * Whether Select2 should be active for this page. 12 * 13 * @return {boolean} 14 * TRUE when RDDS and autocomplete settings allow Select2. 15 */ 16 function isSelect2Enabled() { 17 if (typeof drupalSettings === 'undefined') { 18 return false; 19 } 20 if (!drupalSettings.rdds?.useRdds) { 21 return false; 22 } 23 return drupalSettings.rd_pthv_pdf?.selectAutocomplete !== false; 24 } 25 26 /** 27 * Destroys Select2 on an element when present. 28 * 29 * @param {HTMLElement} element 30 * The native select element. 31 */ 32 function destroySelect2(element) { 33 const $element = $(element); 34 if ($element.hasClass('select2-hidden-accessible')) { 35 $element.off('select2:select select2:unselect'); 36 $element.select2('destroy'); 37 } 38 } 39 40 /** 41 * Initializes Select2 on a native select element. 42 * 43 * @param {HTMLElement} element 44 * The native select element. 45 */ 46 function initSelect2(element) { 47 if (element.disabled) { 48 return; 49 } 50 51 if (element.classList.contains('select2-hidden-accessible')) { 52 return; 53 } 54 55 const $element = $(element); 56 $element.select2({ 57 width: '100%', 58 }); 59 60 $element.on('select2:select select2:unselect', (event) => { 61 event.target.dispatchEvent(new Event('change', { bubbles: true })); 62 }); 63 } 64 65 /** 66 * Refreshes Select2 UI after native option changes. 67 * 68 * @param {HTMLElement} element 69 * The native select element. 70 */ 71 function refreshSelect2(element) { 72 const $element = $(element); 73 if (!$element.hasClass('select2-hidden-accessible')) { 74 return; 75 } 76 $element.trigger('change.select2'); 77 } 78 79 Drupal.rdPthvPdfSelect2 = { 80 init: initSelect2, 81 destroy: destroySelect2, 82 refresh: refreshSelect2, 83 isEnabled: isSelect2Enabled, 84 }; 85 86 Drupal.behaviors.rdPthvPdfSelect2 = { 87 attach(context) { 88 if (!isSelect2Enabled()) { 89 return; 90 } 91 once('rd-pthv-pdf-select2', context.querySelectorAll(selector), context).forEach((element) => { 92 initSelect2(element); 93 }); 94 }, 95 detach(context, settings, trigger) { 96 if (trigger !== 'unload') { 97 return; 98 } 99 context.querySelectorAll(selector).forEach((element) => { 100 destroySelect2(element); 101 once.remove('rd-pthv-pdf-select2', element); 102 }); 103 }, 104 }; 105})(Drupal, once, jQuery);
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.