204 lines
6.2 KiB
JavaScript
204 lines
6.2 KiB
JavaScript
// NCExpandableContainer
|
|||
|
|
// Shared helper for expandable/collapsible containers with optional persistence.
|
||
|
|
//
|
||
|
|
// Usage:
|
||
|
|
// const controller = window.NCExpandableContainer.bind({
|
||
|
|
// toggleEl: 'filtersToggle',
|
||
|
|
// contentEl: 'filtersBody',
|
||
|
|
// chevronEl: 'filtersChevron',
|
||
|
|
// storageKey: 'app:filters-open',
|
||
|
|
// storageFormat: 'flag', // 'json' | 'flag'
|
||
|
|
// defaultExpanded: false,
|
||
|
|
// initFromAria: true,
|
||
|
|
// chevronMode: 'class', // 'class' | 'style'
|
||
|
|
// chevronRotateClass: 'rotate-180',
|
||
|
|
// syncHiddenAttribute: true,
|
||
|
|
// onChange: expanded => { ... },
|
||
|
|
// });
|
||
|
|
|
||
|
|
(function (global) {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
var controllerByToggle = new WeakMap();
|
||
|
|
|
||
|
|
function toElement(target) {
|
||
|
|
if (!target) return null;
|
||
|
|
if (target instanceof Element) return target;
|
||
|
|
|
||
|
|
var value = String(target || '').trim();
|
||
|
|
if (!value) return null;
|
||
|
|
|
||
|
|
if (value.charAt(0) === '#') {
|
||
|
|
try {
|
||
|
|
return document.querySelector(value);
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var byId = document.getElementById(value);
|
||
|
|
if (byId) return byId;
|
||
|
|
|
||
|
|
try {
|
||
|
|
return document.querySelector(value);
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function readStorageBoolean(storageKey, storageFormat) {
|
||
|
|
if (!storageKey) return null;
|
||
|
|
try {
|
||
|
|
var raw = localStorage.getItem(storageKey);
|
||
|
|
if (raw == null) return null;
|
||
|
|
if (storageFormat === 'flag') return raw === '1';
|
||
|
|
return !!JSON.parse(raw);
|
||
|
|
} catch {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function writeStorageBoolean(storageKey, value, storageFormat) {
|
||
|
|
if (!storageKey) return;
|
||
|
|
try {
|
||
|
|
if (storageFormat === 'flag') localStorage.setItem(storageKey, value ? '1' : '0');
|
||
|
|
else localStorage.setItem(storageKey, JSON.stringify(!!value));
|
||
|
|
} catch {
|
||
|
|
// Ignore storage failures.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function applyChevronState(chevronEl, expanded, chevronMode, chevronRotateClass) {
|
||
|
|
if (!chevronEl) return;
|
||
|
|
if (chevronMode === 'style') {
|
||
|
|
chevronEl.style.transform = expanded ? 'rotate(180deg)' : '';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
chevronEl.classList.toggle(chevronRotateClass, !!expanded);
|
||
|
|
}
|
||
|
|
|
||
|
|
function bind(options) {
|
||
|
|
var config = options || {};
|
||
|
|
var toggleEl = toElement(config.toggleEl || config.toggle || config.toggleId);
|
||
|
|
var contentEl = toElement(config.contentEl || config.content || config.contentId);
|
||
|
|
if (!toggleEl || !contentEl) return null;
|
||
|
|
|
||
|
|
if (config.allowRebind !== true && controllerByToggle.has(toggleEl)) {
|
||
|
|
return controllerByToggle.get(toggleEl);
|
||
|
|
}
|
||
|
|
|
||
|
|
var chevronEl = toElement(config.chevronEl || config.chevron || config.chevronId);
|
||
|
|
var hiddenClass = String(config.hiddenClass || 'hidden');
|
||
|
|
var eventName = String(config.eventName || 'click');
|
||
|
|
var storageKey = config.storageKey ? String(config.storageKey) : '';
|
||
|
|
var storageFormat = config.storageFormat === 'flag' ? 'flag' : 'json';
|
||
|
|
var defaultExpanded = config.defaultExpanded === true;
|
||
|
|
var initFromAria = config.initFromAria !== false;
|
||
|
|
var updateAria = config.updateAria !== false;
|
||
|
|
var syncHiddenAttribute = config.syncHiddenAttribute === true;
|
||
|
|
var persistState = config.persistState !== false;
|
||
|
|
var emitInitial = config.emitInitial !== false;
|
||
|
|
var chevronMode = config.chevronMode === 'style' ? 'style' : 'class';
|
||
|
|
var chevronRotateClass = String(config.chevronRotateClass || 'rotate-180');
|
||
|
|
var onChange = typeof config.onChange === 'function' ? config.onChange : null;
|
||
|
|
var onBeforeToggle =
|
||
|
|
typeof config.onBeforeToggle === 'function' ? config.onBeforeToggle : null;
|
||
|
|
|
||
|
|
var expanded = defaultExpanded;
|
||
|
|
|
||
|
|
var stored = readStorageBoolean(storageKey, storageFormat);
|
||
|
|
if (typeof stored === 'boolean') {
|
||
|
|
expanded = stored;
|
||
|
|
} else if (initFromAria && toggleEl.hasAttribute('aria-expanded')) {
|
||
|
|
expanded = toggleEl.getAttribute('aria-expanded') === 'true';
|
||
|
|
}
|
||
|
|
|
||
|
|
function applyExpanded(nextExpanded, opts) {
|
||
|
|
var applyOptions = opts || {};
|
||
|
|
var persist = applyOptions.persist !== false;
|
||
|
|
var emit = applyOptions.emit !== false;
|
||
|
|
|
||
|
|
expanded = !!nextExpanded;
|
||
|
|
|
||
|
|
contentEl.classList.toggle(hiddenClass, !expanded);
|
||
|
|
|
||
|
|
if (syncHiddenAttribute) {
|
||
|
|
contentEl.hidden = !expanded;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (updateAria) {
|
||
|
|
toggleEl.setAttribute('aria-expanded', String(expanded));
|
||
|
|
}
|
||
|
|
|
||
|
|
applyChevronState(chevronEl, expanded, chevronMode, chevronRotateClass);
|
||
|
|
|
||
|
|
if (persist && persistState && storageKey) {
|
||
|
|
writeStorageBoolean(storageKey, expanded, storageFormat);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (emit && onChange) {
|
||
|
|
onChange(expanded, {
|
||
|
|
toggleEl: toggleEl,
|
||
|
|
contentEl: contentEl,
|
||
|
|
chevronEl: chevronEl,
|
||
|
|
controller: controller,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function handleToggle(event) {
|
||
|
|
if (onBeforeToggle) {
|
||
|
|
try {
|
||
|
|
if (onBeforeToggle(event, expanded) === false) return;
|
||
|
|
} catch {
|
||
|
|
// Ignore before-toggle hook errors.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
applyExpanded(!expanded);
|
||
|
|
}
|
||
|
|
|
||
|
|
toggleEl.addEventListener(eventName, handleToggle);
|
||
|
|
|
||
|
|
var controller = {
|
||
|
|
elements: {
|
||
|
|
toggleEl: toggleEl,
|
||
|
|
contentEl: contentEl,
|
||
|
|
chevronEl: chevronEl,
|
||
|
|
},
|
||
|
|
isExpanded: function () {
|
||
|
|
return expanded;
|
||
|
|
},
|
||
|
|
setExpanded: function (nextExpanded, opts) {
|
||
|
|
applyExpanded(nextExpanded, opts);
|
||
|
|
},
|
||
|
|
toggle: function (opts) {
|
||
|
|
applyExpanded(!expanded, opts);
|
||
|
|
},
|
||
|
|
destroy: function () {
|
||
|
|
toggleEl.removeEventListener(eventName, handleToggle);
|
||
|
|
controllerByToggle.delete(toggleEl);
|
||
|
|
},
|
||
|
|
};
|
||
|
|
|
||
|
|
controllerByToggle.set(toggleEl, controller);
|
||
|
|
|
||
|
|
applyExpanded(expanded, { persist: false, emit: emitInitial });
|
||
|
|
|
||
|
|
return controller;
|
||
|
|
}
|
||
|
|
|
||
|
|
function bindMany(configs) {
|
||
|
|
if (!Array.isArray(configs)) return [];
|
||
|
|
return configs.map(function (config) {
|
||
|
|
return bind(config);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
global.NCExpandableContainer = {
|
||
|
|
bind: bind,
|
||
|
|
bindMany: bindMany,
|
||
|
|
readStorageBoolean: readStorageBoolean,
|
||
|
|
writeStorageBoolean: writeStorageBoolean,
|
||
|
|
applyChevronState: applyChevronState,
|
||
|
|
};
|
||
|
|
})(window);
|