1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+
7+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
8+
9+ var _util = require ( './util' ) ;
10+
11+ var _Validator = require ( './Validator' ) ;
12+
13+ var _Validator2 = _interopRequireDefault ( _Validator ) ;
14+
15+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
16+
17+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
18+
19+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
20+
21+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
22+
23+ /**
24+ * validator 组件
25+ */
26+ var Validator = function ( _ValidatorCore ) {
27+ _inherits ( Validator , _ValidatorCore ) ;
28+
29+ function Validator ( ) {
30+ _classCallCheck ( this , Validator ) ;
31+
32+ return _possibleConstructorReturn ( this , ( Validator . __proto__ || Object . getPrototypeOf ( Validator ) ) . apply ( this , arguments ) ) ;
33+ }
34+
35+ _createClass ( Validator , [ {
36+ key : 'afterFieldValidate' ,
37+
38+
39+ /**
40+ * Field 验证之后处理
41+ * @param isSuccess
42+ * @param errors
43+ */
44+ value : function afterFieldValidate ( isSuccess , errors ) {
45+ // 错误信息操作
46+ if ( errors ) {
47+ // 添加错误类信息
48+ var clazz = this . opts . prefix + '-error' ;
49+ errors . clazz = clazz ;
50+ // 设置错误 id
51+ errors . placeId = ( clazz + '_' + ( errors . id || errors . name ) ) . replace ( '-' , '_' ) ;
52+
53+ // 当前条目验证结果展示
54+ if ( isSuccess ) {
55+ this . _removeErrorPlace ( errors ) ;
56+ } else {
57+ this . _addErrorPlace ( errors ) ;
58+ }
59+ }
60+ }
61+
62+ /**
63+ * 绑定用户输入事件和改变事件
64+ * @param {String } name 属性
65+ * @param {String } level 事件级别 off/change/all
66+ */
67+
68+ } , {
69+ key : 'onInputEvent' ,
70+ value : function onInputEvent ( name , level ) {
71+ var validateFieldFunc = function ( that ) {
72+ return function ( e ) {
73+ try {
74+ var evt = ( 0 , _util . getCurrentEvent ) ( e ) ;
75+ var el = evt . target || evt . srcElement ;
76+ var field = that . fields [ el . name ] ;
77+
78+ // 设置触发事件的表单元素
79+ field . el = that . _getArrayByName ( field . name ) ;
80+ // 验证单个表单
81+ return that . _validateField ( field ) ;
82+ } catch ( ex ) {
83+ return null ;
84+ }
85+ } ;
86+ } ( this ) ;
87+
88+ // 绑定表单值改变拦截
89+ var formEls = name ? this . _getArrayByName ( name ) : this . form . elements ;
90+
91+ for ( var i = 0 , formElsLength = formEls . length ; i < formElsLength ; i ++ ) {
92+ var oninput = void 0 ;
93+ var onchange = void 0 ;
94+ var noop = function noop ( ) { } ;
95+ var thatLevel = level || this . opts . eventLevel ;
96+ // 触发事件绑定
97+ switch ( thatLevel ) {
98+ case 'off' :
99+ oninput = noop ;
100+ onchange = noop ;
101+ break ;
102+ case 'change' :
103+ oninput = noop ;
104+ onchange = validateFieldFunc ;
105+ break ;
106+ case 'all' :
107+ oninput = validateFieldFunc ;
108+ onchange = validateFieldFunc ;
109+ break ;
110+ default :
111+ break ;
112+ }
113+ // 针对 IE 浏览器使用 onkeyup 事件
114+ var thisEl = formEls [ i ] ;
115+ if ( ! ! window . ActiveXObject || 'ActiveXObject' in window ) {
116+ thisEl . onkeyup = oninput ;
117+ } else {
118+ thisEl . oninput = oninput ;
119+ }
120+ thisEl . onchange = onchange ;
121+ }
122+ return this ;
123+ }
124+
125+ /**
126+ * 移除当前条目错误信息
127+ * @param {Object } errorObj 验证信息域
128+ */
129+
130+ } , {
131+ key : '_removeErrorPlace' ,
132+ value : function _removeErrorPlace ( errorObj ) {
133+ if ( ! errorObj . el ) {
134+ return ;
135+ }
136+
137+ // 移除表单域错误类
138+ for ( var i = 0 , elLength = errorObj . el . length ; i < elLength ; i ++ ) {
139+ ( 0 , _util . removeClass ) ( errorObj . el [ i ] , errorObj . clazz ) ;
140+ ( 0 , _util . addClass ) ( errorObj . el [ i ] , this . opts . prefix + '-success' ) ;
141+ }
142+
143+ // 移除错误信息节点
144+ var errorEl = document . getElementById ( errorObj . placeId ) ;
145+ errorEl && errorEl . parentNode . removeChild ( errorEl ) ;
146+ }
147+
148+ /**
149+ * 添加当前条目错误信息
150+ * @param {Object } errorObj 验证信息域
151+ */
152+
153+ } , {
154+ key : '_addErrorPlace' ,
155+ value : function _addErrorPlace ( errorObj ) {
156+ if ( ! errorObj . el ) {
157+ return ;
158+ }
159+
160+ // 清除之前保留的错误信息
161+ this . _removeErrorPlace ( errorObj ) ;
162+
163+ var opts = this . opts ;
164+
165+ // 当前表单域添加错误类
166+ for ( var i = 0 , elLength = errorObj . el . length ; i < elLength ; i ++ ) {
167+ ( 0 , _util . removeClass ) ( errorObj . el [ i ] , opts . prefix + '-success' ) ;
168+ ( 0 , _util . addClass ) ( errorObj . el [ i ] , errorObj . clazz ) ;
169+ }
170+
171+ // 创建元素
172+ var errorEl = document . createElement ( opts . errorEl ) ;
173+ ( 0 , _util . addClass ) ( errorEl , errorObj . clazz + '-message' ) ;
174+ errorEl . setAttribute ( 'id' , errorObj . placeId ) ;
175+ errorEl . innerText = errorObj . message ;
176+
177+ // 错误信息位置
178+ if ( typeof opts . errorPlacement === 'function' ) {
179+ // 参数:错误信息节点,当前表单节点
180+ opts . errorPlacement ( errorEl , errorObj . el [ 0 ] ) ;
181+ } else {
182+ // 默认错误信息位置
183+ // label 、 radio 元素错误位置不固定,默认暂不设置
184+ var fieldEl = errorObj . el [ 0 ] ;
185+ if ( ! ( 0 , _util . isRadioOrCheckbox ) ( fieldEl ) ) {
186+ fieldEl . parentNode . appendChild ( errorEl ) ;
187+ }
188+ }
189+ }
190+ } ] ) ;
191+
192+ return Validator ;
193+ } ( _Validator2 . default ) ;
194+
195+ exports . default = Validator ;
196+ module . exports = exports [ 'default' ] ;
0 commit comments