/** * Advanced Taxonomy Reengineering - Multiselect Compatible * Optimized for SmartJobBoard / Academic Careers */ (function() { const reengineerCategories = () => { // Target the specific category select by name or ID pattern const selectNodes = document.querySelectorAll('select[name="categories[]"], select.job-alert-categories'); selectNodes.forEach(selectElement => { // Avoid infinite loops if the script re-runs if (selectElement.dataset.reengineered === 'true') return; const $select = window.jQuery ? jQuery(selectElement) : null; // 1. If SJB has already initialized the Multiselect widget, destroy it to modify the DOM 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 "Header" options if (text.includes('(Do NOT select this one')) { // Extract the clean label (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. Remove the visual "---" separator options else if (text.includes('---')) { option.remove(); } // 4. Move valid sub-options into the active optgroup else if (currentOptGroup && option.value !== "") { currentOptGroup.appendChild(option); } }); selectElement.dataset.reengineered = 'true'; // 5. Re-initialize the SJB Multiselect widget so it renders the new structure if ($select) { $select.multiselect({ selectedText: "# selected", noneSelectedText: "Categories", header: false, menuHeight: 'auto', buttonWidth: 'auto' }); } }); }; // Tactical Execution: Wait for full window load to ensure jQuery and SJB scripts are ready window.addEventListener('load', () => { reengineerCategories(); // Observe for dynamic form injections (e.g., if the form is loaded via AJAX) const observer = new MutationObserver(() => reengineerCategories()); observer.observe(document.body, { childList: true, subtree: true }); }); })();