1Namespace.register('mira.form').FormValidationItem = (function () { 2 3 // Constructor 4 var FormValidationItem = function (element, validators) { 5 6 this._element = document.getElementById(element); 7 8 if (validators) { 9 this._validators = validators; 10 } 11 12 this._wrapper = this._findWrapper(); 13 14 if (this._wrapper) { 15 this._element.addEventListener('blur', this.validate.bind(this)); 16 } 17 18 }; 19 20 21 // Prototype 22 var p = FormValidationItem.prototype; 23 24 /* 25 * Find the nearest .form-item element. 26 * 27 * @method _findWrapper 28 * @class FormValidationItem 29 * @return The nearest parent with a '.form-item'-class. 30 */ 31 32 p._findWrapper = function () { 33 34 var element = this._element, 35 maxLevel = 10, 36 counter = 0; 37 38 // get form-item wrapper 39 while (!mira.element.hasClass(element, 'form-item') && counter < maxLevel) { 40 element = element.parentNode; 41 counter++; 42 } 43 44 return element; 45 46 }; 47 48 /* 49 * Add/remove class to wrapper on validation 50 * 51 * @method _updateWrapper 52 * @class FormValidationItem 53 * @valid Boolean. 54 * 55 */ 56 57 p._updateWrapper = function (valid) { 58 valid ? mira.element.removeClass(this._wrapper, 'form-item-error') : mira.element.addClass(this._wrapper, 'form-item-error'); 59 }; 60 61 /* 62 * Loop through the validators for this form-item. 63 * 64 * @method validate 65 * @class FormValidationItem 66 * 67 */ 68 69 p.validate = function () { 70 if (this._validators) { 71 72 valid = true; 73 74 // loop through validators for this input 75 for (var j = this._validators.length; j--; ) { 76 if (!this._validators[j].isvalid) { 77 valid = false; 78 break; 79 } 80 } 81 82 this._updateWrapper(valid); 83 84 } 85 }; 86 87 return FormValidationItem; 88 89} ());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.