/** * Advanced Taxonomy Reengineering - SJB Final Interceptor * Reconstructs dropdowns into organized hierarchies. */ (function() { const reengineerCategories = () => { // Target the category select by name to ignore dynamic IDs 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. Force-destroy the existing SJB Multiselect widget to allow DOM changes 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 widget to render the new hierarchy if ($select) { $select.multiselect({ selectedText: "# selected", noneSelectedText: "Categories", header: false, menuHeight: 'auto', buttonWidth: 'auto' }); } }); }; // Tactical Delay: SJB initializes its scripts at the very end of the . // We wait 200ms after load to ensure our code runs LAST. window.addEventListener('load', () => { setTimeout(reengineerCategories, 200); // Watch for dynamic form injections (modals, AJAX loads) const observer = new MutationObserver(() => reengineerCategories()); observer.observe(document.body, { childList: true, subtree: true }); }); })();