From 32cf47dc4ebd1142efe702c58547da27a501de6b Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Mon, 20 Apr 2026 11:25:55 +0200 Subject: [PATCH 1/3] Replace runtime with a Stimulus controller Rails ships Stimulus by default, making it the natural host for client-side validation lifecycle management. The previous runtime auto-booted from a UMD bundle served by Sprockets or Webpacker, required manual enable/disable calls, and used a custom jQuery-era event loop that did not handle dynamically inserted fields. This commit replaces all of that with a single Stimulus controller (identifier: client-side-validations) that owns the full lifecycle: - Form context is installed in connect() and torn down in disconnect() - Validated inputs are declared as Stimulus targets; new inputs added via Turbo Stream, cocoon, or nested-form are picked up automatically through inputTargetConnected / inputTargetDisconnected - Confirmation fields use a dedicated 'confirmation' target with a data-client-side-validations-confirms attribute pointing to the primary field's id - Settings travel as a Stimulus value (data-...-settings-value) so the controller owns both config and lifecycle with no global state Breaking changes: - window.ClientSideValidations global and its enable/validate/disable/ reset API are removed; use the Stimulus controller lifecycle instead - data-client-side-validations attribute replaced by the settings value - UMD bundle and Sprockets/Webpacker asset generators removed; the gem now ships a single ESM bundle consumed via the npm package - ajax:beforeSend / remote: true integration removed; validation runs on the native submit event - Stimulus >= 3.2 is now a peer dependency The install generator now copies a Stimulus controller stub into app/javascript/controllers/ alongside the existing initializer. A new Config.stimulus_controller_name option allows projects to mount the controller under a custom identifier. Close #929 --- .rubocop.yml | 2 +- CHANGELOG.md | 14 + README.md | 99 +- client_side_validations.gemspec | 2 +- dist/client-side-validations.esm.js | 1009 +++-- dist/client-side-validations.js | 789 ---- lib/client_side_validations.rb | 5 +- .../action_view/form_builder.rb | 61 +- .../action_view/form_helper.rb | 6 +- lib/client_side_validations/config.rb | 3 +- lib/client_side_validations/files.rb | 10 - lib/client_side_validations/generators.rb | 17 - .../generators/rails_validations.rb | 16 - lib/client_side_validations/version.rb | 2 +- .../copy_assets_generator.rb | 64 - .../install_generator.rb | 18 +- .../client_side_validations_controller.js | 15 + package.json | 17 +- rollup.config.mjs | 26 +- .../client_side_validations_controller.js | 89 + src/core.js | 208 +- src/index.js | 227 +- src/lifecycle.js | 99 + src/validate.js | 174 + test/action_view/cases/helper.rb | 50 +- test/generators/cases/test_generators.rb | 71 +- .../public/test/stimulus/controller.js | 142 + test/javascript/public/vendor/test-bundle.js | 3446 +++++++++++++++++ test/javascript/server.rb | 20 - test/javascript/test-bundle.js | 119 + test/javascript/views/index.erb | 4 +- test/test_loader.rb | 1 - .../assets/javascripts/rails.validations.js | 789 ---- 33 files changed, 4823 insertions(+), 2791 deletions(-) delete mode 100644 dist/client-side-validations.js delete mode 100644 lib/client_side_validations/files.rb delete mode 100644 lib/client_side_validations/generators.rb delete mode 100644 lib/client_side_validations/generators/rails_validations.rb delete mode 100644 lib/generators/client_side_validations/copy_assets_generator.rb create mode 100644 lib/generators/templates/client_side_validations/client_side_validations_controller.js create mode 100644 src/controllers/client_side_validations_controller.js create mode 100644 src/lifecycle.js create mode 100644 src/validate.js create mode 100644 test/javascript/public/test/stimulus/controller.js create mode 100644 test/javascript/public/vendor/test-bundle.js create mode 100644 test/javascript/test-bundle.js delete mode 100644 vendor/assets/javascripts/rails.validations.js diff --git a/.rubocop.yml b/.rubocop.yml index cc32d2ff4..8b8045a5f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -49,7 +49,7 @@ Metrics/MethodLength: - 'test/**/*' Metrics/ModuleLength: - Max: 109 + Max: 150 Exclude: - 'test/**/*' diff --git a/CHANGELOG.md b/CHANGELOG.md index bdeecfb36..c1479fa72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## 25.0.0 / unreleased + +Stimulus-only rewrite. This is a major breaking release. + +* [FEATURE] Breaking change: The runtime is now a [Stimulus](https://stimulus.hotwired.dev) controller (`client-side-validations`). Rails already ships Stimulus via `@hotwired/stimulus-rails`, so no additional dependency is required in typical Rails apps. +* [FEATURE] Breaking change: Forms and inputs are bound through Stimulus targets (`data-controller="client-side-validations"`, `data-client-side-validations-target="input"`, `data-client-side-validations-target="confirmation"`, `data-client-side-validations-confirms="..."`). Inputs added dynamically (cocoon, `stimulus-rails-nested-form`, Turbo Stream appends, etc.) are bound automatically through `inputTargetConnected` / `disconnected`. +* [FEATURE] Breaking change: Settings travel in `data-client-side-validations-settings-value` (the Stimulus value) instead of `data-client-side-validations`. +* [FEATURE] Breaking change: Drop the UMD bundle, the `vendor/assets/javascripts/rails.validations.js` copy, the Sprockets/Webpacker asset generators, and the `rails g client_side_validations:copy_assets` command. The gem ships a single ESM bundle (`dist/client-side-validations.esm.js`) consumed via the npm package. +* [FEATURE] Breaking change: Drop `window.ClientSideValidations` auto-boot and the `enable` / `disable` / `validate` / `reset` public API. Use the Stimulus controller lifecycle instead. Import `register` from the package to attach the controller to your Stimulus `Application` with a custom identifier. +* [FEATURE] Breaking change: Drop the `ajax:beforeSend` / `remote: true` integration. `form_with` no longer coordinates with `jquery-ujs`; validation runs on the native `submit` event. +* [FEATURE] Breaking change: Drop the `data-client-side-validations` HTML attribute. +* [FEATURE] The install generator (`rails g client_side_validations:install`) now copies a Stimulus controller template into `app/javascript/controllers/` alongside the initializer. +* [ENHANCEMENT] New `ClientSideValidations::Config.stimulus_controller_name` configuration option for projects that need to mount the controller under a different identifier. + ## 24.0.0 / 2026-04-19 * [FEATURE] Breaking change: Remove the jQuery runtime dependency and the old jQuery plugin aliases from the published JavaScript assets * [FEATURE] Breaking change: Public JavaScript APIs now work with native DOM elements and DOM collections instead of jQuery-wrapped objects diff --git a/README.md b/README.md index cb5de28e2..342bc6d5f 100644 --- a/README.md +++ b/README.md @@ -54,63 +54,90 @@ config/initializers/client_side_validations.rb ### JavaScript file ### -Instructions depend on your technology stack. +Starting with 25.0, ClientSideValidations is a [Stimulus](https://stimulus.hotwired.dev) controller. +Rails already ships Stimulus via `@hotwired/stimulus-rails`, so no extra JavaScript dependency is +typically required. -ClientSideValidations no longer depends on jQuery. -If you previously installed `jquery-rails`, `jquery_ujs`, or custom jQuery startup code only for ClientSideValidations, you can remove that integration when upgrading to 24.x. - -#### When using Webpacker #### - -Add the following package: +Add the npm package: ```sh yarn add @client-side-validations/client-side-validations +# or +bin/importmap pin @client-side-validations/client-side-validations ``` -Then add the following line to your `app/javascript/packs/application.js` pack: +Then register the controller with your Stimulus `Application`. In a typical +`app/javascript/controllers/index.js`: ```js -// If you are using `import` syntax -import '@client-side-validations/client-side-validations' +import { application } from './application' +import { ClientSideValidationsController } from '@client-side-validations/client-side-validations' -// If you are using `require` syntax -require('@client-side-validations/client-side-validations') +application.register('client-side-validations', ClientSideValidationsController) ``` -##### Heads-up for Turbo and Turbolinks users ##### - -If you are using [Turbo](https://github.com/hotwired/turbo-rails), use the -`import` syntax and make sure that `@client-side-validations/client-side-validations` -is imported **after** `@hotwired/turbo-rails`, so ClientSideValidations can properly detect -`window.Turbo` and attach its event handlers. - -If you are using [Turbolinks](https://github.com/turbolinks/turbolinks) 5.2, -use the `require` syntax and make sure that `@client-side-validations/client-side-validations` -is required **after** `Turbolinks.start()`, so ClientSideValidations can properly -detect `window.Turbolinks` and attach its event handlers. - -#### When using Sprockets #### - -Add the following to your `app/assets/javascripts/application.js` file: +Or, using the bundled `register` helper (equivalent, with the default +identifier `client-side-validations`): ```js -//= require rails.validations +import { application } from './application' +import { register } from '@client-side-validations/client-side-validations' + +register(application) +// or, with a custom identifier +register(application, 'csv-form') ``` -If you are using [Turbolinks](https://github.com/turbolinks/turbolinks), -make sure that `rails.validations` is required **after** `turbolinks`, so -ClientSideValidations can properly attach its event handlers. +The install generator also copies a stub controller at +`app/javascript/controllers/client_side_validations_controller.js` that simply +re-exports the controller from the npm package, so you can adapt it if you +need to customize behaviour. -If you need to copy the asset files from the gem into your project, run: +If you pick a custom identifier, mirror it on the Ruby side: -``` -rails g client_side_validations:copy_assets +```ruby +# config/initializers/client_side_validations.rb +ClientSideValidations::Config.stimulus_controller_name = 'csv-form' ``` -Note: If you run `copy_assets`, you will need to run it again each time you update this project. - ## Migration Guide ## +### 25.x Breaking Changes ### + +Release 25.0 replaces the auto-booting runtime with a Stimulus controller. +Review the following migration steps when upgrading from 24.x: + +* **Data attributes have changed.** Forms emit + `data-controller="client-side-validations"` and + `data-client-side-validations-settings-value='{...}'` instead of + `data-client-side-validations='{...}'`. Validated inputs and confirmation + fields get `data-client-side-validations-target="input"` or + `data-client-side-validations-target="confirmation"`. The Rails form helpers + emit these automatically when `validate: true` is set; you do not need to add + them by hand. +* **The `ClientSideValidations.enable / validate / disable / reset` public API + is gone.** The Stimulus lifecycle (`connect`, `disconnect`, + `inputTargetConnected`, `inputTargetDisconnected`) handles binding and + unbinding. Dynamically inserted inputs (e.g. cocoon, Turbo Stream appends, + `stimulus-rails-nested-form`) are picked up automatically through Stimulus + target callbacks, no `enable()` call required. +* **No automatic `window.ClientSideValidations` global.** Import what you + need directly from the package. The remaining `ClientSideValidations` + export is a configuration namespace (`validators`, `formBuilders`, + `callbacks`, `patterns`, `selectors`). +* **UMD bundle and Sprockets support removed.** Only an ESM bundle is + shipped. The `client_side_validations:copy_assets` generator is gone; use + the npm package through Importmaps, Webpacker, esbuild, Vite, or your + bundler of choice. +* **`ajax:beforeSend` / `remote: true` integration removed.** `form_with` + no longer coordinates with `jquery-ujs`; validation runs on the native + `submit` event. If you relied on this integration, migrate to Turbo or + handle `submit` yourself. +* **`data-client-side-validations` attribute removed.** Read the settings + via `data-client-side-validations-settings-value` (a JSON-encoded Stimulus + value) or via `form.ClientSideValidations.settings` after the controller + connects. + ### 24.x Breaking Changes ### If you are upgrading to 24.x, update your integration code to use the `ClientSideValidations` object directly. diff --git a/client_side_validations.gemspec b/client_side_validations.gemspec index bde6608c1..346d7e65a 100644 --- a/client_side_validations.gemspec +++ b/client_side_validations.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.metadata['changelog_uri'] = 'https://github.com/DavyJonesLocker/client_side_validations/blob/main/CHANGELOG.md' spec.metadata['source_code_uri'] = 'https://github.com/DavyJonesLocker/client_side_validations' - spec.files = Dir.glob('{CHANGELOG.md,LICENSE.md,README.md,lib/**/*.rb,vendor/**/*.js}', File::FNM_DOTMATCH) + spec.files = Dir.glob('{CHANGELOG.md,LICENSE.md,README.md,lib/**/*}', File::FNM_DOTMATCH) spec.require_paths = ['lib'] spec.platform = Gem::Platform::RUBY diff --git a/dist/client-side-validations.esm.js b/dist/client-side-validations.esm.js index 6b67e0de9..1e7a16fab 100644 --- a/dist/client-side-validations.esm.js +++ b/dist/client-side-validations.esm.js @@ -1,44 +1,10 @@ /*! - * Client Side Validations JS - v24.0.0 (https://github.com/DavyJonesLocker/client_side_validations) + * Client Side Validations JS - v25.0.0 (https://github.com/DavyJonesLocker/client_side_validations) * Copyright (c) 2026 Geremia Taglialatela, Brian Cardarella * Licensed under MIT (https://opensource.org/licenses/mit-license.php) */ -const boundEventListeners = new WeakMap(); -const addBoundEventListener = (element, eventName, listener) => { - element.addEventListener(eventName, listener); - const listeners = boundEventListeners.get(element) || []; - listeners.push({ - eventName, - listener - }); - boundEventListeners.set(element, listeners); -}; -const bindElementEvents = (element, eventsToBind) => { - for (const eventName in eventsToBind) { - addBoundEventListener(element, eventName, eventsToBind[eventName]); - } -}; -const clearBoundEventListeners = element => { - const listeners = boundEventListeners.get(element); - if (!listeners) { - return; - } - listeners.forEach(_ref => { - let { - eventName, - listener - } = _ref; - element.removeEventListener(eventName, listener); - }); - boundEventListeners.delete(element); -}; -const dispatchCustomEvent = (element, eventName, detail) => { - element.dispatchEvent(new CustomEvent(eventName, { - bubbles: true, - detail - })); -}; +import { Controller } from '@hotwired/stimulus'; const arrayHasValue = (value, otherValues) => { for (let i = 0, l = otherValues.length; i < l; i++) { @@ -74,17 +40,6 @@ const getDOMElements = target => { const isFormElement = element => { return element.tagName === 'FORM'; }; -const isInputElement = element => { - switch (element.tagName) { - case 'INPUT': - return element.type !== 'submit' && element.type !== 'button'; - case 'SELECT': - case 'TEXTAREA': - return true; - default: - return false; - } -}; const isVisible = element => { return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length); }; @@ -92,26 +47,6 @@ const isValuePresent = value => { return !/^\s*$/.test(value || ''); }; -const isNamedInputElement = element => { - return isInputElement(element) && element.name != null && element.name !== ''; -}; -const getFormControls = form => { - return Array.from(form.elements).filter(isInputElement); -}; -const getFormInputs = form => { - return getFormControls(form).filter(element => { - return isNamedInputElement(element) && !element.disabled && isVisible(element); - }); -}; -const findFormElementByName = (form, name) => { - return getFormControls(form).find(element => element.name === name); -}; -const enableForm = form => { - ClientSideValidations.enablers.form(form); -}; -const enableForms = () => { - document.querySelectorAll(ClientSideValidations.selectors.forms).forEach(enableForm); -}; const ClientSideValidations = { callbacks: { element: { @@ -127,110 +62,6 @@ const ClientSideValidations = { pass: (form, eventData) => {} } }, - eventsToBind: { - form: form => ({ - submit: eventData => { - if (!ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators)) { - eventData.preventDefault(); - eventData.stopImmediatePropagation(); - } - }, - 'ajax:beforeSend': function (eventData) { - if (eventData.target === this) { - ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators); - } - }, - 'form:validate:after': eventData => { - ClientSideValidations.callbacks.form.after(form, eventData); - }, - 'form:validate:before': eventData => { - ClientSideValidations.callbacks.form.before(form, eventData); - }, - 'form:validate:fail': eventData => { - ClientSideValidations.callbacks.form.fail(form, eventData); - }, - 'form:validate:pass': eventData => { - ClientSideValidations.callbacks.form.pass(form, eventData); - } - }), - input: form => ({ - focusout: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - }, - change: function () { - this.dataset.csvChanged = 'true'; - }, - 'element:validate:after': function (eventData) { - ClientSideValidations.callbacks.element.after(this, eventData); - }, - 'element:validate:before': function (eventData) { - ClientSideValidations.callbacks.element.before(this, eventData); - }, - 'element:validate:fail': function (eventData) { - const element = this; - const message = eventData.detail; - ClientSideValidations.callbacks.element.fail(element, message, function () { - form.ClientSideValidations.addError(element, message); - }, eventData); - }, - 'element:validate:pass': function (eventData) { - const element = this; - ClientSideValidations.callbacks.element.pass(element, function () { - form.ClientSideValidations.removeError(element); - }, eventData); - } - }), - inputConfirmation: (elementToConfirm, form) => ({ - focusout: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - }, - keyup: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - } - }) - }, - enablers: { - form: form => { - clearBoundEventListeners(form); - getFormControls(form).forEach(clearBoundEventListeners); - form.ClientSideValidations = { - settings: JSON.parse(form.dataset.clientSideValidations), - addError: (element, message) => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].add(element, form.ClientSideValidations.settings.html_settings, message), - removeError: element => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].remove(element, form.ClientSideValidations.settings.html_settings) - }; - bindElementEvents(form, ClientSideValidations.eventsToBind.form(form)); - getFormInputs(form).forEach(element => { - ClientSideValidations.enablers.input(element); - }); - }, - input: function (input) { - const form = input.form; - if (!form) { - return; - } - clearBoundEventListeners(input); - const eventsToBind = ClientSideValidations.eventsToBind.input(form); - if (input.type !== 'radio' && !(input.id && input.id.endsWith('_confirmation'))) { - input.dataset.csvValidate = 'true'; - bindElementEvents(input, eventsToBind); - } - if (input.type === 'checkbox') { - bindElementEvents(input, { - change: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - } - }); - } - if (input.id && input.id.endsWith('_confirmation')) { - const elementToConfirm = document.getElementById(input.id.match(/(.+)_confirmation/)[1]); - if (elementToConfirm && elementToConfirm.form === form) { - bindElementEvents(input, ClientSideValidations.eventsToBind.inputConfirmation(elementToConfirm, form)); - } - } - } - }, formBuilders: { 'ActionView::Helpers::FormBuilder': { add: (element, settings, message) => { @@ -298,11 +129,6 @@ const ClientSideValidations = { only_integer: /^[+-]?\d+$/ } }, - selectors: { - inputs: 'input:not([type="submit"]):not([type="button"])[name], select[name], textarea[name]', - validate_inputs: 'input[data-csv-validate]:not([type="submit"]):not([type="button"]), select[data-csv-validate], textarea[data-csv-validate]', - forms: 'form[data-client-side-validations]' - }, validators: { all: () => { return { @@ -312,218 +138,407 @@ const ClientSideValidations = { }, local: {}, remote: {} - }, - disable: target => { - getDOMElements(target).forEach(element => { - clearBoundEventListeners(element); - if (isFormElement(element)) { - getFormControls(element).forEach(input => { - clearBoundEventListeners(input); - delete input.dataset.csvValid; - delete input.dataset.csvChanged; - delete input.dataset.csvValidate; - }); - return; - } - delete element.dataset.csvValid; - delete element.dataset.csvChanged; - if (isInputElement(element)) { - delete element.dataset.csvValidate; - } - }); - }, - reset: form => { - ClientSideValidations.disable(form); - for (const key in form.ClientSideValidations.settings.validators) { - const element = findFormElementByName(form, key); - if (element) { - form.ClientSideValidations.removeError(element); - } - } - ClientSideValidations.enablers.form(form); - }, - initializeOnEvent: () => { - if (window.Turbo != null) { - return 'turbo:load'; - } else if (window.Turbolinks != null && window.Turbolinks.supported) { - return window.Turbolinks.EVENTS != null ? 'page:change' : 'turbolinks:load'; - } - }, - start: () => { - const initializeOnEvent = ClientSideValidations.initializeOnEvent(); - if (initializeOnEvent != null) { - document.addEventListener(initializeOnEvent, enableForms); - } else if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', enableForms, { - once: true - }); - } else { - enableForms(); - } } }; -const absenceLocalValidator = (element, options) => { - if (isValuePresent(element.value)) { - return options.message; +function _assertClassBrand(e, t, n) { + if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; + throw new TypeError("Private element is not present on this object"); +} +function _checkPrivateRedeclaration(e, t) { + if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); +} +function _classPrivateMethodInitSpec(e, a) { + _checkPrivateRedeclaration(e, a), a.add(e); +} +function _defineProperty(e, r, t) { + return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { + value: t, + enumerable: true, + configurable: true, + writable: true + }) : e[r] = t, e; +} +function _toPrimitive(t, r) { + if ("object" != typeof t || !t) return t; + var e = t[Symbol.toPrimitive]; + if (void 0 !== e) { + var i = e.call(t, r); + if ("object" != typeof i) return i; + throw new TypeError("@@toPrimitive must return a primitive value."); + } + return ("string" === r ? String : Number)(t); +} +function _toPropertyKey(t) { + var i = _toPrimitive(t, "string"); + return "symbol" == typeof i ? i : i + ""; +} + +const boundEventListeners = new WeakMap(); +const addBoundEventListener = (element, eventName, listener) => { + element.addEventListener(eventName, listener); + const listeners = boundEventListeners.get(element) || []; + listeners.push({ + eventName, + listener + }); + boundEventListeners.set(element, listeners); +}; +const bindElementEvents = (element, eventsToBind) => { + for (const eventName in eventsToBind) { + addBoundEventListener(element, eventName, eventsToBind[eventName]); } }; -const presenceLocalValidator = (element, options) => { - if (!isValuePresent(element.value)) { - return options.message; +const clearBoundEventListeners = element => { + const listeners = boundEventListeners.get(element); + if (!listeners) { + return; } + listeners.forEach(_ref => { + let { + eventName, + listener + } = _ref; + element.removeEventListener(eventName, listener); + }); + boundEventListeners.delete(element); +}; +const dispatchCustomEvent = (element, eventName, detail) => { + element.dispatchEvent(new CustomEvent(eventName, { + bubbles: true, + detail + })); }; -const DEFAULT_ACCEPT_OPTION = ['1', true]; -const isTextAccepted = (value, acceptOption) => { - if (!acceptOption) { - acceptOption = DEFAULT_ACCEPT_OPTION; - } - if (Array.isArray(acceptOption)) { - return arrayHasValue(value, acceptOption); +const cleanNestedElementName = (elementName, nestedMatches, validators) => { + for (const validatorName in validators) { + if (validatorName.match("\\[".concat(nestedMatches[1], "\\].*\\[\\]\\[").concat(nestedMatches[2], "\\]$"))) { + elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]'); + } } - return value === acceptOption; + return elementName; }; -const acceptanceLocalValidator = (element, options) => { - let valid = true; - if (element.type === 'checkbox') { - valid = element.checked; +const cleanElementName = (elementName, validators) => { + elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); + const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); + if (nestedMatches) { + elementName = cleanNestedElementName(elementName, nestedMatches, validators); } - if (element.type === 'text') { - valid = isTextAccepted(element.value, options.accept); + return elementName; +}; +const validatorsFor = (elementName, validators) => { + if (!elementName || !validators) { + return {}; } - if (!valid) { - return options.message; + if (Object.prototype.hasOwnProperty.call(validators, elementName)) { + return validators[elementName]; } + return validators[cleanElementName(elementName, validators)] || {}; }; - -const isMatching = (value, regExpOptions) => { - return new RegExp(regExpOptions.source, regExpOptions.options).test(value); -}; -const hasValidFormat = (value, withOptions, withoutOptions) => { - return withOptions && isMatching(value, withOptions) || withoutOptions && !isMatching(value, withoutOptions); +const getValidationInputs = form => { + return Array.from(form.elements).filter(element => { + if (element.dataset.csvValidate == null || element.disabled) { + return false; + } + return isVisible(element); + }); }; -const formatLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - if (!hasValidFormat(value, options.with, options.without)) { - return options.message; - } +const passElement = element => { + dispatchCustomEvent(element, 'element:validate:pass'); + delete element.dataset.csvValid; }; - -const VALIDATIONS$1 = { - even: a => { - return parseInt(a, 10) % 2 === 0; - }, - greater_than: (a, b) => { - return parseFloat(a) > parseFloat(b); - }, - greater_than_or_equal_to: (a, b) => { - return parseFloat(a) >= parseFloat(b); - }, - equal_to: (a, b) => { - return parseFloat(a) === parseFloat(b); - }, - less_than: (a, b) => { - return parseFloat(a) < parseFloat(b); - }, - less_than_or_equal_to: (a, b) => { - return parseFloat(a) <= parseFloat(b); - }, - odd: a => { - return parseInt(a, 10) % 2 === 1; - }, - other_than: (a, b) => { - return parseFloat(a) !== parseFloat(b); - } +const failElement = (element, message) => { + dispatchCustomEvent(element, 'element:validate:fail', message); + element.dataset.csvValid = 'false'; }; -const formatValue = element => { - const value = element.value || ''; - const numberFormat = element.form.ClientSideValidations.settings.number_format; - return value.trim().replace(new RegExp("\\".concat(numberFormat.separator), 'g'), '.'); +const afterValidate = element => { + dispatchCustomEvent(element, 'element:validate:after'); + return element.dataset.csvValid !== 'false'; }; -const getOtherValue = (validationOption, form) => { - if (!isNaN(parseFloat(validationOption))) { - return validationOption; +const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { + for (const validatorOption in validatorOptions) { + if (!validatorOptions[validatorOption]) { + continue; + } + const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]); + if (message) { + failElement(element, message); + return false; + } } - const validationElements = form.querySelectorAll("[name*=\"".concat(validationOption, "\"]")); - if (validationElements.length === 1) { - const validationElement = validationElements[0]; - const otherFormattedValue = formatValue(validationElement); - if (!isNaN(parseFloat(otherFormattedValue))) { - return otherFormattedValue; + return true; +}; +const executeValidators = (validatorFunctions, element, validators) => { + for (const validator in validators) { + if (!validatorFunctions[validator]) { + continue; + } + if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { + return false; } } + return true; }; -const isValid = (validationFunction, validationOption, formattedValue, form) => { - if (validationFunction.length === 2) { - const otherValue = getOtherValue(validationOption, form); - return otherValue == null || otherValue === '' || validationFunction(formattedValue, otherValue); +const isMarkedForDestroy = element => { + const elementName = element.name; + const form = element.form; + if (form && /\[([^\]]*?)\]$/.test(elementName)) { + const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]'); + const destroyInputElement = form.querySelector("input[name=\"".concat(destroyInputName, "\"]")); + if (destroyInputElement && destroyInputElement.value === '1') { + return true; + } + } + return false; +}; +const executeAllValidators = (element, validators) => { + if (element.dataset.csvChanged === 'false' || element.disabled) { + return; + } + element.dataset.csvChanged = 'false'; + if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { + passElement(element); + } +}; +const validateElement = (element, validators) => { + dispatchCustomEvent(element, 'element:validate:before'); + if (isMarkedForDestroy(element)) { + passElement(element); } else { - return validationFunction(formattedValue); + executeAllValidators(element, validators); } + return afterValidate(element); }; -const runFunctionValidations = (formattedValue, form, options) => { - for (const validation in VALIDATIONS$1) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS$1[validation]; - - // Must check for null because this could be 0 - if (validationOption == null) { - continue; +const validateForm = (form, validators) => { + let valid = true; + dispatchCustomEvent(form, 'form:validate:before'); + getValidationInputs(form).forEach(element => { + if (!validateElement(element, validatorsFor(element.name, validators))) { + valid = false; } - if (!isValid(validationFunction, validationOption, formattedValue, form)) { - return options.messages[validation]; + }); + if (valid) { + dispatchCustomEvent(form, 'form:validate:pass'); + } else { + dispatchCustomEvent(form, 'form:validate:fail'); + } + dispatchCustomEvent(form, 'form:validate:after'); + return valid; +}; +const isValid$1 = (target, validators) => { + const element = getDOMElements(target)[0]; + if (!element) { + return true; + } + if (!validators) { + var _form$ClientSideValid; + const form = isFormElement(element) ? element : element.form; + validators = form === null || form === void 0 || (_form$ClientSideValid = form.ClientSideValidations) === null || _form$ClientSideValid === void 0 || (_form$ClientSideValid = _form$ClientSideValid.settings) === null || _form$ClientSideValid === void 0 ? void 0 : _form$ClientSideValid.validators; + } + if (isFormElement(element)) { + return validateForm(element, validators || {}); + } + return validateElement(element, validatorsFor(element.name, validators || {})); +}; + +const buildErrorHelpers = settings => ({ + addError: (element, message) => ClientSideValidations.formBuilders[settings.html_settings.type].add(element, settings.html_settings, message), + removeError: element => ClientSideValidations.formBuilders[settings.html_settings.type].remove(element, settings.html_settings) +}); +const installFormContext = (form, settings) => { + const { + addError, + removeError + } = buildErrorHelpers(settings); + form.ClientSideValidations = { + settings, + addError, + removeError + }; +}; +const removeFormContext = form => { + delete form.ClientSideValidations; +}; +const bindFormEvents = form => { + bindElementEvents(form, { + submit: event => { + if (!isValid$1(form, form.ClientSideValidations.settings.validators)) { + event.preventDefault(); + event.stopImmediatePropagation(); + } + }, + 'form:validate:after': event => ClientSideValidations.callbacks.form.after(form, event), + 'form:validate:before': event => ClientSideValidations.callbacks.form.before(form, event), + 'form:validate:fail': event => ClientSideValidations.callbacks.form.fail(form, event), + 'form:validate:pass': event => ClientSideValidations.callbacks.form.pass(form, event) + }); +}; +const bindInputEvents = (form, input) => { + input.dataset.csvValidate = 'true'; + bindElementEvents(input, { + focusout: function () { + isValid$1(this, form.ClientSideValidations.settings.validators); + }, + change: function () { + this.dataset.csvChanged = 'true'; + }, + 'element:validate:after': function (event) { + ClientSideValidations.callbacks.element.after(this, event); + }, + 'element:validate:before': function (event) { + ClientSideValidations.callbacks.element.before(this, event); + }, + 'element:validate:fail': function (event) { + const element = this; + const message = event.detail; + ClientSideValidations.callbacks.element.fail(element, message, () => { + form.ClientSideValidations.addError(element, message); + }, event); + }, + 'element:validate:pass': function (event) { + const element = this; + ClientSideValidations.callbacks.element.pass(element, () => { + form.ClientSideValidations.removeError(element); + }, event); } + }); + if (input.type === 'checkbox') { + bindElementEvents(input, { + change: function () { + isValid$1(this, form.ClientSideValidations.settings.validators); + } + }); } }; -const runValidations$1 = (formattedValue, form, options) => { - if (options.only_integer && !ClientSideValidations.patterns.numericality.only_integer.test(formattedValue)) { - return options.messages.only_integer; +const bindConfirmationEvents = (target, confirmation, form) => { + bindElementEvents(confirmation, { + focusout: () => { + target.dataset.csvChanged = 'true'; + isValid$1(target, form.ClientSideValidations.settings.validators); + }, + keyup: () => { + target.dataset.csvChanged = 'true'; + isValid$1(target, form.ClientSideValidations.settings.validators); + } + }); +}; +const unbindElement = element => { + clearBoundEventListeners(element); + delete element.dataset.csvValid; + delete element.dataset.csvChanged; + delete element.dataset.csvValidate; +}; + +var _Class_brand = /*#__PURE__*/new WeakSet(); +class _Class extends Controller { + constructor() { + super(...arguments); + _classPrivateMethodInitSpec(this, _Class_brand); + } + connect() { + this.element.noValidate = true; + installFormContext(this.element, this.settingsValue); + bindFormEvents(this.element); + } + disconnect() { + unbindElement(this.element); + this.inputTargets.forEach(input => { + unbindElement(input); + }); + this.confirmationTargets.forEach(confirmation => { + unbindElement(confirmation); + }); + removeFormContext(this.element); } - if (!ClientSideValidations.patterns.numericality.default.test(formattedValue)) { - return options.messages.numericality; + inputTargetConnected(input) { + if (input.type === 'radio' || !input.form || input.form !== this.element) { + return; + } + bindInputEvents(this.element, input); + } + inputTargetDisconnected(input) { + unbindElement(input); + } + confirmationTargetConnected(confirmation) { + if (!confirmation.form || confirmation.form !== this.element) { + return; + } + const partner = _assertClassBrand(_Class_brand, this, _findConfirmationPartner).call(this, confirmation); + if (partner) { + bindConfirmationEvents(partner, confirmation, this.element); + } + } + confirmationTargetDisconnected(confirmation) { + unbindElement(confirmation); + } + validate() { + return isValid$1(this.element, this.settingsValue.validators); + } + validateElement(element) { + return isValid$1(element, this.settingsValue.validators); + } +} +function _findConfirmationPartner(confirmation) { + const partnerId = confirmation.dataset.clientSideValidationsConfirms; + if (partnerId) { + const partner = document.getElementById(partnerId); + if (partner && partner.form === this.element) return partner; + } + if (confirmation.id && confirmation.id.endsWith('_confirmation')) { + const fallback = document.getElementById(confirmation.id.replace(/_confirmation$/, '')); + if (fallback && fallback.form === this.element) return fallback; + } + return null; +} +_defineProperty(_Class, "targets", ['input', 'confirmation']); +_defineProperty(_Class, "values", { + settings: Object +}); + +const absenceLocalValidator = (element, options) => { + if (isValuePresent(element.value)) { + return options.message; } - return runFunctionValidations(formattedValue, form, options); }; -const numericalityLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; +const presenceLocalValidator = (element, options) => { + if (!isValuePresent(element.value)) { + return options.message; } - const form = element.form; - const formattedValue = formatValue(element); - return runValidations$1(formattedValue, form, options); }; -const VALIDATIONS = { - is: (a, b) => { - return a === parseInt(b, 10); - }, - minimum: (a, b) => { - return a >= parseInt(b, 10); - }, - maximum: (a, b) => { - return a <= parseInt(b, 10); +const DEFAULT_ACCEPT_OPTION = ['1', true]; +const isTextAccepted = (value, acceptOption) => { + if (!acceptOption) { + acceptOption = DEFAULT_ACCEPT_OPTION; } + if (Array.isArray(acceptOption)) { + return arrayHasValue(value, acceptOption); + } + return value === acceptOption; }; -const runValidations = (valueLength, options) => { - for (const validation in VALIDATIONS) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS[validation]; - if (validationOption && !validationFunction(valueLength, validationOption)) { - return options.messages[validation]; - } +const acceptanceLocalValidator = (element, options) => { + let valid = true; + if (element.type === 'checkbox') { + valid = element.checked; + } + if (element.type === 'text') { + valid = isTextAccepted(element.value, options.accept); + } + if (!valid) { + return options.message; } }; -const lengthLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; + +const confirmationLocalValidator = (element, options) => { + let value = element.value; + let confirmationValue = document.getElementById("".concat(element.id, "_confirmation")).value; + if (!options.case_sensitive) { + value = value.toLowerCase(); + confirmationValue = confirmationValue.toLowerCase(); + } + if (value !== confirmationValue) { + return options.message; } - return runValidations(value.length, options); }; const isInList = (value, otherValues) => { @@ -555,18 +570,135 @@ const inclusionLocalValidator = (element, options) => { } }; -const confirmationLocalValidator = (element, options) => { - let value = element.value; - let confirmationValue = document.getElementById("".concat(element.id, "_confirmation")).value; - if (!options.case_sensitive) { - value = value.toLowerCase(); - confirmationValue = confirmationValue.toLowerCase(); +const isMatching = (value, regExpOptions) => { + return new RegExp(regExpOptions.source, regExpOptions.options).test(value); +}; +const hasValidFormat = (value, withOptions, withoutOptions) => { + return withOptions && isMatching(value, withOptions) || withoutOptions && !isMatching(value, withoutOptions); +}; +const formatLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; } - if (value !== confirmationValue) { + if (!hasValidFormat(value, options.with, options.without)) { return options.message; } }; +const VALIDATIONS$1 = { + is: (a, b) => { + return a === parseInt(b, 10); + }, + minimum: (a, b) => { + return a >= parseInt(b, 10); + }, + maximum: (a, b) => { + return a <= parseInt(b, 10); + } +}; +const runValidations$1 = (valueLength, options) => { + for (const validation in VALIDATIONS$1) { + const validationOption = options[validation]; + const validationFunction = VALIDATIONS$1[validation]; + if (validationOption && !validationFunction(valueLength, validationOption)) { + return options.messages[validation]; + } + } +}; +const lengthLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; + } + return runValidations$1(value.length, options); +}; + +const VALIDATIONS = { + even: a => { + return parseInt(a, 10) % 2 === 0; + }, + greater_than: (a, b) => { + return parseFloat(a) > parseFloat(b); + }, + greater_than_or_equal_to: (a, b) => { + return parseFloat(a) >= parseFloat(b); + }, + equal_to: (a, b) => { + return parseFloat(a) === parseFloat(b); + }, + less_than: (a, b) => { + return parseFloat(a) < parseFloat(b); + }, + less_than_or_equal_to: (a, b) => { + return parseFloat(a) <= parseFloat(b); + }, + odd: a => { + return parseInt(a, 10) % 2 === 1; + }, + other_than: (a, b) => { + return parseFloat(a) !== parseFloat(b); + } +}; +const formatValue = element => { + const value = element.value || ''; + const numberFormat = element.form.ClientSideValidations.settings.number_format; + return value.trim().replace(new RegExp("\\".concat(numberFormat.separator), 'g'), '.'); +}; +const getOtherValue = (validationOption, form) => { + if (!isNaN(parseFloat(validationOption))) { + return validationOption; + } + const validationElements = form.querySelectorAll("[name*=\"".concat(validationOption, "\"]")); + if (validationElements.length === 1) { + const validationElement = validationElements[0]; + const otherFormattedValue = formatValue(validationElement); + if (!isNaN(parseFloat(otherFormattedValue))) { + return otherFormattedValue; + } + } +}; +const isValid = (validationFunction, validationOption, formattedValue, form) => { + if (validationFunction.length === 2) { + const otherValue = getOtherValue(validationOption, form); + return otherValue == null || otherValue === '' || validationFunction(formattedValue, otherValue); + } else { + return validationFunction(formattedValue); + } +}; +const runFunctionValidations = (formattedValue, form, options) => { + for (const validation in VALIDATIONS) { + const validationOption = options[validation]; + const validationFunction = VALIDATIONS[validation]; + + // Must check for null because this could be 0 + if (validationOption == null) { + continue; + } + if (!isValid(validationFunction, validationOption, formattedValue, form)) { + return options.messages[validation]; + } + } +}; +const runValidations = (formattedValue, form, options) => { + if (options.only_integer && !ClientSideValidations.patterns.numericality.only_integer.test(formattedValue)) { + return options.messages.only_integer; + } + if (!ClientSideValidations.patterns.numericality.default.test(formattedValue)) { + return options.messages.numericality; + } + return runFunctionValidations(formattedValue, form, options); +}; +const numericalityLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; + } + const form = element.form; + const formattedValue = formatValue(element); + return runValidations(formattedValue, form, options); +}; + const isLocallyUnique = (element, value, otherValue, caseSensitive) => { if (!caseSensitive) { value = value.toLowerCase(); @@ -604,7 +736,7 @@ const uniquenessLocalValidator = (element, options) => { } }; -// Validators will run in the following order +// Validators run in this order ClientSideValidations.validators.local = { absence: absenceLocalValidator, presence: presenceLocalValidator, @@ -617,165 +749,10 @@ ClientSideValidations.validators.local = { confirmation: confirmationLocalValidator, uniqueness: uniquenessLocalValidator }; -ClientSideValidations.enable = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enablers.form(element); - } else if (isInputElement(element)) { - ClientSideValidations.enablers.input(element); - } - }); - return target; -}; -ClientSideValidations.validate = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enable(element); - } - }); - return target; -}; -ClientSideValidations.isValid = (target, validators) => { - const element = getDOMElements(target)[0]; - if (!element) { - return true; - } - if (!validators) { - var _form$ClientSideValid; - const form = isFormElement(element) ? element : element.form; - validators = form === null || form === void 0 || (_form$ClientSideValid = form.ClientSideValidations) === null || _form$ClientSideValid === void 0 || (_form$ClientSideValid = _form$ClientSideValid.settings) === null || _form$ClientSideValid === void 0 ? void 0 : _form$ClientSideValid.validators; - } - if (isFormElement(element)) { - return validateForm(element, validators || {}); - } - return validateElement(element, validatorsFor(element.name, validators || {})); -}; -const cleanNestedElementName = (elementName, nestedMatches, validators) => { - for (const validatorName in validators) { - if (validatorName.match("\\[".concat(nestedMatches[1], "\\].*\\[\\]\\[").concat(nestedMatches[2], "\\]$"))) { - elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]'); - } - } - return elementName; -}; -const cleanElementName = (elementName, validators) => { - elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); - const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); - if (nestedMatches) { - elementName = cleanNestedElementName(elementName, nestedMatches, validators); - } - return elementName; -}; -const validatorsFor = (elementName, validators) => { - if (!elementName || !validators) { - return {}; - } - if (Object.prototype.hasOwnProperty.call(validators, elementName)) { - return validators[elementName]; - } - return validators[cleanElementName(elementName, validators)] || {}; -}; -const getValidationInputs = form => { - return Array.from(form.elements).filter(element => { - if (element.dataset.csvValidate == null || element.disabled) { - return false; - } - return isVisible(element); - }); +ClientSideValidations.isValid = isValid$1; +const register = function (application) { + let identifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'client-side-validations'; + application.register(identifier, _Class); }; -const validateForm = (form, validators) => { - let valid = true; - dispatchCustomEvent(form, 'form:validate:before'); - getValidationInputs(form).forEach(element => { - if (!validateElement(element, validatorsFor(element.name, validators))) { - valid = false; - } - }); - if (valid) { - dispatchCustomEvent(form, 'form:validate:pass'); - } else { - dispatchCustomEvent(form, 'form:validate:fail'); - } - dispatchCustomEvent(form, 'form:validate:after'); - return valid; -}; -const passElement = element => { - dispatchCustomEvent(element, 'element:validate:pass'); - delete element.dataset.csvValid; -}; -const failElement = (element, message) => { - dispatchCustomEvent(element, 'element:validate:fail', message); - element.dataset.csvValid = 'false'; -}; -const afterValidate = element => { - dispatchCustomEvent(element, 'element:validate:after'); - return element.dataset.csvValid !== 'false'; -}; -const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { - for (const validatorOption in validatorOptions) { - if (!validatorOptions[validatorOption]) { - continue; - } - const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]); - if (message) { - failElement(element, message); - return false; - } - } - return true; -}; -const executeValidators = (validatorFunctions, element, validators) => { - for (const validator in validators) { - if (!validatorFunctions[validator]) { - continue; - } - if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { - return false; - } - } - return true; -}; -const isMarkedForDestroy = element => { - const elementName = element.name; - const form = element.form; - if (form && /\[([^\]]*?)\]$/.test(elementName)) { - const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]'); - const destroyInputElement = form.querySelector("input[name=\"".concat(destroyInputName, "\"]")); - if (destroyInputElement && destroyInputElement.value === '1') { - return true; - } - } - return false; -}; -const executeAllValidators = (element, validators) => { - if (element.dataset.csvChanged === 'false' || element.disabled) { - return; - } - element.dataset.csvChanged = 'false'; - if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { - passElement(element); - } -}; -const validateElement = (element, validators) => { - dispatchCustomEvent(element, 'element:validate:before'); - if (isMarkedForDestroy(element)) { - passElement(element); - } else { - executeAllValidators(element, validators); - } - return afterValidate(element); -}; -if (!window.ClientSideValidations) { - window.ClientSideValidations = ClientSideValidations; - if (!isAMD() && !isCommonJS()) { - ClientSideValidations.start(); - } -} -function isAMD() { - return typeof define === 'function' && define.amd; // eslint-disable-line no-undef -} -function isCommonJS() { - return typeof exports === 'object' && typeof module !== 'undefined'; -} -export { ClientSideValidations as default }; +export { ClientSideValidations, _Class as ClientSideValidationsController, ClientSideValidations as default, isValid$1 as isValid, register, validateElement, validateForm, validatorsFor }; diff --git a/dist/client-side-validations.js b/dist/client-side-validations.js deleted file mode 100644 index 415776074..000000000 --- a/dist/client-side-validations.js +++ /dev/null @@ -1,789 +0,0 @@ -/*! - * Client Side Validations JS - v24.0.0 (https://github.com/DavyJonesLocker/client_side_validations) - * Copyright (c) 2026 Geremia Taglialatela, Brian Cardarella - * Licensed under MIT (https://opensource.org/licenses/mit-license.php) - */ - -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ClientSideValidations = factory()); -})(this, (function () { 'use strict'; - - const boundEventListeners = new WeakMap(); - const addBoundEventListener = (element, eventName, listener) => { - element.addEventListener(eventName, listener); - const listeners = boundEventListeners.get(element) || []; - listeners.push({ - eventName, - listener - }); - boundEventListeners.set(element, listeners); - }; - const bindElementEvents = (element, eventsToBind) => { - for (const eventName in eventsToBind) { - addBoundEventListener(element, eventName, eventsToBind[eventName]); - } - }; - const clearBoundEventListeners = element => { - const listeners = boundEventListeners.get(element); - if (!listeners) { - return; - } - listeners.forEach(_ref => { - let { - eventName, - listener - } = _ref; - element.removeEventListener(eventName, listener); - }); - boundEventListeners.delete(element); - }; - const dispatchCustomEvent = (element, eventName, detail) => { - element.dispatchEvent(new CustomEvent(eventName, { - bubbles: true, - detail - })); - }; - - const arrayHasValue = (value, otherValues) => { - for (let i = 0, l = otherValues.length; i < l; i++) { - if (value === otherValues[i]) { - return true; - } - } - return false; - }; - const createElementFromHTML = html => { - const element = document.createElement('div'); - element.innerHTML = html; - return element.firstChild; - }; - const isDOMCollection = target => { - return Array.isArray(target) || typeof NodeList !== 'undefined' && target instanceof NodeList || typeof HTMLCollection !== 'undefined' && target instanceof HTMLCollection || typeof RadioNodeList !== 'undefined' && target instanceof RadioNodeList; - }; - const isDOMElement = target => { - return target != null && target.nodeType === 1; - }; - const getDOMElements = target => { - if (target == null) { - return []; - } - if (isDOMElement(target)) { - return [target]; - } - if (isDOMCollection(target)) { - return Array.from(target).filter(isDOMElement); - } - return []; - }; - const isFormElement = element => { - return element.tagName === 'FORM'; - }; - const isInputElement = element => { - switch (element.tagName) { - case 'INPUT': - return element.type !== 'submit' && element.type !== 'button'; - case 'SELECT': - case 'TEXTAREA': - return true; - default: - return false; - } - }; - const isVisible = element => { - return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length); - }; - const isValuePresent = value => { - return !/^\s*$/.test(value || ''); - }; - - const isNamedInputElement = element => { - return isInputElement(element) && element.name != null && element.name !== ''; - }; - const getFormControls = form => { - return Array.from(form.elements).filter(isInputElement); - }; - const getFormInputs = form => { - return getFormControls(form).filter(element => { - return isNamedInputElement(element) && !element.disabled && isVisible(element); - }); - }; - const findFormElementByName = (form, name) => { - return getFormControls(form).find(element => element.name === name); - }; - const enableForm = form => { - ClientSideValidations.enablers.form(form); - }; - const enableForms = () => { - document.querySelectorAll(ClientSideValidations.selectors.forms).forEach(enableForm); - }; - const ClientSideValidations = { - callbacks: { - element: { - after: (element, eventData) => {}, - before: (element, eventData) => {}, - fail: (element, message, addError, eventData) => addError(), - pass: (element, removeError, eventData) => removeError() - }, - form: { - after: (form, eventData) => {}, - before: (form, eventData) => {}, - fail: (form, eventData) => {}, - pass: (form, eventData) => {} - } - }, - eventsToBind: { - form: form => ({ - submit: eventData => { - if (!ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators)) { - eventData.preventDefault(); - eventData.stopImmediatePropagation(); - } - }, - 'ajax:beforeSend': function (eventData) { - if (eventData.target === this) { - ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators); - } - }, - 'form:validate:after': eventData => { - ClientSideValidations.callbacks.form.after(form, eventData); - }, - 'form:validate:before': eventData => { - ClientSideValidations.callbacks.form.before(form, eventData); - }, - 'form:validate:fail': eventData => { - ClientSideValidations.callbacks.form.fail(form, eventData); - }, - 'form:validate:pass': eventData => { - ClientSideValidations.callbacks.form.pass(form, eventData); - } - }), - input: form => ({ - focusout: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - }, - change: function () { - this.dataset.csvChanged = 'true'; - }, - 'element:validate:after': function (eventData) { - ClientSideValidations.callbacks.element.after(this, eventData); - }, - 'element:validate:before': function (eventData) { - ClientSideValidations.callbacks.element.before(this, eventData); - }, - 'element:validate:fail': function (eventData) { - const element = this; - const message = eventData.detail; - ClientSideValidations.callbacks.element.fail(element, message, function () { - form.ClientSideValidations.addError(element, message); - }, eventData); - }, - 'element:validate:pass': function (eventData) { - const element = this; - ClientSideValidations.callbacks.element.pass(element, function () { - form.ClientSideValidations.removeError(element); - }, eventData); - } - }), - inputConfirmation: (elementToConfirm, form) => ({ - focusout: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - }, - keyup: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - } - }) - }, - enablers: { - form: form => { - clearBoundEventListeners(form); - getFormControls(form).forEach(clearBoundEventListeners); - form.ClientSideValidations = { - settings: JSON.parse(form.dataset.clientSideValidations), - addError: (element, message) => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].add(element, form.ClientSideValidations.settings.html_settings, message), - removeError: element => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].remove(element, form.ClientSideValidations.settings.html_settings) - }; - bindElementEvents(form, ClientSideValidations.eventsToBind.form(form)); - getFormInputs(form).forEach(element => { - ClientSideValidations.enablers.input(element); - }); - }, - input: function (input) { - const form = input.form; - if (!form) { - return; - } - clearBoundEventListeners(input); - const eventsToBind = ClientSideValidations.eventsToBind.input(form); - if (input.type !== 'radio' && !(input.id && input.id.endsWith('_confirmation'))) { - input.dataset.csvValidate = 'true'; - bindElementEvents(input, eventsToBind); - } - if (input.type === 'checkbox') { - bindElementEvents(input, { - change: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - } - }); - } - if (input.id && input.id.endsWith('_confirmation')) { - const elementToConfirm = document.getElementById(input.id.match(/(.+)_confirmation/)[1]); - if (elementToConfirm && elementToConfirm.form === form) { - bindElementEvents(input, ClientSideValidations.eventsToBind.inputConfirmation(elementToConfirm, form)); - } - } - } - }, - formBuilders: { - 'ActionView::Helpers::FormBuilder': { - add: (element, settings, message) => { - if (!element) { - return; - } - const form = element.form; - const inputErrorTemplate = createElementFromHTML(settings.input_tag); - let inputErrorElement = element.closest(".".concat(inputErrorTemplate.getAttribute('class').replace(/ /g, '.'))); - if (!inputErrorElement) { - inputErrorElement = inputErrorTemplate; - if (element.getAttribute('autofocus')) { - element.setAttribute('autofocus', false); - } - element.before(inputErrorElement); - inputErrorElement.querySelector('span#input_tag').replaceWith(element); - const inputErrorLabelMessageElement = inputErrorElement.querySelector('label.message'); - if (inputErrorLabelMessageElement) { - inputErrorLabelMessageElement.setAttribute('for', element.id); - } - } - const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); - if (labelElement) { - const labelErrorTemplate = createElementFromHTML(settings.label_tag); - const labelErrorContainer = labelElement.closest(".".concat(labelErrorTemplate.getAttribute('class').replace(/ /g, '.'))); - if (!labelErrorContainer) { - labelElement.after(labelErrorTemplate); - labelErrorTemplate.querySelector('label#label_tag').replaceWith(labelElement); - } - } - const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); - if (labelMessageElement) { - labelMessageElement.textContent = message; - } - }, - remove: (element, settings) => { - if (!element) { - return; - } - const form = element.form; - const inputErrorClass = createElementFromHTML(settings.input_tag).getAttribute('class'); - const inputErrorElement = element.closest(".".concat(inputErrorClass.replace(/ /g, '.'))); - if (inputErrorElement) { - inputErrorElement.querySelector("#".concat(element.id)).remove(); - inputErrorElement.replaceWith(element); - } - const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); - if (labelElement) { - const labelErrorClass = createElementFromHTML(settings.label_tag).getAttribute('class'); - const labelErrorElement = labelElement.closest(".".concat(labelErrorClass.replace(/ /g, '.'))); - if (labelErrorElement) { - labelErrorElement.replaceWith(labelElement); - } - } - const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); - if (labelMessageElement) { - labelMessageElement.remove(); - } - } - } - }, - patterns: { - numericality: { - default: /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/, - only_integer: /^[+-]?\d+$/ - } - }, - selectors: { - inputs: 'input:not([type="submit"]):not([type="button"])[name], select[name], textarea[name]', - validate_inputs: 'input[data-csv-validate]:not([type="submit"]):not([type="button"]), select[data-csv-validate], textarea[data-csv-validate]', - forms: 'form[data-client-side-validations]' - }, - validators: { - all: () => { - return { - ...ClientSideValidations.validators.local, - ...ClientSideValidations.validators.remote - }; - }, - local: {}, - remote: {} - }, - disable: target => { - getDOMElements(target).forEach(element => { - clearBoundEventListeners(element); - if (isFormElement(element)) { - getFormControls(element).forEach(input => { - clearBoundEventListeners(input); - delete input.dataset.csvValid; - delete input.dataset.csvChanged; - delete input.dataset.csvValidate; - }); - return; - } - delete element.dataset.csvValid; - delete element.dataset.csvChanged; - if (isInputElement(element)) { - delete element.dataset.csvValidate; - } - }); - }, - reset: form => { - ClientSideValidations.disable(form); - for (const key in form.ClientSideValidations.settings.validators) { - const element = findFormElementByName(form, key); - if (element) { - form.ClientSideValidations.removeError(element); - } - } - ClientSideValidations.enablers.form(form); - }, - initializeOnEvent: () => { - if (window.Turbo != null) { - return 'turbo:load'; - } else if (window.Turbolinks != null && window.Turbolinks.supported) { - return window.Turbolinks.EVENTS != null ? 'page:change' : 'turbolinks:load'; - } - }, - start: () => { - const initializeOnEvent = ClientSideValidations.initializeOnEvent(); - if (initializeOnEvent != null) { - document.addEventListener(initializeOnEvent, enableForms); - } else if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', enableForms, { - once: true - }); - } else { - enableForms(); - } - } - }; - - const absenceLocalValidator = (element, options) => { - if (isValuePresent(element.value)) { - return options.message; - } - }; - const presenceLocalValidator = (element, options) => { - if (!isValuePresent(element.value)) { - return options.message; - } - }; - - const DEFAULT_ACCEPT_OPTION = ['1', true]; - const isTextAccepted = (value, acceptOption) => { - if (!acceptOption) { - acceptOption = DEFAULT_ACCEPT_OPTION; - } - if (Array.isArray(acceptOption)) { - return arrayHasValue(value, acceptOption); - } - return value === acceptOption; - }; - const acceptanceLocalValidator = (element, options) => { - let valid = true; - if (element.type === 'checkbox') { - valid = element.checked; - } - if (element.type === 'text') { - valid = isTextAccepted(element.value, options.accept); - } - if (!valid) { - return options.message; - } - }; - - const isMatching = (value, regExpOptions) => { - return new RegExp(regExpOptions.source, regExpOptions.options).test(value); - }; - const hasValidFormat = (value, withOptions, withoutOptions) => { - return withOptions && isMatching(value, withOptions) || withoutOptions && !isMatching(value, withoutOptions); - }; - const formatLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - if (!hasValidFormat(value, options.with, options.without)) { - return options.message; - } - }; - - const VALIDATIONS$1 = { - even: a => { - return parseInt(a, 10) % 2 === 0; - }, - greater_than: (a, b) => { - return parseFloat(a) > parseFloat(b); - }, - greater_than_or_equal_to: (a, b) => { - return parseFloat(a) >= parseFloat(b); - }, - equal_to: (a, b) => { - return parseFloat(a) === parseFloat(b); - }, - less_than: (a, b) => { - return parseFloat(a) < parseFloat(b); - }, - less_than_or_equal_to: (a, b) => { - return parseFloat(a) <= parseFloat(b); - }, - odd: a => { - return parseInt(a, 10) % 2 === 1; - }, - other_than: (a, b) => { - return parseFloat(a) !== parseFloat(b); - } - }; - const formatValue = element => { - const value = element.value || ''; - const numberFormat = element.form.ClientSideValidations.settings.number_format; - return value.trim().replace(new RegExp("\\".concat(numberFormat.separator), 'g'), '.'); - }; - const getOtherValue = (validationOption, form) => { - if (!isNaN(parseFloat(validationOption))) { - return validationOption; - } - const validationElements = form.querySelectorAll("[name*=\"".concat(validationOption, "\"]")); - if (validationElements.length === 1) { - const validationElement = validationElements[0]; - const otherFormattedValue = formatValue(validationElement); - if (!isNaN(parseFloat(otherFormattedValue))) { - return otherFormattedValue; - } - } - }; - const isValid = (validationFunction, validationOption, formattedValue, form) => { - if (validationFunction.length === 2) { - const otherValue = getOtherValue(validationOption, form); - return otherValue == null || otherValue === '' || validationFunction(formattedValue, otherValue); - } else { - return validationFunction(formattedValue); - } - }; - const runFunctionValidations = (formattedValue, form, options) => { - for (const validation in VALIDATIONS$1) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS$1[validation]; - - // Must check for null because this could be 0 - if (validationOption == null) { - continue; - } - if (!isValid(validationFunction, validationOption, formattedValue, form)) { - return options.messages[validation]; - } - } - }; - const runValidations$1 = (formattedValue, form, options) => { - if (options.only_integer && !ClientSideValidations.patterns.numericality.only_integer.test(formattedValue)) { - return options.messages.only_integer; - } - if (!ClientSideValidations.patterns.numericality.default.test(formattedValue)) { - return options.messages.numericality; - } - return runFunctionValidations(formattedValue, form, options); - }; - const numericalityLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - const form = element.form; - const formattedValue = formatValue(element); - return runValidations$1(formattedValue, form, options); - }; - - const VALIDATIONS = { - is: (a, b) => { - return a === parseInt(b, 10); - }, - minimum: (a, b) => { - return a >= parseInt(b, 10); - }, - maximum: (a, b) => { - return a <= parseInt(b, 10); - } - }; - const runValidations = (valueLength, options) => { - for (const validation in VALIDATIONS) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS[validation]; - if (validationOption && !validationFunction(valueLength, validationOption)) { - return options.messages[validation]; - } - } - }; - const lengthLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - return runValidations(value.length, options); - }; - - const isInList = (value, otherValues) => { - const normalizedOtherValues = []; - for (const otherValueIndex in otherValues) { - normalizedOtherValues.push(otherValues[otherValueIndex].toString()); - } - return arrayHasValue(value, normalizedOtherValues); - }; - const isInRange = (value, range) => { - return value >= range[0] && value <= range[1]; - }; - const isIncluded = (value, options, allowBlank) => { - if ((options.allow_blank && !isValuePresent(value)) === allowBlank) { - return true; - } - return options.in && isInList(value, options.in) || options.range && isInRange(value, options.range); - }; - const exclusionLocalValidator = (element, options) => { - const value = element.value; - if (isIncluded(value, options, false) || !options.allow_blank && !isValuePresent(value)) { - return options.message; - } - }; - const inclusionLocalValidator = (element, options) => { - const value = element.value; - if (!isIncluded(value, options, true)) { - return options.message; - } - }; - - const confirmationLocalValidator = (element, options) => { - let value = element.value; - let confirmationValue = document.getElementById("".concat(element.id, "_confirmation")).value; - if (!options.case_sensitive) { - value = value.toLowerCase(); - confirmationValue = confirmationValue.toLowerCase(); - } - if (value !== confirmationValue) { - return options.message; - } - }; - - const isLocallyUnique = (element, value, otherValue, caseSensitive) => { - if (!caseSensitive) { - value = value.toLowerCase(); - otherValue = otherValue.toLowerCase(); - } - if (otherValue === value) { - element.dataset.csvNotLocallyUnique = 'true'; - return false; - } - if (element.dataset.csvNotLocallyUnique) { - delete element.dataset.csvNotLocallyUnique; - element.dataset.csvChanged = 'true'; - } - return true; - }; - const uniquenessLocalValidator = (element, options) => { - const elementName = element.name; - const matches = elementName.match(/^(.+_attributes\])\[\d+\](.+)$/); - if (!matches) { - return; - } - const form = element.form; - const value = element.value; - let valid = true; - const query = "[name^=\"".concat(matches[1], "\"][name$=\"").concat(matches[2], "\"]:not([name=\"").concat(elementName, "\"])"); - const otherElements = form.querySelectorAll(query); - Array.prototype.slice.call(otherElements).forEach(function (otherElement) { - const otherValue = otherElement.value; - if (!isLocallyUnique(otherElement, value, otherValue, options.case_sensitive)) { - valid = false; - } - }); - if (!valid) { - return options.message; - } - }; - - // Validators will run in the following order - ClientSideValidations.validators.local = { - absence: absenceLocalValidator, - presence: presenceLocalValidator, - acceptance: acceptanceLocalValidator, - format: formatLocalValidator, - numericality: numericalityLocalValidator, - length: lengthLocalValidator, - inclusion: inclusionLocalValidator, - exclusion: exclusionLocalValidator, - confirmation: confirmationLocalValidator, - uniqueness: uniquenessLocalValidator - }; - ClientSideValidations.enable = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enablers.form(element); - } else if (isInputElement(element)) { - ClientSideValidations.enablers.input(element); - } - }); - return target; - }; - ClientSideValidations.validate = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enable(element); - } - }); - return target; - }; - ClientSideValidations.isValid = (target, validators) => { - const element = getDOMElements(target)[0]; - if (!element) { - return true; - } - if (!validators) { - var _form$ClientSideValid; - const form = isFormElement(element) ? element : element.form; - validators = form === null || form === void 0 || (_form$ClientSideValid = form.ClientSideValidations) === null || _form$ClientSideValid === void 0 || (_form$ClientSideValid = _form$ClientSideValid.settings) === null || _form$ClientSideValid === void 0 ? void 0 : _form$ClientSideValid.validators; - } - if (isFormElement(element)) { - return validateForm(element, validators || {}); - } - return validateElement(element, validatorsFor(element.name, validators || {})); - }; - const cleanNestedElementName = (elementName, nestedMatches, validators) => { - for (const validatorName in validators) { - if (validatorName.match("\\[".concat(nestedMatches[1], "\\].*\\[\\]\\[").concat(nestedMatches[2], "\\]$"))) { - elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]'); - } - } - return elementName; - }; - const cleanElementName = (elementName, validators) => { - elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); - const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); - if (nestedMatches) { - elementName = cleanNestedElementName(elementName, nestedMatches, validators); - } - return elementName; - }; - const validatorsFor = (elementName, validators) => { - if (!elementName || !validators) { - return {}; - } - if (Object.prototype.hasOwnProperty.call(validators, elementName)) { - return validators[elementName]; - } - return validators[cleanElementName(elementName, validators)] || {}; - }; - const getValidationInputs = form => { - return Array.from(form.elements).filter(element => { - if (element.dataset.csvValidate == null || element.disabled) { - return false; - } - return isVisible(element); - }); - }; - const validateForm = (form, validators) => { - let valid = true; - dispatchCustomEvent(form, 'form:validate:before'); - getValidationInputs(form).forEach(element => { - if (!validateElement(element, validatorsFor(element.name, validators))) { - valid = false; - } - }); - if (valid) { - dispatchCustomEvent(form, 'form:validate:pass'); - } else { - dispatchCustomEvent(form, 'form:validate:fail'); - } - dispatchCustomEvent(form, 'form:validate:after'); - return valid; - }; - const passElement = element => { - dispatchCustomEvent(element, 'element:validate:pass'); - delete element.dataset.csvValid; - }; - const failElement = (element, message) => { - dispatchCustomEvent(element, 'element:validate:fail', message); - element.dataset.csvValid = 'false'; - }; - const afterValidate = element => { - dispatchCustomEvent(element, 'element:validate:after'); - return element.dataset.csvValid !== 'false'; - }; - const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { - for (const validatorOption in validatorOptions) { - if (!validatorOptions[validatorOption]) { - continue; - } - const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]); - if (message) { - failElement(element, message); - return false; - } - } - return true; - }; - const executeValidators = (validatorFunctions, element, validators) => { - for (const validator in validators) { - if (!validatorFunctions[validator]) { - continue; - } - if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { - return false; - } - } - return true; - }; - const isMarkedForDestroy = element => { - const elementName = element.name; - const form = element.form; - if (form && /\[([^\]]*?)\]$/.test(elementName)) { - const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]'); - const destroyInputElement = form.querySelector("input[name=\"".concat(destroyInputName, "\"]")); - if (destroyInputElement && destroyInputElement.value === '1') { - return true; - } - } - return false; - }; - const executeAllValidators = (element, validators) => { - if (element.dataset.csvChanged === 'false' || element.disabled) { - return; - } - element.dataset.csvChanged = 'false'; - if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { - passElement(element); - } - }; - const validateElement = (element, validators) => { - dispatchCustomEvent(element, 'element:validate:before'); - if (isMarkedForDestroy(element)) { - passElement(element); - } else { - executeAllValidators(element, validators); - } - return afterValidate(element); - }; - if (!window.ClientSideValidations) { - window.ClientSideValidations = ClientSideValidations; - if (!isAMD() && !isCommonJS()) { - ClientSideValidations.start(); - } - } - function isAMD() { - return typeof define === 'function' && define.amd; // eslint-disable-line no-undef - } - function isCommonJS() { - return typeof exports === 'object' && typeof module !== 'undefined'; - } - - return ClientSideValidations; - -})); diff --git a/lib/client_side_validations.rb b/lib/client_side_validations.rb index 6ac6ead22..fbf3e6155 100644 --- a/lib/client_side_validations.rb +++ b/lib/client_side_validations.rb @@ -5,7 +5,4 @@ require_relative 'client_side_validations/active_record' if defined?(ActiveRecord) require_relative 'client_side_validations/action_view' if defined?(ActionView) -if defined?(Rails) - require_relative 'client_side_validations/engine' - require_relative 'client_side_validations/generators' -end +require_relative 'client_side_validations/engine' if defined?(Rails) diff --git a/lib/client_side_validations/action_view/form_builder.rb b/lib/client_side_validations/action_view/form_builder.rb index 0bd2ed1be..830e24f6e 100644 --- a/lib/client_side_validations/action_view/form_builder.rb +++ b/lib/client_side_validations/action_view/form_builder.rb @@ -57,7 +57,7 @@ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') %i[collection_check_boxes collection_radio_buttons].each do |method_name| define_method method_name do |method, collection, value_method, text_method, options = {}, html_options = {}, &block| - build_validation_options(method, html_options.merge(name: options[:name])) + build_validation_options(method, html_options.merge(name: options[:name]), html_options) html_options.delete(:validate) super(method, collection, value_method, text_method, options, html_options, &block) end @@ -68,7 +68,7 @@ def check_box(method, options = {}, checked_value = '1', unchecked_value = '0') end def collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) - build_validation_options(method, html_options.merge(name: options[:name])) + build_validation_options(method, html_options.merge(name: options[:name]), html_options) html_options.delete(:validate) super end @@ -95,7 +95,7 @@ def file_field(method, options = {}) end def grouped_collection_select(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) - build_validation_options(method, html_options.merge(name: options[:name])) + build_validation_options(method, html_options.merge(name: options[:name]), html_options) html_options.delete(:validate) super end @@ -107,13 +107,13 @@ def radio_button(method, tag_value, options = {}) end def select(method, choices = nil, options = {}, html_options = {}, &) - build_validation_options(method, html_options.merge(name: options[:name])) + build_validation_options(method, html_options.merge(name: options[:name]), html_options) html_options.delete(:validate) super end def time_zone_select(method, priority_zones = nil, options = {}, html_options = {}) - build_validation_options(method, html_options.merge(name: options[:name])) + build_validation_options(method, html_options.merge(name: options[:name]), html_options) html_options.delete(:validate) super end @@ -126,7 +126,7 @@ def error_field(form_helper, tag, id) &form_helper.class.field_error_proc end - def build_validation_options(method, options = {}) + def build_validation_options(method, options = {}, target_options = options) return unless @options[:validate] index = @default_options[:index].present? ? "[#{@default_options[:index]}]" : '' @@ -137,6 +137,55 @@ def build_validation_options(method, options = {}) name << '[]' if options[:multiple] @options[:validators][@object][method] = { name: name, options: options[:validate] } + + inject_stimulus_target(method, target_options) + end + + def inject_stimulus_target(method, options) + return unless should_inject_target?(method, options) + + method_name = method.to_s + key_base = ClientSideValidations::Config.stimulus_controller_name.tr('-', '_') + data = options[:data] ||= {} + target_name = method_name.end_with?('_confirmation') ? 'confirmation' : 'input' + + append_target(data, :"#{key_base}_target", target_name) + return unless target_name == 'confirmation' + + data[:"#{key_base}_confirms"] ||= "#{@object_name}_#{method_name.delete_suffix('_confirmation')}" + end + + def should_inject_target?(method, options) + return false if @object.nil? + return false if options[:validate] == false + + method_has_validation?(method, options) + end + + def append_target(data, key, value) + existing = data[key].to_s.split(/\s+/).reject(&:empty?) + data[key] = (existing + [value]).uniq.join(' ') + end + + def method_has_validation?(method, options) + return true if options[:validate].is_a?(Hash) + return false unless @object.respond_to?(:client_side_validation_hash) + return false unless @object.respond_to?(:_validators) + + sym = method.to_sym + @object._validators[sym].present? || association_has_validation?(sym) + end + + def association_has_validation?(sym) + name = sym.to_s + assoc = + if name.end_with?('_id') + name.delete_suffix('_id').to_sym + elsif name.end_with?('_ids') + name.delete_suffix('_ids').pluralize.to_sym + end + + assoc && @object._validators[assoc].present? end end end diff --git a/lib/client_side_validations/action_view/form_helper.rb b/lib/client_side_validations/action_view/form_helper.rb index b84196db2..b5827a2e3 100644 --- a/lib/client_side_validations/action_view/form_helper.rb +++ b/lib/client_side_validations/action_view/form_helper.rb @@ -133,7 +133,11 @@ def apply_csv_html_options!(html_options, options, builder) validators: construct_validators } - html_options['data-client-side-validations'] = csv_options.to_json + controller_name = ClientSideValidations::Config.stimulus_controller_name + + existing_controllers = html_options['data-controller'].to_s.split(/\s+/).reject(&:empty?) + html_options['data-controller'] = (existing_controllers + [controller_name]).uniq.join(' ') + html_options["data-#{controller_name}-settings-value"] = csv_options.to_json end end end diff --git a/lib/client_side_validations/config.rb b/lib/client_side_validations/config.rb index 8f75bdf47..9022f7a88 100644 --- a/lib/client_side_validations/config.rb +++ b/lib/client_side_validations/config.rb @@ -3,11 +3,12 @@ module ClientSideValidations module Config class << self - attr_accessor :disabled_validators, :number_format_with_locale, :root_path + attr_accessor :disabled_validators, :number_format_with_locale, :root_path, :stimulus_controller_name end self.disabled_validators = [] self.number_format_with_locale = false self.root_path = nil + self.stimulus_controller_name = 'client-side-validations' end end diff --git a/lib/client_side_validations/files.rb b/lib/client_side_validations/files.rb deleted file mode 100644 index 4f96f0320..000000000 --- a/lib/client_side_validations/files.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -# This is only used by dependent libraries that need to find the files - -module ClientSideValidations - module Files - Initializer = File.expand_path('../generators/templates/client_side_validations/initializer.rb', __dir__) - Javascript = File.expand_path('../../vendor/assets/javascripts/rails.validations.js', __dir__) - end -end diff --git a/lib/client_side_validations/generators.rb b/lib/client_side_validations/generators.rb deleted file mode 100644 index 76070741f..000000000 --- a/lib/client_side_validations/generators.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -module ClientSideValidations - module Generators - @@assets = [] - - def self.register_assets(klass) - @@assets.concat(klass.assets) - end - - def self.assets - @@assets - end - end -end - -require_relative 'generators/rails_validations' diff --git a/lib/client_side_validations/generators/rails_validations.rb b/lib/client_side_validations/generators/rails_validations.rb deleted file mode 100644 index 302055bab..000000000 --- a/lib/client_side_validations/generators/rails_validations.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -module ClientSideValidations - module Generators - class RailsValidations - def self.assets - [{ - path: File.expand_path('../../../vendor/assets/javascripts', __dir__), - file: 'rails.validations.js' - }] - end - - Generators.register_assets(self) - end - end -end diff --git a/lib/client_side_validations/version.rb b/lib/client_side_validations/version.rb index d56451afd..e052cc11d 100644 --- a/lib/client_side_validations/version.rb +++ b/lib/client_side_validations/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ClientSideValidations - VERSION = '24.0.0' + VERSION = '25.0.0' end diff --git a/lib/generators/client_side_validations/copy_assets_generator.rb b/lib/generators/client_side_validations/copy_assets_generator.rb deleted file mode 100644 index aec6b2ab1..000000000 --- a/lib/generators/client_side_validations/copy_assets_generator.rb +++ /dev/null @@ -1,64 +0,0 @@ -# frozen_string_literal: true - -module ClientSideValidations - module Generators - class CopyAssetsGenerator < Rails::Generators::Base - def copy_javascript_asset - return unless instance_of?(CopyAssetsGenerator) || copy_assets? - - assets.each do |asset| - source_paths << asset[:path] - copy_file asset[:file], "#{asset_directory}/#{asset[:file]}" - end - end - - def self.asset_directory - if sprockets? - "app#{Rails.configuration.assets.prefix}/javascripts" - else - 'public/javascripts' - end - end - - def self.assets - ClientSideValidations::Generators.assets - end - - def self.asset_file_names - assets.map { |asset| asset[:file] }.join(', ') - end - - def self.copy_assets? - !sprockets? && !webpacker? - end - - def self.sprockets? - defined?(Sprockets) - end - - def self.webpacker? - defined?(Webpacker) - end - - def self.installation_message - "Copies #{asset_file_names} to #{asset_directory}" - end - - desc installation_message - - private - - def asset_directory - CopyAssetsGenerator.asset_directory - end - - def assets - CopyAssetsGenerator.assets - end - - def copy_assets? - self.class.copy_assets? - end - end - end -end diff --git a/lib/generators/client_side_validations/install_generator.rb b/lib/generators/client_side_validations/install_generator.rb index 286d4d27f..f8d9855ee 100644 --- a/lib/generators/client_side_validations/install_generator.rb +++ b/lib/generators/client_side_validations/install_generator.rb @@ -1,20 +1,22 @@ # frozen_string_literal: true -require_relative 'copy_assets_generator' - module ClientSideValidations module Generators - class InstallGenerator < CopyAssetsGenerator + class InstallGenerator < Rails::Generators::Base + TEMPLATE_ROOT = File.expand_path('../templates/client_side_validations', __dir__) + + source_paths << TEMPLATE_ROOT + + desc 'Copies the initializer and Stimulus controller into the application' + def copy_initializer - source_paths << File.expand_path('../templates/client_side_validations', __dir__) copy_file 'initializer.rb', 'config/initializers/client_side_validations.rb' end - def self.installation_message - "Copies initializer into config/initializers and #{super.downcase}" + def copy_stimulus_controller + copy_file 'client_side_validations_controller.js', + 'app/javascript/controllers/client_side_validations_controller.js' end - - desc installation_message end end end diff --git a/lib/generators/templates/client_side_validations/client_side_validations_controller.js b/lib/generators/templates/client_side_validations/client_side_validations_controller.js new file mode 100644 index 000000000..2a1cea544 --- /dev/null +++ b/lib/generators/templates/client_side_validations/client_side_validations_controller.js @@ -0,0 +1,15 @@ +// Thin Stimulus controller registration for client_side_validations. +// +// If your project uses `eagerLoadControllersFrom('controllers', application)` +// (the default scaffolded by stimulus-rails), importing the controller from +// this file is enough to register it with the identifier inferred from the +// filename (`client-side-validations`). Otherwise register it manually: +// +// import { Application } from '@hotwired/stimulus' +// import { ClientSideValidationsController } from '@client-side-validations/client-side-validations' +// const application = Application.start() +// application.register('client-side-validations', ClientSideValidationsController) + +import { ClientSideValidationsController } from '@client-side-validations/client-side-validations' + +export default ClientSideValidationsController diff --git a/package.json b/package.json index 4684fa735..1ce4e86bc 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "devDependencies": { "@babel/core": "^7.29.0", "@babel/preset-env": "^7.29.2", + "@hotwired/stimulus": "^3.2.2", "@rollup/plugin-babel": "^6.1.0", "@rollup/plugin-node-resolve": "^16.0.3", "chrome-launcher": "^1.2.1", @@ -31,11 +32,19 @@ "eslint-plugin-compat": "^6.2.1", "neostandard": "^0.12.2", "puppeteer-core": "^24.40.0", - "rollup": "^4.60.1", - "rollup-plugin-copy": "^3.5.0" + "rollup": "^4.60.1" }, - "main": "dist/client-side-validations.js", + "peerDependencies": { + "@hotwired/stimulus": ">=3.2" + }, + "main": "dist/client-side-validations.esm.js", "module": "dist/client-side-validations.esm.js", + "exports": { + ".": { + "import": "./dist/client-side-validations.esm.js", + "default": "./dist/client-side-validations.esm.js" + } + }, "engines": { "node": ">= 18.12" }, @@ -62,7 +71,7 @@ "/vendor/" ] }, - "version": "24.0.0", + "version": "25.0.0", "directories": { "lib": "lib", "test": "test" diff --git a/rollup.config.mjs b/rollup.config.mjs index a3e7c2e3e..8ff3c1892 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,5 +1,4 @@ import babel from '@rollup/plugin-babel' -import copy from 'rollup-plugin-copy' import resolve from '@rollup/plugin-node-resolve' import { createRequire } from 'node:module' @@ -19,37 +18,32 @@ const banner = `/*! export default [ { input: 'src/index.js', + external: ['@hotwired/stimulus'], output: [ { - file: pkg.main, + file: pkg.module, banner, - format: 'umd', - name: 'ClientSideValidations' + format: 'es' } ], plugins: [ resolve(), - babel({ babelHelpers: 'bundled' }), - copy({ - targets: [ - { src: pkg.main, dest: 'vendor/assets/javascripts/', rename: 'rails.validations.js' } - ], - hook: 'writeBundle', - verbose: true - }) + babel({ babelHelpers: 'bundled' }) ] }, + // Self-contained bundle used by the QUnit browser test suite. Not published. { - input: 'src/index.js', + input: 'test/javascript/test-bundle.js', output: [ { - file: pkg.module, - banner, - format: 'es' + file: 'test/javascript/public/vendor/test-bundle.js', + format: 'iife', + name: 'ClientSideValidationsTestBundle' } ], plugins: [ + resolve(), babel({ babelHelpers: 'bundled' }) ] } diff --git a/src/controllers/client_side_validations_controller.js b/src/controllers/client_side_validations_controller.js new file mode 100644 index 000000000..1e4d3d9a1 --- /dev/null +++ b/src/controllers/client_side_validations_controller.js @@ -0,0 +1,89 @@ +import { Controller } from '@hotwired/stimulus' + +import { + bindConfirmationEvents, + bindFormEvents, + bindInputEvents, + installFormContext, + removeFormContext, + unbindElement +} from '../lifecycle' + +import { isValid } from '../validate' + +export default class extends Controller { + static targets = ['input', 'confirmation'] + static values = { settings: Object } + + connect () { + this.element.noValidate = true + installFormContext(this.element, this.settingsValue) + bindFormEvents(this.element) + } + + disconnect () { + unbindElement(this.element) + + this.inputTargets.forEach((input) => { + unbindElement(input) + }) + + this.confirmationTargets.forEach((confirmation) => { + unbindElement(confirmation) + }) + + removeFormContext(this.element) + } + + inputTargetConnected (input) { + if (input.type === 'radio' || !input.form || input.form !== this.element) { + return + } + + bindInputEvents(this.element, input) + } + + inputTargetDisconnected (input) { + unbindElement(input) + } + + confirmationTargetConnected (confirmation) { + if (!confirmation.form || confirmation.form !== this.element) { + return + } + + const partner = this.#findConfirmationPartner(confirmation) + + if (partner) { + bindConfirmationEvents(partner, confirmation, this.element) + } + } + + confirmationTargetDisconnected (confirmation) { + unbindElement(confirmation) + } + + validate () { + return isValid(this.element, this.settingsValue.validators) + } + + validateElement (element) { + return isValid(element, this.settingsValue.validators) + } + + #findConfirmationPartner (confirmation) { + const partnerId = confirmation.dataset.clientSideValidationsConfirms + + if (partnerId) { + const partner = document.getElementById(partnerId) + if (partner && partner.form === this.element) return partner + } + + if (confirmation.id && confirmation.id.endsWith('_confirmation')) { + const fallback = document.getElementById(confirmation.id.replace(/_confirmation$/, '')) + if (fallback && fallback.form === this.element) return fallback + } + + return null + } +} diff --git a/src/core.js b/src/core.js index 2a64dabb1..9cfe7a872 100644 --- a/src/core.js +++ b/src/core.js @@ -1,31 +1,4 @@ -import { bindElementEvents, clearBoundEventListeners } from './events' -import { createElementFromHTML, getDOMElements, isFormElement, isInputElement, isVisible } from './utils' - -const isNamedInputElement = (element) => { - return isInputElement(element) && element.name != null && element.name !== '' -} - -const getFormControls = (form) => { - return Array.from(form.elements).filter(isInputElement) -} - -const getFormInputs = (form) => { - return getFormControls(form).filter((element) => { - return isNamedInputElement(element) && !element.disabled && isVisible(element) - }) -} - -const findFormElementByName = (form, name) => { - return getFormControls(form).find((element) => element.name === name) -} - -const enableForm = (form) => { - ClientSideValidations.enablers.form(form) -} - -const enableForms = () => { - document.querySelectorAll(ClientSideValidations.selectors.forms).forEach(enableForm) -} +import { createElementFromHTML } from './utils' const ClientSideValidations = { callbacks: { @@ -42,126 +15,6 @@ const ClientSideValidations = { pass: (form, eventData) => {} } }, - eventsToBind: { - form: (form) => ({ - submit: (eventData) => { - if (!ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators)) { - eventData.preventDefault() - eventData.stopImmediatePropagation() - } - }, - 'ajax:beforeSend': function (eventData) { - if (eventData.target === this) { - ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators) - } - }, - 'form:validate:after': (eventData) => { - ClientSideValidations.callbacks.form.after(form, eventData) - }, - 'form:validate:before': (eventData) => { - ClientSideValidations.callbacks.form.before(form, eventData) - }, - 'form:validate:fail': (eventData) => { - ClientSideValidations.callbacks.form.fail(form, eventData) - }, - 'form:validate:pass': (eventData) => { - ClientSideValidations.callbacks.form.pass(form, eventData) - } - }), - input: (form) => ({ - focusout: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators) - }, - change: function () { - this.dataset.csvChanged = 'true' - }, - 'element:validate:after': function (eventData) { - ClientSideValidations.callbacks.element.after(this, eventData) - }, - 'element:validate:before': function (eventData) { - ClientSideValidations.callbacks.element.before(this, eventData) - }, - 'element:validate:fail': function (eventData) { - const element = this - const message = eventData.detail - - ClientSideValidations.callbacks.element.fail(element, message, function () { - form.ClientSideValidations.addError(element, message) - }, eventData) - }, - 'element:validate:pass': function (eventData) { - const element = this - - ClientSideValidations.callbacks.element.pass(element, function () { - form.ClientSideValidations.removeError(element) - }, eventData) - } - }), - inputConfirmation: (elementToConfirm, form) => ({ - focusout: () => { - elementToConfirm.dataset.csvChanged = 'true' - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators) - }, - keyup: () => { - elementToConfirm.dataset.csvChanged = 'true' - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators) - } - }) - }, - enablers: { - form: (form) => { - clearBoundEventListeners(form) - getFormControls(form).forEach(clearBoundEventListeners) - - form.ClientSideValidations = { - settings: JSON.parse(form.dataset.clientSideValidations), - addError: (element, message) => ClientSideValidations - .formBuilders[form.ClientSideValidations.settings.html_settings.type] - .add(element, form.ClientSideValidations.settings.html_settings, message), - removeError: (element) => ClientSideValidations - .formBuilders[form.ClientSideValidations.settings.html_settings.type] - .remove(element, form.ClientSideValidations.settings.html_settings) - } - - bindElementEvents(form, ClientSideValidations.eventsToBind.form(form)) - - getFormInputs(form).forEach((element) => { - ClientSideValidations.enablers.input(element) - }) - }, - input: function (input) { - const form = input.form - - if (!form) { - return - } - - clearBoundEventListeners(input) - - const eventsToBind = ClientSideValidations.eventsToBind.input(form) - - if (input.type !== 'radio' && !(input.id && input.id.endsWith('_confirmation'))) { - input.dataset.csvValidate = 'true' - bindElementEvents(input, eventsToBind) - } - - if (input.type === 'checkbox') { - bindElementEvents(input, { - change: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators) - } - }) - } - - if (input.id && input.id.endsWith('_confirmation')) { - const elementToConfirm = document.getElementById(input.id.match(/(.+)_confirmation/)[1]) - - if (elementToConfirm && elementToConfirm.form === form) { - bindElementEvents(input, ClientSideValidations.eventsToBind.inputConfirmation(elementToConfirm, form)) - } - } - } - }, formBuilders: { 'ActionView::Helpers::FormBuilder': { add: (element, settings, message) => { @@ -249,11 +102,6 @@ const ClientSideValidations = { only_integer: /^[+-]?\d+$/ } }, - selectors: { - inputs: 'input:not([type="submit"]):not([type="button"])[name], select[name], textarea[name]', - validate_inputs: 'input[data-csv-validate]:not([type="submit"]):not([type="button"]), select[data-csv-validate], textarea[data-csv-validate]', - forms: 'form[data-client-side-validations]' - }, validators: { all: () => { return { @@ -263,60 +111,6 @@ const ClientSideValidations = { }, local: {}, remote: {} - }, - disable: (target) => { - getDOMElements(target).forEach((element) => { - clearBoundEventListeners(element) - - if (isFormElement(element)) { - getFormControls(element).forEach((input) => { - clearBoundEventListeners(input) - delete input.dataset.csvValid - delete input.dataset.csvChanged - delete input.dataset.csvValidate - }) - - return - } - - delete element.dataset.csvValid - delete element.dataset.csvChanged - - if (isInputElement(element)) { - delete element.dataset.csvValidate - } - }) - }, - reset: (form) => { - ClientSideValidations.disable(form) - - for (const key in form.ClientSideValidations.settings.validators) { - const element = findFormElementByName(form, key) - - if (element) { - form.ClientSideValidations.removeError(element) - } - } - - ClientSideValidations.enablers.form(form) - }, - initializeOnEvent: () => { - if (window.Turbo != null) { - return 'turbo:load' - } else if ((window.Turbolinks != null) && window.Turbolinks.supported) { - return (window.Turbolinks.EVENTS != null) ? 'page:change' : 'turbolinks:load' - } - }, - start: () => { - const initializeOnEvent = ClientSideValidations.initializeOnEvent() - - if (initializeOnEvent != null) { - document.addEventListener(initializeOnEvent, enableForms) - } else if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', enableForms, { once: true }) - } else { - enableForms() - } } } diff --git a/src/index.js b/src/index.js index e271cd10e..78726e090 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,18 @@ import ClientSideValidations from './core' -import { dispatchCustomEvent } from './events' -import { getDOMElements, isFormElement, isInputElement, isVisible } from './utils' +import ClientSideValidationsController from './controllers/client_side_validations_controller' + +import { isValid, validateElement, validateForm, validatorsFor } from './validate' import { absenceLocalValidator, presenceLocalValidator } from './validators/local/absence_presence' import { acceptanceLocalValidator } from './validators/local/acceptance' +import { confirmationLocalValidator } from './validators/local/confirmation' +import { exclusionLocalValidator, inclusionLocalValidator } from './validators/local/exclusion_inclusion' import { formatLocalValidator } from './validators/local/format' -import { numericalityLocalValidator } from './validators/local/numericality' import { lengthLocalValidator } from './validators/local/length' -import { exclusionLocalValidator, inclusionLocalValidator } from './validators/local/exclusion_inclusion' -import { confirmationLocalValidator } from './validators/local/confirmation' +import { numericalityLocalValidator } from './validators/local/numericality' import { uniquenessLocalValidator } from './validators/local/uniqueness' -// Validators will run in the following order +// Validators run in this order ClientSideValidations.validators.local = { absence: absenceLocalValidator, presence: presenceLocalValidator, @@ -25,213 +26,19 @@ ClientSideValidations.validators.local = { uniqueness: uniquenessLocalValidator } -ClientSideValidations.enable = (target) => { - getDOMElements(target).forEach((element) => { - if (isFormElement(element)) { - ClientSideValidations.enablers.form(element) - } else if (isInputElement(element)) { - ClientSideValidations.enablers.input(element) - } - }) - - return target -} - -ClientSideValidations.validate = (target) => { - getDOMElements(target).forEach((element) => { - if (isFormElement(element)) { - ClientSideValidations.enable(element) - } - }) - - return target -} - -ClientSideValidations.isValid = (target, validators) => { - const element = getDOMElements(target)[0] - - if (!element) { - return true - } - - if (!validators) { - const form = isFormElement(element) ? element : element.form - validators = form?.ClientSideValidations?.settings?.validators - } - - if (isFormElement(element)) { - return validateForm(element, validators || {}) - } - - return validateElement(element, validatorsFor(element.name, validators || {})) -} - -const cleanNestedElementName = (elementName, nestedMatches, validators) => { - for (const validatorName in validators) { - if (validatorName.match(`\\[${nestedMatches[1]}\\].*\\[\\]\\[${nestedMatches[2]}\\]$`)) { - elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]') - } - } - - return elementName -} - -const cleanElementName = (elementName, validators) => { - elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]') - - const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/) - - if (nestedMatches) { - elementName = cleanNestedElementName(elementName, nestedMatches, validators) - } - - return elementName -} - -const validatorsFor = (elementName, validators) => { - if (!elementName || !validators) { - return {} - } - - if (Object.prototype.hasOwnProperty.call(validators, elementName)) { - return validators[elementName] - } - - return validators[cleanElementName(elementName, validators)] || {} -} - -const getValidationInputs = (form) => { - return Array.from(form.elements).filter((element) => { - if (element.dataset.csvValidate == null || element.disabled) { - return false - } - - return isVisible(element) - }) -} - -const validateForm = (form, validators) => { - let valid = true - - dispatchCustomEvent(form, 'form:validate:before') - - getValidationInputs(form).forEach((element) => { - if (!validateElement(element, validatorsFor(element.name, validators))) { - valid = false - } - }) - - if (valid) { - dispatchCustomEvent(form, 'form:validate:pass') - } else { - dispatchCustomEvent(form, 'form:validate:fail') - } - - dispatchCustomEvent(form, 'form:validate:after') - - return valid -} - -const passElement = (element) => { - dispatchCustomEvent(element, 'element:validate:pass') - delete element.dataset.csvValid -} - -const failElement = (element, message) => { - dispatchCustomEvent(element, 'element:validate:fail', message) - element.dataset.csvValid = 'false' -} - -const afterValidate = (element) => { - dispatchCustomEvent(element, 'element:validate:after') - return element.dataset.csvValid !== 'false' -} - -const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { - for (const validatorOption in validatorOptions) { - if (!validatorOptions[validatorOption]) { - continue - } - - const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]) - - if (message) { - failElement(element, message) - return false - } - } - - return true -} - -const executeValidators = (validatorFunctions, element, validators) => { - for (const validator in validators) { - if (!validatorFunctions[validator]) { - continue - } - - if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { - return false - } - } - - return true -} - -const isMarkedForDestroy = (element) => { - const elementName = element.name - const form = element.form - - if (form && /\[([^\]]*?)\]$/.test(elementName)) { - const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]') - const destroyInputElement = form.querySelector(`input[name="${destroyInputName}"]`) - - if (destroyInputElement && destroyInputElement.value === '1') { - return true - } - } - - return false -} - -const executeAllValidators = (element, validators) => { - if (element.dataset.csvChanged === 'false' || element.disabled) { - return - } - - element.dataset.csvChanged = 'false' - - if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { - passElement(element) - } -} - -const validateElement = (element, validators) => { - dispatchCustomEvent(element, 'element:validate:before') - - if (isMarkedForDestroy(element)) { - passElement(element) - } else { - executeAllValidators(element, validators) - } - - return afterValidate(element) -} - -if (!window.ClientSideValidations) { - window.ClientSideValidations = ClientSideValidations - - if (!isAMD() && !isCommonJS()) { - ClientSideValidations.start() - } -} +ClientSideValidations.isValid = isValid -function isAMD () { - return typeof define === 'function' && define.amd // eslint-disable-line no-undef +export const register = (application, identifier = 'client-side-validations') => { + application.register(identifier, ClientSideValidationsController) } -function isCommonJS () { - return typeof exports === 'object' && typeof module !== 'undefined' +export { + ClientSideValidations, + ClientSideValidationsController, + isValid, + validateElement, + validateForm, + validatorsFor } export default ClientSideValidations diff --git a/src/lifecycle.js b/src/lifecycle.js new file mode 100644 index 000000000..ba4564e55 --- /dev/null +++ b/src/lifecycle.js @@ -0,0 +1,99 @@ +import ClientSideValidations from './core' +import { bindElementEvents, clearBoundEventListeners } from './events' +import { isValid } from './validate' + +const buildErrorHelpers = (settings) => ({ + addError: (element, message) => ClientSideValidations + .formBuilders[settings.html_settings.type] + .add(element, settings.html_settings, message), + removeError: (element) => ClientSideValidations + .formBuilders[settings.html_settings.type] + .remove(element, settings.html_settings) +}) + +export const installFormContext = (form, settings) => { + const { addError, removeError } = buildErrorHelpers(settings) + + form.ClientSideValidations = { settings, addError, removeError } +} + +export const removeFormContext = (form) => { + delete form.ClientSideValidations +} + +export const bindFormEvents = (form) => { + bindElementEvents(form, { + submit: (event) => { + if (!isValid(form, form.ClientSideValidations.settings.validators)) { + event.preventDefault() + event.stopImmediatePropagation() + } + }, + 'form:validate:after': (event) => ClientSideValidations.callbacks.form.after(form, event), + 'form:validate:before': (event) => ClientSideValidations.callbacks.form.before(form, event), + 'form:validate:fail': (event) => ClientSideValidations.callbacks.form.fail(form, event), + 'form:validate:pass': (event) => ClientSideValidations.callbacks.form.pass(form, event) + }) +} + +export const bindInputEvents = (form, input) => { + input.dataset.csvValidate = 'true' + + bindElementEvents(input, { + focusout: function () { + isValid(this, form.ClientSideValidations.settings.validators) + }, + change: function () { + this.dataset.csvChanged = 'true' + }, + 'element:validate:after': function (event) { + ClientSideValidations.callbacks.element.after(this, event) + }, + 'element:validate:before': function (event) { + ClientSideValidations.callbacks.element.before(this, event) + }, + 'element:validate:fail': function (event) { + const element = this + const message = event.detail + + ClientSideValidations.callbacks.element.fail(element, message, () => { + form.ClientSideValidations.addError(element, message) + }, event) + }, + 'element:validate:pass': function (event) { + const element = this + + ClientSideValidations.callbacks.element.pass(element, () => { + form.ClientSideValidations.removeError(element) + }, event) + } + }) + + if (input.type === 'checkbox') { + bindElementEvents(input, { + change: function () { + isValid(this, form.ClientSideValidations.settings.validators) + } + }) + } +} + +export const bindConfirmationEvents = (target, confirmation, form) => { + bindElementEvents(confirmation, { + focusout: () => { + target.dataset.csvChanged = 'true' + isValid(target, form.ClientSideValidations.settings.validators) + }, + keyup: () => { + target.dataset.csvChanged = 'true' + isValid(target, form.ClientSideValidations.settings.validators) + } + }) +} + +export const unbindElement = (element) => { + clearBoundEventListeners(element) + delete element.dataset.csvValid + delete element.dataset.csvChanged + delete element.dataset.csvValidate +} diff --git a/src/validate.js b/src/validate.js new file mode 100644 index 000000000..f5366ce7a --- /dev/null +++ b/src/validate.js @@ -0,0 +1,174 @@ +import ClientSideValidations from './core' +import { dispatchCustomEvent } from './events' +import { getDOMElements, isFormElement, isVisible } from './utils' + +const cleanNestedElementName = (elementName, nestedMatches, validators) => { + for (const validatorName in validators) { + if (validatorName.match(`\\[${nestedMatches[1]}\\].*\\[\\]\\[${nestedMatches[2]}\\]$`)) { + elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]') + } + } + + return elementName +} + +const cleanElementName = (elementName, validators) => { + elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]') + + const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/) + + if (nestedMatches) { + elementName = cleanNestedElementName(elementName, nestedMatches, validators) + } + + return elementName +} + +export const validatorsFor = (elementName, validators) => { + if (!elementName || !validators) { + return {} + } + + if (Object.prototype.hasOwnProperty.call(validators, elementName)) { + return validators[elementName] + } + + return validators[cleanElementName(elementName, validators)] || {} +} + +const getValidationInputs = (form) => { + return Array.from(form.elements).filter((element) => { + if (element.dataset.csvValidate == null || element.disabled) { + return false + } + + return isVisible(element) + }) +} + +const passElement = (element) => { + dispatchCustomEvent(element, 'element:validate:pass') + delete element.dataset.csvValid +} + +const failElement = (element, message) => { + dispatchCustomEvent(element, 'element:validate:fail', message) + element.dataset.csvValid = 'false' +} + +const afterValidate = (element) => { + dispatchCustomEvent(element, 'element:validate:after') + return element.dataset.csvValid !== 'false' +} + +const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { + for (const validatorOption in validatorOptions) { + if (!validatorOptions[validatorOption]) { + continue + } + + const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]) + + if (message) { + failElement(element, message) + return false + } + } + + return true +} + +const executeValidators = (validatorFunctions, element, validators) => { + for (const validator in validators) { + if (!validatorFunctions[validator]) { + continue + } + + if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { + return false + } + } + + return true +} + +const isMarkedForDestroy = (element) => { + const elementName = element.name + const form = element.form + + if (form && /\[([^\]]*?)\]$/.test(elementName)) { + const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]') + const destroyInputElement = form.querySelector(`input[name="${destroyInputName}"]`) + + if (destroyInputElement && destroyInputElement.value === '1') { + return true + } + } + + return false +} + +const executeAllValidators = (element, validators) => { + if (element.dataset.csvChanged === 'false' || element.disabled) { + return + } + + element.dataset.csvChanged = 'false' + + if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { + passElement(element) + } +} + +export const validateElement = (element, validators) => { + dispatchCustomEvent(element, 'element:validate:before') + + if (isMarkedForDestroy(element)) { + passElement(element) + } else { + executeAllValidators(element, validators) + } + + return afterValidate(element) +} + +export const validateForm = (form, validators) => { + let valid = true + + dispatchCustomEvent(form, 'form:validate:before') + + getValidationInputs(form).forEach((element) => { + if (!validateElement(element, validatorsFor(element.name, validators))) { + valid = false + } + }) + + if (valid) { + dispatchCustomEvent(form, 'form:validate:pass') + } else { + dispatchCustomEvent(form, 'form:validate:fail') + } + + dispatchCustomEvent(form, 'form:validate:after') + + return valid +} + +export const isValid = (target, validators) => { + const element = getDOMElements(target)[0] + + if (!element) { + return true + } + + if (!validators) { + const form = isFormElement(element) ? element : element.form + validators = form?.ClientSideValidations?.settings?.validators + } + + if (isFormElement(element)) { + return validateForm(element, validators || {}) + } + + return validateElement(element, validatorsFor(element.name, validators || {})) +} diff --git a/test/action_view/cases/helper.rb b/test/action_view/cases/helper.rb index fdb19b949..5107b2836 100644 --- a/test/action_view/cases/helper.rb +++ b/test/action_view/cases/helper.rb @@ -157,6 +157,7 @@ def form_field(tag, id: nil, name: nil, type: nil, value: nil, multiple: false, txt << %( multiple="multiple") if multiple txt << %( name="#{name}") if name txt << %( id="#{id}") if id + txt << stimulus_target_attributes(id, name || custom_name, type) txt << if %w[select textarea].include?(tag) %(>#{tag_content}) @@ -167,12 +168,33 @@ def form_field(tag, id: nil, name: nil, type: nil, value: nil, multiple: false, txt end + def stimulus_target_attributes(id, name, type = nil) + return '' unless Thread.current[:csv_helper_active] + return '' if name.nil? + return '' if %w[hidden submit button].include?(type.to_s) + + if id.to_s.end_with?('_confirmation') + confirms = id.delete_suffix('_confirmation') + %( data-client-side-validations-target="confirmation" data-client-side-validations-confirms="#{confirms}") + else + names = Thread.current[:csv_helper_validator_names] + if names + normalize = ->(n) { n.to_s.gsub(/_attributes\]\[[^\]]*\]/, '_attributes][]').delete_suffix('[]') } + normalized_names = names.map(&normalize) + return '' unless normalized_names.include?(normalize.call(name)) + end + + %( data-client-side-validations-target="input") + end + end + def form_for_text(action = 'http://www.example.com', id = nil, html_class = nil, remote = nil, validators = nil, file = nil) txt = %(
' end @@ -200,8 +222,9 @@ def form_with_text(action = 'http://www.example.com', id = nil, html_class = nil txt = %(' end + def with_csv_helper_active(validators, no_validate) + previous_active = Thread.current[:csv_helper_active] + previous_names = Thread.current[:csv_helper_validator_names] + Thread.current[:csv_helper_active] = true if validators && !no_validate + Thread.current[:csv_helper_validator_names] = validators.is_a?(Hash) ? validators.keys.map(&:to_s) : nil + yield + ensure + Thread.current[:csv_helper_active] = previous_active + Thread.current[:csv_helper_validator_names] = previous_names + end + def client_side_form_settings_helper { type: 'ActionView::Helpers::FormBuilder', diff --git a/test/generators/cases/test_generators.rb b/test/generators/cases/test_generators.rb index ad3b5b4d4..f8b66fa10 100644 --- a/test/generators/cases/test_generators.rb +++ b/test/generators/cases/test_generators.rb @@ -2,7 +2,6 @@ require 'rails/generators/test_case' require 'client_side_validations' -require 'generators/client_side_validations/copy_assets_generator' require 'generators/client_side_validations/install_generator' class InstallGeneratorTest < Rails::Generators::TestCase @@ -10,79 +9,25 @@ class InstallGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../tmp', __dir__) setup :prepare_destination - test 'Assert all files are properly created without sprockets and webpacker' do - stub_configuration + test 'Installs the initializer' do run_generator assert_file 'config/initializers/client_side_validations.rb' - assert_file 'public/javascripts/rails.validations.js' end - test 'Assert all files are properly created with sprockets' do - stub_configuration - configuration = {} - configuration.stubs(:prefix).returns('/assets') - ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:sprockets?).returns true - Rails.configuration.stubs(:assets).returns(configuration) + test 'Installs the Stimulus controller' do run_generator - assert_file 'config/initializers/client_side_validations.rb' - assert_no_file 'public/javascripts/rails.validations.js' + assert_file 'app/javascript/controllers/client_side_validations_controller.js' do |content| + assert_match(/ClientSideValidationsController/, content) + assert_match(%r{@client-side-validations/client-side-validations}, content) + end end - test 'Assert all files are properly created with webpacker' do - stub_configuration - configuration = {} - configuration.stubs(:prefix).returns('/assets') - ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:webpacker?).returns true - Rails.configuration.stubs(:assets).returns(configuration) + test 'Does not copy the legacy asset bundle' do run_generator - assert_file 'config/initializers/client_side_validations.rb' assert_no_file 'public/javascripts/rails.validations.js' - end - - private - - def stub_configuration - Rails.stubs(:configuration).returns(mock('Configuration')) - end -end - -class CopyAssetsGeneratorTest < Rails::Generators::TestCase - tests ClientSideValidations::Generators::CopyAssetsGenerator - destination File.expand_path('../tmp', __dir__) - setup :prepare_destination - - test 'Assert file is properly created without sprockets and webpacker' do - stub_configuration - run_generator - - assert_file 'public/javascripts/rails.validations.js' - end - - test 'Assert file is properly created with sprockets' do - stub_configuration - configuration = {} - configuration.stubs(:prefix).returns('/assets') - ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:sprockets?).returns true - Rails.configuration.stubs(:assets).returns(configuration) - run_generator - - assert_file 'app/assets/javascripts/rails.validations.js' - end - - test 'Assert file is properly created with webpacker' do - stub_configuration - ClientSideValidations::Generators::CopyAssetsGenerator.stubs(:webpacker?).returns true - run_generator - - assert_file 'public/javascripts/rails.validations.js' - end - - private - - def stub_configuration - Rails.stubs(:configuration).returns(mock('Configuration')) + assert_no_file 'app/assets/javascripts/rails.validations.js' end end diff --git a/test/javascript/public/test/stimulus/controller.js b/test/javascript/public/test/stimulus/controller.js new file mode 100644 index 000000000..a4573cabb --- /dev/null +++ b/test/javascript/public/test/stimulus/controller.js @@ -0,0 +1,142 @@ +// End-to-end tests for the Stimulus integration. These tests exercise the +// real `client-side-validations` Stimulus controller (connected via +// `window.Stimulus` in the test bundle) rather than the lifecycle helpers +// used elsewhere in the suite. + +var stimulusSettings = { + html_settings: { + type: 'ActionView::Helpers::FormBuilder', + input_tag: '
', + label_tag: '
' + }, + validators: { 'user[name]': { presence: [{ message: 'must be present' }] } } +} + +var waitForStimulus = function () { + return new Promise(function (resolve) { + setTimeout(resolve, 0) + }) +} + +QUnit.module('Stimulus controller', { + beforeEach: function () { + var fixture = document.getElementById('qunit-fixture') + var form = document.createElement('form') + + form.action = '/users' + form.method = 'post' + form.id = 'stim_user' + form.setAttribute('data-controller', 'client-side-validations') + form.setAttribute('data-client-side-validations-settings-value', JSON.stringify(stimulusSettings)) + + fixture.appendChild(form) + } +}) + +QUnit.test('connect installs form context and applies novalidate', function (assert) { + var done = assert.async() + var form = document.getElementById('stim_user') + + waitForStimulus().then(function () { + assert.ok(form.noValidate, 'form is marked novalidate') + assert.ok(form.ClientSideValidations, 'form context is installed') + assert.deepEqual( + form.ClientSideValidations.settings.validators, + stimulusSettings.validators, + 'settings are exposed from the controller value' + ) + done() + }) +}) + +QUnit.test('input target bound on connect validates on focusout', function (assert) { + var done = assert.async() + var form = document.getElementById('stim_user') + var input = document.createElement('input') + + input.type = 'text' + input.name = 'user[name]' + input.id = 'user_name' + input.setAttribute('data-client-side-validations-target', 'input') + form.appendChild(input) + + waitForStimulus().then(function () { + input.dispatchEvent(new Event('focusout', { bubbles: true })) + assert.ok( + input.parentElement.classList.contains('field_with_errors'), + 'empty required input is flagged' + ) + + input.value = 'alice' + input.dataset.csvChanged = 'true' + input.dispatchEvent(new Event('focusout', { bubbles: true })) + assert.notOk( + input.parentElement.classList.contains('field_with_errors'), + 'valid input clears errors' + ) + done() + }) +}) + +QUnit.test('dynamically inserted input target is bound', function (assert) { + var done = assert.async() + var form = document.getElementById('stim_user') + + waitForStimulus().then(function () { + var input = document.createElement('input') + input.type = 'text' + input.name = 'user[name]' + input.id = 'user_name' + input.setAttribute('data-client-side-validations-target', 'input') + form.appendChild(input) + + return waitForStimulus().then(function () { + input.dispatchEvent(new Event('focusout', { bubbles: true })) + assert.ok( + input.parentElement.classList.contains('field_with_errors'), + 'late-added target is validated' + ) + done() + }) + }) +}) + +QUnit.test('disconnect tears down form context when controller removed', function (assert) { + var done = assert.async() + var form = document.getElementById('stim_user') + + waitForStimulus().then(function () { + form.removeAttribute('data-controller') + return waitForStimulus() + }).then(function () { + assert.notOk(form.ClientSideValidations, 'form context removed') + done() + }) +}) + +QUnit.test('submit on invalid form is prevented', function (assert) { + var done = assert.async() + var form = document.getElementById('stim_user') + var input = document.createElement('input') + + input.type = 'text' + input.name = 'user[name]' + input.id = 'user_name' + input.setAttribute('data-client-side-validations-target', 'input') + form.appendChild(input) + + waitForStimulus().then(function () { + form.requestSubmit() + + setTimeout(function () { + var iframe = document.querySelector('iframe') + var response = iframe && iframe.contentDocument && iframe.contentDocument.querySelector('#response') + assert.notOk(response, 'form did not submit to server') + assert.ok( + input.parentElement.classList.contains('field_with_errors'), + 'invalid input is flagged after submit attempt' + ) + done() + }, 200) + }) +}) diff --git a/test/javascript/public/vendor/test-bundle.js b/test/javascript/public/vendor/test-bundle.js new file mode 100644 index 000000000..6505a251b --- /dev/null +++ b/test/javascript/public/vendor/test-bundle.js @@ -0,0 +1,3446 @@ +(function () { + 'use strict'; + + /* + Stimulus 3.2.1 + Copyright © 2023 Basecamp, LLC + */ + class EventListener { + constructor(eventTarget, eventName, eventOptions) { + this.eventTarget = eventTarget; + this.eventName = eventName; + this.eventOptions = eventOptions; + this.unorderedBindings = new Set(); + } + connect() { + this.eventTarget.addEventListener(this.eventName, this, this.eventOptions); + } + disconnect() { + this.eventTarget.removeEventListener(this.eventName, this, this.eventOptions); + } + bindingConnected(binding) { + this.unorderedBindings.add(binding); + } + bindingDisconnected(binding) { + this.unorderedBindings.delete(binding); + } + handleEvent(event) { + const extendedEvent = extendEvent(event); + for (const binding of this.bindings) { + if (extendedEvent.immediatePropagationStopped) { + break; + } else { + binding.handleEvent(extendedEvent); + } + } + } + hasBindings() { + return this.unorderedBindings.size > 0; + } + get bindings() { + return Array.from(this.unorderedBindings).sort((left, right) => { + const leftIndex = left.index, + rightIndex = right.index; + return leftIndex < rightIndex ? -1 : leftIndex > rightIndex ? 1 : 0; + }); + } + } + function extendEvent(event) { + if ("immediatePropagationStopped" in event) { + return event; + } else { + const { + stopImmediatePropagation + } = event; + return Object.assign(event, { + immediatePropagationStopped: false, + stopImmediatePropagation() { + this.immediatePropagationStopped = true; + stopImmediatePropagation.call(this); + } + }); + } + } + class Dispatcher { + constructor(application) { + this.application = application; + this.eventListenerMaps = new Map(); + this.started = false; + } + start() { + if (!this.started) { + this.started = true; + this.eventListeners.forEach(eventListener => eventListener.connect()); + } + } + stop() { + if (this.started) { + this.started = false; + this.eventListeners.forEach(eventListener => eventListener.disconnect()); + } + } + get eventListeners() { + return Array.from(this.eventListenerMaps.values()).reduce((listeners, map) => listeners.concat(Array.from(map.values())), []); + } + bindingConnected(binding) { + this.fetchEventListenerForBinding(binding).bindingConnected(binding); + } + bindingDisconnected(binding, clearEventListeners = false) { + this.fetchEventListenerForBinding(binding).bindingDisconnected(binding); + if (clearEventListeners) this.clearEventListenersForBinding(binding); + } + handleError(error, message, detail = {}) { + this.application.handleError(error, `Error ${message}`, detail); + } + clearEventListenersForBinding(binding) { + const eventListener = this.fetchEventListenerForBinding(binding); + if (!eventListener.hasBindings()) { + eventListener.disconnect(); + this.removeMappedEventListenerFor(binding); + } + } + removeMappedEventListenerFor(binding) { + const { + eventTarget, + eventName, + eventOptions + } = binding; + const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget); + const cacheKey = this.cacheKey(eventName, eventOptions); + eventListenerMap.delete(cacheKey); + if (eventListenerMap.size == 0) this.eventListenerMaps.delete(eventTarget); + } + fetchEventListenerForBinding(binding) { + const { + eventTarget, + eventName, + eventOptions + } = binding; + return this.fetchEventListener(eventTarget, eventName, eventOptions); + } + fetchEventListener(eventTarget, eventName, eventOptions) { + const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget); + const cacheKey = this.cacheKey(eventName, eventOptions); + let eventListener = eventListenerMap.get(cacheKey); + if (!eventListener) { + eventListener = this.createEventListener(eventTarget, eventName, eventOptions); + eventListenerMap.set(cacheKey, eventListener); + } + return eventListener; + } + createEventListener(eventTarget, eventName, eventOptions) { + const eventListener = new EventListener(eventTarget, eventName, eventOptions); + if (this.started) { + eventListener.connect(); + } + return eventListener; + } + fetchEventListenerMapForEventTarget(eventTarget) { + let eventListenerMap = this.eventListenerMaps.get(eventTarget); + if (!eventListenerMap) { + eventListenerMap = new Map(); + this.eventListenerMaps.set(eventTarget, eventListenerMap); + } + return eventListenerMap; + } + cacheKey(eventName, eventOptions) { + const parts = [eventName]; + Object.keys(eventOptions).sort().forEach(key => { + parts.push(`${eventOptions[key] ? "" : "!"}${key}`); + }); + return parts.join(":"); + } + } + const defaultActionDescriptorFilters = { + stop({ + event, + value + }) { + if (value) event.stopPropagation(); + return true; + }, + prevent({ + event, + value + }) { + if (value) event.preventDefault(); + return true; + }, + self({ + event, + value, + element + }) { + if (value) { + return element === event.target; + } else { + return true; + } + } + }; + const descriptorPattern = /^(?:(?:([^.]+?)\+)?(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)(?:#([^:]+?))(?::(.+))?$/; + function parseActionDescriptorString(descriptorString) { + const source = descriptorString.trim(); + const matches = source.match(descriptorPattern) || []; + let eventName = matches[2]; + let keyFilter = matches[3]; + if (keyFilter && !["keydown", "keyup", "keypress"].includes(eventName)) { + eventName += `.${keyFilter}`; + keyFilter = ""; + } + return { + eventTarget: parseEventTarget(matches[4]), + eventName, + eventOptions: matches[7] ? parseEventOptions(matches[7]) : {}, + identifier: matches[5], + methodName: matches[6], + keyFilter: matches[1] || keyFilter + }; + } + function parseEventTarget(eventTargetName) { + if (eventTargetName == "window") { + return window; + } else if (eventTargetName == "document") { + return document; + } + } + function parseEventOptions(eventOptions) { + return eventOptions.split(":").reduce((options, token) => Object.assign(options, { + [token.replace(/^!/, "")]: !/^!/.test(token) + }), {}); + } + function stringifyEventTarget(eventTarget) { + if (eventTarget == window) { + return "window"; + } else if (eventTarget == document) { + return "document"; + } + } + function camelize(value) { + return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase()); + } + function namespaceCamelize(value) { + return camelize(value.replace(/--/g, "-").replace(/__/g, "_")); + } + function capitalize(value) { + return value.charAt(0).toUpperCase() + value.slice(1); + } + function dasherize(value) { + return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`); + } + function tokenize(value) { + return value.match(/[^\s]+/g) || []; + } + function isSomething(object) { + return object !== null && object !== undefined; + } + function hasProperty(object, property) { + return Object.prototype.hasOwnProperty.call(object, property); + } + const allModifiers = ["meta", "ctrl", "alt", "shift"]; + class Action { + constructor(element, index, descriptor, schema) { + this.element = element; + this.index = index; + this.eventTarget = descriptor.eventTarget || element; + this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name"); + this.eventOptions = descriptor.eventOptions || {}; + this.identifier = descriptor.identifier || error("missing identifier"); + this.methodName = descriptor.methodName || error("missing method name"); + this.keyFilter = descriptor.keyFilter || ""; + this.schema = schema; + } + static forToken(token, schema) { + return new this(token.element, token.index, parseActionDescriptorString(token.content), schema); + } + toString() { + const eventFilter = this.keyFilter ? `.${this.keyFilter}` : ""; + const eventTarget = this.eventTargetName ? `@${this.eventTargetName}` : ""; + return `${this.eventName}${eventFilter}${eventTarget}->${this.identifier}#${this.methodName}`; + } + shouldIgnoreKeyboardEvent(event) { + if (!this.keyFilter) { + return false; + } + const filters = this.keyFilter.split("+"); + if (this.keyFilterDissatisfied(event, filters)) { + return true; + } + const standardFilter = filters.filter(key => !allModifiers.includes(key))[0]; + if (!standardFilter) { + return false; + } + if (!hasProperty(this.keyMappings, standardFilter)) { + error(`contains unknown key filter: ${this.keyFilter}`); + } + return this.keyMappings[standardFilter].toLowerCase() !== event.key.toLowerCase(); + } + shouldIgnoreMouseEvent(event) { + if (!this.keyFilter) { + return false; + } + const filters = [this.keyFilter]; + if (this.keyFilterDissatisfied(event, filters)) { + return true; + } + return false; + } + get params() { + const params = {}; + const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`, "i"); + for (const { + name, + value + } of Array.from(this.element.attributes)) { + const match = name.match(pattern); + const key = match && match[1]; + if (key) { + params[camelize(key)] = typecast(value); + } + } + return params; + } + get eventTargetName() { + return stringifyEventTarget(this.eventTarget); + } + get keyMappings() { + return this.schema.keyMappings; + } + keyFilterDissatisfied(event, filters) { + const [meta, ctrl, alt, shift] = allModifiers.map(modifier => filters.includes(modifier)); + return event.metaKey !== meta || event.ctrlKey !== ctrl || event.altKey !== alt || event.shiftKey !== shift; + } + } + const defaultEventNames = { + a: () => "click", + button: () => "click", + form: () => "submit", + details: () => "toggle", + input: e => e.getAttribute("type") == "submit" ? "click" : "input", + select: () => "change", + textarea: () => "input" + }; + function getDefaultEventNameForElement(element) { + const tagName = element.tagName.toLowerCase(); + if (tagName in defaultEventNames) { + return defaultEventNames[tagName](element); + } + } + function error(message) { + throw new Error(message); + } + function typecast(value) { + try { + return JSON.parse(value); + } catch (o_O) { + return value; + } + } + class Binding { + constructor(context, action) { + this.context = context; + this.action = action; + } + get index() { + return this.action.index; + } + get eventTarget() { + return this.action.eventTarget; + } + get eventOptions() { + return this.action.eventOptions; + } + get identifier() { + return this.context.identifier; + } + handleEvent(event) { + const actionEvent = this.prepareActionEvent(event); + if (this.willBeInvokedByEvent(event) && this.applyEventModifiers(actionEvent)) { + this.invokeWithEvent(actionEvent); + } + } + get eventName() { + return this.action.eventName; + } + get method() { + const method = this.controller[this.methodName]; + if (typeof method == "function") { + return method; + } + throw new Error(`Action "${this.action}" references undefined method "${this.methodName}"`); + } + applyEventModifiers(event) { + const { + element + } = this.action; + const { + actionDescriptorFilters + } = this.context.application; + const { + controller + } = this.context; + let passes = true; + for (const [name, value] of Object.entries(this.eventOptions)) { + if (name in actionDescriptorFilters) { + const filter = actionDescriptorFilters[name]; + passes = passes && filter({ + name, + value, + event, + element, + controller + }); + } else { + continue; + } + } + return passes; + } + prepareActionEvent(event) { + return Object.assign(event, { + params: this.action.params + }); + } + invokeWithEvent(event) { + const { + target, + currentTarget + } = event; + try { + this.method.call(this.controller, event); + this.context.logDebugActivity(this.methodName, { + event, + target, + currentTarget, + action: this.methodName + }); + } catch (error) { + const { + identifier, + controller, + element, + index + } = this; + const detail = { + identifier, + controller, + element, + index, + event + }; + this.context.handleError(error, `invoking action "${this.action}"`, detail); + } + } + willBeInvokedByEvent(event) { + const eventTarget = event.target; + if (event instanceof KeyboardEvent && this.action.shouldIgnoreKeyboardEvent(event)) { + return false; + } + if (event instanceof MouseEvent && this.action.shouldIgnoreMouseEvent(event)) { + return false; + } + if (this.element === eventTarget) { + return true; + } else if (eventTarget instanceof Element && this.element.contains(eventTarget)) { + return this.scope.containsElement(eventTarget); + } else { + return this.scope.containsElement(this.action.element); + } + } + get controller() { + return this.context.controller; + } + get methodName() { + return this.action.methodName; + } + get element() { + return this.scope.element; + } + get scope() { + return this.context.scope; + } + } + class ElementObserver { + constructor(element, delegate) { + this.mutationObserverInit = { + attributes: true, + childList: true, + subtree: true + }; + this.element = element; + this.started = false; + this.delegate = delegate; + this.elements = new Set(); + this.mutationObserver = new MutationObserver(mutations => this.processMutations(mutations)); + } + start() { + if (!this.started) { + this.started = true; + this.mutationObserver.observe(this.element, this.mutationObserverInit); + this.refresh(); + } + } + pause(callback) { + if (this.started) { + this.mutationObserver.disconnect(); + this.started = false; + } + callback(); + if (!this.started) { + this.mutationObserver.observe(this.element, this.mutationObserverInit); + this.started = true; + } + } + stop() { + if (this.started) { + this.mutationObserver.takeRecords(); + this.mutationObserver.disconnect(); + this.started = false; + } + } + refresh() { + if (this.started) { + const matches = new Set(this.matchElementsInTree()); + for (const element of Array.from(this.elements)) { + if (!matches.has(element)) { + this.removeElement(element); + } + } + for (const element of Array.from(matches)) { + this.addElement(element); + } + } + } + processMutations(mutations) { + if (this.started) { + for (const mutation of mutations) { + this.processMutation(mutation); + } + } + } + processMutation(mutation) { + if (mutation.type == "attributes") { + this.processAttributeChange(mutation.target, mutation.attributeName); + } else if (mutation.type == "childList") { + this.processRemovedNodes(mutation.removedNodes); + this.processAddedNodes(mutation.addedNodes); + } + } + processAttributeChange(element, attributeName) { + if (this.elements.has(element)) { + if (this.delegate.elementAttributeChanged && this.matchElement(element)) { + this.delegate.elementAttributeChanged(element, attributeName); + } else { + this.removeElement(element); + } + } else if (this.matchElement(element)) { + this.addElement(element); + } + } + processRemovedNodes(nodes) { + for (const node of Array.from(nodes)) { + const element = this.elementFromNode(node); + if (element) { + this.processTree(element, this.removeElement); + } + } + } + processAddedNodes(nodes) { + for (const node of Array.from(nodes)) { + const element = this.elementFromNode(node); + if (element && this.elementIsActive(element)) { + this.processTree(element, this.addElement); + } + } + } + matchElement(element) { + return this.delegate.matchElement(element); + } + matchElementsInTree(tree = this.element) { + return this.delegate.matchElementsInTree(tree); + } + processTree(tree, processor) { + for (const element of this.matchElementsInTree(tree)) { + processor.call(this, element); + } + } + elementFromNode(node) { + if (node.nodeType == Node.ELEMENT_NODE) { + return node; + } + } + elementIsActive(element) { + if (element.isConnected != this.element.isConnected) { + return false; + } else { + return this.element.contains(element); + } + } + addElement(element) { + if (!this.elements.has(element)) { + if (this.elementIsActive(element)) { + this.elements.add(element); + if (this.delegate.elementMatched) { + this.delegate.elementMatched(element); + } + } + } + } + removeElement(element) { + if (this.elements.has(element)) { + this.elements.delete(element); + if (this.delegate.elementUnmatched) { + this.delegate.elementUnmatched(element); + } + } + } + } + class AttributeObserver { + constructor(element, attributeName, delegate) { + this.attributeName = attributeName; + this.delegate = delegate; + this.elementObserver = new ElementObserver(element, this); + } + get element() { + return this.elementObserver.element; + } + get selector() { + return `[${this.attributeName}]`; + } + start() { + this.elementObserver.start(); + } + pause(callback) { + this.elementObserver.pause(callback); + } + stop() { + this.elementObserver.stop(); + } + refresh() { + this.elementObserver.refresh(); + } + get started() { + return this.elementObserver.started; + } + matchElement(element) { + return element.hasAttribute(this.attributeName); + } + matchElementsInTree(tree) { + const match = this.matchElement(tree) ? [tree] : []; + const matches = Array.from(tree.querySelectorAll(this.selector)); + return match.concat(matches); + } + elementMatched(element) { + if (this.delegate.elementMatchedAttribute) { + this.delegate.elementMatchedAttribute(element, this.attributeName); + } + } + elementUnmatched(element) { + if (this.delegate.elementUnmatchedAttribute) { + this.delegate.elementUnmatchedAttribute(element, this.attributeName); + } + } + elementAttributeChanged(element, attributeName) { + if (this.delegate.elementAttributeValueChanged && this.attributeName == attributeName) { + this.delegate.elementAttributeValueChanged(element, attributeName); + } + } + } + function add(map, key, value) { + fetch(map, key).add(value); + } + function del(map, key, value) { + fetch(map, key).delete(value); + prune(map, key); + } + function fetch(map, key) { + let values = map.get(key); + if (!values) { + values = new Set(); + map.set(key, values); + } + return values; + } + function prune(map, key) { + const values = map.get(key); + if (values != null && values.size == 0) { + map.delete(key); + } + } + class Multimap { + constructor() { + this.valuesByKey = new Map(); + } + get keys() { + return Array.from(this.valuesByKey.keys()); + } + get values() { + const sets = Array.from(this.valuesByKey.values()); + return sets.reduce((values, set) => values.concat(Array.from(set)), []); + } + get size() { + const sets = Array.from(this.valuesByKey.values()); + return sets.reduce((size, set) => size + set.size, 0); + } + add(key, value) { + add(this.valuesByKey, key, value); + } + delete(key, value) { + del(this.valuesByKey, key, value); + } + has(key, value) { + const values = this.valuesByKey.get(key); + return values != null && values.has(value); + } + hasKey(key) { + return this.valuesByKey.has(key); + } + hasValue(value) { + const sets = Array.from(this.valuesByKey.values()); + return sets.some(set => set.has(value)); + } + getValuesForKey(key) { + const values = this.valuesByKey.get(key); + return values ? Array.from(values) : []; + } + getKeysForValue(value) { + return Array.from(this.valuesByKey).filter(([_key, values]) => values.has(value)).map(([key, _values]) => key); + } + } + class SelectorObserver { + constructor(element, selector, delegate, details) { + this._selector = selector; + this.details = details; + this.elementObserver = new ElementObserver(element, this); + this.delegate = delegate; + this.matchesByElement = new Multimap(); + } + get started() { + return this.elementObserver.started; + } + get selector() { + return this._selector; + } + set selector(selector) { + this._selector = selector; + this.refresh(); + } + start() { + this.elementObserver.start(); + } + pause(callback) { + this.elementObserver.pause(callback); + } + stop() { + this.elementObserver.stop(); + } + refresh() { + this.elementObserver.refresh(); + } + get element() { + return this.elementObserver.element; + } + matchElement(element) { + const { + selector + } = this; + if (selector) { + const matches = element.matches(selector); + if (this.delegate.selectorMatchElement) { + return matches && this.delegate.selectorMatchElement(element, this.details); + } + return matches; + } else { + return false; + } + } + matchElementsInTree(tree) { + const { + selector + } = this; + if (selector) { + const match = this.matchElement(tree) ? [tree] : []; + const matches = Array.from(tree.querySelectorAll(selector)).filter(match => this.matchElement(match)); + return match.concat(matches); + } else { + return []; + } + } + elementMatched(element) { + const { + selector + } = this; + if (selector) { + this.selectorMatched(element, selector); + } + } + elementUnmatched(element) { + const selectors = this.matchesByElement.getKeysForValue(element); + for (const selector of selectors) { + this.selectorUnmatched(element, selector); + } + } + elementAttributeChanged(element, _attributeName) { + const { + selector + } = this; + if (selector) { + const matches = this.matchElement(element); + const matchedBefore = this.matchesByElement.has(selector, element); + if (matches && !matchedBefore) { + this.selectorMatched(element, selector); + } else if (!matches && matchedBefore) { + this.selectorUnmatched(element, selector); + } + } + } + selectorMatched(element, selector) { + this.delegate.selectorMatched(element, selector, this.details); + this.matchesByElement.add(selector, element); + } + selectorUnmatched(element, selector) { + this.delegate.selectorUnmatched(element, selector, this.details); + this.matchesByElement.delete(selector, element); + } + } + class StringMapObserver { + constructor(element, delegate) { + this.element = element; + this.delegate = delegate; + this.started = false; + this.stringMap = new Map(); + this.mutationObserver = new MutationObserver(mutations => this.processMutations(mutations)); + } + start() { + if (!this.started) { + this.started = true; + this.mutationObserver.observe(this.element, { + attributes: true, + attributeOldValue: true + }); + this.refresh(); + } + } + stop() { + if (this.started) { + this.mutationObserver.takeRecords(); + this.mutationObserver.disconnect(); + this.started = false; + } + } + refresh() { + if (this.started) { + for (const attributeName of this.knownAttributeNames) { + this.refreshAttribute(attributeName, null); + } + } + } + processMutations(mutations) { + if (this.started) { + for (const mutation of mutations) { + this.processMutation(mutation); + } + } + } + processMutation(mutation) { + const attributeName = mutation.attributeName; + if (attributeName) { + this.refreshAttribute(attributeName, mutation.oldValue); + } + } + refreshAttribute(attributeName, oldValue) { + const key = this.delegate.getStringMapKeyForAttribute(attributeName); + if (key != null) { + if (!this.stringMap.has(attributeName)) { + this.stringMapKeyAdded(key, attributeName); + } + const value = this.element.getAttribute(attributeName); + if (this.stringMap.get(attributeName) != value) { + this.stringMapValueChanged(value, key, oldValue); + } + if (value == null) { + const oldValue = this.stringMap.get(attributeName); + this.stringMap.delete(attributeName); + if (oldValue) this.stringMapKeyRemoved(key, attributeName, oldValue); + } else { + this.stringMap.set(attributeName, value); + } + } + } + stringMapKeyAdded(key, attributeName) { + if (this.delegate.stringMapKeyAdded) { + this.delegate.stringMapKeyAdded(key, attributeName); + } + } + stringMapValueChanged(value, key, oldValue) { + if (this.delegate.stringMapValueChanged) { + this.delegate.stringMapValueChanged(value, key, oldValue); + } + } + stringMapKeyRemoved(key, attributeName, oldValue) { + if (this.delegate.stringMapKeyRemoved) { + this.delegate.stringMapKeyRemoved(key, attributeName, oldValue); + } + } + get knownAttributeNames() { + return Array.from(new Set(this.currentAttributeNames.concat(this.recordedAttributeNames))); + } + get currentAttributeNames() { + return Array.from(this.element.attributes).map(attribute => attribute.name); + } + get recordedAttributeNames() { + return Array.from(this.stringMap.keys()); + } + } + class TokenListObserver { + constructor(element, attributeName, delegate) { + this.attributeObserver = new AttributeObserver(element, attributeName, this); + this.delegate = delegate; + this.tokensByElement = new Multimap(); + } + get started() { + return this.attributeObserver.started; + } + start() { + this.attributeObserver.start(); + } + pause(callback) { + this.attributeObserver.pause(callback); + } + stop() { + this.attributeObserver.stop(); + } + refresh() { + this.attributeObserver.refresh(); + } + get element() { + return this.attributeObserver.element; + } + get attributeName() { + return this.attributeObserver.attributeName; + } + elementMatchedAttribute(element) { + this.tokensMatched(this.readTokensForElement(element)); + } + elementAttributeValueChanged(element) { + const [unmatchedTokens, matchedTokens] = this.refreshTokensForElement(element); + this.tokensUnmatched(unmatchedTokens); + this.tokensMatched(matchedTokens); + } + elementUnmatchedAttribute(element) { + this.tokensUnmatched(this.tokensByElement.getValuesForKey(element)); + } + tokensMatched(tokens) { + tokens.forEach(token => this.tokenMatched(token)); + } + tokensUnmatched(tokens) { + tokens.forEach(token => this.tokenUnmatched(token)); + } + tokenMatched(token) { + this.delegate.tokenMatched(token); + this.tokensByElement.add(token.element, token); + } + tokenUnmatched(token) { + this.delegate.tokenUnmatched(token); + this.tokensByElement.delete(token.element, token); + } + refreshTokensForElement(element) { + const previousTokens = this.tokensByElement.getValuesForKey(element); + const currentTokens = this.readTokensForElement(element); + const firstDifferingIndex = zip(previousTokens, currentTokens).findIndex(([previousToken, currentToken]) => !tokensAreEqual(previousToken, currentToken)); + if (firstDifferingIndex == -1) { + return [[], []]; + } else { + return [previousTokens.slice(firstDifferingIndex), currentTokens.slice(firstDifferingIndex)]; + } + } + readTokensForElement(element) { + const attributeName = this.attributeName; + const tokenString = element.getAttribute(attributeName) || ""; + return parseTokenString(tokenString, element, attributeName); + } + } + function parseTokenString(tokenString, element, attributeName) { + return tokenString.trim().split(/\s+/).filter(content => content.length).map((content, index) => ({ + element, + attributeName, + content, + index + })); + } + function zip(left, right) { + const length = Math.max(left.length, right.length); + return Array.from({ + length + }, (_, index) => [left[index], right[index]]); + } + function tokensAreEqual(left, right) { + return left && right && left.index == right.index && left.content == right.content; + } + class ValueListObserver { + constructor(element, attributeName, delegate) { + this.tokenListObserver = new TokenListObserver(element, attributeName, this); + this.delegate = delegate; + this.parseResultsByToken = new WeakMap(); + this.valuesByTokenByElement = new WeakMap(); + } + get started() { + return this.tokenListObserver.started; + } + start() { + this.tokenListObserver.start(); + } + stop() { + this.tokenListObserver.stop(); + } + refresh() { + this.tokenListObserver.refresh(); + } + get element() { + return this.tokenListObserver.element; + } + get attributeName() { + return this.tokenListObserver.attributeName; + } + tokenMatched(token) { + const { + element + } = token; + const { + value + } = this.fetchParseResultForToken(token); + if (value) { + this.fetchValuesByTokenForElement(element).set(token, value); + this.delegate.elementMatchedValue(element, value); + } + } + tokenUnmatched(token) { + const { + element + } = token; + const { + value + } = this.fetchParseResultForToken(token); + if (value) { + this.fetchValuesByTokenForElement(element).delete(token); + this.delegate.elementUnmatchedValue(element, value); + } + } + fetchParseResultForToken(token) { + let parseResult = this.parseResultsByToken.get(token); + if (!parseResult) { + parseResult = this.parseToken(token); + this.parseResultsByToken.set(token, parseResult); + } + return parseResult; + } + fetchValuesByTokenForElement(element) { + let valuesByToken = this.valuesByTokenByElement.get(element); + if (!valuesByToken) { + valuesByToken = new Map(); + this.valuesByTokenByElement.set(element, valuesByToken); + } + return valuesByToken; + } + parseToken(token) { + try { + const value = this.delegate.parseValueForToken(token); + return { + value + }; + } catch (error) { + return { + error + }; + } + } + } + class BindingObserver { + constructor(context, delegate) { + this.context = context; + this.delegate = delegate; + this.bindingsByAction = new Map(); + } + start() { + if (!this.valueListObserver) { + this.valueListObserver = new ValueListObserver(this.element, this.actionAttribute, this); + this.valueListObserver.start(); + } + } + stop() { + if (this.valueListObserver) { + this.valueListObserver.stop(); + delete this.valueListObserver; + this.disconnectAllActions(); + } + } + get element() { + return this.context.element; + } + get identifier() { + return this.context.identifier; + } + get actionAttribute() { + return this.schema.actionAttribute; + } + get schema() { + return this.context.schema; + } + get bindings() { + return Array.from(this.bindingsByAction.values()); + } + connectAction(action) { + const binding = new Binding(this.context, action); + this.bindingsByAction.set(action, binding); + this.delegate.bindingConnected(binding); + } + disconnectAction(action) { + const binding = this.bindingsByAction.get(action); + if (binding) { + this.bindingsByAction.delete(action); + this.delegate.bindingDisconnected(binding); + } + } + disconnectAllActions() { + this.bindings.forEach(binding => this.delegate.bindingDisconnected(binding, true)); + this.bindingsByAction.clear(); + } + parseValueForToken(token) { + const action = Action.forToken(token, this.schema); + if (action.identifier == this.identifier) { + return action; + } + } + elementMatchedValue(element, action) { + this.connectAction(action); + } + elementUnmatchedValue(element, action) { + this.disconnectAction(action); + } + } + class ValueObserver { + constructor(context, receiver) { + this.context = context; + this.receiver = receiver; + this.stringMapObserver = new StringMapObserver(this.element, this); + this.valueDescriptorMap = this.controller.valueDescriptorMap; + } + start() { + this.stringMapObserver.start(); + this.invokeChangedCallbacksForDefaultValues(); + } + stop() { + this.stringMapObserver.stop(); + } + get element() { + return this.context.element; + } + get controller() { + return this.context.controller; + } + getStringMapKeyForAttribute(attributeName) { + if (attributeName in this.valueDescriptorMap) { + return this.valueDescriptorMap[attributeName].name; + } + } + stringMapKeyAdded(key, attributeName) { + const descriptor = this.valueDescriptorMap[attributeName]; + if (!this.hasValue(key)) { + this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), descriptor.writer(descriptor.defaultValue)); + } + } + stringMapValueChanged(value, name, oldValue) { + const descriptor = this.valueDescriptorNameMap[name]; + if (value === null) return; + if (oldValue === null) { + oldValue = descriptor.writer(descriptor.defaultValue); + } + this.invokeChangedCallback(name, value, oldValue); + } + stringMapKeyRemoved(key, attributeName, oldValue) { + const descriptor = this.valueDescriptorNameMap[key]; + if (this.hasValue(key)) { + this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), oldValue); + } else { + this.invokeChangedCallback(key, descriptor.writer(descriptor.defaultValue), oldValue); + } + } + invokeChangedCallbacksForDefaultValues() { + for (const { + key, + name, + defaultValue, + writer + } of this.valueDescriptors) { + if (defaultValue != undefined && !this.controller.data.has(key)) { + this.invokeChangedCallback(name, writer(defaultValue), undefined); + } + } + } + invokeChangedCallback(name, rawValue, rawOldValue) { + const changedMethodName = `${name}Changed`; + const changedMethod = this.receiver[changedMethodName]; + if (typeof changedMethod == "function") { + const descriptor = this.valueDescriptorNameMap[name]; + try { + const value = descriptor.reader(rawValue); + let oldValue = rawOldValue; + if (rawOldValue) { + oldValue = descriptor.reader(rawOldValue); + } + changedMethod.call(this.receiver, value, oldValue); + } catch (error) { + if (error instanceof TypeError) { + error.message = `Stimulus Value "${this.context.identifier}.${descriptor.name}" - ${error.message}`; + } + throw error; + } + } + } + get valueDescriptors() { + const { + valueDescriptorMap + } = this; + return Object.keys(valueDescriptorMap).map(key => valueDescriptorMap[key]); + } + get valueDescriptorNameMap() { + const descriptors = {}; + Object.keys(this.valueDescriptorMap).forEach(key => { + const descriptor = this.valueDescriptorMap[key]; + descriptors[descriptor.name] = descriptor; + }); + return descriptors; + } + hasValue(attributeName) { + const descriptor = this.valueDescriptorNameMap[attributeName]; + const hasMethodName = `has${capitalize(descriptor.name)}`; + return this.receiver[hasMethodName]; + } + } + class TargetObserver { + constructor(context, delegate) { + this.context = context; + this.delegate = delegate; + this.targetsByName = new Multimap(); + } + start() { + if (!this.tokenListObserver) { + this.tokenListObserver = new TokenListObserver(this.element, this.attributeName, this); + this.tokenListObserver.start(); + } + } + stop() { + if (this.tokenListObserver) { + this.disconnectAllTargets(); + this.tokenListObserver.stop(); + delete this.tokenListObserver; + } + } + tokenMatched({ + element, + content: name + }) { + if (this.scope.containsElement(element)) { + this.connectTarget(element, name); + } + } + tokenUnmatched({ + element, + content: name + }) { + this.disconnectTarget(element, name); + } + connectTarget(element, name) { + var _a; + if (!this.targetsByName.has(name, element)) { + this.targetsByName.add(name, element); + (_a = this.tokenListObserver) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.targetConnected(element, name)); + } + } + disconnectTarget(element, name) { + var _a; + if (this.targetsByName.has(name, element)) { + this.targetsByName.delete(name, element); + (_a = this.tokenListObserver) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.targetDisconnected(element, name)); + } + } + disconnectAllTargets() { + for (const name of this.targetsByName.keys) { + for (const element of this.targetsByName.getValuesForKey(name)) { + this.disconnectTarget(element, name); + } + } + } + get attributeName() { + return `data-${this.context.identifier}-target`; + } + get element() { + return this.context.element; + } + get scope() { + return this.context.scope; + } + } + function readInheritableStaticArrayValues(constructor, propertyName) { + const ancestors = getAncestorsForConstructor(constructor); + return Array.from(ancestors.reduce((values, constructor) => { + getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name)); + return values; + }, new Set())); + } + function readInheritableStaticObjectPairs(constructor, propertyName) { + const ancestors = getAncestorsForConstructor(constructor); + return ancestors.reduce((pairs, constructor) => { + pairs.push(...getOwnStaticObjectPairs(constructor, propertyName)); + return pairs; + }, []); + } + function getAncestorsForConstructor(constructor) { + const ancestors = []; + while (constructor) { + ancestors.push(constructor); + constructor = Object.getPrototypeOf(constructor); + } + return ancestors.reverse(); + } + function getOwnStaticArrayValues(constructor, propertyName) { + const definition = constructor[propertyName]; + return Array.isArray(definition) ? definition : []; + } + function getOwnStaticObjectPairs(constructor, propertyName) { + const definition = constructor[propertyName]; + return definition ? Object.keys(definition).map(key => [key, definition[key]]) : []; + } + class OutletObserver { + constructor(context, delegate) { + this.started = false; + this.context = context; + this.delegate = delegate; + this.outletsByName = new Multimap(); + this.outletElementsByName = new Multimap(); + this.selectorObserverMap = new Map(); + this.attributeObserverMap = new Map(); + } + start() { + if (!this.started) { + this.outletDefinitions.forEach(outletName => { + this.setupSelectorObserverForOutlet(outletName); + this.setupAttributeObserverForOutlet(outletName); + }); + this.started = true; + this.dependentContexts.forEach(context => context.refresh()); + } + } + refresh() { + this.selectorObserverMap.forEach(observer => observer.refresh()); + this.attributeObserverMap.forEach(observer => observer.refresh()); + } + stop() { + if (this.started) { + this.started = false; + this.disconnectAllOutlets(); + this.stopSelectorObservers(); + this.stopAttributeObservers(); + } + } + stopSelectorObservers() { + if (this.selectorObserverMap.size > 0) { + this.selectorObserverMap.forEach(observer => observer.stop()); + this.selectorObserverMap.clear(); + } + } + stopAttributeObservers() { + if (this.attributeObserverMap.size > 0) { + this.attributeObserverMap.forEach(observer => observer.stop()); + this.attributeObserverMap.clear(); + } + } + selectorMatched(element, _selector, { + outletName + }) { + const outlet = this.getOutlet(element, outletName); + if (outlet) { + this.connectOutlet(outlet, element, outletName); + } + } + selectorUnmatched(element, _selector, { + outletName + }) { + const outlet = this.getOutletFromMap(element, outletName); + if (outlet) { + this.disconnectOutlet(outlet, element, outletName); + } + } + selectorMatchElement(element, { + outletName + }) { + const selector = this.selector(outletName); + const hasOutlet = this.hasOutlet(element, outletName); + const hasOutletController = element.matches(`[${this.schema.controllerAttribute}~=${outletName}]`); + if (selector) { + return hasOutlet && hasOutletController && element.matches(selector); + } else { + return false; + } + } + elementMatchedAttribute(_element, attributeName) { + const outletName = this.getOutletNameFromOutletAttributeName(attributeName); + if (outletName) { + this.updateSelectorObserverForOutlet(outletName); + } + } + elementAttributeValueChanged(_element, attributeName) { + const outletName = this.getOutletNameFromOutletAttributeName(attributeName); + if (outletName) { + this.updateSelectorObserverForOutlet(outletName); + } + } + elementUnmatchedAttribute(_element, attributeName) { + const outletName = this.getOutletNameFromOutletAttributeName(attributeName); + if (outletName) { + this.updateSelectorObserverForOutlet(outletName); + } + } + connectOutlet(outlet, element, outletName) { + var _a; + if (!this.outletElementsByName.has(outletName, element)) { + this.outletsByName.add(outletName, outlet); + this.outletElementsByName.add(outletName, element); + (_a = this.selectorObserverMap.get(outletName)) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.outletConnected(outlet, element, outletName)); + } + } + disconnectOutlet(outlet, element, outletName) { + var _a; + if (this.outletElementsByName.has(outletName, element)) { + this.outletsByName.delete(outletName, outlet); + this.outletElementsByName.delete(outletName, element); + (_a = this.selectorObserverMap.get(outletName)) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.outletDisconnected(outlet, element, outletName)); + } + } + disconnectAllOutlets() { + for (const outletName of this.outletElementsByName.keys) { + for (const element of this.outletElementsByName.getValuesForKey(outletName)) { + for (const outlet of this.outletsByName.getValuesForKey(outletName)) { + this.disconnectOutlet(outlet, element, outletName); + } + } + } + } + updateSelectorObserverForOutlet(outletName) { + const observer = this.selectorObserverMap.get(outletName); + if (observer) { + observer.selector = this.selector(outletName); + } + } + setupSelectorObserverForOutlet(outletName) { + const selector = this.selector(outletName); + const selectorObserver = new SelectorObserver(document.body, selector, this, { + outletName + }); + this.selectorObserverMap.set(outletName, selectorObserver); + selectorObserver.start(); + } + setupAttributeObserverForOutlet(outletName) { + const attributeName = this.attributeNameForOutletName(outletName); + const attributeObserver = new AttributeObserver(this.scope.element, attributeName, this); + this.attributeObserverMap.set(outletName, attributeObserver); + attributeObserver.start(); + } + selector(outletName) { + return this.scope.outlets.getSelectorForOutletName(outletName); + } + attributeNameForOutletName(outletName) { + return this.scope.schema.outletAttributeForScope(this.identifier, outletName); + } + getOutletNameFromOutletAttributeName(attributeName) { + return this.outletDefinitions.find(outletName => this.attributeNameForOutletName(outletName) === attributeName); + } + get outletDependencies() { + const dependencies = new Multimap(); + this.router.modules.forEach(module => { + const constructor = module.definition.controllerConstructor; + const outlets = readInheritableStaticArrayValues(constructor, "outlets"); + outlets.forEach(outlet => dependencies.add(outlet, module.identifier)); + }); + return dependencies; + } + get outletDefinitions() { + return this.outletDependencies.getKeysForValue(this.identifier); + } + get dependentControllerIdentifiers() { + return this.outletDependencies.getValuesForKey(this.identifier); + } + get dependentContexts() { + const identifiers = this.dependentControllerIdentifiers; + return this.router.contexts.filter(context => identifiers.includes(context.identifier)); + } + hasOutlet(element, outletName) { + return !!this.getOutlet(element, outletName) || !!this.getOutletFromMap(element, outletName); + } + getOutlet(element, outletName) { + return this.application.getControllerForElementAndIdentifier(element, outletName); + } + getOutletFromMap(element, outletName) { + return this.outletsByName.getValuesForKey(outletName).find(outlet => outlet.element === element); + } + get scope() { + return this.context.scope; + } + get schema() { + return this.context.schema; + } + get identifier() { + return this.context.identifier; + } + get application() { + return this.context.application; + } + get router() { + return this.application.router; + } + } + class Context { + constructor(module, scope) { + this.logDebugActivity = (functionName, detail = {}) => { + const { + identifier, + controller, + element + } = this; + detail = Object.assign({ + identifier, + controller, + element + }, detail); + this.application.logDebugActivity(this.identifier, functionName, detail); + }; + this.module = module; + this.scope = scope; + this.controller = new module.controllerConstructor(this); + this.bindingObserver = new BindingObserver(this, this.dispatcher); + this.valueObserver = new ValueObserver(this, this.controller); + this.targetObserver = new TargetObserver(this, this); + this.outletObserver = new OutletObserver(this, this); + try { + this.controller.initialize(); + this.logDebugActivity("initialize"); + } catch (error) { + this.handleError(error, "initializing controller"); + } + } + connect() { + this.bindingObserver.start(); + this.valueObserver.start(); + this.targetObserver.start(); + this.outletObserver.start(); + try { + this.controller.connect(); + this.logDebugActivity("connect"); + } catch (error) { + this.handleError(error, "connecting controller"); + } + } + refresh() { + this.outletObserver.refresh(); + } + disconnect() { + try { + this.controller.disconnect(); + this.logDebugActivity("disconnect"); + } catch (error) { + this.handleError(error, "disconnecting controller"); + } + this.outletObserver.stop(); + this.targetObserver.stop(); + this.valueObserver.stop(); + this.bindingObserver.stop(); + } + get application() { + return this.module.application; + } + get identifier() { + return this.module.identifier; + } + get schema() { + return this.application.schema; + } + get dispatcher() { + return this.application.dispatcher; + } + get element() { + return this.scope.element; + } + get parentElement() { + return this.element.parentElement; + } + handleError(error, message, detail = {}) { + const { + identifier, + controller, + element + } = this; + detail = Object.assign({ + identifier, + controller, + element + }, detail); + this.application.handleError(error, `Error ${message}`, detail); + } + targetConnected(element, name) { + this.invokeControllerMethod(`${name}TargetConnected`, element); + } + targetDisconnected(element, name) { + this.invokeControllerMethod(`${name}TargetDisconnected`, element); + } + outletConnected(outlet, element, name) { + this.invokeControllerMethod(`${namespaceCamelize(name)}OutletConnected`, outlet, element); + } + outletDisconnected(outlet, element, name) { + this.invokeControllerMethod(`${namespaceCamelize(name)}OutletDisconnected`, outlet, element); + } + invokeControllerMethod(methodName, ...args) { + const controller = this.controller; + if (typeof controller[methodName] == "function") { + controller[methodName](...args); + } + } + } + function bless(constructor) { + return shadow(constructor, getBlessedProperties(constructor)); + } + function shadow(constructor, properties) { + const shadowConstructor = extend(constructor); + const shadowProperties = getShadowProperties(constructor.prototype, properties); + Object.defineProperties(shadowConstructor.prototype, shadowProperties); + return shadowConstructor; + } + function getBlessedProperties(constructor) { + const blessings = readInheritableStaticArrayValues(constructor, "blessings"); + return blessings.reduce((blessedProperties, blessing) => { + const properties = blessing(constructor); + for (const key in properties) { + const descriptor = blessedProperties[key] || {}; + blessedProperties[key] = Object.assign(descriptor, properties[key]); + } + return blessedProperties; + }, {}); + } + function getShadowProperties(prototype, properties) { + return getOwnKeys(properties).reduce((shadowProperties, key) => { + const descriptor = getShadowedDescriptor(prototype, properties, key); + if (descriptor) { + Object.assign(shadowProperties, { + [key]: descriptor + }); + } + return shadowProperties; + }, {}); + } + function getShadowedDescriptor(prototype, properties, key) { + const shadowingDescriptor = Object.getOwnPropertyDescriptor(prototype, key); + const shadowedByValue = shadowingDescriptor && "value" in shadowingDescriptor; + if (!shadowedByValue) { + const descriptor = Object.getOwnPropertyDescriptor(properties, key).value; + if (shadowingDescriptor) { + descriptor.get = shadowingDescriptor.get || descriptor.get; + descriptor.set = shadowingDescriptor.set || descriptor.set; + } + return descriptor; + } + } + const getOwnKeys = (() => { + if (typeof Object.getOwnPropertySymbols == "function") { + return object => [...Object.getOwnPropertyNames(object), ...Object.getOwnPropertySymbols(object)]; + } else { + return Object.getOwnPropertyNames; + } + })(); + const extend = (() => { + function extendWithReflect(constructor) { + function extended() { + return Reflect.construct(constructor, arguments, new.target); + } + extended.prototype = Object.create(constructor.prototype, { + constructor: { + value: extended + } + }); + Reflect.setPrototypeOf(extended, constructor); + return extended; + } + function testReflectExtension() { + const a = function () { + this.a.call(this); + }; + const b = extendWithReflect(a); + b.prototype.a = function () {}; + return new b(); + } + try { + testReflectExtension(); + return extendWithReflect; + } catch (error) { + return constructor => class extended extends constructor {}; + } + })(); + function blessDefinition(definition) { + return { + identifier: definition.identifier, + controllerConstructor: bless(definition.controllerConstructor) + }; + } + class Module { + constructor(application, definition) { + this.application = application; + this.definition = blessDefinition(definition); + this.contextsByScope = new WeakMap(); + this.connectedContexts = new Set(); + } + get identifier() { + return this.definition.identifier; + } + get controllerConstructor() { + return this.definition.controllerConstructor; + } + get contexts() { + return Array.from(this.connectedContexts); + } + connectContextForScope(scope) { + const context = this.fetchContextForScope(scope); + this.connectedContexts.add(context); + context.connect(); + } + disconnectContextForScope(scope) { + const context = this.contextsByScope.get(scope); + if (context) { + this.connectedContexts.delete(context); + context.disconnect(); + } + } + fetchContextForScope(scope) { + let context = this.contextsByScope.get(scope); + if (!context) { + context = new Context(this, scope); + this.contextsByScope.set(scope, context); + } + return context; + } + } + class ClassMap { + constructor(scope) { + this.scope = scope; + } + has(name) { + return this.data.has(this.getDataKey(name)); + } + get(name) { + return this.getAll(name)[0]; + } + getAll(name) { + const tokenString = this.data.get(this.getDataKey(name)) || ""; + return tokenize(tokenString); + } + getAttributeName(name) { + return this.data.getAttributeNameForKey(this.getDataKey(name)); + } + getDataKey(name) { + return `${name}-class`; + } + get data() { + return this.scope.data; + } + } + class DataMap { + constructor(scope) { + this.scope = scope; + } + get element() { + return this.scope.element; + } + get identifier() { + return this.scope.identifier; + } + get(key) { + const name = this.getAttributeNameForKey(key); + return this.element.getAttribute(name); + } + set(key, value) { + const name = this.getAttributeNameForKey(key); + this.element.setAttribute(name, value); + return this.get(key); + } + has(key) { + const name = this.getAttributeNameForKey(key); + return this.element.hasAttribute(name); + } + delete(key) { + if (this.has(key)) { + const name = this.getAttributeNameForKey(key); + this.element.removeAttribute(name); + return true; + } else { + return false; + } + } + getAttributeNameForKey(key) { + return `data-${this.identifier}-${dasherize(key)}`; + } + } + class Guide { + constructor(logger) { + this.warnedKeysByObject = new WeakMap(); + this.logger = logger; + } + warn(object, key, message) { + let warnedKeys = this.warnedKeysByObject.get(object); + if (!warnedKeys) { + warnedKeys = new Set(); + this.warnedKeysByObject.set(object, warnedKeys); + } + if (!warnedKeys.has(key)) { + warnedKeys.add(key); + this.logger.warn(message, object); + } + } + } + function attributeValueContainsToken(attributeName, token) { + return `[${attributeName}~="${token}"]`; + } + class TargetSet { + constructor(scope) { + this.scope = scope; + } + get element() { + return this.scope.element; + } + get identifier() { + return this.scope.identifier; + } + get schema() { + return this.scope.schema; + } + has(targetName) { + return this.find(targetName) != null; + } + find(...targetNames) { + return targetNames.reduce((target, targetName) => target || this.findTarget(targetName) || this.findLegacyTarget(targetName), undefined); + } + findAll(...targetNames) { + return targetNames.reduce((targets, targetName) => [...targets, ...this.findAllTargets(targetName), ...this.findAllLegacyTargets(targetName)], []); + } + findTarget(targetName) { + const selector = this.getSelectorForTargetName(targetName); + return this.scope.findElement(selector); + } + findAllTargets(targetName) { + const selector = this.getSelectorForTargetName(targetName); + return this.scope.findAllElements(selector); + } + getSelectorForTargetName(targetName) { + const attributeName = this.schema.targetAttributeForScope(this.identifier); + return attributeValueContainsToken(attributeName, targetName); + } + findLegacyTarget(targetName) { + const selector = this.getLegacySelectorForTargetName(targetName); + return this.deprecate(this.scope.findElement(selector), targetName); + } + findAllLegacyTargets(targetName) { + const selector = this.getLegacySelectorForTargetName(targetName); + return this.scope.findAllElements(selector).map(element => this.deprecate(element, targetName)); + } + getLegacySelectorForTargetName(targetName) { + const targetDescriptor = `${this.identifier}.${targetName}`; + return attributeValueContainsToken(this.schema.targetAttribute, targetDescriptor); + } + deprecate(element, targetName) { + if (element) { + const { + identifier + } = this; + const attributeName = this.schema.targetAttribute; + const revisedAttributeName = this.schema.targetAttributeForScope(identifier); + this.guide.warn(element, `target:${targetName}`, `Please replace ${attributeName}="${identifier}.${targetName}" with ${revisedAttributeName}="${targetName}". ` + `The ${attributeName} attribute is deprecated and will be removed in a future version of Stimulus.`); + } + return element; + } + get guide() { + return this.scope.guide; + } + } + class OutletSet { + constructor(scope, controllerElement) { + this.scope = scope; + this.controllerElement = controllerElement; + } + get element() { + return this.scope.element; + } + get identifier() { + return this.scope.identifier; + } + get schema() { + return this.scope.schema; + } + has(outletName) { + return this.find(outletName) != null; + } + find(...outletNames) { + return outletNames.reduce((outlet, outletName) => outlet || this.findOutlet(outletName), undefined); + } + findAll(...outletNames) { + return outletNames.reduce((outlets, outletName) => [...outlets, ...this.findAllOutlets(outletName)], []); + } + getSelectorForOutletName(outletName) { + const attributeName = this.schema.outletAttributeForScope(this.identifier, outletName); + return this.controllerElement.getAttribute(attributeName); + } + findOutlet(outletName) { + const selector = this.getSelectorForOutletName(outletName); + if (selector) return this.findElement(selector, outletName); + } + findAllOutlets(outletName) { + const selector = this.getSelectorForOutletName(outletName); + return selector ? this.findAllElements(selector, outletName) : []; + } + findElement(selector, outletName) { + const elements = this.scope.queryElements(selector); + return elements.filter(element => this.matchesElement(element, selector, outletName))[0]; + } + findAllElements(selector, outletName) { + const elements = this.scope.queryElements(selector); + return elements.filter(element => this.matchesElement(element, selector, outletName)); + } + matchesElement(element, selector, outletName) { + const controllerAttribute = element.getAttribute(this.scope.schema.controllerAttribute) || ""; + return element.matches(selector) && controllerAttribute.split(" ").includes(outletName); + } + } + class Scope { + constructor(schema, element, identifier, logger) { + this.targets = new TargetSet(this); + this.classes = new ClassMap(this); + this.data = new DataMap(this); + this.containsElement = element => { + return element.closest(this.controllerSelector) === this.element; + }; + this.schema = schema; + this.element = element; + this.identifier = identifier; + this.guide = new Guide(logger); + this.outlets = new OutletSet(this.documentScope, element); + } + findElement(selector) { + return this.element.matches(selector) ? this.element : this.queryElements(selector).find(this.containsElement); + } + findAllElements(selector) { + return [...(this.element.matches(selector) ? [this.element] : []), ...this.queryElements(selector).filter(this.containsElement)]; + } + queryElements(selector) { + return Array.from(this.element.querySelectorAll(selector)); + } + get controllerSelector() { + return attributeValueContainsToken(this.schema.controllerAttribute, this.identifier); + } + get isDocumentScope() { + return this.element === document.documentElement; + } + get documentScope() { + return this.isDocumentScope ? this : new Scope(this.schema, document.documentElement, this.identifier, this.guide.logger); + } + } + class ScopeObserver { + constructor(element, schema, delegate) { + this.element = element; + this.schema = schema; + this.delegate = delegate; + this.valueListObserver = new ValueListObserver(this.element, this.controllerAttribute, this); + this.scopesByIdentifierByElement = new WeakMap(); + this.scopeReferenceCounts = new WeakMap(); + } + start() { + this.valueListObserver.start(); + } + stop() { + this.valueListObserver.stop(); + } + get controllerAttribute() { + return this.schema.controllerAttribute; + } + parseValueForToken(token) { + const { + element, + content: identifier + } = token; + return this.parseValueForElementAndIdentifier(element, identifier); + } + parseValueForElementAndIdentifier(element, identifier) { + const scopesByIdentifier = this.fetchScopesByIdentifierForElement(element); + let scope = scopesByIdentifier.get(identifier); + if (!scope) { + scope = this.delegate.createScopeForElementAndIdentifier(element, identifier); + scopesByIdentifier.set(identifier, scope); + } + return scope; + } + elementMatchedValue(element, value) { + const referenceCount = (this.scopeReferenceCounts.get(value) || 0) + 1; + this.scopeReferenceCounts.set(value, referenceCount); + if (referenceCount == 1) { + this.delegate.scopeConnected(value); + } + } + elementUnmatchedValue(element, value) { + const referenceCount = this.scopeReferenceCounts.get(value); + if (referenceCount) { + this.scopeReferenceCounts.set(value, referenceCount - 1); + if (referenceCount == 1) { + this.delegate.scopeDisconnected(value); + } + } + } + fetchScopesByIdentifierForElement(element) { + let scopesByIdentifier = this.scopesByIdentifierByElement.get(element); + if (!scopesByIdentifier) { + scopesByIdentifier = new Map(); + this.scopesByIdentifierByElement.set(element, scopesByIdentifier); + } + return scopesByIdentifier; + } + } + class Router { + constructor(application) { + this.application = application; + this.scopeObserver = new ScopeObserver(this.element, this.schema, this); + this.scopesByIdentifier = new Multimap(); + this.modulesByIdentifier = new Map(); + } + get element() { + return this.application.element; + } + get schema() { + return this.application.schema; + } + get logger() { + return this.application.logger; + } + get controllerAttribute() { + return this.schema.controllerAttribute; + } + get modules() { + return Array.from(this.modulesByIdentifier.values()); + } + get contexts() { + return this.modules.reduce((contexts, module) => contexts.concat(module.contexts), []); + } + start() { + this.scopeObserver.start(); + } + stop() { + this.scopeObserver.stop(); + } + loadDefinition(definition) { + this.unloadIdentifier(definition.identifier); + const module = new Module(this.application, definition); + this.connectModule(module); + const afterLoad = definition.controllerConstructor.afterLoad; + if (afterLoad) { + afterLoad.call(definition.controllerConstructor, definition.identifier, this.application); + } + } + unloadIdentifier(identifier) { + const module = this.modulesByIdentifier.get(identifier); + if (module) { + this.disconnectModule(module); + } + } + getContextForElementAndIdentifier(element, identifier) { + const module = this.modulesByIdentifier.get(identifier); + if (module) { + return module.contexts.find(context => context.element == element); + } + } + proposeToConnectScopeForElementAndIdentifier(element, identifier) { + const scope = this.scopeObserver.parseValueForElementAndIdentifier(element, identifier); + if (scope) { + this.scopeObserver.elementMatchedValue(scope.element, scope); + } else { + console.error(`Couldn't find or create scope for identifier: "${identifier}" and element:`, element); + } + } + handleError(error, message, detail) { + this.application.handleError(error, message, detail); + } + createScopeForElementAndIdentifier(element, identifier) { + return new Scope(this.schema, element, identifier, this.logger); + } + scopeConnected(scope) { + this.scopesByIdentifier.add(scope.identifier, scope); + const module = this.modulesByIdentifier.get(scope.identifier); + if (module) { + module.connectContextForScope(scope); + } + } + scopeDisconnected(scope) { + this.scopesByIdentifier.delete(scope.identifier, scope); + const module = this.modulesByIdentifier.get(scope.identifier); + if (module) { + module.disconnectContextForScope(scope); + } + } + connectModule(module) { + this.modulesByIdentifier.set(module.identifier, module); + const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier); + scopes.forEach(scope => module.connectContextForScope(scope)); + } + disconnectModule(module) { + this.modulesByIdentifier.delete(module.identifier); + const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier); + scopes.forEach(scope => module.disconnectContextForScope(scope)); + } + } + const defaultSchema = { + controllerAttribute: "data-controller", + actionAttribute: "data-action", + targetAttribute: "data-target", + targetAttributeForScope: identifier => `data-${identifier}-target`, + outletAttributeForScope: (identifier, outlet) => `data-${identifier}-${outlet}-outlet`, + keyMappings: Object.assign(Object.assign({ + enter: "Enter", + tab: "Tab", + esc: "Escape", + space: " ", + up: "ArrowUp", + down: "ArrowDown", + left: "ArrowLeft", + right: "ArrowRight", + home: "Home", + end: "End", + page_up: "PageUp", + page_down: "PageDown" + }, objectFromEntries("abcdefghijklmnopqrstuvwxyz".split("").map(c => [c, c]))), objectFromEntries("0123456789".split("").map(n => [n, n]))) + }; + function objectFromEntries(array) { + return array.reduce((memo, [k, v]) => Object.assign(Object.assign({}, memo), { + [k]: v + }), {}); + } + class Application { + constructor(element = document.documentElement, schema = defaultSchema) { + this.logger = console; + this.debug = false; + this.logDebugActivity = (identifier, functionName, detail = {}) => { + if (this.debug) { + this.logFormattedMessage(identifier, functionName, detail); + } + }; + this.element = element; + this.schema = schema; + this.dispatcher = new Dispatcher(this); + this.router = new Router(this); + this.actionDescriptorFilters = Object.assign({}, defaultActionDescriptorFilters); + } + static start(element, schema) { + const application = new this(element, schema); + application.start(); + return application; + } + async start() { + await domReady(); + this.logDebugActivity("application", "starting"); + this.dispatcher.start(); + this.router.start(); + this.logDebugActivity("application", "start"); + } + stop() { + this.logDebugActivity("application", "stopping"); + this.dispatcher.stop(); + this.router.stop(); + this.logDebugActivity("application", "stop"); + } + register(identifier, controllerConstructor) { + this.load({ + identifier, + controllerConstructor + }); + } + registerActionOption(name, filter) { + this.actionDescriptorFilters[name] = filter; + } + load(head, ...rest) { + const definitions = Array.isArray(head) ? head : [head, ...rest]; + definitions.forEach(definition => { + if (definition.controllerConstructor.shouldLoad) { + this.router.loadDefinition(definition); + } + }); + } + unload(head, ...rest) { + const identifiers = Array.isArray(head) ? head : [head, ...rest]; + identifiers.forEach(identifier => this.router.unloadIdentifier(identifier)); + } + get controllers() { + return this.router.contexts.map(context => context.controller); + } + getControllerForElementAndIdentifier(element, identifier) { + const context = this.router.getContextForElementAndIdentifier(element, identifier); + return context ? context.controller : null; + } + handleError(error, message, detail) { + var _a; + this.logger.error(`%s\n\n%o\n\n%o`, message, error, detail); + (_a = window.onerror) === null || _a === void 0 ? void 0 : _a.call(window, message, "", 0, 0, error); + } + logFormattedMessage(identifier, functionName, detail = {}) { + detail = Object.assign({ + application: this + }, detail); + this.logger.groupCollapsed(`${identifier} #${functionName}`); + this.logger.log("details:", Object.assign({}, detail)); + this.logger.groupEnd(); + } + } + function domReady() { + return new Promise(resolve => { + if (document.readyState == "loading") { + document.addEventListener("DOMContentLoaded", () => resolve()); + } else { + resolve(); + } + }); + } + function ClassPropertiesBlessing(constructor) { + const classes = readInheritableStaticArrayValues(constructor, "classes"); + return classes.reduce((properties, classDefinition) => { + return Object.assign(properties, propertiesForClassDefinition(classDefinition)); + }, {}); + } + function propertiesForClassDefinition(key) { + return { + [`${key}Class`]: { + get() { + const { + classes + } = this; + if (classes.has(key)) { + return classes.get(key); + } else { + const attribute = classes.getAttributeName(key); + throw new Error(`Missing attribute "${attribute}"`); + } + } + }, + [`${key}Classes`]: { + get() { + return this.classes.getAll(key); + } + }, + [`has${capitalize(key)}Class`]: { + get() { + return this.classes.has(key); + } + } + }; + } + function OutletPropertiesBlessing(constructor) { + const outlets = readInheritableStaticArrayValues(constructor, "outlets"); + return outlets.reduce((properties, outletDefinition) => { + return Object.assign(properties, propertiesForOutletDefinition(outletDefinition)); + }, {}); + } + function getOutletController(controller, element, identifier) { + return controller.application.getControllerForElementAndIdentifier(element, identifier); + } + function getControllerAndEnsureConnectedScope(controller, element, outletName) { + let outletController = getOutletController(controller, element, outletName); + if (outletController) return outletController; + controller.application.router.proposeToConnectScopeForElementAndIdentifier(element, outletName); + outletController = getOutletController(controller, element, outletName); + if (outletController) return outletController; + } + function propertiesForOutletDefinition(name) { + const camelizedName = namespaceCamelize(name); + return { + [`${camelizedName}Outlet`]: { + get() { + const outletElement = this.outlets.find(name); + const selector = this.outlets.getSelectorForOutletName(name); + if (outletElement) { + const outletController = getControllerAndEnsureConnectedScope(this, outletElement, name); + if (outletController) return outletController; + throw new Error(`The provided outlet element is missing an outlet controller "${name}" instance for host controller "${this.identifier}"`); + } + throw new Error(`Missing outlet element "${name}" for host controller "${this.identifier}". Stimulus couldn't find a matching outlet element using selector "${selector}".`); + } + }, + [`${camelizedName}Outlets`]: { + get() { + const outlets = this.outlets.findAll(name); + if (outlets.length > 0) { + return outlets.map(outletElement => { + const outletController = getControllerAndEnsureConnectedScope(this, outletElement, name); + if (outletController) return outletController; + console.warn(`The provided outlet element is missing an outlet controller "${name}" instance for host controller "${this.identifier}"`, outletElement); + }).filter(controller => controller); + } + return []; + } + }, + [`${camelizedName}OutletElement`]: { + get() { + const outletElement = this.outlets.find(name); + const selector = this.outlets.getSelectorForOutletName(name); + if (outletElement) { + return outletElement; + } else { + throw new Error(`Missing outlet element "${name}" for host controller "${this.identifier}". Stimulus couldn't find a matching outlet element using selector "${selector}".`); + } + } + }, + [`${camelizedName}OutletElements`]: { + get() { + return this.outlets.findAll(name); + } + }, + [`has${capitalize(camelizedName)}Outlet`]: { + get() { + return this.outlets.has(name); + } + } + }; + } + function TargetPropertiesBlessing(constructor) { + const targets = readInheritableStaticArrayValues(constructor, "targets"); + return targets.reduce((properties, targetDefinition) => { + return Object.assign(properties, propertiesForTargetDefinition(targetDefinition)); + }, {}); + } + function propertiesForTargetDefinition(name) { + return { + [`${name}Target`]: { + get() { + const target = this.targets.find(name); + if (target) { + return target; + } else { + throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`); + } + } + }, + [`${name}Targets`]: { + get() { + return this.targets.findAll(name); + } + }, + [`has${capitalize(name)}Target`]: { + get() { + return this.targets.has(name); + } + } + }; + } + function ValuePropertiesBlessing(constructor) { + const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values"); + const propertyDescriptorMap = { + valueDescriptorMap: { + get() { + return valueDefinitionPairs.reduce((result, valueDefinitionPair) => { + const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair, this.identifier); + const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key); + return Object.assign(result, { + [attributeName]: valueDescriptor + }); + }, {}); + } + } + }; + return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => { + return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair)); + }, propertyDescriptorMap); + } + function propertiesForValueDefinitionPair(valueDefinitionPair, controller) { + const definition = parseValueDefinitionPair(valueDefinitionPair, controller); + const { + key, + name, + reader: read, + writer: write + } = definition; + return { + [name]: { + get() { + const value = this.data.get(key); + if (value !== null) { + return read(value); + } else { + return definition.defaultValue; + } + }, + set(value) { + if (value === undefined) { + this.data.delete(key); + } else { + this.data.set(key, write(value)); + } + } + }, + [`has${capitalize(name)}`]: { + get() { + return this.data.has(key) || definition.hasCustomDefaultValue; + } + } + }; + } + function parseValueDefinitionPair([token, typeDefinition], controller) { + return valueDescriptorForTokenAndTypeDefinition({ + controller, + token, + typeDefinition + }); + } + function parseValueTypeConstant(constant) { + switch (constant) { + case Array: + return "array"; + case Boolean: + return "boolean"; + case Number: + return "number"; + case Object: + return "object"; + case String: + return "string"; + } + } + function parseValueTypeDefault(defaultValue) { + switch (typeof defaultValue) { + case "boolean": + return "boolean"; + case "number": + return "number"; + case "string": + return "string"; + } + if (Array.isArray(defaultValue)) return "array"; + if (Object.prototype.toString.call(defaultValue) === "[object Object]") return "object"; + } + function parseValueTypeObject(payload) { + const { + controller, + token, + typeObject + } = payload; + const hasType = isSomething(typeObject.type); + const hasDefault = isSomething(typeObject.default); + const fullObject = hasType && hasDefault; + const onlyType = hasType && !hasDefault; + const onlyDefault = !hasType && hasDefault; + const typeFromObject = parseValueTypeConstant(typeObject.type); + const typeFromDefaultValue = parseValueTypeDefault(payload.typeObject.default); + if (onlyType) return typeFromObject; + if (onlyDefault) return typeFromDefaultValue; + if (typeFromObject !== typeFromDefaultValue) { + const propertyPath = controller ? `${controller}.${token}` : token; + throw new Error(`The specified default value for the Stimulus Value "${propertyPath}" must match the defined type "${typeFromObject}". The provided default value of "${typeObject.default}" is of type "${typeFromDefaultValue}".`); + } + if (fullObject) return typeFromObject; + } + function parseValueTypeDefinition(payload) { + const { + controller, + token, + typeDefinition + } = payload; + const typeObject = { + controller, + token, + typeObject: typeDefinition + }; + const typeFromObject = parseValueTypeObject(typeObject); + const typeFromDefaultValue = parseValueTypeDefault(typeDefinition); + const typeFromConstant = parseValueTypeConstant(typeDefinition); + const type = typeFromObject || typeFromDefaultValue || typeFromConstant; + if (type) return type; + const propertyPath = controller ? `${controller}.${typeDefinition}` : token; + throw new Error(`Unknown value type "${propertyPath}" for "${token}" value`); + } + function defaultValueForDefinition(typeDefinition) { + const constant = parseValueTypeConstant(typeDefinition); + if (constant) return defaultValuesByType[constant]; + const hasDefault = hasProperty(typeDefinition, "default"); + const hasType = hasProperty(typeDefinition, "type"); + const typeObject = typeDefinition; + if (hasDefault) return typeObject.default; + if (hasType) { + const { + type + } = typeObject; + const constantFromType = parseValueTypeConstant(type); + if (constantFromType) return defaultValuesByType[constantFromType]; + } + return typeDefinition; + } + function valueDescriptorForTokenAndTypeDefinition(payload) { + const { + token, + typeDefinition + } = payload; + const key = `${dasherize(token)}-value`; + const type = parseValueTypeDefinition(payload); + return { + type, + key, + name: camelize(key), + get defaultValue() { + return defaultValueForDefinition(typeDefinition); + }, + get hasCustomDefaultValue() { + return parseValueTypeDefault(typeDefinition) !== undefined; + }, + reader: readers[type], + writer: writers[type] || writers.default + }; + } + const defaultValuesByType = { + get array() { + return []; + }, + boolean: false, + number: 0, + get object() { + return {}; + }, + string: "" + }; + const readers = { + array(value) { + const array = JSON.parse(value); + if (!Array.isArray(array)) { + throw new TypeError(`expected value of type "array" but instead got value "${value}" of type "${parseValueTypeDefault(array)}"`); + } + return array; + }, + boolean(value) { + return !(value == "0" || String(value).toLowerCase() == "false"); + }, + number(value) { + return Number(value.replace(/_/g, "")); + }, + object(value) { + const object = JSON.parse(value); + if (object === null || typeof object != "object" || Array.isArray(object)) { + throw new TypeError(`expected value of type "object" but instead got value "${value}" of type "${parseValueTypeDefault(object)}"`); + } + return object; + }, + string(value) { + return value; + } + }; + const writers = { + default: writeString, + array: writeJSON, + object: writeJSON + }; + function writeJSON(value) { + return JSON.stringify(value); + } + function writeString(value) { + return `${value}`; + } + class Controller { + constructor(context) { + this.context = context; + } + static get shouldLoad() { + return true; + } + static afterLoad(_identifier, _application) { + return; + } + get application() { + return this.context.application; + } + get scope() { + return this.context.scope; + } + get element() { + return this.scope.element; + } + get identifier() { + return this.scope.identifier; + } + get targets() { + return this.scope.targets; + } + get outlets() { + return this.scope.outlets; + } + get classes() { + return this.scope.classes; + } + get data() { + return this.scope.data; + } + initialize() {} + connect() {} + disconnect() {} + dispatch(eventName, { + target = this.element, + detail = {}, + prefix = this.identifier, + bubbles = true, + cancelable = true + } = {}) { + const type = prefix ? `${prefix}:${eventName}` : eventName; + const event = new CustomEvent(type, { + detail, + bubbles, + cancelable + }); + target.dispatchEvent(event); + return event; + } + } + Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing, OutletPropertiesBlessing]; + Controller.targets = []; + Controller.outlets = []; + Controller.values = {}; + + const arrayHasValue = (value, otherValues) => { + for (let i = 0, l = otherValues.length; i < l; i++) { + if (value === otherValues[i]) { + return true; + } + } + return false; + }; + const createElementFromHTML = html => { + const element = document.createElement('div'); + element.innerHTML = html; + return element.firstChild; + }; + const isDOMCollection = target => { + return Array.isArray(target) || typeof NodeList !== 'undefined' && target instanceof NodeList || typeof HTMLCollection !== 'undefined' && target instanceof HTMLCollection || typeof RadioNodeList !== 'undefined' && target instanceof RadioNodeList; + }; + const isDOMElement = target => { + return target != null && target.nodeType === 1; + }; + const getDOMElements = target => { + if (target == null) { + return []; + } + if (isDOMElement(target)) { + return [target]; + } + if (isDOMCollection(target)) { + return Array.from(target).filter(isDOMElement); + } + return []; + }; + const isFormElement = element => { + return element.tagName === 'FORM'; + }; + const isVisible = element => { + return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length); + }; + const isValuePresent = value => { + return !/^\s*$/.test(value || ''); + }; + + const ClientSideValidations = { + callbacks: { + element: { + after: (element, eventData) => {}, + before: (element, eventData) => {}, + fail: (element, message, addError, eventData) => addError(), + pass: (element, removeError, eventData) => removeError() + }, + form: { + after: (form, eventData) => {}, + before: (form, eventData) => {}, + fail: (form, eventData) => {}, + pass: (form, eventData) => {} + } + }, + formBuilders: { + 'ActionView::Helpers::FormBuilder': { + add: (element, settings, message) => { + if (!element) { + return; + } + const form = element.form; + const inputErrorTemplate = createElementFromHTML(settings.input_tag); + let inputErrorElement = element.closest(".".concat(inputErrorTemplate.getAttribute('class').replace(/ /g, '.'))); + if (!inputErrorElement) { + inputErrorElement = inputErrorTemplate; + if (element.getAttribute('autofocus')) { + element.setAttribute('autofocus', false); + } + element.before(inputErrorElement); + inputErrorElement.querySelector('span#input_tag').replaceWith(element); + const inputErrorLabelMessageElement = inputErrorElement.querySelector('label.message'); + if (inputErrorLabelMessageElement) { + inputErrorLabelMessageElement.setAttribute('for', element.id); + } + } + const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); + if (labelElement) { + const labelErrorTemplate = createElementFromHTML(settings.label_tag); + const labelErrorContainer = labelElement.closest(".".concat(labelErrorTemplate.getAttribute('class').replace(/ /g, '.'))); + if (!labelErrorContainer) { + labelElement.after(labelErrorTemplate); + labelErrorTemplate.querySelector('label#label_tag').replaceWith(labelElement); + } + } + const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); + if (labelMessageElement) { + labelMessageElement.textContent = message; + } + }, + remove: (element, settings) => { + if (!element) { + return; + } + const form = element.form; + const inputErrorClass = createElementFromHTML(settings.input_tag).getAttribute('class'); + const inputErrorElement = element.closest(".".concat(inputErrorClass.replace(/ /g, '.'))); + if (inputErrorElement) { + inputErrorElement.querySelector("#".concat(element.id)).remove(); + inputErrorElement.replaceWith(element); + } + const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); + if (labelElement) { + const labelErrorClass = createElementFromHTML(settings.label_tag).getAttribute('class'); + const labelErrorElement = labelElement.closest(".".concat(labelErrorClass.replace(/ /g, '.'))); + if (labelErrorElement) { + labelErrorElement.replaceWith(labelElement); + } + } + const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); + if (labelMessageElement) { + labelMessageElement.remove(); + } + } + } + }, + patterns: { + numericality: { + default: /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/, + only_integer: /^[+-]?\d+$/ + } + }, + validators: { + all: () => { + return { + ...ClientSideValidations.validators.local, + ...ClientSideValidations.validators.remote + }; + }, + local: {}, + remote: {} + } + }; + + function _assertClassBrand(e, t, n) { + if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; + throw new TypeError("Private element is not present on this object"); + } + function _checkPrivateRedeclaration(e, t) { + if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + } + function _classPrivateMethodInitSpec(e, a) { + _checkPrivateRedeclaration(e, a), a.add(e); + } + function _defineProperty(e, r, t) { + return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { + value: t, + enumerable: true, + configurable: true, + writable: true + }) : e[r] = t, e; + } + function _toPrimitive(t, r) { + if ("object" != typeof t || !t) return t; + var e = t[Symbol.toPrimitive]; + if (void 0 !== e) { + var i = e.call(t, r); + if ("object" != typeof i) return i; + throw new TypeError("@@toPrimitive must return a primitive value."); + } + return ("string" === r ? String : Number)(t); + } + function _toPropertyKey(t) { + var i = _toPrimitive(t, "string"); + return "symbol" == typeof i ? i : i + ""; + } + + const boundEventListeners = new WeakMap(); + const addBoundEventListener = (element, eventName, listener) => { + element.addEventListener(eventName, listener); + const listeners = boundEventListeners.get(element) || []; + listeners.push({ + eventName, + listener + }); + boundEventListeners.set(element, listeners); + }; + const bindElementEvents = (element, eventsToBind) => { + for (const eventName in eventsToBind) { + addBoundEventListener(element, eventName, eventsToBind[eventName]); + } + }; + const clearBoundEventListeners = element => { + const listeners = boundEventListeners.get(element); + if (!listeners) { + return; + } + listeners.forEach(_ref => { + let { + eventName, + listener + } = _ref; + element.removeEventListener(eventName, listener); + }); + boundEventListeners.delete(element); + }; + const dispatchCustomEvent = (element, eventName, detail) => { + element.dispatchEvent(new CustomEvent(eventName, { + bubbles: true, + detail + })); + }; + + const cleanNestedElementName = (elementName, nestedMatches, validators) => { + for (const validatorName in validators) { + if (validatorName.match("\\[".concat(nestedMatches[1], "\\].*\\[\\]\\[").concat(nestedMatches[2], "\\]$"))) { + elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]'); + } + } + return elementName; + }; + const cleanElementName = (elementName, validators) => { + elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); + const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); + if (nestedMatches) { + elementName = cleanNestedElementName(elementName, nestedMatches, validators); + } + return elementName; + }; + const validatorsFor = (elementName, validators) => { + if (!elementName || !validators) { + return {}; + } + if (Object.prototype.hasOwnProperty.call(validators, elementName)) { + return validators[elementName]; + } + return validators[cleanElementName(elementName, validators)] || {}; + }; + const getValidationInputs = form => { + return Array.from(form.elements).filter(element => { + if (element.dataset.csvValidate == null || element.disabled) { + return false; + } + return isVisible(element); + }); + }; + const passElement = element => { + dispatchCustomEvent(element, 'element:validate:pass'); + delete element.dataset.csvValid; + }; + const failElement = (element, message) => { + dispatchCustomEvent(element, 'element:validate:fail', message); + element.dataset.csvValid = 'false'; + }; + const afterValidate = element => { + dispatchCustomEvent(element, 'element:validate:after'); + return element.dataset.csvValid !== 'false'; + }; + const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { + for (const validatorOption in validatorOptions) { + if (!validatorOptions[validatorOption]) { + continue; + } + const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]); + if (message) { + failElement(element, message); + return false; + } + } + return true; + }; + const executeValidators = (validatorFunctions, element, validators) => { + for (const validator in validators) { + if (!validatorFunctions[validator]) { + continue; + } + if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { + return false; + } + } + return true; + }; + const isMarkedForDestroy = element => { + const elementName = element.name; + const form = element.form; + if (form && /\[([^\]]*?)\]$/.test(elementName)) { + const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]'); + const destroyInputElement = form.querySelector("input[name=\"".concat(destroyInputName, "\"]")); + if (destroyInputElement && destroyInputElement.value === '1') { + return true; + } + } + return false; + }; + const executeAllValidators = (element, validators) => { + if (element.dataset.csvChanged === 'false' || element.disabled) { + return; + } + element.dataset.csvChanged = 'false'; + if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { + passElement(element); + } + }; + const validateElement = (element, validators) => { + dispatchCustomEvent(element, 'element:validate:before'); + if (isMarkedForDestroy(element)) { + passElement(element); + } else { + executeAllValidators(element, validators); + } + return afterValidate(element); + }; + const validateForm = (form, validators) => { + let valid = true; + dispatchCustomEvent(form, 'form:validate:before'); + getValidationInputs(form).forEach(element => { + if (!validateElement(element, validatorsFor(element.name, validators))) { + valid = false; + } + }); + if (valid) { + dispatchCustomEvent(form, 'form:validate:pass'); + } else { + dispatchCustomEvent(form, 'form:validate:fail'); + } + dispatchCustomEvent(form, 'form:validate:after'); + return valid; + }; + const isValid$1 = (target, validators) => { + const element = getDOMElements(target)[0]; + if (!element) { + return true; + } + if (!validators) { + var _form$ClientSideValid; + const form = isFormElement(element) ? element : element.form; + validators = form === null || form === void 0 || (_form$ClientSideValid = form.ClientSideValidations) === null || _form$ClientSideValid === void 0 || (_form$ClientSideValid = _form$ClientSideValid.settings) === null || _form$ClientSideValid === void 0 ? void 0 : _form$ClientSideValid.validators; + } + if (isFormElement(element)) { + return validateForm(element, validators || {}); + } + return validateElement(element, validatorsFor(element.name, validators || {})); + }; + + const buildErrorHelpers = settings => ({ + addError: (element, message) => ClientSideValidations.formBuilders[settings.html_settings.type].add(element, settings.html_settings, message), + removeError: element => ClientSideValidations.formBuilders[settings.html_settings.type].remove(element, settings.html_settings) + }); + const installFormContext = (form, settings) => { + const { + addError, + removeError + } = buildErrorHelpers(settings); + form.ClientSideValidations = { + settings, + addError, + removeError + }; + }; + const removeFormContext = form => { + delete form.ClientSideValidations; + }; + const bindFormEvents = form => { + bindElementEvents(form, { + submit: event => { + if (!isValid$1(form, form.ClientSideValidations.settings.validators)) { + event.preventDefault(); + event.stopImmediatePropagation(); + } + }, + 'form:validate:after': event => ClientSideValidations.callbacks.form.after(form, event), + 'form:validate:before': event => ClientSideValidations.callbacks.form.before(form, event), + 'form:validate:fail': event => ClientSideValidations.callbacks.form.fail(form, event), + 'form:validate:pass': event => ClientSideValidations.callbacks.form.pass(form, event) + }); + }; + const bindInputEvents = (form, input) => { + input.dataset.csvValidate = 'true'; + bindElementEvents(input, { + focusout: function () { + isValid$1(this, form.ClientSideValidations.settings.validators); + }, + change: function () { + this.dataset.csvChanged = 'true'; + }, + 'element:validate:after': function (event) { + ClientSideValidations.callbacks.element.after(this, event); + }, + 'element:validate:before': function (event) { + ClientSideValidations.callbacks.element.before(this, event); + }, + 'element:validate:fail': function (event) { + const element = this; + const message = event.detail; + ClientSideValidations.callbacks.element.fail(element, message, () => { + form.ClientSideValidations.addError(element, message); + }, event); + }, + 'element:validate:pass': function (event) { + const element = this; + ClientSideValidations.callbacks.element.pass(element, () => { + form.ClientSideValidations.removeError(element); + }, event); + } + }); + if (input.type === 'checkbox') { + bindElementEvents(input, { + change: function () { + isValid$1(this, form.ClientSideValidations.settings.validators); + } + }); + } + }; + const bindConfirmationEvents = (target, confirmation, form) => { + bindElementEvents(confirmation, { + focusout: () => { + target.dataset.csvChanged = 'true'; + isValid$1(target, form.ClientSideValidations.settings.validators); + }, + keyup: () => { + target.dataset.csvChanged = 'true'; + isValid$1(target, form.ClientSideValidations.settings.validators); + } + }); + }; + const unbindElement = element => { + clearBoundEventListeners(element); + delete element.dataset.csvValid; + delete element.dataset.csvChanged; + delete element.dataset.csvValidate; + }; + + var _Class_brand = /*#__PURE__*/new WeakSet(); + class _Class extends Controller { + constructor() { + super(...arguments); + _classPrivateMethodInitSpec(this, _Class_brand); + } + connect() { + this.element.noValidate = true; + installFormContext(this.element, this.settingsValue); + bindFormEvents(this.element); + } + disconnect() { + unbindElement(this.element); + this.inputTargets.forEach(input => { + unbindElement(input); + }); + this.confirmationTargets.forEach(confirmation => { + unbindElement(confirmation); + }); + removeFormContext(this.element); + } + inputTargetConnected(input) { + if (input.type === 'radio' || !input.form || input.form !== this.element) { + return; + } + bindInputEvents(this.element, input); + } + inputTargetDisconnected(input) { + unbindElement(input); + } + confirmationTargetConnected(confirmation) { + if (!confirmation.form || confirmation.form !== this.element) { + return; + } + const partner = _assertClassBrand(_Class_brand, this, _findConfirmationPartner).call(this, confirmation); + if (partner) { + bindConfirmationEvents(partner, confirmation, this.element); + } + } + confirmationTargetDisconnected(confirmation) { + unbindElement(confirmation); + } + validate() { + return isValid$1(this.element, this.settingsValue.validators); + } + validateElement(element) { + return isValid$1(element, this.settingsValue.validators); + } + } + function _findConfirmationPartner(confirmation) { + const partnerId = confirmation.dataset.clientSideValidationsConfirms; + if (partnerId) { + const partner = document.getElementById(partnerId); + if (partner && partner.form === this.element) return partner; + } + if (confirmation.id && confirmation.id.endsWith('_confirmation')) { + const fallback = document.getElementById(confirmation.id.replace(/_confirmation$/, '')); + if (fallback && fallback.form === this.element) return fallback; + } + return null; + } + _defineProperty(_Class, "targets", ['input', 'confirmation']); + _defineProperty(_Class, "values", { + settings: Object + }); + + const absenceLocalValidator = (element, options) => { + if (isValuePresent(element.value)) { + return options.message; + } + }; + const presenceLocalValidator = (element, options) => { + if (!isValuePresent(element.value)) { + return options.message; + } + }; + + const DEFAULT_ACCEPT_OPTION = ['1', true]; + const isTextAccepted = (value, acceptOption) => { + if (!acceptOption) { + acceptOption = DEFAULT_ACCEPT_OPTION; + } + if (Array.isArray(acceptOption)) { + return arrayHasValue(value, acceptOption); + } + return value === acceptOption; + }; + const acceptanceLocalValidator = (element, options) => { + let valid = true; + if (element.type === 'checkbox') { + valid = element.checked; + } + if (element.type === 'text') { + valid = isTextAccepted(element.value, options.accept); + } + if (!valid) { + return options.message; + } + }; + + const confirmationLocalValidator = (element, options) => { + let value = element.value; + let confirmationValue = document.getElementById("".concat(element.id, "_confirmation")).value; + if (!options.case_sensitive) { + value = value.toLowerCase(); + confirmationValue = confirmationValue.toLowerCase(); + } + if (value !== confirmationValue) { + return options.message; + } + }; + + const isInList = (value, otherValues) => { + const normalizedOtherValues = []; + for (const otherValueIndex in otherValues) { + normalizedOtherValues.push(otherValues[otherValueIndex].toString()); + } + return arrayHasValue(value, normalizedOtherValues); + }; + const isInRange = (value, range) => { + return value >= range[0] && value <= range[1]; + }; + const isIncluded = (value, options, allowBlank) => { + if ((options.allow_blank && !isValuePresent(value)) === allowBlank) { + return true; + } + return options.in && isInList(value, options.in) || options.range && isInRange(value, options.range); + }; + const exclusionLocalValidator = (element, options) => { + const value = element.value; + if (isIncluded(value, options, false) || !options.allow_blank && !isValuePresent(value)) { + return options.message; + } + }; + const inclusionLocalValidator = (element, options) => { + const value = element.value; + if (!isIncluded(value, options, true)) { + return options.message; + } + }; + + const isMatching = (value, regExpOptions) => { + return new RegExp(regExpOptions.source, regExpOptions.options).test(value); + }; + const hasValidFormat = (value, withOptions, withoutOptions) => { + return withOptions && isMatching(value, withOptions) || withoutOptions && !isMatching(value, withoutOptions); + }; + const formatLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; + } + if (!hasValidFormat(value, options.with, options.without)) { + return options.message; + } + }; + + const VALIDATIONS$1 = { + is: (a, b) => { + return a === parseInt(b, 10); + }, + minimum: (a, b) => { + return a >= parseInt(b, 10); + }, + maximum: (a, b) => { + return a <= parseInt(b, 10); + } + }; + const runValidations$1 = (valueLength, options) => { + for (const validation in VALIDATIONS$1) { + const validationOption = options[validation]; + const validationFunction = VALIDATIONS$1[validation]; + if (validationOption && !validationFunction(valueLength, validationOption)) { + return options.messages[validation]; + } + } + }; + const lengthLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; + } + return runValidations$1(value.length, options); + }; + + const VALIDATIONS = { + even: a => { + return parseInt(a, 10) % 2 === 0; + }, + greater_than: (a, b) => { + return parseFloat(a) > parseFloat(b); + }, + greater_than_or_equal_to: (a, b) => { + return parseFloat(a) >= parseFloat(b); + }, + equal_to: (a, b) => { + return parseFloat(a) === parseFloat(b); + }, + less_than: (a, b) => { + return parseFloat(a) < parseFloat(b); + }, + less_than_or_equal_to: (a, b) => { + return parseFloat(a) <= parseFloat(b); + }, + odd: a => { + return parseInt(a, 10) % 2 === 1; + }, + other_than: (a, b) => { + return parseFloat(a) !== parseFloat(b); + } + }; + const formatValue = element => { + const value = element.value || ''; + const numberFormat = element.form.ClientSideValidations.settings.number_format; + return value.trim().replace(new RegExp("\\".concat(numberFormat.separator), 'g'), '.'); + }; + const getOtherValue = (validationOption, form) => { + if (!isNaN(parseFloat(validationOption))) { + return validationOption; + } + const validationElements = form.querySelectorAll("[name*=\"".concat(validationOption, "\"]")); + if (validationElements.length === 1) { + const validationElement = validationElements[0]; + const otherFormattedValue = formatValue(validationElement); + if (!isNaN(parseFloat(otherFormattedValue))) { + return otherFormattedValue; + } + } + }; + const isValid = (validationFunction, validationOption, formattedValue, form) => { + if (validationFunction.length === 2) { + const otherValue = getOtherValue(validationOption, form); + return otherValue == null || otherValue === '' || validationFunction(formattedValue, otherValue); + } else { + return validationFunction(formattedValue); + } + }; + const runFunctionValidations = (formattedValue, form, options) => { + for (const validation in VALIDATIONS) { + const validationOption = options[validation]; + const validationFunction = VALIDATIONS[validation]; + + // Must check for null because this could be 0 + if (validationOption == null) { + continue; + } + if (!isValid(validationFunction, validationOption, formattedValue, form)) { + return options.messages[validation]; + } + } + }; + const runValidations = (formattedValue, form, options) => { + if (options.only_integer && !ClientSideValidations.patterns.numericality.only_integer.test(formattedValue)) { + return options.messages.only_integer; + } + if (!ClientSideValidations.patterns.numericality.default.test(formattedValue)) { + return options.messages.numericality; + } + return runFunctionValidations(formattedValue, form, options); + }; + const numericalityLocalValidator = (element, options) => { + const value = element.value; + if (options.allow_blank && !isValuePresent(value)) { + return; + } + const form = element.form; + const formattedValue = formatValue(element); + return runValidations(formattedValue, form, options); + }; + + const isLocallyUnique = (element, value, otherValue, caseSensitive) => { + if (!caseSensitive) { + value = value.toLowerCase(); + otherValue = otherValue.toLowerCase(); + } + if (otherValue === value) { + element.dataset.csvNotLocallyUnique = 'true'; + return false; + } + if (element.dataset.csvNotLocallyUnique) { + delete element.dataset.csvNotLocallyUnique; + element.dataset.csvChanged = 'true'; + } + return true; + }; + const uniquenessLocalValidator = (element, options) => { + const elementName = element.name; + const matches = elementName.match(/^(.+_attributes\])\[\d+\](.+)$/); + if (!matches) { + return; + } + const form = element.form; + const value = element.value; + let valid = true; + const query = "[name^=\"".concat(matches[1], "\"][name$=\"").concat(matches[2], "\"]:not([name=\"").concat(elementName, "\"])"); + const otherElements = form.querySelectorAll(query); + Array.prototype.slice.call(otherElements).forEach(function (otherElement) { + const otherValue = otherElement.value; + if (!isLocallyUnique(otherElement, value, otherValue, options.case_sensitive)) { + valid = false; + } + }); + if (!valid) { + return options.message; + } + }; + + // Validators run in this order + ClientSideValidations.validators.local = { + absence: absenceLocalValidator, + presence: presenceLocalValidator, + acceptance: acceptanceLocalValidator, + format: formatLocalValidator, + numericality: numericalityLocalValidator, + length: lengthLocalValidator, + inclusion: inclusionLocalValidator, + exclusion: exclusionLocalValidator, + confirmation: confirmationLocalValidator, + uniqueness: uniquenessLocalValidator + }; + ClientSideValidations.isValid = isValid$1; + const register = function (application) { + let identifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'client-side-validations'; + application.register(identifier, _Class); + }; + + // Bundled exclusively for the QUnit browser test suite. Not published. + // + // Wires the Stimulus-only client_side_validations runtime into a single + // classic-script payload that exposes everything the QUnit fixtures need on + // `window`. Most QUnit tests cover the underlying lifecycle layer directly so + // they can stay synchronous; a separate Stimulus controller module exercises + // the Stimulus integration end-to-end. + + const application = Application.start(); + register(application); + const isValidatableInput = element => { + if (!element.name) return false; + if (element.type === 'submit' || element.type === 'button') return false; + if (element.type === 'radio') return false; + return true; + }; + const setupForm = (form, settings) => { + form.noValidate = true; + installFormContext(form, settings); + bindFormEvents(form); + Array.from(form.elements).forEach(input => { + if (!isValidatableInput(input)) return; + if (input.id && input.id.endsWith('_confirmation')) { + const partner = document.getElementById(input.id.replace(/_confirmation$/, '')); + if (partner && partner.form === form) { + bindConfirmationEvents(partner, input, form); + } + return; + } + bindInputEvents(form, input); + }); + }; + const teardownForm = form => { + unbindElement(form); + Array.from(form.elements).forEach(unbindElement); + removeFormContext(form); + }; + const resetForm = form => { + if (!form.ClientSideValidations) return; + const settings = form.ClientSideValidations.settings; + for (const key in settings.validators) { + const element = Array.from(form.elements).find(el => el.name === key); + if (element) form.ClientSideValidations.removeError(element); + } + teardownForm(form); + setupForm(form, settings); + }; + window.Stimulus = application; + window.ClientSideValidations = ClientSideValidations; + window.ClientSideValidationsController = _Class; + window.csvSetup = setupForm; + window.csvTeardown = teardownForm; + window.csvReset = resetForm; + + // Back-compat shims so existing QUnit test bodies keep working unchanged. + const disable = target => { + if (target == null) return; + if (target instanceof HTMLFormElement) { + teardownForm(target); + return; + } + if (target instanceof Element) { + unbindElement(target); + return; + } + if (typeof target.length === 'number' || Symbol.iterator in Object(target)) { + Array.from(target).forEach(node => { + if (node instanceof Element) unbindElement(node); + }); + } + }; + const enable = target => { + if (target instanceof HTMLFormElement) { + var _target$ClientSideVal; + const settings = ((_target$ClientSideVal = target.ClientSideValidations) === null || _target$ClientSideVal === void 0 ? void 0 : _target$ClientSideVal.settings) || JSON.parse(target.dataset.clientSideValidations || '{}'); + setupForm(target, settings); + return; + } + if (target instanceof Element && target.form) { + bindInputEvents(target.form, target); + } + }; + ClientSideValidations.validate = form => setupForm(form, JSON.parse(form.dataset.clientSideValidations || '{}')); + ClientSideValidations.disable = disable; + ClientSideValidations.enable = enable; + ClientSideValidations.reset = resetForm; + ClientSideValidations.isValid = isValid$1; + +})(); diff --git a/test/javascript/server.rb b/test/javascript/server.rb index a4b3939cf..d1df852a3 100644 --- a/test/javascript/server.rb +++ b/test/javascript/server.rb @@ -8,26 +8,6 @@ disable :logging -class AssetPath < Rack::Static - def call(env) - path = env['PATH_INFO'] - - if can_serve(path) - env['PATH_INFO'] = (path == '/' ? @index : @urls[path]) if overwrite_file_path(path) - response = @file_server.call(env) - if response.first == 404 - @app.call(env) - else - response - end - else - @app.call(env) - end - end -end - -use AssetPath, urls: ['/vendor/assets/javascripts'], root: File.expand_path('../..', settings.root) - QUNIT_VERSION = '2.25.0' helpers do diff --git a/test/javascript/test-bundle.js b/test/javascript/test-bundle.js new file mode 100644 index 000000000..bd733c32e --- /dev/null +++ b/test/javascript/test-bundle.js @@ -0,0 +1,119 @@ +// Bundled exclusively for the QUnit browser test suite. Not published. +// +// Wires the Stimulus-only client_side_validations runtime into a single +// classic-script payload that exposes everything the QUnit fixtures need on +// `window`. Most QUnit tests cover the underlying lifecycle layer directly so +// they can stay synchronous; a separate Stimulus controller module exercises +// the Stimulus integration end-to-end. + +import { Application } from '@hotwired/stimulus' + +import { + ClientSideValidations, + ClientSideValidationsController, + isValid, + register +} from '../../src/index.js' + +import { + bindConfirmationEvents, + bindFormEvents, + bindInputEvents, + installFormContext, + removeFormContext, + unbindElement +} from '../../src/lifecycle.js' + +const application = Application.start() +register(application) + +const isValidatableInput = (element) => { + if (!element.name) return false + if (element.type === 'submit' || element.type === 'button') return false + if (element.type === 'radio') return false + return true +} + +const setupForm = (form, settings) => { + form.noValidate = true + installFormContext(form, settings) + bindFormEvents(form) + + Array.from(form.elements).forEach((input) => { + if (!isValidatableInput(input)) return + + if (input.id && input.id.endsWith('_confirmation')) { + const partner = document.getElementById(input.id.replace(/_confirmation$/, '')) + if (partner && partner.form === form) { + bindConfirmationEvents(partner, input, form) + } + return + } + + bindInputEvents(form, input) + }) +} + +const teardownForm = (form) => { + unbindElement(form) + Array.from(form.elements).forEach(unbindElement) + removeFormContext(form) +} + +const resetForm = (form) => { + if (!form.ClientSideValidations) return + const settings = form.ClientSideValidations.settings + for (const key in settings.validators) { + const element = Array.from(form.elements).find((el) => el.name === key) + if (element) form.ClientSideValidations.removeError(element) + } + teardownForm(form) + setupForm(form, settings) +} + +window.Stimulus = application +window.ClientSideValidations = ClientSideValidations +window.ClientSideValidationsController = ClientSideValidationsController +window.csvSetup = setupForm +window.csvTeardown = teardownForm +window.csvReset = resetForm + +// Back-compat shims so existing QUnit test bodies keep working unchanged. +const disable = (target) => { + if (target == null) return + + if (target instanceof HTMLFormElement) { + teardownForm(target) + return + } + + if (target instanceof Element) { + unbindElement(target) + return + } + + if (typeof target.length === 'number' || Symbol.iterator in Object(target)) { + Array.from(target).forEach((node) => { + if (node instanceof Element) unbindElement(node) + }) + } +} + +const enable = (target) => { + if (target instanceof HTMLFormElement) { + const settings = target.ClientSideValidations?.settings || + JSON.parse(target.dataset.clientSideValidations || '{}') + setupForm(target, settings) + return + } + + if (target instanceof Element && target.form) { + bindInputEvents(target.form, target) + } +} + +ClientSideValidations.validate = (form) => setupForm(form, JSON.parse(form.dataset.clientSideValidations || '{}')) +ClientSideValidations.disable = disable +ClientSideValidations.enable = enable +ClientSideValidations.reset = resetForm +ClientSideValidations.isValid = isValid diff --git a/test/javascript/views/index.erb b/test/javascript/views/index.erb index 53330fb08..b0923c4f0 100644 --- a/test/javascript/views/index.erb +++ b/test/javascript/views/index.erb @@ -3,8 +3,8 @@
-<%= script_tag '/vendor/assets/javascripts/rails.validations.js' %> +<%= script_tag '/vendor/test-bundle.js' %> <%= script_tag "https://cdn.jsdelivr.net/npm/qunit@#{qunit_version}/qunit/qunit.js" %> <%= script_tag 'settings' %> <%= script_tag 'validateElement' %> -<%= test :validators, :form_builders, :callbacks %> +<%= test :validators, :form_builders, :callbacks, :stimulus %> diff --git a/test/test_loader.rb b/test/test_loader.rb index 24ebe315e..dbdcb9cf1 100644 --- a/test/test_loader.rb +++ b/test/test_loader.rb @@ -4,5 +4,4 @@ require 'base_helper' require 'client_side_validations' -require 'client_side_validations/files' require 'client_side_validations/version' diff --git a/vendor/assets/javascripts/rails.validations.js b/vendor/assets/javascripts/rails.validations.js deleted file mode 100644 index 415776074..000000000 --- a/vendor/assets/javascripts/rails.validations.js +++ /dev/null @@ -1,789 +0,0 @@ -/*! - * Client Side Validations JS - v24.0.0 (https://github.com/DavyJonesLocker/client_side_validations) - * Copyright (c) 2026 Geremia Taglialatela, Brian Cardarella - * Licensed under MIT (https://opensource.org/licenses/mit-license.php) - */ - -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.ClientSideValidations = factory()); -})(this, (function () { 'use strict'; - - const boundEventListeners = new WeakMap(); - const addBoundEventListener = (element, eventName, listener) => { - element.addEventListener(eventName, listener); - const listeners = boundEventListeners.get(element) || []; - listeners.push({ - eventName, - listener - }); - boundEventListeners.set(element, listeners); - }; - const bindElementEvents = (element, eventsToBind) => { - for (const eventName in eventsToBind) { - addBoundEventListener(element, eventName, eventsToBind[eventName]); - } - }; - const clearBoundEventListeners = element => { - const listeners = boundEventListeners.get(element); - if (!listeners) { - return; - } - listeners.forEach(_ref => { - let { - eventName, - listener - } = _ref; - element.removeEventListener(eventName, listener); - }); - boundEventListeners.delete(element); - }; - const dispatchCustomEvent = (element, eventName, detail) => { - element.dispatchEvent(new CustomEvent(eventName, { - bubbles: true, - detail - })); - }; - - const arrayHasValue = (value, otherValues) => { - for (let i = 0, l = otherValues.length; i < l; i++) { - if (value === otherValues[i]) { - return true; - } - } - return false; - }; - const createElementFromHTML = html => { - const element = document.createElement('div'); - element.innerHTML = html; - return element.firstChild; - }; - const isDOMCollection = target => { - return Array.isArray(target) || typeof NodeList !== 'undefined' && target instanceof NodeList || typeof HTMLCollection !== 'undefined' && target instanceof HTMLCollection || typeof RadioNodeList !== 'undefined' && target instanceof RadioNodeList; - }; - const isDOMElement = target => { - return target != null && target.nodeType === 1; - }; - const getDOMElements = target => { - if (target == null) { - return []; - } - if (isDOMElement(target)) { - return [target]; - } - if (isDOMCollection(target)) { - return Array.from(target).filter(isDOMElement); - } - return []; - }; - const isFormElement = element => { - return element.tagName === 'FORM'; - }; - const isInputElement = element => { - switch (element.tagName) { - case 'INPUT': - return element.type !== 'submit' && element.type !== 'button'; - case 'SELECT': - case 'TEXTAREA': - return true; - default: - return false; - } - }; - const isVisible = element => { - return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length); - }; - const isValuePresent = value => { - return !/^\s*$/.test(value || ''); - }; - - const isNamedInputElement = element => { - return isInputElement(element) && element.name != null && element.name !== ''; - }; - const getFormControls = form => { - return Array.from(form.elements).filter(isInputElement); - }; - const getFormInputs = form => { - return getFormControls(form).filter(element => { - return isNamedInputElement(element) && !element.disabled && isVisible(element); - }); - }; - const findFormElementByName = (form, name) => { - return getFormControls(form).find(element => element.name === name); - }; - const enableForm = form => { - ClientSideValidations.enablers.form(form); - }; - const enableForms = () => { - document.querySelectorAll(ClientSideValidations.selectors.forms).forEach(enableForm); - }; - const ClientSideValidations = { - callbacks: { - element: { - after: (element, eventData) => {}, - before: (element, eventData) => {}, - fail: (element, message, addError, eventData) => addError(), - pass: (element, removeError, eventData) => removeError() - }, - form: { - after: (form, eventData) => {}, - before: (form, eventData) => {}, - fail: (form, eventData) => {}, - pass: (form, eventData) => {} - } - }, - eventsToBind: { - form: form => ({ - submit: eventData => { - if (!ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators)) { - eventData.preventDefault(); - eventData.stopImmediatePropagation(); - } - }, - 'ajax:beforeSend': function (eventData) { - if (eventData.target === this) { - ClientSideValidations.isValid(form, form.ClientSideValidations.settings.validators); - } - }, - 'form:validate:after': eventData => { - ClientSideValidations.callbacks.form.after(form, eventData); - }, - 'form:validate:before': eventData => { - ClientSideValidations.callbacks.form.before(form, eventData); - }, - 'form:validate:fail': eventData => { - ClientSideValidations.callbacks.form.fail(form, eventData); - }, - 'form:validate:pass': eventData => { - ClientSideValidations.callbacks.form.pass(form, eventData); - } - }), - input: form => ({ - focusout: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - }, - change: function () { - this.dataset.csvChanged = 'true'; - }, - 'element:validate:after': function (eventData) { - ClientSideValidations.callbacks.element.after(this, eventData); - }, - 'element:validate:before': function (eventData) { - ClientSideValidations.callbacks.element.before(this, eventData); - }, - 'element:validate:fail': function (eventData) { - const element = this; - const message = eventData.detail; - ClientSideValidations.callbacks.element.fail(element, message, function () { - form.ClientSideValidations.addError(element, message); - }, eventData); - }, - 'element:validate:pass': function (eventData) { - const element = this; - ClientSideValidations.callbacks.element.pass(element, function () { - form.ClientSideValidations.removeError(element); - }, eventData); - } - }), - inputConfirmation: (elementToConfirm, form) => ({ - focusout: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - }, - keyup: () => { - elementToConfirm.dataset.csvChanged = 'true'; - ClientSideValidations.isValid(elementToConfirm, form.ClientSideValidations.settings.validators); - } - }) - }, - enablers: { - form: form => { - clearBoundEventListeners(form); - getFormControls(form).forEach(clearBoundEventListeners); - form.ClientSideValidations = { - settings: JSON.parse(form.dataset.clientSideValidations), - addError: (element, message) => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].add(element, form.ClientSideValidations.settings.html_settings, message), - removeError: element => ClientSideValidations.formBuilders[form.ClientSideValidations.settings.html_settings.type].remove(element, form.ClientSideValidations.settings.html_settings) - }; - bindElementEvents(form, ClientSideValidations.eventsToBind.form(form)); - getFormInputs(form).forEach(element => { - ClientSideValidations.enablers.input(element); - }); - }, - input: function (input) { - const form = input.form; - if (!form) { - return; - } - clearBoundEventListeners(input); - const eventsToBind = ClientSideValidations.eventsToBind.input(form); - if (input.type !== 'radio' && !(input.id && input.id.endsWith('_confirmation'))) { - input.dataset.csvValidate = 'true'; - bindElementEvents(input, eventsToBind); - } - if (input.type === 'checkbox') { - bindElementEvents(input, { - change: function () { - ClientSideValidations.isValid(this, form.ClientSideValidations.settings.validators); - } - }); - } - if (input.id && input.id.endsWith('_confirmation')) { - const elementToConfirm = document.getElementById(input.id.match(/(.+)_confirmation/)[1]); - if (elementToConfirm && elementToConfirm.form === form) { - bindElementEvents(input, ClientSideValidations.eventsToBind.inputConfirmation(elementToConfirm, form)); - } - } - } - }, - formBuilders: { - 'ActionView::Helpers::FormBuilder': { - add: (element, settings, message) => { - if (!element) { - return; - } - const form = element.form; - const inputErrorTemplate = createElementFromHTML(settings.input_tag); - let inputErrorElement = element.closest(".".concat(inputErrorTemplate.getAttribute('class').replace(/ /g, '.'))); - if (!inputErrorElement) { - inputErrorElement = inputErrorTemplate; - if (element.getAttribute('autofocus')) { - element.setAttribute('autofocus', false); - } - element.before(inputErrorElement); - inputErrorElement.querySelector('span#input_tag').replaceWith(element); - const inputErrorLabelMessageElement = inputErrorElement.querySelector('label.message'); - if (inputErrorLabelMessageElement) { - inputErrorLabelMessageElement.setAttribute('for', element.id); - } - } - const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); - if (labelElement) { - const labelErrorTemplate = createElementFromHTML(settings.label_tag); - const labelErrorContainer = labelElement.closest(".".concat(labelErrorTemplate.getAttribute('class').replace(/ /g, '.'))); - if (!labelErrorContainer) { - labelElement.after(labelErrorTemplate); - labelErrorTemplate.querySelector('label#label_tag').replaceWith(labelElement); - } - } - const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); - if (labelMessageElement) { - labelMessageElement.textContent = message; - } - }, - remove: (element, settings) => { - if (!element) { - return; - } - const form = element.form; - const inputErrorClass = createElementFromHTML(settings.input_tag).getAttribute('class'); - const inputErrorElement = element.closest(".".concat(inputErrorClass.replace(/ /g, '.'))); - if (inputErrorElement) { - inputErrorElement.querySelector("#".concat(element.id)).remove(); - inputErrorElement.replaceWith(element); - } - const labelElement = form.querySelector("label[for=\"".concat(element.id, "\"]:not(.message)")); - if (labelElement) { - const labelErrorClass = createElementFromHTML(settings.label_tag).getAttribute('class'); - const labelErrorElement = labelElement.closest(".".concat(labelErrorClass.replace(/ /g, '.'))); - if (labelErrorElement) { - labelErrorElement.replaceWith(labelElement); - } - } - const labelMessageElement = form.querySelector("label.message[for=\"".concat(element.id, "\"]")); - if (labelMessageElement) { - labelMessageElement.remove(); - } - } - } - }, - patterns: { - numericality: { - default: /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/, - only_integer: /^[+-]?\d+$/ - } - }, - selectors: { - inputs: 'input:not([type="submit"]):not([type="button"])[name], select[name], textarea[name]', - validate_inputs: 'input[data-csv-validate]:not([type="submit"]):not([type="button"]), select[data-csv-validate], textarea[data-csv-validate]', - forms: 'form[data-client-side-validations]' - }, - validators: { - all: () => { - return { - ...ClientSideValidations.validators.local, - ...ClientSideValidations.validators.remote - }; - }, - local: {}, - remote: {} - }, - disable: target => { - getDOMElements(target).forEach(element => { - clearBoundEventListeners(element); - if (isFormElement(element)) { - getFormControls(element).forEach(input => { - clearBoundEventListeners(input); - delete input.dataset.csvValid; - delete input.dataset.csvChanged; - delete input.dataset.csvValidate; - }); - return; - } - delete element.dataset.csvValid; - delete element.dataset.csvChanged; - if (isInputElement(element)) { - delete element.dataset.csvValidate; - } - }); - }, - reset: form => { - ClientSideValidations.disable(form); - for (const key in form.ClientSideValidations.settings.validators) { - const element = findFormElementByName(form, key); - if (element) { - form.ClientSideValidations.removeError(element); - } - } - ClientSideValidations.enablers.form(form); - }, - initializeOnEvent: () => { - if (window.Turbo != null) { - return 'turbo:load'; - } else if (window.Turbolinks != null && window.Turbolinks.supported) { - return window.Turbolinks.EVENTS != null ? 'page:change' : 'turbolinks:load'; - } - }, - start: () => { - const initializeOnEvent = ClientSideValidations.initializeOnEvent(); - if (initializeOnEvent != null) { - document.addEventListener(initializeOnEvent, enableForms); - } else if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', enableForms, { - once: true - }); - } else { - enableForms(); - } - } - }; - - const absenceLocalValidator = (element, options) => { - if (isValuePresent(element.value)) { - return options.message; - } - }; - const presenceLocalValidator = (element, options) => { - if (!isValuePresent(element.value)) { - return options.message; - } - }; - - const DEFAULT_ACCEPT_OPTION = ['1', true]; - const isTextAccepted = (value, acceptOption) => { - if (!acceptOption) { - acceptOption = DEFAULT_ACCEPT_OPTION; - } - if (Array.isArray(acceptOption)) { - return arrayHasValue(value, acceptOption); - } - return value === acceptOption; - }; - const acceptanceLocalValidator = (element, options) => { - let valid = true; - if (element.type === 'checkbox') { - valid = element.checked; - } - if (element.type === 'text') { - valid = isTextAccepted(element.value, options.accept); - } - if (!valid) { - return options.message; - } - }; - - const isMatching = (value, regExpOptions) => { - return new RegExp(regExpOptions.source, regExpOptions.options).test(value); - }; - const hasValidFormat = (value, withOptions, withoutOptions) => { - return withOptions && isMatching(value, withOptions) || withoutOptions && !isMatching(value, withoutOptions); - }; - const formatLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - if (!hasValidFormat(value, options.with, options.without)) { - return options.message; - } - }; - - const VALIDATIONS$1 = { - even: a => { - return parseInt(a, 10) % 2 === 0; - }, - greater_than: (a, b) => { - return parseFloat(a) > parseFloat(b); - }, - greater_than_or_equal_to: (a, b) => { - return parseFloat(a) >= parseFloat(b); - }, - equal_to: (a, b) => { - return parseFloat(a) === parseFloat(b); - }, - less_than: (a, b) => { - return parseFloat(a) < parseFloat(b); - }, - less_than_or_equal_to: (a, b) => { - return parseFloat(a) <= parseFloat(b); - }, - odd: a => { - return parseInt(a, 10) % 2 === 1; - }, - other_than: (a, b) => { - return parseFloat(a) !== parseFloat(b); - } - }; - const formatValue = element => { - const value = element.value || ''; - const numberFormat = element.form.ClientSideValidations.settings.number_format; - return value.trim().replace(new RegExp("\\".concat(numberFormat.separator), 'g'), '.'); - }; - const getOtherValue = (validationOption, form) => { - if (!isNaN(parseFloat(validationOption))) { - return validationOption; - } - const validationElements = form.querySelectorAll("[name*=\"".concat(validationOption, "\"]")); - if (validationElements.length === 1) { - const validationElement = validationElements[0]; - const otherFormattedValue = formatValue(validationElement); - if (!isNaN(parseFloat(otherFormattedValue))) { - return otherFormattedValue; - } - } - }; - const isValid = (validationFunction, validationOption, formattedValue, form) => { - if (validationFunction.length === 2) { - const otherValue = getOtherValue(validationOption, form); - return otherValue == null || otherValue === '' || validationFunction(formattedValue, otherValue); - } else { - return validationFunction(formattedValue); - } - }; - const runFunctionValidations = (formattedValue, form, options) => { - for (const validation in VALIDATIONS$1) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS$1[validation]; - - // Must check for null because this could be 0 - if (validationOption == null) { - continue; - } - if (!isValid(validationFunction, validationOption, formattedValue, form)) { - return options.messages[validation]; - } - } - }; - const runValidations$1 = (formattedValue, form, options) => { - if (options.only_integer && !ClientSideValidations.patterns.numericality.only_integer.test(formattedValue)) { - return options.messages.only_integer; - } - if (!ClientSideValidations.patterns.numericality.default.test(formattedValue)) { - return options.messages.numericality; - } - return runFunctionValidations(formattedValue, form, options); - }; - const numericalityLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - const form = element.form; - const formattedValue = formatValue(element); - return runValidations$1(formattedValue, form, options); - }; - - const VALIDATIONS = { - is: (a, b) => { - return a === parseInt(b, 10); - }, - minimum: (a, b) => { - return a >= parseInt(b, 10); - }, - maximum: (a, b) => { - return a <= parseInt(b, 10); - } - }; - const runValidations = (valueLength, options) => { - for (const validation in VALIDATIONS) { - const validationOption = options[validation]; - const validationFunction = VALIDATIONS[validation]; - if (validationOption && !validationFunction(valueLength, validationOption)) { - return options.messages[validation]; - } - } - }; - const lengthLocalValidator = (element, options) => { - const value = element.value; - if (options.allow_blank && !isValuePresent(value)) { - return; - } - return runValidations(value.length, options); - }; - - const isInList = (value, otherValues) => { - const normalizedOtherValues = []; - for (const otherValueIndex in otherValues) { - normalizedOtherValues.push(otherValues[otherValueIndex].toString()); - } - return arrayHasValue(value, normalizedOtherValues); - }; - const isInRange = (value, range) => { - return value >= range[0] && value <= range[1]; - }; - const isIncluded = (value, options, allowBlank) => { - if ((options.allow_blank && !isValuePresent(value)) === allowBlank) { - return true; - } - return options.in && isInList(value, options.in) || options.range && isInRange(value, options.range); - }; - const exclusionLocalValidator = (element, options) => { - const value = element.value; - if (isIncluded(value, options, false) || !options.allow_blank && !isValuePresent(value)) { - return options.message; - } - }; - const inclusionLocalValidator = (element, options) => { - const value = element.value; - if (!isIncluded(value, options, true)) { - return options.message; - } - }; - - const confirmationLocalValidator = (element, options) => { - let value = element.value; - let confirmationValue = document.getElementById("".concat(element.id, "_confirmation")).value; - if (!options.case_sensitive) { - value = value.toLowerCase(); - confirmationValue = confirmationValue.toLowerCase(); - } - if (value !== confirmationValue) { - return options.message; - } - }; - - const isLocallyUnique = (element, value, otherValue, caseSensitive) => { - if (!caseSensitive) { - value = value.toLowerCase(); - otherValue = otherValue.toLowerCase(); - } - if (otherValue === value) { - element.dataset.csvNotLocallyUnique = 'true'; - return false; - } - if (element.dataset.csvNotLocallyUnique) { - delete element.dataset.csvNotLocallyUnique; - element.dataset.csvChanged = 'true'; - } - return true; - }; - const uniquenessLocalValidator = (element, options) => { - const elementName = element.name; - const matches = elementName.match(/^(.+_attributes\])\[\d+\](.+)$/); - if (!matches) { - return; - } - const form = element.form; - const value = element.value; - let valid = true; - const query = "[name^=\"".concat(matches[1], "\"][name$=\"").concat(matches[2], "\"]:not([name=\"").concat(elementName, "\"])"); - const otherElements = form.querySelectorAll(query); - Array.prototype.slice.call(otherElements).forEach(function (otherElement) { - const otherValue = otherElement.value; - if (!isLocallyUnique(otherElement, value, otherValue, options.case_sensitive)) { - valid = false; - } - }); - if (!valid) { - return options.message; - } - }; - - // Validators will run in the following order - ClientSideValidations.validators.local = { - absence: absenceLocalValidator, - presence: presenceLocalValidator, - acceptance: acceptanceLocalValidator, - format: formatLocalValidator, - numericality: numericalityLocalValidator, - length: lengthLocalValidator, - inclusion: inclusionLocalValidator, - exclusion: exclusionLocalValidator, - confirmation: confirmationLocalValidator, - uniqueness: uniquenessLocalValidator - }; - ClientSideValidations.enable = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enablers.form(element); - } else if (isInputElement(element)) { - ClientSideValidations.enablers.input(element); - } - }); - return target; - }; - ClientSideValidations.validate = target => { - getDOMElements(target).forEach(element => { - if (isFormElement(element)) { - ClientSideValidations.enable(element); - } - }); - return target; - }; - ClientSideValidations.isValid = (target, validators) => { - const element = getDOMElements(target)[0]; - if (!element) { - return true; - } - if (!validators) { - var _form$ClientSideValid; - const form = isFormElement(element) ? element : element.form; - validators = form === null || form === void 0 || (_form$ClientSideValid = form.ClientSideValidations) === null || _form$ClientSideValid === void 0 || (_form$ClientSideValid = _form$ClientSideValid.settings) === null || _form$ClientSideValid === void 0 ? void 0 : _form$ClientSideValid.validators; - } - if (isFormElement(element)) { - return validateForm(element, validators || {}); - } - return validateElement(element, validatorsFor(element.name, validators || {})); - }; - const cleanNestedElementName = (elementName, nestedMatches, validators) => { - for (const validatorName in validators) { - if (validatorName.match("\\[".concat(nestedMatches[1], "\\].*\\[\\]\\[").concat(nestedMatches[2], "\\]$"))) { - elementName = elementName.replace(/\[[\da-z_]+\]\[(\w+)\]$/g, '[][$1]'); - } - } - return elementName; - }; - const cleanElementName = (elementName, validators) => { - elementName = elementName.replace(/\[(\w+_attributes)\]\[[\da-z_]+\](?=\[(?:\w+_attributes)\])/g, '[$1][]'); - const nestedMatches = elementName.match(/\[(\w+_attributes)\].*\[(\w+)\]$/); - if (nestedMatches) { - elementName = cleanNestedElementName(elementName, nestedMatches, validators); - } - return elementName; - }; - const validatorsFor = (elementName, validators) => { - if (!elementName || !validators) { - return {}; - } - if (Object.prototype.hasOwnProperty.call(validators, elementName)) { - return validators[elementName]; - } - return validators[cleanElementName(elementName, validators)] || {}; - }; - const getValidationInputs = form => { - return Array.from(form.elements).filter(element => { - if (element.dataset.csvValidate == null || element.disabled) { - return false; - } - return isVisible(element); - }); - }; - const validateForm = (form, validators) => { - let valid = true; - dispatchCustomEvent(form, 'form:validate:before'); - getValidationInputs(form).forEach(element => { - if (!validateElement(element, validatorsFor(element.name, validators))) { - valid = false; - } - }); - if (valid) { - dispatchCustomEvent(form, 'form:validate:pass'); - } else { - dispatchCustomEvent(form, 'form:validate:fail'); - } - dispatchCustomEvent(form, 'form:validate:after'); - return valid; - }; - const passElement = element => { - dispatchCustomEvent(element, 'element:validate:pass'); - delete element.dataset.csvValid; - }; - const failElement = (element, message) => { - dispatchCustomEvent(element, 'element:validate:fail', message); - element.dataset.csvValid = 'false'; - }; - const afterValidate = element => { - dispatchCustomEvent(element, 'element:validate:after'); - return element.dataset.csvValid !== 'false'; - }; - const executeValidator = (validatorFunctions, validatorFunction, validatorOptions, element) => { - for (const validatorOption in validatorOptions) { - if (!validatorOptions[validatorOption]) { - continue; - } - const message = validatorFunction.call(validatorFunctions, element, validatorOptions[validatorOption]); - if (message) { - failElement(element, message); - return false; - } - } - return true; - }; - const executeValidators = (validatorFunctions, element, validators) => { - for (const validator in validators) { - if (!validatorFunctions[validator]) { - continue; - } - if (!executeValidator(validatorFunctions, validatorFunctions[validator], validators[validator], element)) { - return false; - } - } - return true; - }; - const isMarkedForDestroy = element => { - const elementName = element.name; - const form = element.form; - if (form && /\[([^\]]*?)\]$/.test(elementName)) { - const destroyInputName = elementName.replace(/\[([^\]]*?)\]$/, '[_destroy]'); - const destroyInputElement = form.querySelector("input[name=\"".concat(destroyInputName, "\"]")); - if (destroyInputElement && destroyInputElement.value === '1') { - return true; - } - } - return false; - }; - const executeAllValidators = (element, validators) => { - if (element.dataset.csvChanged === 'false' || element.disabled) { - return; - } - element.dataset.csvChanged = 'false'; - if (executeValidators(ClientSideValidations.validators.all(), element, validators)) { - passElement(element); - } - }; - const validateElement = (element, validators) => { - dispatchCustomEvent(element, 'element:validate:before'); - if (isMarkedForDestroy(element)) { - passElement(element); - } else { - executeAllValidators(element, validators); - } - return afterValidate(element); - }; - if (!window.ClientSideValidations) { - window.ClientSideValidations = ClientSideValidations; - if (!isAMD() && !isCommonJS()) { - ClientSideValidations.start(); - } - } - function isAMD() { - return typeof define === 'function' && define.amd; // eslint-disable-line no-undef - } - function isCommonJS() { - return typeof exports === 'object' && typeof module !== 'undefined'; - } - - return ClientSideValidations; - -})); From 5a238e97e76d9cec394bc2e16fc204591d54ffec Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Mon, 20 Apr 2026 23:19:08 +0200 Subject: [PATCH 2/3] Changes --- README.md | 249 ++++++++++++++++++++++++++++++--------------------- package.json | 4 +- 2 files changed, 148 insertions(+), 105 deletions(-) diff --git a/README.md b/README.md index 342bc6d5f..923d904c8 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,9 @@ Review the following migration steps when upgrading from 24.x: * **No automatic `window.ClientSideValidations` global.** Import what you need directly from the package. The remaining `ClientSideValidations` export is a configuration namespace (`validators`, `formBuilders`, - `callbacks`, `patterns`, `selectors`). + `callbacks`, `patterns`). The validation helpers (`isValid`, + `validateElement`, `validateForm`, `validatorsFor`) are exported + separately. * **UMD bundle and Sprockets support removed.** Only an ESM bundle is shipped. The `client_side_validations:copy_assets` generator is gone; use the npm package through Importmaps, Webpacker, esbuild, Vite, or your @@ -140,24 +142,24 @@ Review the following migration steps when upgrading from 24.x: ### 24.x Breaking Changes ### -If you are upgrading to 24.x, update your integration code to use the `ClientSideValidations` object directly. - -The old jQuery plugin methods are removed. Use the DOM-first public API instead: - -```js -ClientSideValidations.enable(form) -ClientSideValidations.validate(form) -ClientSideValidations.isValid(form, validators) -ClientSideValidations.disable(form) -ClientSideValidations.reset(form) -``` - -These methods accept native DOM elements and DOM collections. They do not accept jQuery objects or CSS selector strings. - -Custom validators, form builders, and callbacks now receive native DOM nodes instead of jQuery wrappers. Update any custom code to use DOM APIs such as `.value`, `.form`, `.closest()`, and `querySelector()`. -Local validators are called as `(element, options)`. Form callbacks receive `(form, eventData)`, and element callbacks receive either `(element, message, callback)` or `(element, callback)` depending on the event. - -All runtime-owned validation state attributes are now namespaced under `csv`. If you read or write these attributes in custom selectors, callbacks, or validators, update them to the scoped names: +If you are upgrading across both 24.x and 25.x, keep the following 24.x +changes in mind as well: + +* The old jQuery plugin methods were removed in favor of native DOM + APIs. 25.0 then replaced 24.x's imperative lifecycle helpers with the + Stimulus controller described above, so do not carry forward any + `enable`, `validate`, `disable`, or `reset` calls from older examples. +* Custom validators, form builders, and callbacks now receive native DOM + nodes instead of jQuery wrappers. Update any custom code to use DOM + APIs such as `.value`, `.form`, `.closest()`, and `querySelector()`. +* Local validators are called as `(element, options)`. Form callbacks + receive `(form, eventData)`, and element callbacks receive either + `(element, message, callback, eventData)` or + `(element, callback, eventData)` depending on the event. + +All runtime-owned validation state attributes are namespaced under +`csv`. If you read or write these attributes in custom code, update them +to the scoped names: ```text data-changed => data-csv-changed @@ -166,9 +168,14 @@ data-validate => data-csv-validate data-not-locally-unique => data-csv-not-locally-unique ``` -The matching dataset properties are `element.dataset.csvChanged`, `element.dataset.csvValid`, `element.dataset.csvValidate`, and `element.dataset.csvNotLocallyUnique`. `csvChanged` is stored as the string values `'true'` and `'false'`. +The matching dataset properties are `element.dataset.csvChanged`, +`element.dataset.csvValid`, `element.dataset.csvValidate`, and +`element.dataset.csvNotLocallyUnique`. `csvChanged` is stored as the +string values `'true'` and `'false'`. -**jQuery namespaced events are removed.** Events are now plain native DOM custom events. If your application listens to or unbinds events using jQuery-style namespacing, you must update those calls. +**jQuery namespaced events are removed.** Events are now plain native +DOM custom events. If your application listens to or unbinds events +using jQuery-style namespacing, update those calls. Before: @@ -184,11 +191,22 @@ form.addEventListener('form:validate:before', handler) // store and pass the handler reference to removeEventListener when unbinding ``` -The full list of native events dispatched by ClientSideValidations: `form:validate:before`, `form:validate:after`, `form:validate:pass`, `form:validate:fail`, `element:validate:before`, `element:validate:after`, `element:validate:pass`, `element:validate:fail`. +The full list of native events dispatched by ClientSideValidations is: +`form:validate:before`, `form:validate:after`, `form:validate:pass`, +`form:validate:fail`, `element:validate:before`, +`element:validate:after`, `element:validate:pass`, +`element:validate:fail`. -If you are upgrading from a version older than 23.0.0, the `data-csv-*` renaming above is required for any custom code that still reads or writes the old attribute names. If you are already on 23.x, the new 24.x upgrade step is to update any local uniqueness integrations that still reference `data-not-locally-unique` or `element.dataset.notLocallyUnique`. +If you are upgrading from a version older than 23.0.0, the +`data-csv-*` renaming above is required for any custom code that still +reads or writes the old attribute names. If you are already on 23.x, +the new 24.x upgrade step is to update any local uniqueness +integrations that still reference `data-not-locally-unique` or +`element.dataset.notLocallyUnique`. -If your application vendors the compiled asset with `rails g client_side_validations:copy_assets`, run that generator again after upgrading so your copied asset matches the current jQuery-free bundle. +If you previously vendored the compiled asset with +`rails g client_side_validations:copy_assets`, remove that integration +when upgrading to 25.x; the generator no longer exists. ## Initializer ## @@ -319,11 +337,19 @@ You can even turn them off per fieldset: Please note that `pass` callback will also be performed on fields that skip validations. -## Understanding the client side validations data attribute ## +## Understanding the Stimulus settings value ## + +A rendered form with validations will always have: + +* `data-controller="client-side-validations"` +* `data-client-side-validations-settings-value='...'` -A rendered form with validations will always have a `data-client-side-validations` attribute. +After Stimulus connects, the same data is available at +`form.ClientSideValidations.settings`. -The objects it contains will have different keys depending upon the `FormBuilder` being used. However, `html_settings` and `validators` will always be present. +The settings object contains different keys depending on the +`FormBuilder` being used. However, `html_settings` and `validators` will +always be present. ### `html_settings` ### @@ -347,7 +373,17 @@ If you need to add more validators but don't want them rendered on the form imme ... ``` -In the above example `age` and `bio` will not render as inputs on the form but their validators will be properly added to the `validators` object for use later. If you do intend to dynamically render these inputs later the `name` attributes on the inputs will have to match with the keys on the `validators` object, and the inputs will have to be enabled for client side validation. +In the above example `age` and `bio` will not render as inputs on the +form, but their validators will still be added to the `validators` +object for later use. If you do dynamically render these inputs later, +their `name` attributes must match the keys on the `validators` object. +If the fields are rendered by ClientSideValidations-aware Rails helpers, +the correct Stimulus target attributes are added automatically. If you +add markup by hand, keep the fields inside the same validated form and +add `data-client-side-validations-target="input"` yourself (or use the +`confirmation` target plus +`data-client-side-validations-confirms="field_id"` for confirmation +inputs). You can add all attributes with validators for the given object by passing nothing: @@ -377,25 +413,36 @@ were overwritten by the call to `FormBuilder#validate` ## Customize Error Rendering ## -`ClientSideValidations` will use `ActiveRecord::Base.field_error_proc` to render the error messages. Other `FormBuilders` will use their own settings. +`ClientSideValidations` uses `ActionView::Base.field_error_proc` (or the +current form builder's equivalent settings) to decide how error markup +should look on the server. If you change that markup in +`config/initializers/client_side_validations.rb`, mirror the same shape +in the client-side form builder functions. -If you need to change the markup of how the errors are rendered you can modify that in `config/initializers/client_side_validations.rb` - -*Please Note* if you modify the markup, you will also need to modify `ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder']`'s `add` and `remove` functions. You can override the behavior by creating a new JavaScript file called `rails.validations.actionView.js` that contains the following: +A simple way to do that is to create +`app/javascript/client_side_validations.js` and import it once from your +JavaScript entrypoint (`app/javascript/application.js`, +`application.ts`, etc.): ```js -window.ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = { - add: function(element, settings, message) { +import ClientSideValidations from '@client-side-validations/client-side-validations' + +ClientSideValidations.formBuilders['ActionView::Helpers::FormBuilder'] = { + add (element, settings, message) { // custom add code here }, - remove: function(element, settings) { + remove (element, settings) { // custom remove code here } } ``` -Please view the code in `rails.validations.js` to see how the existing `add` and `remove` functions work and how best to override for your specific use-case. +If you prefer, you can place the same code in the generated +`app/javascript/controllers/client_side_validations_controller.js` +before exporting the controller. Either way, make sure the +customization runs once before the form is validated for the first +time. ## Custom Validators ## @@ -434,16 +481,15 @@ en: email: "Not an email address" ``` -Finally we need to add a client side validator. This can be done by hooking into the `ClientSideValidations.validator` object. Create a new file `app/assets/javascripts/rails.validations.customValidators.js` +Finally we need to add a client side validator. Register it in the same +`app/javascript/client_side_validations.js` file (or any module that is +imported once during boot): ```js -// The options variable is a JSON Object -// The element variable is a DOM element -window.ClientSideValidations.validators.local.email = function (element, options) { - // Your validator code goes in here +import ClientSideValidations from '@client-side-validations/client-side-validations' + +ClientSideValidations.validators.local.email = (element, options) => { if (!/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test(element.value)) { - // When the value fails to pass validation you need to return the error message. - // It can be derived from validator.message return options.message } } @@ -460,41 +506,21 @@ end Client Side Validations will apply the new validator and validate your forms as needed. -## Enabling, Disabling, and Resetting on the client ## - -There are many reasons why you might want to enable, disable, or even completely reset the bound validation events on the client. `ClientSideValidations` offers a simple API for this. - -### Enabling ### - -If you have rendered a new form via AJAX into your page you will need to enable that form for validation: - -```js -ClientSideValidations.enable(newForm) -``` - -You should attach this to an event that is fired when the new HTML renders. - -You can use the same function if you introduce new inputs to an existing form: - -```js -ClientSideValidations.enable(newInput) -``` - -### Disabling ### - -If you wish to turn off validations entirely on a form: - -```js -ClientSideValidations.disable(form) -``` - -### Resetting ### +## Dynamic forms and inputs ## -You can reset the current state of the validations, clear all error messages, and reattach clean event handlers: +Stimulus now owns binding and unbinding, so there is no separate +`enable`, `disable`, or `reset` API in 25.x. -```js -ClientSideValidations.reset(form) -``` +* If you insert a new form generated with `validate: true`, Stimulus + connects automatically when the form enters the DOM. +* If you append new fields generated by the Rails helpers inside an + existing validated form, the helper-generated target attributes let + Stimulus bind them automatically. +* If you append markup by hand, keep it inside the same form and add + `data-client-side-validations-target="input"` (or `confirmation` + with `data-client-side-validations-confirms="field_id"`) yourself. +* To stop validation for a dynamic field, remove the element or remove + the target attribute and let Stimulus disconnect it. ## Callbacks ## @@ -509,41 +535,52 @@ ClientSideValidations.reset(form) * `ClientSideValidations.callbacks.form.fail(form, eventData)` * `ClientSideValidations.callbacks.form.pass(form, eventData)` -The names of the callbacks should be pretty straight forward. For example, `ClientSideValidations.callbacks.form.fail` will be called if a form failed to validate. And `ClientSideValidations.callbacks.element.before` will be called before that particular element's validations are run. +The names of the callbacks are literal: for example, +`ClientSideValidations.callbacks.form.fail` runs when a form fails +validation, and `ClientSideValidations.callbacks.element.before` runs +before a particular element is validated. -All element callbacks receive the DOM element as the first parameter and the native event object as the second parameter. `ClientSideValidations.callbacks.element.fail()` receives the failed message as the second parameter, the callback for adding error fields as the third parameter, and the eventData object as the fourth parameter. `ClientSideValidations.callbacks.element.pass()` receives the callback for removing the error fields as the second parameter. The error field callbacks must still be invoked by your custom callback. +The `eventData` argument is the native `CustomEvent` dispatched by the +runtime. `ClientSideValidations.callbacks.element.fail()` receives the +failed message as the second parameter, the callback for adding error +fields as the third parameter, and the eventData object as the fourth +parameter. `ClientSideValidations.callbacks.element.pass()` receives +the callback for removing the error fields as the second parameter. +If you override either callback, you must still invoke `addError()` or +`removeError()` yourself. -All form callbacks receive the DOM form element as the first parameter and the native event object as the second parameter. +A simple setup is to create `app/javascript/client_side_validations.js` +and import it once from your JavaScript entrypoint: -Here is an example callback that animates the error message when validation fails: +```js +import ClientSideValidations from '@client-side-validations/client-side-validations' -``` javascript -window.ClientSideValidations.callbacks.element.fail = function (element, message, callback) { - callback() +ClientSideValidations.callbacks.element.fail = (element, message, addError, event) => { + addError() - var messageElement = element.parentElement.querySelector('.message') + const messageElement = element.parentElement.querySelector('.message') - if (messageElement) { - if (typeof messageElement.animate === 'function') { - messageElement.animate( - [ - { opacity: 0, transform: 'translateX(-8px)' }, - { opacity: 1, transform: 'translateX(0)' } - ], - { duration: 250, easing: 'ease-out', fill: 'both' } - ) - } else { - messageElement.style.opacity = '1' - messageElement.style.transform = 'translateX(0)' - } + if (messageElement && typeof messageElement.animate === 'function') { + messageElement.animate( + [ + { opacity: 0, transform: 'translateX(-8px)' }, + { opacity: 1, transform: 'translateX(0)' } + ], + { duration: 250, easing: 'ease-out', fill: 'both' } + ) } } -window.ClientSideValidations.callbacks.element.pass = function (element, callback) { - callback() +ClientSideValidations.callbacks.element.pass = (element, removeError, event) => { + removeError() } ``` +If you prefer to keep all ClientSideValidations-specific wiring in one +place, the generated +`app/javascript/controllers/client_side_validations_controller.js` stub +is also a good home for these assignments. + ``` css .message { background-color: red; @@ -574,15 +611,19 @@ By default, ClientSideValidations will automatically validate the form. If for some reason you would like to manually validate the form (for example you're working with a multi-step form), you can use the following approach: ```js +import { isValid } from '@client-side-validations/client-side-validations' + const input = document.getElementById('myInputField') const form = input.form -const validators = form.ClientSideValidations.settings.validators -// Validate a single field -ClientSideValidations.isValid(input, validators) +// Wait until Stimulus has connected the form, then validate. +isValid(input) +isValid(form) -// Validate the whole form -ClientSideValidations.isValid(form, validators) +// Or pass validators explicitly if you need them. +const validators = form.ClientSideValidations.settings.validators +isValid(input, validators) +isValid(form, validators) ``` To manually validate a single field, you may also trigger a focusout event: diff --git a/package.json b/package.json index 1ce4e86bc..7adeb98a4 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,9 @@ ".": { "import": "./dist/client-side-validations.esm.js", "default": "./dist/client-side-validations.esm.js" - } + }, + "./src/*": "./src/*", + "./src/*.js": "./src/*.js" }, "engines": { "node": ">= 18.12" From 9560b211483adc23510a23f14b1828be5a6e76e8 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Mon, 20 Apr 2026 23:22:30 +0200 Subject: [PATCH 3/3] fix --- client_side_validations.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client_side_validations.gemspec b/client_side_validations.gemspec index 346d7e65a..998453345 100644 --- a/client_side_validations.gemspec +++ b/client_side_validations.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.metadata['changelog_uri'] = 'https://github.com/DavyJonesLocker/client_side_validations/blob/main/CHANGELOG.md' spec.metadata['source_code_uri'] = 'https://github.com/DavyJonesLocker/client_side_validations' - spec.files = Dir.glob('{CHANGELOG.md,LICENSE.md,README.md,lib/**/*}', File::FNM_DOTMATCH) + spec.files = Dir.glob('{CHANGELOG.md,LICENSE.md,README.md,lib/**/*.rb}', File::FNM_DOTMATCH) spec.require_paths = ['lib'] spec.platform = Gem::Platform::RUBY