'use strict'; !function($) { /** * Abide module. * @module foundation.abide */ class Abide { /** * Creates a new instance of Abide. * @class * @fires Abide#init * @param {Object} element - jQuery object to add the trigger to. * @param {Object} options - Overrides to the default plugin settings. */ constructor(element, options = {}) { this.$element = element; this.options = $.extend({}, Abide.defaults, this.$element.data(), options); this._init(); Foundation.registerPlugin(this, 'Abide'); } /** * Initializes the Abide plugin and calls functions to get Abide functioning on load. * @private */ _init() { this.$inputs = this.$element.find('input, textarea, select').not('[data-abide-ignore]'); 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)); }); } } /** * 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 '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; } /** * Based on $el, get the first element with selector in this order: * 1. The element's direct sibling('s). * 3. The element's parent's children. * * 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 $error = $el.siblings(this.options.formErrorSelector); if (!$error.length) { $error = $el.parent().find(this.options.formErrorSelector); } return $error; } /** * Get the first element in this order: * 2. The