11import { FormControl , FormGroup } from '@angular/forms' ;
22
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-
323/** @inheritdoc */
33- var TypedFormControl = /** @class */ ( function ( _super ) {
34- __extends ( TypedFormControl , _super ) ;
4+ class TypedFormControl extends FormControl {
355 /** @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 ,
4010 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 ) ;
4413 }
4514 /** @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+ }
4918 /** @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+ }
5322 /** @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+ }
5726 /**
5827 * Sets new validators and updates possible error keys list.
5928 *
6029 * @param newValidators new validators
6130 */
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 ] )
6635 : null ) ;
6736 this . registeredValidators = this . generateValidatorNames ( newValidators ) ;
68- } ;
37+ }
6938 /**
7039 * Generates validator name list.
7140 *
7241 * @param validatorOpts options to handle
7342 */
74- TypedFormControl . prototype . generateValidatorNames = function ( validatorOpts ) {
75- var keys = [ ] ;
43+ generateValidatorNames ( validatorOpts ) {
44+ let keys = [ ] ;
7645 if ( validatorOpts ) {
77- var validatorsList = [ validatorOpts . validators ] ;
46+ let validatorsList = [ validatorOpts . validators ] ;
7847 if ( validatorOpts . asyncValidators ) {
7948 validatorsList . push ( validatorOpts . asyncValidators ) ;
8049 }
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 ] )
8453 // filter duplicates
85- . filter ( function ( key , index , array ) { return array . indexOf ( key ) === index ; } ) ) ;
54+ . filter ( ( key , index , array ) => array . indexOf ( key ) === index ) ) ;
8655 } ) ;
8756 }
8857 return keys ;
89- } ;
90- return TypedFormControl ;
91- } ( FormControl ) ) ;
58+ }
59+ }
9260
9361/**
9462 * This is the base from control factory for each model. Based on this class, each model is implementing it's own
9563 * FormControlFactory.
9664 * Main purpose of this factory is to provide an easy way of creating form-controls with the correct validators.
9765 * The factory also ensures that model and validator match one another.
9866 */
99- var BaseFormControlFactory = /** @ class */ ( function ( ) {
67+ class BaseFormControlFactory {
10068 /**
10169 * Constructor.
10270 *
10371 * @param model The model object.
10472 * @param validators properties validators map
10573 */
106- function BaseFormControlFactory ( model , validators ) {
74+ constructor ( model , validators ) {
10775 this . map = new Map ( ) ;
108- for ( var property in model ) {
76+ for ( const property in model ) {
10977 if ( ! model . hasOwnProperty ( property ) ) {
11078 continue ;
11179 }
@@ -122,92 +90,89 @@ var BaseFormControlFactory = /** @class */ (function () {
12290 * @param controlOpts add custom validators to the default ones given in the constructor, optional async validators
12391 * and update mode.
12492 */
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 ) ;
12795 if ( model ) {
12896 return new TypedFormControl ( model . value , {
129- validators : model . validators . concat ( ( controlOpts ? controlOpts . validators : [ ] ) ) ,
97+ validators : [ ... model . validators , ... ( controlOpts ? controlOpts . validators : [ ] ) ] ,
13098 asyncValidators : controlOpts ? controlOpts . asyncValidators : undefined ,
13199 updateOn : controlOpts ? controlOpts . updateOn : undefined ,
132100 } ) ;
133101 }
134102 return new TypedFormControl ( ) ;
135- } ;
136- return BaseFormControlFactory ;
137- } ( ) ) ;
103+ }
104+ }
138105
139106/**
140107 * Typed FormGroup.
141108 */
142- var TypedFormGroup = /** @class */ ( function ( _super ) {
143- __extends ( TypedFormGroup , _super ) ;
109+ class TypedFormGroup extends FormGroup {
144110 /** @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 ] ;
150116 if ( control instanceof TypedFormControl ) {
151117 Object . defineProperty ( map , controlName , {
152- get : function ( ) {
118+ get : ( ) => {
153119 return control . registeredValidators ;
154120 } ,
155121 } ) ;
156122 }
157123 } ) ;
158- _this . registeredValidatorsMap = map ;
159- return _this ;
124+ this . registeredValidatorsMap = map ;
160125 }
161126 /** @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+ }
165130 /** @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+ }
169134 /**
170135 * Returns group if available.
171136 *
172137 * @param path path to group
173138 */
174- TypedFormGroup . prototype . getNestedGroup = function ( path ) {
175- var control = this . get ( path ) ;
139+ getNestedGroup ( path ) {
140+ const control = this . get ( path ) ;
176141 if ( control instanceof TypedFormGroup ) {
177142 return control ;
178143 }
179144 return null ;
180- } ;
145+ }
181146 /**
182147 * Detects if an error is present for given control name.
183148 *
184149 * @param name control name of the form group
185150 */
186- TypedFormGroup . prototype . hasControlErrors = function ( name ) {
187- var control = this . get ( name ) ;
151+ hasControlErrors ( name ) {
152+ const control = this . get ( name ) ;
188153 return ! ! ( control && control . errors ) ;
189- } ;
154+ }
190155 /**
191156 * Detects if control has validator for given control name and validator name.
192157 *
193158 * @param name control name of the form group
194159 * @param validatorName validator name
195160 */
196- TypedFormGroup . prototype . isValidatorRegistered = function ( name , validatorName ) {
161+ isValidatorRegistered ( name , validatorName ) {
197162 return ( this . registeredValidatorsMap [ name ] &&
198- this . registeredValidatorsMap [ name ] . some ( function ( errorKey ) { return errorKey === validatorName ; } ) ) ;
199- } ;
163+ this . registeredValidatorsMap [ name ] . some ( errorKey => errorKey === validatorName ) ) ;
164+ }
200165 /**
201166 * Returns an error key for the next error.
202167 *
203168 * @param name control key of the form group
204169 */
205- TypedFormGroup . prototype . nextControlErrorKey = function ( name ) {
206- var control = this . get ( name ) ;
170+ nextControlErrorKey ( name ) {
171+ const control = this . get ( name ) ;
207172 if ( control && control . errors ) {
208173 // 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 ) ) ;
211176 if ( ! error ) {
212177 // fallback to all errors including custom errors set after backend calls
213178 error = Object . keys ( control . errors ) . shift ( ) ;
@@ -217,27 +182,25 @@ var TypedFormGroup = /** @class */ (function (_super) {
217182 }
218183 }
219184 return '' ;
220- } ;
185+ }
221186 /**
222187 * Dispatches errors to this control and to child controls using given error map.
223188 *
224189 * @param errors error map
225190 * @param contextPath optional context path to errors set to
226191 */
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 ) ) ;
232196 if ( control ) {
233197 // enables showing errors in view
234198 control . enable ( ) ;
235199 control . markAsTouched ( ) ;
236200 control . setErrors ( errors [ path ] ) ;
237201 }
238202 } ) ;
239- } ;
240- return TypedFormGroup ;
241- } ( FormGroup ) ) ;
203+ }
204+ }
242205
243206export { BaseFormControlFactory , TypedFormControl , TypedFormGroup } ;
0 commit comments