1/** 2 * Validation wrapper. 3 * 4 * @author Htmlstream 5 * @version 1.0 6 * @requires 7 * 8 */ 9var isEmpty = function isEmpty(f) { 10 return (/^function[^{]+\{\s*\}/m.test(f.toString()) 11 ); 12} 13 14;(function ($) { 15 'use strict'; 16 17 $.HSCore.components.HSValidation = { 18 /** 19 * 20 * 21 * @var Object _baseConfig 22 */ 23 _baseConfig: { 24 errorElement: 'div', 25 errorClass: 'invalid-feedback', 26 rules: {}, 27 onkeyup: function(element){$(element).valid()}, 28 errorPlacement: function(){}, 29 highlight: function(){}, 30 unhighlight: function(){}, 31 submitHandler: function(){} 32 }, 33 34 /** 35 * 36 * 37 * @var jQuery pageCollection 38 */ 39 pageCollection: $(), 40 41 /** 42 * Initialization of Validation wrapper. 43 * 44 * @param String selector (optional) 45 * @param Object config (optional) 46 * 47 * @return jQuery pageCollection - collection of initialized items. 48 */ 49 50 init: function (selector, config) { 51 this.collection = selector && $(selector).length ? $(selector) : $(); 52 if (!$(selector).length) return; 53 54 this.config = config && $.isPlainObject(config) ? 55 $.extend({}, this._baseConfig, config) : this._baseConfig; 56 57 this.config.itemSelector = selector; 58 59 this.initValidation(); 60 61 return this.pageCollection; 62 }, 63 64 initValidation: function () { 65 //Variables 66 var $self = this, 67 config = $self.config, 68 collection = $self.pageCollection; 69 70 //Actions 71 this.collection.each(function (i, el) { 72 //Variables 73 var $this = $(el); 74 75 if ($this.hasClass('js-step-form')) { 76 $.validator.setDefaults({ 77 ignore: ':hidden:not(.active select)' 78 }); 79 } else { 80 $.validator.setDefaults({ 81 ignore: ':hidden:not(select)' 82 }); 83 } 84 85 $this.validate({ 86 errorElement: config['errorElement'], 87 errorClass: config['errorClass'], 88 rules: config['rules'], 89 onkeyup: config['onkeyup'], 90 errorPlacement: isEmpty(config['errorPlacement']) === true ? $self.errorPlacement : config['errorPlacement'], 91 highlight: isEmpty(config['highlight']) === true ? $self.highlight : config['highlight'], 92 unhighlight: isEmpty(config['unhighlight']) === true ? $self.unHighlight : config['unhighlight'], 93 submitHandler: isEmpty(config['submitHandler']) === true ? $self.submitHandler : config['submitHandler'] 94 }); 95 96 if($this.find('select').length) { 97 98 $('select').change(function () { 99 $(this).valid(); 100 }); 101 102 } 103 104 //Actions 105 collection = collection.add($this); 106 }); 107 }, 108 109 errorPlacement: function (error, element) { 110 var $this = $(element), 111 errorMsgClasses = $this.data('error-msg-classes'); 112 113 error.addClass(errorMsgClasses); 114 error.appendTo(element.parents('.js-form-message')); 115 }, 116 117 highlight: function (element) { 118 var $this = $(element), 119 errorClass = $this.data('error-class'), 120 successClass = $this.data('success-class'); 121 122 $this.parents('.js-form-message').removeClass(successClass).addClass(errorClass); 123 }, 124 125 unHighlight: function (element) { 126 var $this = $(element), 127 errorClass = $this.data('error-class'), 128 successClass = $this.data('success-class'); 129 130 $this.parents('.js-form-message').removeClass(errorClass).addClass(successClass); 131 }, 132 133 submitHandler: function (form) { 134 form.submit(); 135 } 136 } 137})(jQuery);
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.