1/** 2 * @file 3 * When a centre is selected, sync the selected option's data-* attributes into 4 * other form fields: 5 * - data-manager-email -> "manager_email" field 6 * - data-centre-affi -> "is_affiliate_centre" checkbox (checked when "1") 7 * - data-centre-status -> "center_status" select (preselected by term ID) 8 * - data-centre-type -> "type_of_centre" select (preselected by term ID) 9 * 10 * Auto-detects any centre select that has been enhanced with these per-option 11 * attributes (see ck_centre_form_alter()), so it works on every webform that 12 * uses the centre entity-select. Missing target fields are simply ignored. 13 */ 14(function (Drupal, once) { 15 'use strict'; 16 17 /** 18 * Set a select's value if it has an option for it, notifying listeners. 19 */ 20 function setSelectValue(select, value) { 21 if (!select) { 22 return; 23 } 24 var hasOption = Array.prototype.some.call(select.options, function (option) { 25 return option.value === value; 26 }); 27 var newValue = hasOption ? value : ''; 28 if (select.value !== newValue) { 29 select.value = newValue; 30 // Notify webform conditionals/listeners of the change. 31 select.dispatchEvent(new Event('change', { bubbles: true })); 32 } 33 } 34 35 /** 36 * Sync the selected centre's data into the related form fields. 37 */ 38 function syncCentreFields(select) { 39 var form = select.closest('form'); 40 if (!form) { 41 return; 42 } 43 var option = select.options[select.selectedIndex]; 44 45 // Manager email -> text field. 46 var email = form.querySelector('[name="manager_email"]'); 47 if (email) { 48 email.value = option ? (option.getAttribute('data-manager-email') || '') : ''; 49 } 50 51 // Affiliate flag -> checkbox. 52 var affiliate = form.querySelector('[name="is_affiliate_centre"]'); 53 if (affiliate) { 54 var isAffiliate = !!option && option.getAttribute('data-centre-affi') === '1'; 55 if (affiliate.checked !== isAffiliate) { 56 affiliate.checked = isAffiliate; 57 // Notify webform conditionals/listeners of the change. 58 affiliate.dispatchEvent(new Event('change', { bubbles: true })); 59 } 60 } 61 62 // Centre status taxonomy term -> "center_status" select. 63 var status = form.querySelector('[name="center_status"]'); 64 if (status) { 65 setSelectValue(status, option ? (option.getAttribute('data-centre-status') || '') : ''); 66 } 67 68 // Centre type taxonomy term -> "type_of_centre" select. 69 var type = form.querySelector('[name="type_of_centre"]'); 70 if (type) { 71 setSelectValue(type, option ? (option.getAttribute('data-centre-type') || '') : ''); 72 } 73 } 74 75 Drupal.behaviors.ckCentreManagerEmail = { 76 attach: function (context) { 77 // Only target selects whose options carry the manager email data. 78 var selects = context.querySelectorAll ? context.querySelectorAll('select') : []; 79 var centreSelects = Array.prototype.filter.call(selects, function (select) { 80 return Array.prototype.some.call(select.options, function (option) { 81 return option.hasAttribute('data-manager-email'); 82 }); 83 }); 84 85 once('ck-centre-manager-email', centreSelects).forEach(function (select) { 86 select.addEventListener('change', function () { 87 syncCentreFields(select); 88 }); 89 // Reflect any pre-selected centre on initial load. 90 syncCentreFields(select); 91 }); 92 } 93 }; 94})(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.