'use strict'; import $ from 'jquery'; import { Plugin } from './foundation.core.plugin'; import { GetYoDigits } from './foundation.core.utils'; /** * Abide module. * @module foundation.abide */ class Abide extends Plugin { /** * Creates a new instance of Abide. * @class * @name Abide * @fires Abide#init * @param {Object} element - jQuery object to add the trigger to. * @param {Object} options - Overrides to the default plugin settings. */ _setup(element, options = {}) { this.$element = element; this.options = $.extend(true, {}, Abide.defaults, this.$element.data(), options); this.className = 'Abide'; // ie9 back compat this._init(); } /** * Initializes the Abide plugin and calls functions to get Abide functioning on load. * @private */ _init() { this.$inputs = $.merge( // Consider as input to validate: this.$element.find('input').not('[type=submit]'), // * all input fields expect submit this.$element.find('textarea, select') // * all textareas and select fields ); const $globalErrors = this.$element.find('[data-abide-error]'); // Add a11y attributes to all fields if (this.options.a11yAttributes) { this.$inputs.each((i, input) => this.addA11yAttributes($(input))); $globalErrors.each((i, error) => this.addGlobalErrorA11yAttributes($(error))); } this._events(); } /** * Initializes events for Abide. * @private */ _events() { this.$element.off('.abide') .on('reset.zf.abide', () => { this.resetForm(); }) .on('submit.zf.abide', () => { return this.validateForm(); }); if (this.options.validateOn === 'fieldChange') { this.$inputs .off('change.zf.abide') .on('change.zf.abide', (e) => { this.validateInput($(e.target)); }); } if (this.options.liveValidate) { this.$inputs .off('input.zf.abide') .on('input.zf.abide', (e) => { this.validateInput($(e.target)); }); } if (this.options.validateOnBlur) { this.$inputs .off('blur.zf.abide') .on('blur.zf.abide', (e) => { this.validateInput($(e.target)); }); } } /** * Calls necessary functions to update Abide upon DOM change * @private */ _reflow() { this._init(); } /** * Checks whether or not a form element has the required attribute and if it's checked or not * @param {Object} element - jQuery object to check for required attribute * @returns {Boolean} Boolean value depends on whether or not attribute is checked or empty */ requiredCheck($el) { if (!$el.attr('required')) return true; var isGood = true; switch ($el[0].type) { case 'checkbox': isGood = $el[0].checked; break; case 'select': case 'select-one': case 'select-multiple': var opt = $el.find('option:selected'); if (!opt.length || !opt.val()) isGood = false; break; default: if(!$el.val() || !$el.val().length) isGood = false; } return isGood; } /** * Get: * - Based on $el, the first element(s) corresponding to `formErrorSelector` in this order: * 1. The element's direct sibling('s). * 2. The element's parent's children. * - Element(s) with the attribute `[data-form-error-for]` set with the element's id. * * This allows for multiple form errors per input, though if none are found, no form errors will be shown. * * @param {Object} $el - jQuery object to use as reference to find the form error selector. * @returns {Object} jQuery object with the selector. */ findFormError($el) { var id = $el[0].id; var $error = $el.siblings(this.options.formErrorSelector); if (!$error.length) { $error = $el.parent().find(this.options.formErrorSelector); } if (id) { $error = $error.add(this.$element.find(`[data-form-error-for="${id}"]`)); } return $error; } /** * Get the first element in this order: * 2. The