/** * Advanced Taxonomy Reengineering - Multiselect Compatible * Optimized for Academic Careers Job Alert Form */ (function() { const reengineerCategories = () => { // Target the category dropdown specifically const selectNodes = document.querySelectorAll('select[name="categories[]"]'); selectNodes.forEach(selectElement => { // Prevent duplicate processing if (selectElement.dataset.reengineered === 'true') return; const $select = window.jQuery ? jQuery(selectElement) : null; // 1. Destroy existing Multiselect widget to allow DOM manipulation if ($select && $select.data("ech-multiselect")) { $select.multiselect('destroy'); } const optionsArray = Array.from(selectElement.options); let currentOptGroup = null; optionsArray.forEach(option => { const text = option.text; // 2. Identify and Clean Header Labels if (text.includes('(Do NOT select this one')) { // Extract prefix (e.g., "FACULTY, POSTDOC, DEPARTMENT HEAD") const cleanLabel = text.split(':')[0].trim(); currentOptGroup = document.createElement('optgroup'); currentOptGroup.label = cleanLabel; selectElement.insertBefore(currentOptGroup, option); option.remove(); } // 3. Purge legacy separator lines else if (text.includes('---')) { option.remove(); } // 4. Group valid options under the current header else if (currentOptGroup && option.value !== "") { currentOptGroup.appendChild(option); } }); selectElement.dataset.reengineered = 'true'; // 5. Re-initialize the SJB Multiselect widget with original settings if ($select) { $select.multiselect({ selectedText: "# selected", noneSelectedText: "Categories", header: false, menuHeight: 'auto', buttonWidth: 'auto' }); } }); }; // Execute on load and via MutationObserver for dynamic form updates if (document.readyState === 'complete') { reengineerCategories(); } else { window.addEventListener('load', reengineerCategories); } const observer = new MutationObserver(() => reengineerCategories()); observer.observe(document.body, { childList: true, subtree: true }); })();