(function (window, document) { 'use strict'; if (window.CQDatatable && window.CQDatatable.__v === '1') return; var __cqSelectionStore = {}; var __cqSelectionProxy = null; var __cqSelectionAliasInstalled = false; function currentCheckedKeysFromDom() { return getCheckedKeys(document); } function currentSelectionKeys() { var domKeys = currentCheckedKeysFromDom(); if (domKeys && domKeys.length) return Array.from(new Set(domKeys)); return Object.keys(__cqSelectionStore); } function ensureProxy() { if (__cqSelectionProxy) return __cqSelectionProxy; __cqSelectionProxy = new Proxy(__cqSelectionStore, { ownKeys: function () { return currentSelectionKeys(); }, getOwnPropertyDescriptor: function (target, prop) { if (typeof prop === 'string') { if (Object.prototype.hasOwnProperty.call(target, prop) || currentSelectionKeys().indexOf(prop) !== -1) { return { enumerable: true, configurable: true }; } } return Object.getOwnPropertyDescriptor(target, prop); }, has: function (target, prop) { if (typeof prop === 'string' && currentSelectionKeys().indexOf(prop) !== -1) return true; return prop in target; }, get: function (target, prop) { if (prop === '__rawStore') return target; if (typeof prop === 'string') { if (Object.prototype.hasOwnProperty.call(target, prop)) return target[prop]; if (currentSelectionKeys().indexOf(prop) !== -1) return true; } return target[prop]; }, set: function (target, prop, value) { target[prop] = value; return true; }, deleteProperty: function (target, prop) { delete target[prop]; return true; } }); return __cqSelectionProxy; } function installSelectionAliases() { if (__cqSelectionAliasInstalled) return; __cqSelectionAliasInstalled = true; var proxy = ensureProxy(); function defineAlias(name) { var existing = null; try { existing = window[name]; } catch (_) { existing = null; } try { delete window[name]; } catch (_) { } try { Object.defineProperty(window, name, { configurable: true, enumerable: true, get: function () { return proxy; }, set: function (value) { if (!value || typeof value !== 'object') return; Object.keys(__cqSelectionStore).forEach(function (k) { delete __cqSelectionStore[k]; }); Object.keys(value).forEach(function (k) { if (value[k]) __cqSelectionStore[k] = true; }); } }); } catch (_) { window[name] = proxy; } if (existing && typeof existing === 'object') { Object.keys(existing).forEach(function (k) { if (existing[k]) __cqSelectionStore[k] = true; }); } } defineAlias('tableSelection'); defineAlias('invoiceTableSelection'); } function ensureStore() { installSelectionAliases(); return ensureProxy(); } function keyToId(key) { return String(key || '').replace(/^chk_/, ''); } function keysToIds(keys) { return (keys || []) .filter(function (k) { return typeof k === 'string' && k.indexOf('chk_') === 0; }) .map(keyToId) .filter(Boolean); } function getRowCheckboxes(scope) { var root = scope || document; return Array.prototype.slice.call( root.querySelectorAll('input.row-checkbox[type="checkbox"], input.check-item[type="checkbox"]') ); } function getCheckedKeys(scope) { return getRowCheckboxes(scope) .filter(function (cb) { return !!(cb && cb.checked && cb.id); }) .map(function (cb) { return cb.id; }); } function getScopeKnownKeys(scope) { return getRowCheckboxes(scope) .filter(function (cb) { return !!(cb && cb.id); }) .map(function (cb) { return cb.id; }); } function scopeContainsTableSelection(scope) { var root = scope || document; if (!root || root === document) return false; try { return !!root.querySelector('.cq-invoice-dt-root, input.row-checkbox[type="checkbox"], input.check-item[type="checkbox"]'); } catch (_) { return false; } } function syncCheckbox(checkbox, checked) { if (!checkbox || !checkbox.id) return; var isChecked = (typeof checked === 'boolean') ? checked : !!checkbox.checked; var store = ensureStore(); if (isChecked) store[checkbox.id] = true; else delete store[checkbox.id]; } function isSelectedKey(key) { return !!ensureStore()[key]; } function pruneToKnownKeys(knownKeys) { var store = ensureStore(); var known = {}; (knownKeys || []).forEach(function (k) { if (k) known[String(k)] = true; }); Object.keys(store).forEach(function (key) { if (!known[key]) delete store[key]; }); } function pruneToDom(scope) { pruneToKnownKeys(getCheckedKeys(scope)); } function getSelectedIds(opts) { var options = opts || {}; var scope = options.scope || document; var preferDom = (options.preferDom !== false); var boxes = getRowCheckboxes(scope); if (preferDom && boxes.length) { return Array.from(new Set( boxes.filter(function (cb) { return !!cb.checked; }).map(function (cb) { return keyToId(cb.id); }) )).filter(Boolean); } if (scopeContainsTableSelection(scope)) { return keysToIds(getScopeKnownKeys(scope).filter(function (key) { return !!ensureStore()[key]; })); } return keysToIds(Object.keys(ensureStore())); } function clearSelection(opts) { var options = opts || {}; var scope = options.scope || document; var uncheckDom = (options.uncheckDom !== false); var clearStore = (options.clearStore !== false); if (clearStore) { var store = ensureStore(); if (scopeContainsTableSelection(scope)) { getScopeKnownKeys(scope).forEach(function (k) { delete store[k]; }); } else { Object.keys(store).forEach(function (k) { delete store[k]; }); } } if (uncheckDom) { getRowCheckboxes(scope).forEach(function (cb) { cb.checked = false; }); Array.prototype.slice.call( scope.querySelectorAll('input[type="checkbox"][id^="all_"], input[type="checkbox"][id*="all_"], input.cq-dt-select-all') ).forEach(function (cb) { cb.checked = false; cb.indeterminate = false; }); } if (typeof window.showHideDropdownMenu === 'function') { window.showHideDropdownMenu(false); } } function installSelectionGuard() { if (window.__cqSelectionGuardInstalled || !document.body) return; window.__cqSelectionGuardInstalled = true; document.body.addEventListener('htmx:beforeSwap', function (evt) { var t = evt && evt.detail && evt.detail.target; if (t && t.id === 'main') clearSelection({ scope: t, uncheckDom: false, clearStore: true }); }); document.body.addEventListener('htmx:afterSwap', function (evt) { var t = evt && evt.detail && evt.detail.target; if (t && t.id === 'main') clearSelection({ scope: t, uncheckDom: true, clearStore: true }); }); } if (document.body) { installSelectionGuard(); } else { document.addEventListener('DOMContentLoaded', installSelectionGuard, { once: true }); } var api = { __v: '1', ensureStore: ensureStore, keyToId: keyToId, keysToIds: keysToIds, getRowCheckboxes: getRowCheckboxes, getCheckedKeys: getCheckedKeys, syncCheckbox: syncCheckbox, isSelectedKey: isSelectedKey, pruneToKnownKeys: pruneToKnownKeys, pruneToDom: pruneToDom, getSelectedIds: getSelectedIds, clearSelection: clearSelection }; window.CQDatatable = api; // Backward-compatible globals used across legacy pages. window.cqSelectionKeysToIds = window.cqSelectionKeysToIds || keysToIds; window.cqGetCheckedSelectionKeysFromDom = window.cqGetCheckedSelectionKeysFromDom || function () { return getCheckedKeys(document); }; window.cqPruneTableSelectionToDom = window.cqPruneTableSelectionToDom || function () { return pruneToDom(document); }; window.cqGetSelectedIds = window.cqGetSelectedIds || function () { return getSelectedIds({ scope: document, preferDom: true }); }; window.cqClearTableSelection = window.cqClearTableSelection || function (uncheckDom) { return clearSelection({ scope: document, uncheckDom: uncheckDom !== false, clearStore: true }); }; })(window, document);