/** * 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 leaves a sibling *
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
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