Replace Tom Select with ITFlow's own select picker: native select stays the source of truth, search, checklists for multi selects, add-your-own for tag fields, phone sheet, and client/agent/contact avatars, tag and category colours, priority dots and asset icons in the options

This commit is contained in:
johnnyq
2026-10-13 12:41:29 -04:00
parent cc9f34e01a
commit 69484a8a2e
156 changed files with 2256 additions and 1182 deletions

View File

@@ -159,18 +159,11 @@ function itflowInit() {
}, 5000);
})();
// Enhance any select that is not enhanced yet. On a normal page load
// js/tom_select.js has already done the work and every element here is
// guarded, so this is a no-op; it earns its place on ajax modals, whose
// markup does not exist until includes/modal_footer.php re-executes this
// file. Scoped to `select.select2` deliberately - a bare '.select2' also
// matches the .ts-wrapper divs left by earlier enhancement, because Tom
// Select copies the source element's classes onto them. See
// js/tom_select.js.
itflowStep('tom-select', function () {
document.querySelectorAll('select.select2').forEach(function (el) {
initTomSelect(el);
});
// Any select2 field not picked up yet. js/itflow_select.js has normally
// done them all already and enhances new ones as they arrive, so this is
// a belt-and-braces pass that finds nothing to do.
itflowStep('select', function () {
itflowSelectAll(document);
});
// Initialize TinyMCE
@@ -943,13 +936,6 @@ function itflowCopyFeedbackTarget(trigger) {
return (menu && menu.parentElement) || trigger;
}
/*
* The Tom Select layer (initTomSelect / refreshTomSelect / clearTomSelect /
* setTomSelectValue) lives in js/tom_select.js, which footer.php loads right
* after the library so the enhancement happens before the page's heavy libs
* are parsed. Those functions are global; this file only calls them.
*/
/**
* Show a Bootstrap toast from JavaScript.
*

1234
js/itflow_select.js Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -2,7 +2,7 @@
* ITFlow - intl-tel-input wiring, shared by the agent side and the client portal.
*
* Lived in js/app.js until the portal needed it too. app.js cannot be loaded in
* the portal - it calls DataTables, TinyMCE, Tom Select, Flatpickr and IMask
* the portal - it calls DataTables, TinyMCE, Flatpickr and IMask
* unconditionally with no typeof guards, none of which the portal loads - so
* the choice was to copy this or to split it out. Copying it would have left
* two implementations of a subtle piece of logic to drift apart, so it is here.

View File

@@ -2,7 +2,7 @@
* ITFlow - DataTables for the client portal.
*
* The agent side initialises DataTables from js/app.js, which the portal cannot
* load: app.js calls TinyMCE, Tom Select, Flatpickr and IMask unconditionally
* load: app.js calls TinyMCE, Flatpickr and IMask unconditionally
* and none of them exist here. This is the portal's own one-liner rather than a
* shared extraction, because unlike the phone-input logic there is no real
* behaviour to keep in step - just a constructor.

View File

@@ -59,7 +59,11 @@ function initProductAutocomplete(availableProducts) {
document.getElementById('desc').value = item.description;
document.getElementById('qty').value = 1;
document.getElementById('price').value = item.price;
setTomSelectValue(document.getElementById('tax'), item.tax);
const tax = document.getElementById('tax');
if (tax) {
tax.value = item.tax;
tax.dispatchEvent(new Event('change', { bubbles: true }));
}
if (productIdInput) {
productIdInput.value = item.prod_id;
}

View File

@@ -1,133 +0,0 @@
/**
* Tom Select integration.
*
* Replaces Select2. Tom Select is vanilla JS and exposes its instance on the
* element as `el.tomselect`, so nothing here needs jQuery.
*
* Select2 concepts and their equivalents, for anyone reading this later:
* $(el).select2({tags:true}) -> create: true
* $(el).val(null).trigger('change') -> el.tomselect.clear()
* $(el).trigger('change.select2') -> el.tomselect.sync() (options replaced)
* $(el).on('select2:select', fn) -> el.tomselect.on('change', fn)
*
* WHY THIS IS ITS OWN FILE RATHER THAN PART OF js/app.js
*
* The visible flash on page load was a timing problem, not a styling one. The
* browser paints the native <select> as soon as it parses it, and the swap to
* Tom Select could not happen until app.js ran - which is dead last in
* includes/footer.php, behind roughly a megabyte of parser-blocking library
* code (tinymce 486K, intl-tel-input 316K, DataTables 123K, sweetalert2 49K,
* adminlte 28K and the rest). Every byte of that had to download, parse and
* execute first, and the native controls sat on screen for the whole of it.
*
* Splitting the Tom Select layer out lets footer.php load it immediately after
* the library itself, ahead of everything that has nothing to do with it. Only
* bootstrap.bundle and http.js are still in front of it.
*
* The sweep at the bottom runs synchronously rather than on DOMContentLoaded,
* on purpose: DOMContentLoaded does not fire until every parser-blocking
* script in the body has run, which is the exact wait this file exists to
* avoid. Running inline is safe because this script tag sits after the closing
* .app-wrapper div, so the page markup above it is already parsed.
*
* js/app.js keeps its own tom-select step. That is not redundant - it is what
* enhances selects inside ajax modals, whose markup does not exist yet at this
* point and whose footer (includes/modal_footer.php) re-executes app.js. On a
* normal page load that later pass finds everything already enhanced and the
* guard in initTomSelect makes it a no-op.
*/
function initTomSelect(el, options) {
if (!el || el.tomselect) {
return el ? el.tomselect : null;
}
/*
* Only ever enhance a real form control.
*
* Tom Select copies the source element's classes onto its own wrapper, so
* an enhanced <select class="form-select select2"> leaves a sibling
* <div class="ts-wrapper form-select select2 ..."> behind it. That means a
* querySelectorAll('.select2') run AFTER any enhancement matches the
* wrapper too, and the wrapper has no .tomselect of its own so the guard
* above waves it through. Constructing Tom Select on a <div> yields a
* second, empty control - no options, no placeholder - stacked on top of
* the working one, which reads as "the select is blank".
*
* The sweeps below and in js/app.js are scoped to `select.select2` so this
* cannot happen, but the check belongs here as well: initTomSelect is
* called directly from agent/js/share_modal.js and anywhere else that
* grows a call site later.
*/
if (el.tagName !== 'SELECT' && el.tagName !== 'INPUT') {
return null;
}
/*
* data-tags="true" is the select2 attribute for "the user can type a value
* that is not in the list". select2 read data-* attributes as options on
* its own, so the old bare $('.select2').select2() honoured it with no
* code; Tom Select does not, and every data-tags field quietly became a
* pick-from-list-only control in the migration (ticket watchers, trip
* destination, client referral, invoice scope, default start page).
*
* createOnBlur commits whatever is typed when focus leaves the control, so
* typing an address and clicking the modal's submit button posts it,
* rather than posting the empty value because Enter was never pressed.
*/
var tags = el.getAttribute('data-tags') === 'true';
var settings = Object.assign({
create: tags,
createOnBlur: tags,
allowEmptyOption: true,
plugins: el.multiple ? ['remove_button'] : [],
placeholder: el.getAttribute('data-placeholder') || undefined
}, options || {});
return new TomSelect(el, settings);
}
/** Re-read the <option> list after it has been replaced server-side. */
function refreshTomSelect(el) {
if (el && el.tomselect) {
el.tomselect.sync();
}
}
/** Clear a selection (single or multiple) without firing a server round-trip. */
function clearTomSelect(el) {
if (el && el.tomselect) {
el.tomselect.clear(true);
}
}
/**
* Set a select's value from code. A plain el.value = x (or jQuery .val()) does
* not repaint a Tom Select widget - the underlying <select> changes but the
* visible control does not. Falls back to a native change event when the
* element was never enhanced.
*/
function setTomSelectValue(el, value) {
if (!el) {
return;
}
if (el.tomselect) {
el.tomselect.setValue(value);
return;
}
el.value = value;
el.dispatchEvent(new Event('change', { bubbles: true }));
}
/**
* Enhance every .select2 already in the document.
*
* Wrapped in try/catch for the same reason app.js wraps its init steps: one
* malformed select must not stop the rest of the page's scripts from running.
*/
(function () {
try {
document.querySelectorAll('select.select2').forEach(function (el) {
initTomSelect(el);
});
} catch (e) {
console.error('itflow init [tom-select-eager] failed:', e);
}
})();