1
1
import { FormControl , FormGroup } from '@angular/forms' ;
2
2
3
- /*! *****************************************************************************
4
- Copyright (c) Microsoft Corporation. All rights reserved.
5
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
6
- this file except in compliance with the License. You may obtain a copy of the
7
- License at http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
11
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
12
- MERCHANTABLITY OR NON-INFRINGEMENT.
13
-
14
- See the Apache Version 2.0 License for specific language governing permissions
15
- and limitations under the License.
16
- ***************************************************************************** */
17
- /* global Reflect, Promise */
18
-
19
- var extendStatics = function ( d , b ) {
20
- extendStatics = Object . setPrototypeOf ||
21
- ( { __proto__ : [ ] } instanceof Array && function ( d , b ) { d . __proto__ = b ; } ) ||
22
- function ( d , b ) { for ( var p in b ) if ( b . hasOwnProperty ( p ) ) d [ p ] = b [ p ] ; } ;
23
- return extendStatics ( d , b ) ;
24
- } ;
25
-
26
- function __extends ( d , b ) {
27
- extendStatics ( d , b ) ;
28
- function __ ( ) { this . constructor = d ; }
29
- d . prototype = b === null ? Object . create ( b ) : ( __ . prototype = b . prototype , new __ ( ) ) ;
30
- }
31
-
32
3
/** @inheritdoc */
33
- var TypedFormControl = /** @class */ ( function ( _super ) {
34
- __extends ( TypedFormControl , _super ) ;
4
+ class TypedFormControl extends FormControl {
35
5
/** @inheritdoc */
36
- function TypedFormControl ( formState , opts ) {
37
- var _this = _super . call ( this , formState , {
38
- validators : opts ? opts . validators . map ( function ( validator ) { return validator && validator [ 1 ] ; } ) : null ,
39
- asyncValidators : opts && opts . asyncValidators ? opts . asyncValidators . map ( function ( validator ) { return validator && validator [ 1 ] ; } ) : null ,
6
+ constructor ( formState , opts ) {
7
+ super ( formState , {
8
+ validators : opts ? opts . validators . map ( validator => validator && validator [ 1 ] ) : null ,
9
+ asyncValidators : opts && opts . asyncValidators ? opts . asyncValidators . map ( validator => validator && validator [ 1 ] ) : null ,
40
10
updateOn : opts && opts . updateOn ? opts . updateOn : 'change' ,
41
- } ) || this ;
42
- _this . registeredValidators = _this . generateValidatorNames ( opts ) ;
43
- return _this ;
11
+ } ) ;
12
+ this . registeredValidators = this . generateValidatorNames ( opts ) ;
44
13
}
45
14
/** @inheritdoc */
46
- TypedFormControl . prototype . patchValue = function ( value , options ) {
47
- _super . prototype . patchValue . call ( this , value , options ) ;
48
- } ;
15
+ patchValue ( value , options ) {
16
+ super . patchValue ( value , options ) ;
17
+ }
49
18
/** @inheritdoc */
50
- TypedFormControl . prototype . setValue = function ( value , options ) {
51
- _super . prototype . setValue . call ( this , value , options ) ;
52
- } ;
19
+ setValue ( value , options ) {
20
+ super . setValue ( value , options ) ;
21
+ }
53
22
/** @inheritdoc */
54
- TypedFormControl . prototype . reset = function ( formState , options ) {
55
- _super . prototype . reset . call ( this , formState , options ) ;
56
- } ;
23
+ reset ( formState , options ) {
24
+ super . reset ( formState , options ) ;
25
+ }
57
26
/**
58
27
* Sets new validators and updates possible error keys list.
59
28
*
60
29
* @param newValidators new validators
61
30
*/
62
- TypedFormControl . prototype . setNewValidators = function ( newValidators ) {
63
- _super . prototype . setValidators . call ( this , newValidators ? newValidators . validators . map ( function ( validator ) { return validator && validator [ 1 ] ; } ) : null ) ;
64
- _super . prototype . setAsyncValidators . call ( this , newValidators && newValidators . asyncValidators
65
- ? newValidators . asyncValidators . map ( function ( validator ) { return validator && validator [ 1 ] ; } )
31
+ setNewValidators ( newValidators ) {
32
+ super . setValidators ( newValidators ? newValidators . validators . map ( validator => validator && validator [ 1 ] ) : null ) ;
33
+ super . setAsyncValidators ( newValidators && newValidators . asyncValidators
34
+ ? newValidators . asyncValidators . map ( validator => validator && validator [ 1 ] )
66
35
: null ) ;
67
36
this . registeredValidators = this . generateValidatorNames ( newValidators ) ;
68
- } ;
37
+ }
69
38
/**
70
39
* Generates validator name list.
71
40
*
72
41
* @param validatorOpts options to handle
73
42
*/
74
- TypedFormControl . prototype . generateValidatorNames = function ( validatorOpts ) {
75
- var keys = [ ] ;
43
+ generateValidatorNames ( validatorOpts ) {
44
+ let keys = [ ] ;
76
45
if ( validatorOpts ) {
77
- var validatorsList = [ validatorOpts . validators ] ;
46
+ let validatorsList = [ validatorOpts . validators ] ;
78
47
if ( validatorOpts . asyncValidators ) {
79
48
validatorsList . push ( validatorOpts . asyncValidators ) ;
80
49
}
81
- validatorsList . forEach ( function ( validators ) {
82
- keys . push . apply ( keys , validators
83
- . map ( function ( validator ) { return validator && validator [ 0 ] ; } )
50
+ validatorsList . forEach ( ( validators ) => {
51
+ keys . push ( ... validators
52
+ . map ( validator => validator && validator [ 0 ] )
84
53
// filter duplicates
85
- . filter ( function ( key , index , array ) { return array . indexOf ( key ) === index ; } ) ) ;
54
+ . filter ( ( key , index , array ) => array . indexOf ( key ) === index ) ) ;
86
55
} ) ;
87
56
}
88
57
return keys ;
89
- } ;
90
- return TypedFormControl ;
91
- } ( FormControl ) ) ;
58
+ }
59
+ }
92
60
93
61
/**
94
62
* This is the base from control factory for each model. Based on this class, each model is implementing it's own
95
63
* FormControlFactory.
96
64
* Main purpose of this factory is to provide an easy way of creating form-controls with the correct validators.
97
65
* The factory also ensures that model and validator match one another.
98
66
*/
99
- var BaseFormControlFactory = /** @ class */ ( function ( ) {
67
+ class BaseFormControlFactory {
100
68
/**
101
69
* Constructor.
102
70
*
103
71
* @param model The model object.
104
72
* @param validators properties validators map
105
73
*/
106
- function BaseFormControlFactory ( model , validators ) {
74
+ constructor ( model , validators ) {
107
75
this . map = new Map ( ) ;
108
- for ( var property in model ) {
76
+ for ( const property in model ) {
109
77
if ( ! model . hasOwnProperty ( property ) ) {
110
78
continue ;
111
79
}
@@ -122,92 +90,89 @@ var BaseFormControlFactory = /** @class */ (function () {
122
90
* @param controlOpts add custom validators to the default ones given in the constructor, optional async validators
123
91
* and update mode.
124
92
*/
125
- BaseFormControlFactory . prototype . createFormControl = function ( property , controlOpts ) {
126
- var model = this . map . get ( property ) ;
93
+ createFormControl ( property , controlOpts ) {
94
+ const model = this . map . get ( property ) ;
127
95
if ( model ) {
128
96
return new TypedFormControl ( model . value , {
129
- validators : model . validators . concat ( ( controlOpts ? controlOpts . validators : [ ] ) ) ,
97
+ validators : [ ... model . validators , ... ( controlOpts ? controlOpts . validators : [ ] ) ] ,
130
98
asyncValidators : controlOpts ? controlOpts . asyncValidators : undefined ,
131
99
updateOn : controlOpts ? controlOpts . updateOn : undefined ,
132
100
} ) ;
133
101
}
134
102
return new TypedFormControl ( ) ;
135
- } ;
136
- return BaseFormControlFactory ;
137
- } ( ) ) ;
103
+ }
104
+ }
138
105
139
106
/**
140
107
* Typed FormGroup.
141
108
*/
142
- var TypedFormGroup = /** @class */ ( function ( _super ) {
143
- __extends ( TypedFormGroup , _super ) ;
109
+ class TypedFormGroup extends FormGroup {
144
110
/** @inheritdoc */
145
- function TypedFormGroup ( controls , validatorOrOpts , asyncValidator ) {
146
- var _this = _super . call ( this , controls , validatorOrOpts , asyncValidator ) || this ;
147
- var map = { } ;
148
- Object . keys ( controls ) . forEach ( function ( controlName ) {
149
- var control = controls [ controlName ] ;
111
+ constructor ( controls , validatorOrOpts , asyncValidator ) {
112
+ super ( controls , validatorOrOpts , asyncValidator ) ;
113
+ const map = { } ;
114
+ Object . keys ( controls ) . forEach ( controlName => {
115
+ const control = controls [ controlName ] ;
150
116
if ( control instanceof TypedFormControl ) {
151
117
Object . defineProperty ( map , controlName , {
152
- get : function ( ) {
118
+ get : ( ) => {
153
119
return control . registeredValidators ;
154
120
} ,
155
121
} ) ;
156
122
}
157
123
} ) ;
158
- _this . registeredValidatorsMap = map ;
159
- return _this ;
124
+ this . registeredValidatorsMap = map ;
160
125
}
161
126
/** @inheritdoc */
162
- TypedFormGroup . prototype . patchValue = function ( value , options ) {
163
- _super . prototype . patchValue . call ( this , value , options ) ;
164
- } ;
127
+ patchValue ( value , options ) {
128
+ super . patchValue ( value , options ) ;
129
+ }
165
130
/** @inheritdoc */
166
- TypedFormGroup . prototype . get = function ( path ) {
167
- return _super . prototype . get . call ( this , path ) ;
168
- } ;
131
+ get ( path ) {
132
+ return super . get ( path ) ;
133
+ }
169
134
/**
170
135
* Returns group if available.
171
136
*
172
137
* @param path path to group
173
138
*/
174
- TypedFormGroup . prototype . getNestedGroup = function ( path ) {
175
- var control = this . get ( path ) ;
139
+ getNestedGroup ( path ) {
140
+ const control = this . get ( path ) ;
176
141
if ( control instanceof TypedFormGroup ) {
177
142
return control ;
178
143
}
179
144
return null ;
180
- } ;
145
+ }
181
146
/**
182
147
* Detects if an error is present for given control name.
183
148
*
184
149
* @param name control name of the form group
185
150
*/
186
- TypedFormGroup . prototype . hasControlErrors = function ( name ) {
187
- var control = this . get ( name ) ;
151
+ hasControlErrors ( name ) {
152
+ const control = this . get ( name ) ;
188
153
return ! ! ( control && control . errors ) ;
189
- } ;
154
+ }
190
155
/**
191
156
* Detects if control has validator for given control name and validator name.
192
157
*
193
158
* @param name control name of the form group
194
159
* @param validatorName validator name
195
160
*/
196
- TypedFormGroup . prototype . isValidatorRegistered = function ( name , validatorName ) {
161
+ isValidatorRegistered ( name , validatorName ) {
197
162
return ( this . registeredValidatorsMap [ name ] &&
198
- this . registeredValidatorsMap [ name ] . some ( function ( errorKey ) { return errorKey === validatorName ; } ) ) ;
199
- } ;
163
+ this . registeredValidatorsMap [ name ] . some ( errorKey => errorKey === validatorName ) ) ;
164
+ }
200
165
/**
201
166
* Returns an error key for the next error.
202
167
*
203
168
* @param name control key of the form group
204
169
*/
205
- TypedFormGroup . prototype . nextControlErrorKey = function ( name ) {
206
- var control = this . get ( name ) ;
170
+ nextControlErrorKey ( name ) {
171
+ const control = this . get ( name ) ;
207
172
if ( control && control . errors ) {
208
173
// try client side keys first for correct order
209
- var error = this . registeredValidatorsMap [ name ] &&
210
- this . registeredValidatorsMap [ name ] . find ( function ( validatorKey ) { return control . hasError ( validatorKey ) ; } ) ;
174
+ let error = this . registeredValidatorsMap [ name ] &&
175
+ this . registeredValidatorsMap [ name ] . find ( validatorKey => control . hasError ( validatorKey ) ) ;
211
176
if ( ! error ) {
212
177
// fallback to all errors including custom errors set after backend calls
213
178
error = Object . keys ( control . errors ) . shift ( ) ;
@@ -217,27 +182,25 @@ var TypedFormGroup = /** @class */ (function (_super) {
217
182
}
218
183
}
219
184
return '' ;
220
- } ;
185
+ }
221
186
/**
222
187
* Dispatches errors to this control and to child controls using given error map.
223
188
*
224
189
* @param errors error map
225
190
* @param contextPath optional context path to errors set to
226
191
*/
227
- TypedFormGroup . prototype . dispatchErrors = function ( errors , contextPath ) {
228
- var _this = this ;
229
- var paths = Object . keys ( errors ) ;
230
- paths . forEach ( function ( path ) {
231
- var control = _this . get ( ( contextPath ? contextPath + "." + path : path ) ) ;
192
+ dispatchErrors ( errors , contextPath ) {
193
+ const paths = Object . keys ( errors ) ;
194
+ paths . forEach ( path => {
195
+ const control = this . get ( ( contextPath ? `${ contextPath } .${ path } ` : path ) ) ;
232
196
if ( control ) {
233
197
// enables showing errors in view
234
198
control . enable ( ) ;
235
199
control . markAsTouched ( ) ;
236
200
control . setErrors ( errors [ path ] ) ;
237
201
}
238
202
} ) ;
239
- } ;
240
- return TypedFormGroup ;
241
- } ( FormGroup ) ) ;
203
+ }
204
+ }
242
205
243
206
export { BaseFormControlFactory , TypedFormControl , TypedFormGroup } ;
0 commit comments