@nativescript/compiler-angular 0.0.0-stage → 9.2.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/angular-libraries.js +173 -0
- package/dist/angular.js +2895 -0
- package/dist/component-bases.js +746 -0
- package/dist/core-api.js +8 -0
- package/dist/entry.js +118 -0
- package/dist/index.js +157 -0
- package/dist/kotlin.js +280 -0
- package/dist/modules.js +698 -0
- package/dist/ng-modules.js +66 -0
- package/dist/package.js +15 -0
- package/dist/refusals.js +5 -0
- package/dist/semantics.js +213 -0
- package/dist/swift.js +340 -0
- package/kit-android/fromcore/AngularAPI.kt +172 -0
- package/kit-android/fromcore/AngularRouter.kt +587 -0
- package/kit-android/handport/Router.kt +151 -0
- package/kit-android/main/AngularChangeDetection.kt +250 -0
- package/kit-android/main/AngularCore.kt +685 -0
- package/kit-android/main/AngularHttp.kt +431 -0
- package/kit-android/main/AngularRouterValues.kt +154 -0
- package/kit-android/main/AngularRx.kt +224 -0
- package/kit-apple/AngularAPI.swift +224 -0
- package/kit-apple/AngularChangeDetection.swift +248 -0
- package/kit-apple/AngularCore.swift +764 -0
- package/kit-apple/AngularHttp.swift +467 -0
- package/kit-apple/AngularRouter.swift +626 -0
- package/kit-apple/AngularRouterValues.swift +285 -0
- package/kit-apple/AngularRx.swift +246 -0
- package/kit-apple/kit-index.json +739 -0
- package/package.json +53 -4
- package/runtime/angular-forms.ts +3099 -0
- package/runtime/angular-rx.ts +550 -0
- package/runtime/angular.ts +826 -0
- package/README.md +0 -3
|
@@ -0,0 +1,3099 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @angular/forms 22.2.3 (packages/forms/src: model/*, validators.ts, form_builder.ts), ported for the release build,
|
|
3
|
+
* which compiles this module in place of the package.
|
|
4
|
+
*
|
|
5
|
+
* The MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2010-2026 Google LLC. https://angular.dev/license
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in
|
|
17
|
+
* all copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
25
|
+
* THE SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
// Differences from the package, each forced by what the release build compiles:
|
|
29
|
+
// - No rxjs: valueChanges, statusChanges and events are EventEmitters (Angular's EventEmitter is a Subject), and an async
|
|
30
|
+
// validator's Promise or Observable is subscribed to directly, with forkJoin's semantics where several are composed
|
|
31
|
+
// (which needs each Observable to complete: the Android build's rxjs has no completion).
|
|
32
|
+
// - `value` and `errors` are accessors over private fields; `status`, `pristine` and `touched` are plain fields
|
|
33
|
+
// written through methods, not signals (the package reads its signals untracked, and the computed `_status`,
|
|
34
|
+
// `_pristine`, `_touched` and `_hasRequired` that its directives read are left out).
|
|
35
|
+
// - ControlEvent declares `source`, which each event passes to it.
|
|
36
|
+
// - A FormGroup's `controls` is its own object holding the controls it was given (an object literal compiles to a
|
|
37
|
+
// fixed shape, from which removeControl could not delete a key), and a FormArray's its own array: the native
|
|
38
|
+
// build reads an array of controls typed, which copies it.
|
|
39
|
+
// - `new FormControl(x)` is a FormControl<any> unless its type argument is given: the package's typing of the
|
|
40
|
+
// constructor (`FormControl<T | null>`, or `FormControl<T>` when nonNullable) needs a class expression.
|
|
41
|
+
// - NonNullableFormBuilder and UntypedFormBuilder are services of their own that make controls as FormBuilder does,
|
|
42
|
+
// not FormBuilders (`instanceof FormBuilder` is false), and `nonNullable` gives a NonNullableFormBuilder. Each
|
|
43
|
+
// injector gives the one root instance (`shared`), which `inject()` reads.
|
|
44
|
+
// - Errors carry the package's development-mode messages; its development-mode console warnings are not printed.
|
|
45
|
+
import { EventEmitter, InjectionToken, untracked } from '@angular/core';
|
|
46
|
+
import { type Observable } from 'rxjs';
|
|
47
|
+
import type { AsyncValidator, ControlValueAccessor, Validator } from '@angular#forms';
|
|
48
|
+
|
|
49
|
+
// ---- directives/validators.ts and directives/control_value_accessor.ts (the types) ----
|
|
50
|
+
|
|
51
|
+
// The interfaces an app implements are declarations only: a class implementing one is called through it untyped.
|
|
52
|
+
export type { AsyncValidator, ControlValueAccessor, Validator } from '@angular#forms';
|
|
53
|
+
|
|
54
|
+
export type ValidationErrors = { [key: string]: any };
|
|
55
|
+
|
|
56
|
+
export interface ValidatorFn {
|
|
57
|
+
(control: AbstractControl): ValidationErrors | null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface AsyncValidatorFn {
|
|
61
|
+
(control: AbstractControl): Promise<ValidationErrors | null> | Observable<ValidationErrors | null>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const NG_VALUE_ACCESSOR = new InjectionToken<ReadonlyArray<ControlValueAccessor>>('NgValueAccessor');
|
|
65
|
+
|
|
66
|
+
// ---- validators.ts ----
|
|
67
|
+
|
|
68
|
+
function isEmptyInputValue(value: unknown): boolean {
|
|
69
|
+
return value == null || lengthOrSize(value) === 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function lengthOrSize(value: unknown): number | null {
|
|
73
|
+
// Read untyped: narrowed, a typed array or Set (`Set<number>`) would be cast to one of any values.
|
|
74
|
+
const untyped: any = value;
|
|
75
|
+
if (value == null) {
|
|
76
|
+
return null;
|
|
77
|
+
} else if (Array.isArray(value) || typeof value === 'string') {
|
|
78
|
+
return untyped.length;
|
|
79
|
+
} else if (value instanceof Set) {
|
|
80
|
+
return untyped.size;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const NG_VALIDATORS = new InjectionToken<ReadonlyArray<Validator | Function>>('NgValidators');
|
|
86
|
+
export const NG_ASYNC_VALIDATORS = new InjectionToken<ReadonlyArray<Validator | Function>>('NgAsyncValidators');
|
|
87
|
+
|
|
88
|
+
const EMAIL_REGEXP =
|
|
89
|
+
/^(?=.{1,254}$)(?=.{1,64}@)[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
90
|
+
|
|
91
|
+
export class Validators {
|
|
92
|
+
static min(min: number): ValidatorFn {
|
|
93
|
+
return minValidator(min);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static max(max: number): ValidatorFn {
|
|
97
|
+
return maxValidator(max);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static required(control: AbstractControl): ValidationErrors | null {
|
|
101
|
+
return requiredValidator(control);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static requiredTrue(control: AbstractControl): ValidationErrors | null {
|
|
105
|
+
return requiredTrueValidator(control);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static email(control: AbstractControl): ValidationErrors | null {
|
|
109
|
+
return emailValidator(control);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static minLength(minLength: number): ValidatorFn {
|
|
113
|
+
return minLengthValidator(minLength);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
static maxLength(maxLength: number): ValidatorFn {
|
|
117
|
+
return maxLengthValidator(maxLength);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
static pattern(pattern: string | RegExp): ValidatorFn {
|
|
121
|
+
return patternValidator(pattern);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static nullValidator(control: AbstractControl): ValidationErrors | null {
|
|
125
|
+
return nullValidator(control);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
static compose(validators: null): null;
|
|
129
|
+
static compose(validators: (ValidatorFn | null | undefined)[]): ValidatorFn | null;
|
|
130
|
+
static compose(validators: (ValidatorFn | null | undefined)[] | null): ValidatorFn | null {
|
|
131
|
+
return compose(validators);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
static composeAsync(validators: (AsyncValidatorFn | null)[]): AsyncValidatorFn | null {
|
|
135
|
+
return composeAsync(validators);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function minValidator(min: number): ValidatorFn {
|
|
140
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
141
|
+
if (control.value == null || min == null) {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const value = parseFloat(control.value);
|
|
145
|
+
return !isNaN(value) && value < min ? { 'min': { 'min': min, 'actual': control.value } } : null;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function maxValidator(max: number): ValidatorFn {
|
|
150
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
151
|
+
if (control.value == null || max == null) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const value = parseFloat(control.value);
|
|
155
|
+
return !isNaN(value) && value > max ? { 'max': { 'max': max, 'actual': control.value } } : null;
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function requiredValidator(control: AbstractControl): ValidationErrors | null {
|
|
160
|
+
return isEmptyInputValue(control.value) ? { 'required': true } : null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function requiredTrueValidator(control: AbstractControl): ValidationErrors | null {
|
|
164
|
+
return control.value === true ? null : { 'required': true };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function emailValidator(control: AbstractControl): ValidationErrors | null {
|
|
168
|
+
if (isEmptyInputValue(control.value)) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
return EMAIL_REGEXP.test(control.value) ? null : { 'email': true };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function minLengthValidator(minLength: number): ValidatorFn {
|
|
175
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
176
|
+
const length = control.value?.length ?? lengthOrSize(control.value);
|
|
177
|
+
if (length === null || length === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
return length < minLength ? { 'minlength': { 'requiredLength': minLength, 'actualLength': length } } : null;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function maxLengthValidator(maxLength: number): ValidatorFn {
|
|
185
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
186
|
+
const length = control.value?.length ?? lengthOrSize(control.value);
|
|
187
|
+
if (length !== null && length > maxLength) {
|
|
188
|
+
return { 'maxlength': { 'requiredLength': maxLength, 'actualLength': length } };
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function patternValidator(pattern: string | RegExp): ValidatorFn {
|
|
195
|
+
if (!pattern) return nullValidator;
|
|
196
|
+
let regex: RegExp;
|
|
197
|
+
let regexStr: string;
|
|
198
|
+
if (typeof pattern === 'string') {
|
|
199
|
+
regexStr = '';
|
|
200
|
+
if (pattern.charAt(0) !== '^') regexStr += '^';
|
|
201
|
+
regexStr += pattern;
|
|
202
|
+
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
|
|
203
|
+
regex = new RegExp(regexStr);
|
|
204
|
+
} else {
|
|
205
|
+
regexStr = pattern.toString();
|
|
206
|
+
regex = pattern;
|
|
207
|
+
}
|
|
208
|
+
return (control: AbstractControl): ValidationErrors | null => {
|
|
209
|
+
if (isEmptyInputValue(control.value)) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const value: string = control.value;
|
|
213
|
+
return regex.test(value) ? null : { 'pattern': { 'requiredPattern': regexStr, 'actualValue': value } };
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function nullValidator(control: AbstractControl): ValidationErrors | null {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function isPresent(o: any): boolean {
|
|
222
|
+
return o != null;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function isPromise(obj: any): boolean {
|
|
226
|
+
return !!obj && typeof obj.then === 'function';
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function isSubscribable(obj: any): boolean {
|
|
230
|
+
return !!obj && typeof obj.subscribe === 'function';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** What rxjs's `subscribe` takes: an observer, or its `next`. */
|
|
234
|
+
function asObserver(observerOrNext: any): any {
|
|
235
|
+
if (typeof observerOrNext !== 'function') return observerOrNext;
|
|
236
|
+
return { next: observerOrNext };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** rxjs's `from(promise)`: the resolved value, then completion, unless unsubscribed first. */
|
|
240
|
+
class PromiseSource {
|
|
241
|
+
constructor(private readonly promise: any) {}
|
|
242
|
+
|
|
243
|
+
subscribe(observerOrNext: any): any {
|
|
244
|
+
const observer = asObserver(observerOrNext);
|
|
245
|
+
const subscription = new Subscription();
|
|
246
|
+
const settled: any = this.promise;
|
|
247
|
+
settled.then(
|
|
248
|
+
(value: any) => {
|
|
249
|
+
if (subscription.closed) return;
|
|
250
|
+
observer.next?.(value);
|
|
251
|
+
observer.complete?.();
|
|
252
|
+
},
|
|
253
|
+
(error: any) => {
|
|
254
|
+
if (!subscription.closed) observer.error?.(error);
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
return subscription;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
class Subscription {
|
|
262
|
+
closed = false;
|
|
263
|
+
private readonly inner: any[] = [];
|
|
264
|
+
|
|
265
|
+
add(inner: any): void {
|
|
266
|
+
if (this.closed) inner.unsubscribe();
|
|
267
|
+
else this.inner.push(inner);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
unsubscribe(): void {
|
|
271
|
+
if (this.closed) return;
|
|
272
|
+
this.closed = true;
|
|
273
|
+
for (const s of this.inner) s.unsubscribe();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function toObservable(value: any): any {
|
|
278
|
+
const obs = isPromise(value) ? new PromiseSource(value) : value;
|
|
279
|
+
if (!isSubscribable(obs)) {
|
|
280
|
+
let errorMessage = `Expected async validator to return Promise or Observable.`;
|
|
281
|
+
if (typeof value === 'object') {
|
|
282
|
+
errorMessage += ' Are you using a synchronous validator where an async validator is expected?';
|
|
283
|
+
}
|
|
284
|
+
throw new Error(`NG01101: ${errorMessage} Find more at https://v22.angular.dev/errors/NG01101`);
|
|
285
|
+
}
|
|
286
|
+
return obs;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** rxjs's `forkJoin(sources).pipe(map(project))`: each source's last value once all complete; nothing if one completes empty. */
|
|
290
|
+
class ForkJoin {
|
|
291
|
+
constructor(private readonly sources: any[], private readonly project: (values: any[]) => any) {}
|
|
292
|
+
|
|
293
|
+
subscribe(observerOrNext: any): any {
|
|
294
|
+
const observer = asObserver(observerOrNext);
|
|
295
|
+
const subscription = new Subscription();
|
|
296
|
+
const values: any[] = this.sources.map(() => undefined);
|
|
297
|
+
const has: boolean[] = this.sources.map(() => false);
|
|
298
|
+
let remaining = this.sources.length;
|
|
299
|
+
if (remaining === 0) {
|
|
300
|
+
observer.complete?.();
|
|
301
|
+
return subscription;
|
|
302
|
+
}
|
|
303
|
+
this.sources.forEach((source, index) => {
|
|
304
|
+
if (subscription.closed) return;
|
|
305
|
+
subscription.add(source.subscribe({
|
|
306
|
+
next: (value: any) => {
|
|
307
|
+
values[index] = value;
|
|
308
|
+
has[index] = true;
|
|
309
|
+
},
|
|
310
|
+
error: (error: any) => {
|
|
311
|
+
if (subscription.closed) return;
|
|
312
|
+
subscription.unsubscribe();
|
|
313
|
+
observer.error?.(error);
|
|
314
|
+
},
|
|
315
|
+
complete: () => {
|
|
316
|
+
if (subscription.closed) return;
|
|
317
|
+
remaining--;
|
|
318
|
+
if (!has[index]) {
|
|
319
|
+
subscription.unsubscribe();
|
|
320
|
+
observer.complete?.();
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
if (remaining === 0) {
|
|
324
|
+
subscription.unsubscribe();
|
|
325
|
+
observer.next?.(this.project(values));
|
|
326
|
+
observer.complete?.();
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
}));
|
|
330
|
+
});
|
|
331
|
+
return subscription;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function mergeErrors(arrayOfErrors: any[]): ValidationErrors | null {
|
|
336
|
+
const res: { [key: string]: any } = {};
|
|
337
|
+
for (const errors of arrayOfErrors) {
|
|
338
|
+
if (errors != null) for (const key of Object.keys(errors)) res[key] = errors[key];
|
|
339
|
+
}
|
|
340
|
+
return Object.keys(res).length === 0 ? null : res;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function isValidatorFn(validator: any): boolean {
|
|
344
|
+
return !validator.validate;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Untyped until each is told apart: a Validator object read as a function would be wrapped as one.
|
|
348
|
+
function normalizeValidators(validators: any[]): ValidatorFn[] {
|
|
349
|
+
const normalized: ValidatorFn[] = [];
|
|
350
|
+
for (const validator of validators) {
|
|
351
|
+
if (isValidatorFn(validator)) normalized.push(validator);
|
|
352
|
+
else normalized.push((c: AbstractControl): ValidationErrors | null => (validator as any).validate(c));
|
|
353
|
+
}
|
|
354
|
+
return normalized;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function normalizeAsyncValidators(validators: any[]): AsyncValidatorFn[] {
|
|
358
|
+
const normalized: AsyncValidatorFn[] = [];
|
|
359
|
+
for (const validator of validators) {
|
|
360
|
+
if (isValidatorFn(validator)) normalized.push(validator);
|
|
361
|
+
else normalized.push((c: AbstractControl): any => (validator as any).validate(c));
|
|
362
|
+
}
|
|
363
|
+
return normalized;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function compose(validators: (ValidatorFn | null | undefined)[] | null): ValidatorFn | null {
|
|
367
|
+
if (!validators) return null;
|
|
368
|
+
const presentValidators: ValidatorFn[] = [];
|
|
369
|
+
for (const v of validators) if (v != null) presentValidators.push(v);
|
|
370
|
+
return composePresent(presentValidators);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function composePresent(presentValidators: ValidatorFn[]): ValidatorFn | null {
|
|
374
|
+
if (presentValidators.length == 0) return null;
|
|
375
|
+
return function (control: AbstractControl) {
|
|
376
|
+
const results: any[] = [];
|
|
377
|
+
for (const validator of presentValidators) results.push(validator(control));
|
|
378
|
+
return mergeErrors(results);
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export function composeValidators(validators: any[]): ValidatorFn | null {
|
|
383
|
+
return validators != null ? composePresent(normalizeValidators(validators)) : null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function composeAsync(validators: (AsyncValidatorFn | null)[]): AsyncValidatorFn | null {
|
|
387
|
+
if (!validators) return null;
|
|
388
|
+
const presentValidators: AsyncValidatorFn[] = [];
|
|
389
|
+
for (const v of validators) if (v != null) presentValidators.push(v);
|
|
390
|
+
return composeAsyncPresent(presentValidators);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function composeAsyncPresent(presentValidators: AsyncValidatorFn[]): AsyncValidatorFn | null {
|
|
394
|
+
if (presentValidators.length == 0) return null;
|
|
395
|
+
return function (control: AbstractControl) {
|
|
396
|
+
const observables: any[] = [];
|
|
397
|
+
for (const validator of presentValidators) observables.push(toObservable(validator(control)));
|
|
398
|
+
return new ForkJoin(observables, (values: any[]) => mergeErrors(values));
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function composeAsyncValidators(validators: any[]): AsyncValidatorFn | null {
|
|
403
|
+
return validators != null ? composeAsyncPresent(normalizeAsyncValidators(validators)) : null;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export function mergeValidators<V>(controlValidators: V | V[] | null, dirValidator: V): V[] {
|
|
407
|
+
if (controlValidators === null) return [dirValidator];
|
|
408
|
+
return Array.isArray(controlValidators) ? [...controlValidators, dirValidator] : [controlValidators, dirValidator];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function getControlValidators(control: AbstractControl): ValidatorFn | ValidatorFn[] | null {
|
|
412
|
+
return control._rawValidators;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export function getControlAsyncValidators(control: AbstractControl): AsyncValidatorFn | AsyncValidatorFn[] | null {
|
|
416
|
+
return control._rawAsyncValidators;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Validators are held untyped from here on: a function boxed once keeps its identity, which hasValidator compares.
|
|
420
|
+
|
|
421
|
+
export function makeValidatorsArray(validators: any): any[] {
|
|
422
|
+
if (!validators) return [];
|
|
423
|
+
return Array.isArray(validators) ? validators : [validators];
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export function hasValidator(validators: any, validator: any): boolean {
|
|
427
|
+
return Array.isArray(validators) ? validators.includes(validator) : validators === validator;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export function addValidators(validators: any, currentValidators: any): any[] {
|
|
431
|
+
const current = makeValidatorsArray(currentValidators);
|
|
432
|
+
const validatorsToAdd = makeValidatorsArray(validators);
|
|
433
|
+
validatorsToAdd.forEach((v: any) => {
|
|
434
|
+
if (!hasValidator(current, v)) {
|
|
435
|
+
current.push(v);
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
return current;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
export function removeValidators(validators: any, currentValidators: any): any[] {
|
|
442
|
+
return makeValidatorsArray(currentValidators).filter((v: any) => !hasValidator(validators, v));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// ---- model/abstract_model.ts ----
|
|
446
|
+
|
|
447
|
+
export const VALID = 'VALID';
|
|
448
|
+
export const INVALID = 'INVALID';
|
|
449
|
+
export const PENDING = 'PENDING';
|
|
450
|
+
export const DISABLED = 'DISABLED';
|
|
451
|
+
|
|
452
|
+
export type FormControlStatus = 'VALID' | 'INVALID' | 'PENDING' | 'DISABLED';
|
|
453
|
+
|
|
454
|
+
export abstract class ControlEvent<T = any> {
|
|
455
|
+
constructor(public readonly source: AbstractControl<unknown>) {}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export class ValueChangeEvent<T> extends ControlEvent<T> {
|
|
459
|
+
constructor(public readonly value: T, source: AbstractControl) {
|
|
460
|
+
super(source);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export class PristineChangeEvent extends ControlEvent {
|
|
465
|
+
constructor(public readonly pristine: boolean, source: AbstractControl) {
|
|
466
|
+
super(source);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export class TouchedChangeEvent extends ControlEvent {
|
|
471
|
+
constructor(public readonly touched: boolean, source: AbstractControl) {
|
|
472
|
+
super(source);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export class StatusChangeEvent extends ControlEvent {
|
|
477
|
+
constructor(public readonly status: FormControlStatus, source: AbstractControl) {
|
|
478
|
+
super(source);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
export class FormSubmittedEvent extends ControlEvent {
|
|
483
|
+
constructor(source: AbstractControl) {
|
|
484
|
+
super(source);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
export class FormResetEvent extends ControlEvent {
|
|
489
|
+
constructor(source: AbstractControl) {
|
|
490
|
+
super(source);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export function pickValidators(validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null): ValidatorFn | ValidatorFn[] | null {
|
|
495
|
+
return (isOptionsObj(validatorOrOpts) ? validatorOrOpts.validators : validatorOrOpts) || null;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function coerceToValidator(validator: any): ValidatorFn | null {
|
|
499
|
+
return Array.isArray(validator) ? composeValidators(validator) : validator || null;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export function pickAsyncValidators(asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null): AsyncValidatorFn | AsyncValidatorFn[] | null {
|
|
503
|
+
return (isOptionsObj(validatorOrOpts) ? validatorOrOpts.asyncValidators : asyncValidator) || null;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function coerceToAsyncValidator(asyncValidator: any): AsyncValidatorFn | null {
|
|
507
|
+
return Array.isArray(asyncValidator) ? composeAsyncValidators(asyncValidator) : asyncValidator || null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export type FormHooks = 'change' | 'blur' | 'submit';
|
|
511
|
+
|
|
512
|
+
export interface AbstractControlOptions {
|
|
513
|
+
validators?: ValidatorFn | ValidatorFn[] | null;
|
|
514
|
+
asyncValidators?: AsyncValidatorFn | AsyncValidatorFn[] | null;
|
|
515
|
+
updateOn?: 'change' | 'blur' | 'submit';
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export function isOptionsObj(validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null): validatorOrOpts is AbstractControlOptions {
|
|
519
|
+
return validatorOrOpts != null && !Array.isArray(validatorOrOpts) && typeof validatorOrOpts === 'object';
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function describeKey(isFormGroup: boolean, key: string | number): string {
|
|
523
|
+
return isFormGroup ? `with name: '${key}'` : `at index: ${key}`;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/** `count` is how many controls the group or array has, `present` whether `key` names one. */
|
|
527
|
+
export function assertControlPresent(count: number, present: boolean, isGroup: boolean, key: string | number): void {
|
|
528
|
+
if (!count) {
|
|
529
|
+
throw new Error(`NG01000: \n There are no form controls registered with this ${isGroup ? 'group' : 'array'} yet. If you're using ngModel,\n you may want to check next tick (e.g. use setTimeout).\n `);
|
|
530
|
+
}
|
|
531
|
+
if (!present) {
|
|
532
|
+
throw new Error(`NG01001: Cannot find form control ${describeKey(isGroup, key)}`);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
export function assertAllValuesPresent(control: AbstractControl, isGroup: boolean, value: any): void {
|
|
537
|
+
control._forEachChild((_: AbstractControl, key: any) => {
|
|
538
|
+
if (value[key] === undefined) {
|
|
539
|
+
throw new Error(`NG01002: Must supply a value for form control ${describeKey(isGroup, key)}. Find more at https://v22.angular.dev/errors/NG01002`);
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
export type ɵIsAny<T, Y, N> = 0 extends 1 & T ? Y : N;
|
|
545
|
+
|
|
546
|
+
export type ɵTypedOrUntyped<T, Typed, Untyped> = ɵIsAny<T, Untyped, Typed>;
|
|
547
|
+
|
|
548
|
+
export type ɵValue<T extends AbstractControl | undefined> = T extends AbstractControl<any, any> ? T['value'] : never;
|
|
549
|
+
|
|
550
|
+
export type ɵRawValue<T extends AbstractControl | undefined> = T extends AbstractControl<any, any> ? (T['setValue'] extends (v: infer R) => void ? R : never) : never;
|
|
551
|
+
|
|
552
|
+
export type ɵTokenize<S extends string, D extends string> = string extends S ? string[] : S extends `${infer T}${D}${infer U}` ? [T, ...ɵTokenize<U, D>] : [S];
|
|
553
|
+
|
|
554
|
+
export type ɵCoerceStrArrToNumArr<S> = S extends [infer Head, ...infer Tail] ? (Head extends `${number}` ? [number, ...ɵCoerceStrArrToNumArr<Tail>] : [Head, ...ɵCoerceStrArrToNumArr<Tail>]) : [];
|
|
555
|
+
|
|
556
|
+
export type ɵNavigate<T, K extends Array<string | number>> = T extends object
|
|
557
|
+
? K extends [infer Head, ...infer Tail]
|
|
558
|
+
? Head extends keyof T
|
|
559
|
+
? Tail extends (string | number)[]
|
|
560
|
+
? [] extends Tail
|
|
561
|
+
? T[Head]
|
|
562
|
+
: ɵNavigate<T[Head], Tail>
|
|
563
|
+
: any
|
|
564
|
+
: never
|
|
565
|
+
: any
|
|
566
|
+
: any;
|
|
567
|
+
|
|
568
|
+
export type ɵWriteable<T> = { -readonly [P in keyof T]: T[P] };
|
|
569
|
+
|
|
570
|
+
export type ɵGetProperty<T, K> = K extends string ? ɵGetProperty<T, ɵCoerceStrArrToNumArr<ɵTokenize<K, '.'>>> : ɵWriteable<K> extends Array<string | number> ? ɵNavigate<T, ɵWriteable<K>> : any;
|
|
571
|
+
|
|
572
|
+
export abstract class AbstractControl<TValue = any, TRawValue extends TValue = TValue, TValueWithOptionalControlStates = any> {
|
|
573
|
+
_pendingDirty = false;
|
|
574
|
+
_hasOwnPendingAsyncValidator: null | { emitEvent: boolean; shouldHaveEmitted: boolean } = null;
|
|
575
|
+
_pendingTouched = false;
|
|
576
|
+
_onCollectionChange: () => void = () => {};
|
|
577
|
+
_updateOn?: FormHooks;
|
|
578
|
+
private _parent: AbstractControl | null = null;
|
|
579
|
+
private _asyncValidationSubscription: any = undefined;
|
|
580
|
+
// Held untyped and called as script calls them: a validator's result may be any object, read here as errors.
|
|
581
|
+
private _composedValidatorFn: any = null;
|
|
582
|
+
private _composedAsyncValidatorFn: any = null;
|
|
583
|
+
_rawValidators: any = null;
|
|
584
|
+
_rawAsyncValidators: any = null;
|
|
585
|
+
_value: any = undefined;
|
|
586
|
+
private _errors: ValidationErrors | null = null;
|
|
587
|
+
private _statusValue: FormControlStatus | undefined = undefined;
|
|
588
|
+
private _pristineValue = true;
|
|
589
|
+
private _touchedValue = false;
|
|
590
|
+
readonly _events = new EventEmitter<ControlEvent<TValue>>();
|
|
591
|
+
public readonly events: EventEmitter<ControlEvent<TValue>> = this._events;
|
|
592
|
+
public readonly valueChanges: EventEmitter<TValue> = new EventEmitter<TValue>();
|
|
593
|
+
public readonly statusChanges: EventEmitter<FormControlStatus> = new EventEmitter<FormControlStatus>();
|
|
594
|
+
// Held untyped: a callback is removed by identity, which a typed function array's element read loses on iOS.
|
|
595
|
+
_onDisabledChange: any[] = [];
|
|
596
|
+
|
|
597
|
+
constructor(validators: ValidatorFn | ValidatorFn[] | null, asyncValidators: AsyncValidatorFn | AsyncValidatorFn[] | null) {
|
|
598
|
+
this._assignValidators(validators);
|
|
599
|
+
this._assignAsyncValidators(asyncValidators);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
get value(): TValue {
|
|
603
|
+
return this._value;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
get validator(): ValidatorFn | null {
|
|
607
|
+
return this._composedValidatorFn;
|
|
608
|
+
}
|
|
609
|
+
set validator(validatorFn: ValidatorFn | null) {
|
|
610
|
+
this._rawValidators = this._composedValidatorFn = validatorFn;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
get asyncValidator(): AsyncValidatorFn | null {
|
|
614
|
+
return this._composedAsyncValidatorFn;
|
|
615
|
+
}
|
|
616
|
+
set asyncValidator(asyncValidatorFn: AsyncValidatorFn | null) {
|
|
617
|
+
this._rawAsyncValidators = this._composedAsyncValidatorFn = asyncValidatorFn;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
get parent(): FormGroup | FormArray | null {
|
|
621
|
+
const parent: any = this._parent;
|
|
622
|
+
return parent;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
get status(): FormControlStatus {
|
|
626
|
+
return this._currentStatus()!;
|
|
627
|
+
}
|
|
628
|
+
/** The status, undefined until the first is calculated: construction reads `disabled` before then. */
|
|
629
|
+
private _currentStatus(): FormControlStatus | undefined {
|
|
630
|
+
return this._statusValue;
|
|
631
|
+
}
|
|
632
|
+
private _setStatus(v: FormControlStatus): void {
|
|
633
|
+
if (this._statusValue === v) return;
|
|
634
|
+
this._statusValue = v;
|
|
635
|
+
this._stateChanged();
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* What Angular's NgControlStatus learns from the `_status`, `_pristine` and `_touched` signals its host bindings read:
|
|
640
|
+
* one of them changed, so the view showing it is checked again.
|
|
641
|
+
*/
|
|
642
|
+
_stateListeners: ControlStateObserver[] = [];
|
|
643
|
+
|
|
644
|
+
private _stateChanged(): void {
|
|
645
|
+
for (const observer of this._stateListeners.slice()) observer.controlStateChanged();
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/** A form's submitted state changed: Angular's NgControlStatusGroup reads it as a signal. */
|
|
649
|
+
_stateChangedOutside(): void {
|
|
650
|
+
this._stateChanged();
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
get valid(): boolean {
|
|
654
|
+
return this._currentStatus() === VALID;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
get invalid(): boolean {
|
|
658
|
+
return this._currentStatus() === INVALID;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
get pending(): boolean {
|
|
662
|
+
return this._currentStatus() === PENDING;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
get disabled(): boolean {
|
|
666
|
+
return this._currentStatus() === DISABLED;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
get enabled(): boolean {
|
|
670
|
+
return this._currentStatus() !== DISABLED;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
get errors(): ValidationErrors | null {
|
|
674
|
+
return this._errors;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
get pristine(): boolean {
|
|
678
|
+
return this._pristineValue;
|
|
679
|
+
}
|
|
680
|
+
private _setPristine(v: boolean): void {
|
|
681
|
+
if (this._pristineValue === v) return;
|
|
682
|
+
this._pristineValue = v;
|
|
683
|
+
this._stateChanged();
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
get dirty(): boolean {
|
|
687
|
+
return !this.pristine;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
get touched(): boolean {
|
|
691
|
+
return this._touchedValue;
|
|
692
|
+
}
|
|
693
|
+
private _setTouched(v: boolean): void {
|
|
694
|
+
if (this._touchedValue === v) return;
|
|
695
|
+
this._touchedValue = v;
|
|
696
|
+
this._stateChanged();
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
get untouched(): boolean {
|
|
700
|
+
return !this.touched;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
get updateOn(): FormHooks {
|
|
704
|
+
return this._updateOn ? this._updateOn : this._parent ? this._parent.updateOn : 'change';
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
setValidators(validators: ValidatorFn | ValidatorFn[] | null): void {
|
|
708
|
+
this._assignValidators(validators);
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
setAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[] | null): void {
|
|
712
|
+
this._assignAsyncValidators(validators);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
addValidators(validators: ValidatorFn | ValidatorFn[]): void {
|
|
716
|
+
this._assignValidators(addValidators(validators, this._rawValidators));
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
addAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void {
|
|
720
|
+
this._assignAsyncValidators(addValidators(validators, this._rawAsyncValidators));
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
removeValidators(validators: ValidatorFn | ValidatorFn[]): void {
|
|
724
|
+
this._assignValidators(removeValidators(validators, this._rawValidators));
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
removeAsyncValidators(validators: AsyncValidatorFn | AsyncValidatorFn[]): void {
|
|
728
|
+
this._assignAsyncValidators(removeValidators(validators, this._rawAsyncValidators));
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
hasValidator(validator: ValidatorFn): boolean {
|
|
732
|
+
return hasValidator(this._rawValidators, validator);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
hasAsyncValidator(validator: AsyncValidatorFn): boolean {
|
|
736
|
+
return hasValidator(this._rawAsyncValidators, validator);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
clearValidators(): void {
|
|
740
|
+
this.validator = null;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
clearAsyncValidators(): void {
|
|
744
|
+
this.asyncValidator = null;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
markAsTouched(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
748
|
+
const changed = this.touched === false;
|
|
749
|
+
this._setTouched(true);
|
|
750
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
751
|
+
if (!opts.onlySelf) {
|
|
752
|
+
this._parent?.markAsTouched({ ...opts, sourceControl });
|
|
753
|
+
}
|
|
754
|
+
if (changed && opts.emitEvent !== false) {
|
|
755
|
+
this._events.next(new TouchedChangeEvent(true, sourceControl));
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
markAllAsDirty(opts: { emitEvent?: boolean } = {}): void {
|
|
760
|
+
this.markAsDirty({ onlySelf: true, emitEvent: opts.emitEvent, sourceControl: this });
|
|
761
|
+
this._forEachChild((control: AbstractControl) => control.markAllAsDirty(opts));
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
markAllAsTouched(opts: { emitEvent?: boolean } = {}): void {
|
|
765
|
+
this.markAsTouched({ onlySelf: true, emitEvent: opts.emitEvent, sourceControl: this });
|
|
766
|
+
this._forEachChild((control: AbstractControl) => control.markAllAsTouched(opts));
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
markAsUntouched(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
770
|
+
const changed = this.touched === true;
|
|
771
|
+
this._setTouched(false);
|
|
772
|
+
this._pendingTouched = false;
|
|
773
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
774
|
+
this._forEachChild((control: AbstractControl) => {
|
|
775
|
+
control.markAsUntouched({ onlySelf: true, emitEvent: opts.emitEvent, sourceControl });
|
|
776
|
+
});
|
|
777
|
+
if (!opts.onlySelf) {
|
|
778
|
+
this._parent?._updateTouched(opts, sourceControl);
|
|
779
|
+
}
|
|
780
|
+
if (changed && opts.emitEvent !== false) {
|
|
781
|
+
this._events.next(new TouchedChangeEvent(false, sourceControl));
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
markAsDirty(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
786
|
+
const changed = this.pristine === true;
|
|
787
|
+
this._setPristine(false);
|
|
788
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
789
|
+
if (!opts.onlySelf) {
|
|
790
|
+
this._parent?.markAsDirty({ ...opts, sourceControl });
|
|
791
|
+
}
|
|
792
|
+
if (changed && opts.emitEvent !== false) {
|
|
793
|
+
this._events.next(new PristineChangeEvent(false, sourceControl));
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
markAsPristine(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
798
|
+
const changed = this.pristine === false;
|
|
799
|
+
this._setPristine(true);
|
|
800
|
+
this._pendingDirty = false;
|
|
801
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
802
|
+
this._forEachChild((control: AbstractControl) => {
|
|
803
|
+
control.markAsPristine({ onlySelf: true, emitEvent: opts.emitEvent });
|
|
804
|
+
});
|
|
805
|
+
if (!opts.onlySelf) {
|
|
806
|
+
this._parent?._updatePristine(opts, sourceControl);
|
|
807
|
+
}
|
|
808
|
+
if (changed && opts.emitEvent !== false) {
|
|
809
|
+
this._events.next(new PristineChangeEvent(true, sourceControl));
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
markAsPending(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
814
|
+
this._setStatus(PENDING);
|
|
815
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
816
|
+
if (opts.emitEvent !== false) {
|
|
817
|
+
this._events.next(new StatusChangeEvent(this.status, sourceControl));
|
|
818
|
+
this.statusChanges.emit(this.status);
|
|
819
|
+
}
|
|
820
|
+
if (!opts.onlySelf) {
|
|
821
|
+
this._parent?.markAsPending({ ...opts, sourceControl });
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
disable(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
826
|
+
const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
|
|
827
|
+
this._setStatus(DISABLED);
|
|
828
|
+
this._errors = null;
|
|
829
|
+
this._forEachChild((control: AbstractControl) => {
|
|
830
|
+
control.disable({ ...opts, onlySelf: true });
|
|
831
|
+
});
|
|
832
|
+
this._updateValue();
|
|
833
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
834
|
+
if (opts.emitEvent !== false) {
|
|
835
|
+
this._events.next(new ValueChangeEvent(this.value, sourceControl));
|
|
836
|
+
this._events.next(new StatusChangeEvent(this.status, sourceControl));
|
|
837
|
+
this.valueChanges.emit(this.value);
|
|
838
|
+
this.statusChanges.emit(this.status);
|
|
839
|
+
}
|
|
840
|
+
this._updateAncestors({ ...opts, skipPristineCheck }, this);
|
|
841
|
+
this._onDisabledChange.forEach((changeFn: any) => changeFn(true));
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
enable(opts: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
845
|
+
const skipPristineCheck = this._parentMarkedDirty(opts.onlySelf);
|
|
846
|
+
this._setStatus(VALID);
|
|
847
|
+
this._forEachChild((control: AbstractControl) => {
|
|
848
|
+
control.enable({ ...opts, onlySelf: true });
|
|
849
|
+
});
|
|
850
|
+
this.updateValueAndValidity({ onlySelf: true, emitEvent: opts.emitEvent });
|
|
851
|
+
this._updateAncestors({ ...opts, skipPristineCheck }, this);
|
|
852
|
+
this._onDisabledChange.forEach((changeFn: any) => changeFn(false));
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
private _updateAncestors(opts: { onlySelf?: boolean; emitEvent?: boolean; skipPristineCheck?: boolean }, sourceControl: AbstractControl): void {
|
|
856
|
+
if (!opts.onlySelf) {
|
|
857
|
+
this._parent?.updateValueAndValidity(opts);
|
|
858
|
+
if (!opts.skipPristineCheck) {
|
|
859
|
+
this._parent?._updatePristine({}, sourceControl);
|
|
860
|
+
}
|
|
861
|
+
this._parent?._updateTouched({}, sourceControl);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
setParent(parent: FormGroup | FormArray | null): void {
|
|
866
|
+
this._parent = parent;
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
abstract setValue(value: TRawValue, options?: Object): void;
|
|
870
|
+
|
|
871
|
+
abstract patchValue(value: TValue, options?: Object): void;
|
|
872
|
+
|
|
873
|
+
abstract reset(value?: TValueWithOptionalControlStates, options?: Object): void;
|
|
874
|
+
|
|
875
|
+
getRawValue(): any {
|
|
876
|
+
return this.value;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
updateValueAndValidity(opts: { onlySelf?: boolean; emitEvent?: boolean; sourceControl?: AbstractControl } = {}): void {
|
|
880
|
+
this._setInitialStatus();
|
|
881
|
+
this._updateValue();
|
|
882
|
+
if (this.enabled) {
|
|
883
|
+
const shouldHaveEmitted = this._cancelExistingSubscription();
|
|
884
|
+
this._errors = this._runValidator();
|
|
885
|
+
this._setStatus(this._calculateStatus());
|
|
886
|
+
if (this.status === VALID || this.status === PENDING) {
|
|
887
|
+
this._runAsyncValidator(shouldHaveEmitted, opts.emitEvent);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
const sourceControl = opts.sourceControl ?? this;
|
|
891
|
+
if (opts.emitEvent !== false) {
|
|
892
|
+
this._events.next(new ValueChangeEvent<TValue>(this.value, sourceControl));
|
|
893
|
+
this._events.next(new StatusChangeEvent(this.status, sourceControl));
|
|
894
|
+
this.valueChanges.emit(this.value);
|
|
895
|
+
this.statusChanges.emit(this.status);
|
|
896
|
+
}
|
|
897
|
+
if (!opts.onlySelf) {
|
|
898
|
+
this._parent?.updateValueAndValidity({ ...opts, sourceControl });
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
_updateTreeValidity(opts: { emitEvent?: boolean } = { emitEvent: true }): void {
|
|
903
|
+
this._forEachChild((ctrl: AbstractControl) => ctrl._updateTreeValidity(opts));
|
|
904
|
+
this.updateValueAndValidity({ onlySelf: true, emitEvent: opts.emitEvent });
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
private _setInitialStatus(): void {
|
|
908
|
+
this._setStatus(this._allControlsDisabled() ? DISABLED : VALID);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
private _runValidator(): ValidationErrors | null {
|
|
912
|
+
const validator = this._composedValidatorFn;
|
|
913
|
+
return validator ? validator(this) : null;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
private _runAsyncValidator(shouldHaveEmitted: boolean, emitEvent?: boolean): void {
|
|
917
|
+
const asyncValidator = this._composedAsyncValidatorFn;
|
|
918
|
+
if (asyncValidator) {
|
|
919
|
+
this._setStatus(PENDING);
|
|
920
|
+
this._hasOwnPendingAsyncValidator = {
|
|
921
|
+
emitEvent: emitEvent !== false,
|
|
922
|
+
shouldHaveEmitted: shouldHaveEmitted !== false,
|
|
923
|
+
};
|
|
924
|
+
const obs = toObservable(asyncValidator(this));
|
|
925
|
+
this._asyncValidationSubscription = obs.subscribe((errors: ValidationErrors | null) => {
|
|
926
|
+
this._hasOwnPendingAsyncValidator = null;
|
|
927
|
+
this.setErrors(errors, { emitEvent, shouldHaveEmitted });
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
private _cancelExistingSubscription(): boolean {
|
|
933
|
+
const subscription = this._asyncValidationSubscription;
|
|
934
|
+
if (subscription) {
|
|
935
|
+
subscription.unsubscribe();
|
|
936
|
+
const shouldHaveEmitted = (this._hasOwnPendingAsyncValidator?.emitEvent || this._hasOwnPendingAsyncValidator?.shouldHaveEmitted) ?? false;
|
|
937
|
+
this._hasOwnPendingAsyncValidator = null;
|
|
938
|
+
return shouldHaveEmitted;
|
|
939
|
+
}
|
|
940
|
+
return false;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
setErrors(errors: ValidationErrors | null, opts: { emitEvent?: boolean; shouldHaveEmitted?: boolean } = {}): void {
|
|
944
|
+
this._errors = errors;
|
|
945
|
+
this._updateControlsErrors(opts.emitEvent !== false, this, opts.shouldHaveEmitted);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
get<P extends string | readonly (string | number)[]>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>> | null;
|
|
949
|
+
get<P extends string | Array<string | number>>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>> | null;
|
|
950
|
+
get<P extends string | (string | number)[]>(path: P): AbstractControl<ɵGetProperty<TRawValue, P>> | null {
|
|
951
|
+
if (path == null) return null;
|
|
952
|
+
const currPath: Array<string | number> = Array.isArray(path) ? path : String(path).split('.');
|
|
953
|
+
if (currPath.length === 0) return null;
|
|
954
|
+
let control: AbstractControl | null = this;
|
|
955
|
+
for (const name of currPath) control = control && control._find(name);
|
|
956
|
+
return control;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
getError(errorCode: string, path?: Array<string | number> | string): any {
|
|
960
|
+
const control = path ? this.get(path) : this;
|
|
961
|
+
const errors = control?.errors;
|
|
962
|
+
return errors ? errors[errorCode] : null;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
hasError(errorCode: string, path?: Array<string | number> | string): boolean {
|
|
966
|
+
return !!this.getError(errorCode, path);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
get root(): AbstractControl {
|
|
970
|
+
let x: AbstractControl = this;
|
|
971
|
+
while (x._parent) {
|
|
972
|
+
x = x._parent;
|
|
973
|
+
}
|
|
974
|
+
return x;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
_updateControlsErrors(emitEvent: boolean, changedControl: AbstractControl, shouldHaveEmitted?: boolean): void {
|
|
978
|
+
this._setStatus(this._calculateStatus());
|
|
979
|
+
if (emitEvent) {
|
|
980
|
+
this.statusChanges.emit(this.status);
|
|
981
|
+
}
|
|
982
|
+
if (emitEvent || shouldHaveEmitted) {
|
|
983
|
+
this._events.next(new StatusChangeEvent(this.status, changedControl));
|
|
984
|
+
}
|
|
985
|
+
if (this._parent) {
|
|
986
|
+
this._parent._updateControlsErrors(emitEvent, changedControl, shouldHaveEmitted);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
private _calculateStatus(): FormControlStatus {
|
|
991
|
+
if (this._allControlsDisabled()) return DISABLED;
|
|
992
|
+
if (this.errors) return INVALID;
|
|
993
|
+
if (this._hasOwnPendingAsyncValidator || this._anyControlsHaveStatus(PENDING)) return PENDING;
|
|
994
|
+
if (this._anyControlsHaveStatus(INVALID)) return INVALID;
|
|
995
|
+
return VALID;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
abstract _updateValue(): void;
|
|
999
|
+
|
|
1000
|
+
abstract _forEachChild(cb: (c: AbstractControl, key: any) => void): void;
|
|
1001
|
+
|
|
1002
|
+
abstract _anyControls(condition: (c: AbstractControl) => boolean): boolean;
|
|
1003
|
+
|
|
1004
|
+
abstract _allControlsDisabled(): boolean;
|
|
1005
|
+
|
|
1006
|
+
abstract _syncPendingControls(): boolean;
|
|
1007
|
+
|
|
1008
|
+
_anyControlsHaveStatus(status: FormControlStatus): boolean {
|
|
1009
|
+
return this._anyControls((control: AbstractControl) => control.status === status);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
_anyControlsDirty(): boolean {
|
|
1013
|
+
return this._anyControls((control: AbstractControl) => control.dirty);
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
_anyControlsTouched(): boolean {
|
|
1017
|
+
return this._anyControls((control: AbstractControl) => control.touched);
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
_updatePristine(opts: { onlySelf?: boolean }, changedControl: AbstractControl): void {
|
|
1021
|
+
const newPristine = !this._anyControlsDirty();
|
|
1022
|
+
const changed = this.pristine !== newPristine;
|
|
1023
|
+
this._setPristine(newPristine);
|
|
1024
|
+
if (!opts.onlySelf) {
|
|
1025
|
+
this._parent?._updatePristine(opts, changedControl);
|
|
1026
|
+
}
|
|
1027
|
+
if (changed) {
|
|
1028
|
+
this._events.next(new PristineChangeEvent(this.pristine, changedControl));
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
_updateTouched(opts: { onlySelf?: boolean } = {}, changedControl: AbstractControl): void {
|
|
1033
|
+
this._setTouched(this._anyControlsTouched());
|
|
1034
|
+
this._events.next(new TouchedChangeEvent(this.touched, changedControl));
|
|
1035
|
+
if (!opts.onlySelf) {
|
|
1036
|
+
this._parent?._updateTouched(opts, changedControl);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
_registerOnCollectionChange(fn: () => void): void {
|
|
1041
|
+
this._onCollectionChange = fn;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
_setUpdateStrategy(opts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null): void {
|
|
1045
|
+
if (isOptionsObj(opts) && opts.updateOn != null) {
|
|
1046
|
+
this._updateOn = opts.updateOn!;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
private _parentMarkedDirty(onlySelf?: boolean): boolean {
|
|
1051
|
+
const parent = this._parent;
|
|
1052
|
+
return !onlySelf && !!parent?.dirty && !parent._anyControlsDirty();
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
_find(name: string | number): AbstractControl | null {
|
|
1056
|
+
return null;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
_assignValidators(validators: any): void {
|
|
1060
|
+
this._rawValidators = Array.isArray(validators) ? validators.slice() : validators;
|
|
1061
|
+
this._composedValidatorFn = coerceToValidator(this._rawValidators);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
_assignAsyncValidators(validators: any): void {
|
|
1065
|
+
this._rawAsyncValidators = Array.isArray(validators) ? validators.slice() : validators;
|
|
1066
|
+
this._composedAsyncValidatorFn = coerceToAsyncValidator(this._rawAsyncValidators);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
export function hasOwnControl(controls: { [key: string]: AbstractControl<any> }, name: string | number | symbol): boolean {
|
|
1071
|
+
return Object.prototype.hasOwnProperty.call(controls, name);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// ---- model/form_control.ts ----
|
|
1075
|
+
|
|
1076
|
+
export interface FormControlState<T> {
|
|
1077
|
+
value: T;
|
|
1078
|
+
disabled: boolean;
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1081
|
+
export interface FormControlOptions extends AbstractControlOptions {
|
|
1082
|
+
nonNullable?: boolean;
|
|
1083
|
+
initialValueIsDefault?: boolean;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function isFormControlState(formState: unknown): formState is FormControlState<unknown> {
|
|
1087
|
+
const state: any = formState;
|
|
1088
|
+
return typeof state === 'object' && state !== null && Object.keys(state).length === 2 && 'value' in state && 'disabled' in state;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
const NULL: any = null;
|
|
1092
|
+
|
|
1093
|
+
function removeListItem<T>(list: T[], el: T): void {
|
|
1094
|
+
const index = list.indexOf(el);
|
|
1095
|
+
if (index > -1) list.splice(index, 1);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
export class FormControl<TValue = any> extends AbstractControl<TValue> {
|
|
1099
|
+
public defaultValue: TValue = NULL;
|
|
1100
|
+
_onChange: any[] = [];
|
|
1101
|
+
_pendingValue: any = undefined;
|
|
1102
|
+
_pendingChange: boolean = false;
|
|
1103
|
+
|
|
1104
|
+
constructor(formState: any = null, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
|
|
1105
|
+
super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
|
|
1106
|
+
this._applyFormState(formState);
|
|
1107
|
+
this._setUpdateStrategy(validatorOrOpts);
|
|
1108
|
+
this.updateValueAndValidity({
|
|
1109
|
+
onlySelf: true,
|
|
1110
|
+
emitEvent: !!this.asyncValidator,
|
|
1111
|
+
});
|
|
1112
|
+
if (isOptionsObj(validatorOrOpts) && ((validatorOrOpts as FormControlOptions).nonNullable || (validatorOrOpts as FormControlOptions).initialValueIsDefault)) {
|
|
1113
|
+
if (isFormControlState(formState)) {
|
|
1114
|
+
this.defaultValue = formState.value as TValue;
|
|
1115
|
+
} else {
|
|
1116
|
+
this.defaultValue = formState;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
override setValue(value: TValue, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean } = {}): void {
|
|
1122
|
+
untracked(() => {
|
|
1123
|
+
this._value = this._pendingValue = value;
|
|
1124
|
+
if (this._onChange.length && options.emitModelToViewChange !== false) {
|
|
1125
|
+
this._onChange.forEach((changeFn: any) => changeFn(this.value, options.emitViewToModelChange !== false));
|
|
1126
|
+
}
|
|
1127
|
+
this.updateValueAndValidity(options);
|
|
1128
|
+
});
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
override patchValue(value: TValue, options: { onlySelf?: boolean; emitEvent?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean } = {}): void {
|
|
1132
|
+
this.setValue(value, options);
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
override reset(formState: TValue | FormControlState<TValue> = this.defaultValue, options: { onlySelf?: boolean; emitEvent?: boolean; overwriteDefaultValue?: boolean } = {}): void {
|
|
1136
|
+
this._applyFormState(formState);
|
|
1137
|
+
this.markAsPristine(options);
|
|
1138
|
+
this.markAsUntouched(options);
|
|
1139
|
+
this.setValue(this.value, options);
|
|
1140
|
+
if (options.overwriteDefaultValue) {
|
|
1141
|
+
this.defaultValue = this.value;
|
|
1142
|
+
}
|
|
1143
|
+
this._pendingChange = false;
|
|
1144
|
+
if (options?.emitEvent !== false) {
|
|
1145
|
+
this._events.next(new FormResetEvent(this));
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
override getRawValue(): TValue {
|
|
1150
|
+
return this.value;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
override _updateValue(): void {}
|
|
1154
|
+
|
|
1155
|
+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
|
|
1156
|
+
return false;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
override _allControlsDisabled(): boolean {
|
|
1160
|
+
return this.disabled;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
registerOnChange(fn: Function): void {
|
|
1164
|
+
this._onChange.push(fn);
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
_unregisterOnChange(fn: (value?: any, emitModelEvent?: boolean) => void): void {
|
|
1168
|
+
removeListItem(this._onChange, fn);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
registerOnDisabledChange(fn: (isDisabled: boolean) => void): void {
|
|
1172
|
+
this._onDisabledChange.push(fn);
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
_unregisterOnDisabledChange(fn: (isDisabled: boolean) => void): void {
|
|
1176
|
+
removeListItem(this._onDisabledChange, fn);
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
override _forEachChild(cb: (c: AbstractControl, key: any) => void): void {}
|
|
1180
|
+
|
|
1181
|
+
override _syncPendingControls(): boolean {
|
|
1182
|
+
if (this.updateOn === 'submit') {
|
|
1183
|
+
if (this._pendingDirty) this.markAsDirty();
|
|
1184
|
+
if (this._pendingTouched) this.markAsTouched();
|
|
1185
|
+
if (this._pendingChange) {
|
|
1186
|
+
this.setValue(this._pendingValue, { onlySelf: true, emitModelToViewChange: false });
|
|
1187
|
+
return true;
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return false;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
private _applyFormState(formState: any): void {
|
|
1194
|
+
if (isFormControlState(formState)) {
|
|
1195
|
+
this._value = this._pendingValue = formState.value;
|
|
1196
|
+
formState.disabled ? this.disable({ onlySelf: true, emitEvent: false }) : this.enable({ onlySelf: true, emitEvent: false });
|
|
1197
|
+
} else {
|
|
1198
|
+
this._value = this._pendingValue = formState;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
export type UntypedFormControl = FormControl<any>;
|
|
1204
|
+
export const UntypedFormControl = FormControl;
|
|
1205
|
+
|
|
1206
|
+
export const isFormControl = (control: unknown): control is FormControl => control instanceof FormControl;
|
|
1207
|
+
|
|
1208
|
+
// ---- model/form_group.ts ----
|
|
1209
|
+
|
|
1210
|
+
export type ɵFormGroupArgumentValue<T extends { [K in keyof T]?: AbstractControl<any> }> = ɵTypedOrUntyped<T, Partial<{ [K in keyof T]: ɵValue<T[K]> | FormControlState<ɵValue<T[K]>> }>, { [key: string]: any }>;
|
|
1211
|
+
|
|
1212
|
+
export type ɵFormGroupValue<T extends { [K in keyof T]?: AbstractControl<any> }> = ɵTypedOrUntyped<T, Partial<{ [K in keyof T]: ɵValue<T[K]> }>, { [key: string]: any }>;
|
|
1213
|
+
|
|
1214
|
+
export type ɵFormGroupRawValue<T extends { [K in keyof T]?: AbstractControl<any> }> = ɵTypedOrUntyped<T, { [K in keyof T]: ɵRawValue<T[K]> }, { [key: string]: any }>;
|
|
1215
|
+
|
|
1216
|
+
export type ɵOptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? K : never }[keyof T];
|
|
1217
|
+
|
|
1218
|
+
export class FormGroup<TControl extends { [K in keyof TControl]: AbstractControl<any> } = any> extends AbstractControl<
|
|
1219
|
+
ɵTypedOrUntyped<TControl, ɵFormGroupValue<TControl>, any>,
|
|
1220
|
+
ɵTypedOrUntyped<TControl, ɵFormGroupRawValue<TControl>, any>,
|
|
1221
|
+
ɵTypedOrUntyped<TControl, ɵFormGroupArgumentValue<TControl>, any>
|
|
1222
|
+
> {
|
|
1223
|
+
public controls: ɵTypedOrUntyped<TControl, TControl, { [key: string]: AbstractControl<any> }>;
|
|
1224
|
+
|
|
1225
|
+
constructor(controls: TControl, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
|
|
1226
|
+
super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
|
|
1227
|
+
const own: any = {};
|
|
1228
|
+
const given: any = controls;
|
|
1229
|
+
for (const key of Object.keys(given)) own[key] = given[key];
|
|
1230
|
+
this.controls = own;
|
|
1231
|
+
this._setUpdateStrategy(validatorOrOpts);
|
|
1232
|
+
this._setUpControls();
|
|
1233
|
+
this.updateValueAndValidity({
|
|
1234
|
+
onlySelf: true,
|
|
1235
|
+
emitEvent: !!this.asyncValidator,
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
registerControl<K extends string & keyof TControl>(name: K, control: TControl[K]): TControl[K];
|
|
1240
|
+
registerControl(this: FormGroup<{ [key: string]: AbstractControl<any> }>, name: string, control: AbstractControl<any>): AbstractControl<any>;
|
|
1241
|
+
registerControl(name: string, control: AbstractControl<any>): any {
|
|
1242
|
+
const existingControl = this._find(name);
|
|
1243
|
+
if (existingControl) return existingControl;
|
|
1244
|
+
(this.controls as any)[name] = control;
|
|
1245
|
+
control.setParent(this as FormGroup);
|
|
1246
|
+
control._registerOnCollectionChange(this._onCollectionChange);
|
|
1247
|
+
return control;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
addControl(this: FormGroup<{ [key: string]: AbstractControl<any> }>, name: string, control: AbstractControl, options?: { emitEvent?: boolean }): void;
|
|
1251
|
+
addControl<K extends string & keyof TControl>(name: K, control: Required<TControl>[K], options?: { emitEvent?: boolean }): void;
|
|
1252
|
+
addControl(name: string, control: AbstractControl, options: { emitEvent?: boolean } = {}): void {
|
|
1253
|
+
this.registerControl(name as any, control as any);
|
|
1254
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1255
|
+
this._onCollectionChange();
|
|
1256
|
+
}
|
|
1257
|
+
|
|
1258
|
+
removeControl(this: FormGroup<{ [key: string]: AbstractControl<any> }>, name: string, options?: { emitEvent?: boolean }): void;
|
|
1259
|
+
removeControl<S extends string>(name: ɵOptionalKeys<TControl> & S, options?: { emitEvent?: boolean }): void;
|
|
1260
|
+
removeControl(name: string, options: { emitEvent?: boolean } = {}): void {
|
|
1261
|
+
const existingControl = this._find(name);
|
|
1262
|
+
if (existingControl) existingControl._registerOnCollectionChange(() => {});
|
|
1263
|
+
delete (this.controls as any)[name];
|
|
1264
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1265
|
+
this._onCollectionChange();
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
setControl<K extends string & keyof TControl>(name: K, control: TControl[K], options?: { emitEvent?: boolean }): void;
|
|
1269
|
+
setControl(this: FormGroup<{ [key: string]: AbstractControl<any> }>, name: string, control: AbstractControl, options?: { emitEvent?: boolean }): void;
|
|
1270
|
+
setControl(name: string, control: AbstractControl, options: { emitEvent?: boolean } = {}): void {
|
|
1271
|
+
const existingControl = this._find(name);
|
|
1272
|
+
if (existingControl) existingControl._registerOnCollectionChange(() => {});
|
|
1273
|
+
delete (this.controls as any)[name];
|
|
1274
|
+
if (control) this.registerControl(name as any, control as any);
|
|
1275
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1276
|
+
this._onCollectionChange();
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
contains<K extends string>(controlName: K): boolean;
|
|
1280
|
+
contains(this: FormGroup<{ [key: string]: AbstractControl<any> }>, controlName: string): boolean;
|
|
1281
|
+
contains(controlName: string): boolean {
|
|
1282
|
+
return this._find(controlName)?.enabled === true;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
override setValue(value: ɵFormGroupRawValue<TControl>, options: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
1286
|
+
untracked(() => {
|
|
1287
|
+
assertAllValuesPresent(this, true, value);
|
|
1288
|
+
const given: any = value;
|
|
1289
|
+
Object.keys(given).forEach((name) => {
|
|
1290
|
+
assertControlPresent(Object.keys(this.controls).length, hasOwnControl(this.controls, name), true, name);
|
|
1291
|
+
const target: AbstractControl = (this.controls as any)[name];
|
|
1292
|
+
target.setValue(given[name], {
|
|
1293
|
+
onlySelf: true,
|
|
1294
|
+
emitEvent: options.emitEvent,
|
|
1295
|
+
});
|
|
1296
|
+
});
|
|
1297
|
+
this.updateValueAndValidity(options);
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
override patchValue(value: ɵFormGroupValue<TControl>, options: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
1302
|
+
if (value == null) return;
|
|
1303
|
+
const given: any = value;
|
|
1304
|
+
Object.keys(given).forEach((name) => {
|
|
1305
|
+
const existingControl = this._find(name);
|
|
1306
|
+
if (existingControl) {
|
|
1307
|
+
existingControl.patchValue(given[name], { onlySelf: true, emitEvent: options.emitEvent });
|
|
1308
|
+
}
|
|
1309
|
+
});
|
|
1310
|
+
this.updateValueAndValidity(options);
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
override reset(value: ɵTypedOrUntyped<TControl, ɵFormGroupArgumentValue<TControl>, any> = {} as any, options: { onlySelf?: boolean; emitEvent?: boolean; overwriteDefaultValue?: boolean } = {}): void {
|
|
1314
|
+
const given: any = value;
|
|
1315
|
+
this._forEachChild((control: AbstractControl, name: any) => {
|
|
1316
|
+
control.reset(given ? given[name] : null, { ...options, onlySelf: true });
|
|
1317
|
+
});
|
|
1318
|
+
this._updatePristine(options, this);
|
|
1319
|
+
this._updateTouched(options, this);
|
|
1320
|
+
this.updateValueAndValidity(options);
|
|
1321
|
+
if (options?.emitEvent !== false) {
|
|
1322
|
+
this._events.next(new FormResetEvent(this));
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
override getRawValue(): ɵTypedOrUntyped<TControl, ɵFormGroupRawValue<TControl>, any> {
|
|
1327
|
+
const acc: any = {};
|
|
1328
|
+
this._forEachChild((control: AbstractControl, name: any) => {
|
|
1329
|
+
acc[name] = control.getRawValue();
|
|
1330
|
+
});
|
|
1331
|
+
return acc;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
override _syncPendingControls(): boolean {
|
|
1335
|
+
let subtreeUpdated = false;
|
|
1336
|
+
this._forEachChild((child: AbstractControl) => {
|
|
1337
|
+
subtreeUpdated = child._syncPendingControls() ? true : subtreeUpdated;
|
|
1338
|
+
});
|
|
1339
|
+
if (subtreeUpdated) this.updateValueAndValidity({ onlySelf: true });
|
|
1340
|
+
return subtreeUpdated;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
override _forEachChild(cb: (c: AbstractControl, key: any) => void): void {
|
|
1344
|
+
const controls: { [key: string]: AbstractControl } = this.controls;
|
|
1345
|
+
Object.keys(controls).forEach((key) => {
|
|
1346
|
+
const control = controls[key];
|
|
1347
|
+
control && cb(control, key);
|
|
1348
|
+
});
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
_setUpControls(): void {
|
|
1352
|
+
this._forEachChild((control: AbstractControl) => {
|
|
1353
|
+
control.setParent(this);
|
|
1354
|
+
control._registerOnCollectionChange(this._onCollectionChange);
|
|
1355
|
+
});
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
override _updateValue(): void {
|
|
1359
|
+
this._value = this._reduceValue();
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
|
|
1363
|
+
const controls: { [key: string]: AbstractControl } = this.controls;
|
|
1364
|
+
for (const controlName of Object.keys(controls)) {
|
|
1365
|
+
if (this.contains(controlName) && condition(controls[controlName])) {
|
|
1366
|
+
return true;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
return false;
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
_reduceValue(): any {
|
|
1373
|
+
const acc: any = {};
|
|
1374
|
+
this._forEachChild((control: AbstractControl, name: any) => {
|
|
1375
|
+
if (control.enabled || this.disabled) {
|
|
1376
|
+
acc[name] = control.value;
|
|
1377
|
+
}
|
|
1378
|
+
});
|
|
1379
|
+
return acc;
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
override _allControlsDisabled(): boolean {
|
|
1383
|
+
const controls: { [key: string]: AbstractControl } = this.controls;
|
|
1384
|
+
for (const controlName of Object.keys(controls)) {
|
|
1385
|
+
if (controls[controlName].enabled) {
|
|
1386
|
+
return false;
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
return Object.keys(controls).length > 0 || this.disabled;
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
override _find(name: string | number): AbstractControl | null {
|
|
1393
|
+
const controls: { [key: string]: AbstractControl } = this.controls;
|
|
1394
|
+
return hasOwnControl(controls, name) ? controls[String(name)] : null;
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
export type UntypedFormGroup = FormGroup<any>;
|
|
1399
|
+
export const UntypedFormGroup = FormGroup;
|
|
1400
|
+
|
|
1401
|
+
export const isFormGroup = (control: unknown): control is FormGroup => control instanceof FormGroup;
|
|
1402
|
+
|
|
1403
|
+
export class FormRecord<TControl extends AbstractControl = AbstractControl> extends FormGroup<{ [key: string]: TControl }> {}
|
|
1404
|
+
|
|
1405
|
+
export const isFormRecord = (control: unknown): control is FormRecord => control instanceof FormRecord;
|
|
1406
|
+
|
|
1407
|
+
// ---- model/form_array.ts ----
|
|
1408
|
+
|
|
1409
|
+
export type ɵFormArrayValue<T extends AbstractControl<any>> = ɵTypedOrUntyped<T, Array<ɵValue<T>>, any[]>;
|
|
1410
|
+
|
|
1411
|
+
export type ɵFormArrayRawValue<T extends AbstractControl<any>> = ɵTypedOrUntyped<T, Array<ɵRawValue<T>>, any[]>;
|
|
1412
|
+
|
|
1413
|
+
export class FormArray<TControl extends AbstractControl<any> = any> extends AbstractControl<ɵTypedOrUntyped<TControl, ɵFormArrayValue<TControl>, any>, ɵTypedOrUntyped<TControl, ɵFormArrayRawValue<TControl>, any>> {
|
|
1414
|
+
private _list: AbstractControl[];
|
|
1415
|
+
|
|
1416
|
+
get controls(): ɵTypedOrUntyped<TControl, Array<TControl>, Array<AbstractControl<any>>> {
|
|
1417
|
+
return this._list as any;
|
|
1418
|
+
}
|
|
1419
|
+
set controls(controls: ɵTypedOrUntyped<TControl, Array<TControl>, Array<AbstractControl<any>>>) {
|
|
1420
|
+
this._list = controls as any;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
constructor(controls: Array<TControl>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null) {
|
|
1424
|
+
super(pickValidators(validatorOrOpts), pickAsyncValidators(asyncValidator, validatorOrOpts));
|
|
1425
|
+
const given: AbstractControl[] = controls as any;
|
|
1426
|
+
this._list = given.slice();
|
|
1427
|
+
this._setUpdateStrategy(validatorOrOpts);
|
|
1428
|
+
this._setUpControls();
|
|
1429
|
+
this.updateValueAndValidity({
|
|
1430
|
+
onlySelf: true,
|
|
1431
|
+
emitEvent: !!this.asyncValidator,
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
at(index: number): ɵTypedOrUntyped<TControl, TControl, AbstractControl<any>> {
|
|
1436
|
+
const list: any = this._list;
|
|
1437
|
+
return list[this._adjustIndex(index)];
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/** `at(index)`, undefined past the end as JavaScript's array read is. */
|
|
1441
|
+
private _at(index: number): AbstractControl | undefined {
|
|
1442
|
+
const list: any = this._list;
|
|
1443
|
+
return list[this._adjustIndex(index)];
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
push(control: TControl | Array<TControl>, options: { emitEvent?: boolean } = {}): void {
|
|
1447
|
+
const list = this._list;
|
|
1448
|
+
if (Array.isArray(control)) {
|
|
1449
|
+
const many: AbstractControl[] = control;
|
|
1450
|
+
for (const ctrl of many) {
|
|
1451
|
+
list.push(ctrl);
|
|
1452
|
+
this._registerControl(ctrl);
|
|
1453
|
+
}
|
|
1454
|
+
} else {
|
|
1455
|
+
list.push(control);
|
|
1456
|
+
this._registerControl(control);
|
|
1457
|
+
}
|
|
1458
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1459
|
+
this._onCollectionChange();
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
insert(index: number, control: TControl, options: { emitEvent?: boolean } = {}): void {
|
|
1463
|
+
const list = this._list;
|
|
1464
|
+
list.splice(index, 0, control);
|
|
1465
|
+
this._registerControl(control);
|
|
1466
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1467
|
+
}
|
|
1468
|
+
|
|
1469
|
+
removeAt(index: number, options: { emitEvent?: boolean } = {}): void {
|
|
1470
|
+
const list = this._list;
|
|
1471
|
+
let adjustedIndex = this._adjustIndex(index);
|
|
1472
|
+
if (adjustedIndex < 0) adjustedIndex = 0;
|
|
1473
|
+
if (list[adjustedIndex]) list[adjustedIndex]._registerOnCollectionChange(() => {});
|
|
1474
|
+
list.splice(adjustedIndex, 1);
|
|
1475
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
setControl(index: number, control: TControl, options: { emitEvent?: boolean } = {}): void {
|
|
1479
|
+
const list = this._list;
|
|
1480
|
+
let adjustedIndex = this._adjustIndex(index);
|
|
1481
|
+
if (adjustedIndex < 0) adjustedIndex = 0;
|
|
1482
|
+
if (list[adjustedIndex]) list[adjustedIndex]._registerOnCollectionChange(() => {});
|
|
1483
|
+
list.splice(adjustedIndex, 1);
|
|
1484
|
+
if (control) {
|
|
1485
|
+
list.splice(adjustedIndex, 0, control);
|
|
1486
|
+
this._registerControl(control);
|
|
1487
|
+
}
|
|
1488
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1489
|
+
this._onCollectionChange();
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
get length(): number {
|
|
1493
|
+
return this._list.length;
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
override setValue(value: ɵFormArrayRawValue<TControl>, options: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
1497
|
+
untracked(() => {
|
|
1498
|
+
assertAllValuesPresent(this, false, value);
|
|
1499
|
+
const given: any[] = value;
|
|
1500
|
+
given.forEach((newValue: any, index: number) => {
|
|
1501
|
+
assertControlPresent(this._list.length, index >= 0 && index < this._list.length, false, index);
|
|
1502
|
+
this._at(index)!.setValue(newValue, { onlySelf: true, emitEvent: options.emitEvent });
|
|
1503
|
+
});
|
|
1504
|
+
this.updateValueAndValidity(options);
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
override patchValue(value: ɵFormArrayValue<TControl>, options: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
1509
|
+
if (value == null) return;
|
|
1510
|
+
const given: any[] = value;
|
|
1511
|
+
given.forEach((newValue: any, index: number) => {
|
|
1512
|
+
const control = this._at(index);
|
|
1513
|
+
if (control) {
|
|
1514
|
+
control.patchValue(newValue, { onlySelf: true, emitEvent: options.emitEvent });
|
|
1515
|
+
}
|
|
1516
|
+
});
|
|
1517
|
+
this.updateValueAndValidity(options);
|
|
1518
|
+
}
|
|
1519
|
+
|
|
1520
|
+
override reset(value: ɵTypedOrUntyped<TControl, ɵFormArrayValue<TControl>, any> = [] as any, options: { onlySelf?: boolean; emitEvent?: boolean; overwriteDefaultValue?: boolean } = {}): void {
|
|
1521
|
+
const given: any = value;
|
|
1522
|
+
this._forEachChild((control: AbstractControl, index: any) => {
|
|
1523
|
+
control.reset(given[index], { ...options, onlySelf: true });
|
|
1524
|
+
});
|
|
1525
|
+
this._updatePristine(options, this);
|
|
1526
|
+
this._updateTouched(options, this);
|
|
1527
|
+
this.updateValueAndValidity(options);
|
|
1528
|
+
if (options?.emitEvent !== false) {
|
|
1529
|
+
this._events.next(new FormResetEvent(this));
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
override getRawValue(): ɵFormArrayRawValue<TControl> {
|
|
1534
|
+
const list = this._list;
|
|
1535
|
+
return list.map((control: AbstractControl) => control.getRawValue()) as any;
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
clear(options: { emitEvent?: boolean } = {}): void {
|
|
1539
|
+
const list = this._list;
|
|
1540
|
+
if (list.length < 1) return;
|
|
1541
|
+
this._forEachChild((control: AbstractControl) => control._registerOnCollectionChange(() => {}));
|
|
1542
|
+
list.splice(0);
|
|
1543
|
+
this.updateValueAndValidity({ emitEvent: options.emitEvent });
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
private _adjustIndex(index: number): number {
|
|
1547
|
+
return index < 0 ? index + this.length : index;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
override _syncPendingControls(): boolean {
|
|
1551
|
+
let subtreeUpdated = false;
|
|
1552
|
+
this._forEachChild((child: AbstractControl) => {
|
|
1553
|
+
subtreeUpdated = child._syncPendingControls() ? true : subtreeUpdated;
|
|
1554
|
+
});
|
|
1555
|
+
if (subtreeUpdated) this.updateValueAndValidity({ onlySelf: true });
|
|
1556
|
+
return subtreeUpdated;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
override _forEachChild(cb: (c: AbstractControl, key: any) => void): void {
|
|
1560
|
+
const list = this._list;
|
|
1561
|
+
list.forEach((control: AbstractControl, index: number) => {
|
|
1562
|
+
cb(control, index);
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
override _updateValue(): void {
|
|
1567
|
+
const list = this._list;
|
|
1568
|
+
this._value = list.filter((control) => control.enabled || this.disabled).map((control) => control.value);
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
|
|
1572
|
+
const list = this._list;
|
|
1573
|
+
return list.some((control) => control.enabled && condition(control));
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
_setUpControls(): void {
|
|
1577
|
+
this._forEachChild((control: AbstractControl) => this._registerControl(control));
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
override _allControlsDisabled(): boolean {
|
|
1581
|
+
const list = this._list;
|
|
1582
|
+
for (const control of list) {
|
|
1583
|
+
if (control.enabled) return false;
|
|
1584
|
+
}
|
|
1585
|
+
return list.length > 0 || this.disabled;
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
private _registerControl(control: AbstractControl): void {
|
|
1589
|
+
control.setParent(this);
|
|
1590
|
+
control._registerOnCollectionChange(this._onCollectionChange);
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
override _find(name: string | number): AbstractControl | null {
|
|
1594
|
+
if (typeof name === 'number') return this._at(name) ?? null;
|
|
1595
|
+
// A path's segment indexes the array as JavaScript's `controls[name]` does: only a canonical index names an element.
|
|
1596
|
+
const index = Number(name);
|
|
1597
|
+
return Number.isInteger(index) && index >= 0 && String(index) === name ? this._list[index] ?? null : null;
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
export type UntypedFormArray = FormArray<any>;
|
|
1602
|
+
export const UntypedFormArray = FormArray;
|
|
1603
|
+
|
|
1604
|
+
export const isFormArray = (control: unknown): control is FormArray => control instanceof FormArray;
|
|
1605
|
+
|
|
1606
|
+
// ---- form_builder.ts ----
|
|
1607
|
+
|
|
1608
|
+
function isAbstractControlOptions(options: AbstractControlOptions | { [key: string]: any } | null | undefined): options is AbstractControlOptions {
|
|
1609
|
+
return !!options && ((options as AbstractControlOptions).asyncValidators !== undefined || (options as AbstractControlOptions).validators !== undefined || (options as AbstractControlOptions).updateOn !== undefined);
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
type ValidatorConfig = ValidatorFn | AsyncValidatorFn | ValidatorFn[] | AsyncValidatorFn[];
|
|
1613
|
+
type PermissiveControlConfig<T> = Array<T | FormControlState<T> | ValidatorConfig>;
|
|
1614
|
+
interface PermissiveAbstractControlOptions extends Omit<AbstractControlOptions, 'updateOn'> {
|
|
1615
|
+
updateOn?: string;
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
export type ɵNullableFormControls<T> = { [K in keyof T]: ɵElement<T[K], null> };
|
|
1619
|
+
|
|
1620
|
+
export type ɵNonNullableFormControls<T> = { [K in keyof T]: ɵElement<T[K], never> };
|
|
1621
|
+
|
|
1622
|
+
export type ControlConfig<T> = [T | FormControlState<T>, (ValidatorFn | ValidatorFn[])?, (AsyncValidatorFn | AsyncValidatorFn[])?];
|
|
1623
|
+
|
|
1624
|
+
export type ɵElement<T, N extends null> = [T] extends [FormControl<infer U>]
|
|
1625
|
+
? FormControl<U>
|
|
1626
|
+
: [T] extends [FormControl<infer U> | undefined]
|
|
1627
|
+
? FormControl<U>
|
|
1628
|
+
: [T] extends [FormGroup<infer U>]
|
|
1629
|
+
? FormGroup<U>
|
|
1630
|
+
: [T] extends [FormGroup<infer U> | undefined]
|
|
1631
|
+
? FormGroup<U>
|
|
1632
|
+
: [T] extends [FormRecord<infer U>]
|
|
1633
|
+
? FormRecord<U>
|
|
1634
|
+
: [T] extends [FormRecord<infer U> | undefined]
|
|
1635
|
+
? FormRecord<U>
|
|
1636
|
+
: [T] extends [FormArray<infer U>]
|
|
1637
|
+
? FormArray<U>
|
|
1638
|
+
: [T] extends [FormArray<infer U> | undefined]
|
|
1639
|
+
? FormArray<U>
|
|
1640
|
+
: [T] extends [AbstractControl<infer U>]
|
|
1641
|
+
? AbstractControl<U>
|
|
1642
|
+
: [T] extends [AbstractControl<infer U> | undefined]
|
|
1643
|
+
? AbstractControl<U>
|
|
1644
|
+
: [T] extends [FormControlState<infer U>]
|
|
1645
|
+
? FormControl<U | N>
|
|
1646
|
+
: [T] extends [PermissiveControlConfig<infer U>]
|
|
1647
|
+
? FormControl<Exclude<U, ValidatorConfig | PermissiveAbstractControlOptions> | N>
|
|
1648
|
+
: FormControl<T | N>;
|
|
1649
|
+
|
|
1650
|
+
export class FormBuilder {
|
|
1651
|
+
private static _shared: FormBuilder | undefined;
|
|
1652
|
+
|
|
1653
|
+
/** The root injector's FormBuilder, which `inject(FormBuilder)` gives. */
|
|
1654
|
+
static get shared(): FormBuilder {
|
|
1655
|
+
if (!FormBuilder._shared) FormBuilder._shared = new FormBuilder();
|
|
1656
|
+
return FormBuilder._shared;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
private useNonNullable: boolean = false;
|
|
1660
|
+
|
|
1661
|
+
get nonNullable(): NonNullableFormBuilder {
|
|
1662
|
+
return new NonNullableFormBuilder();
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
static _nonNullable(): FormBuilder {
|
|
1666
|
+
const nnfb = new FormBuilder();
|
|
1667
|
+
nnfb.useNonNullable = true;
|
|
1668
|
+
return nnfb;
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
group<T extends {}>(controls: T, options?: AbstractControlOptions | null): FormGroup<ɵNullableFormControls<T>>;
|
|
1672
|
+
group(controls: { [key: string]: any }, options: { [key: string]: any }): FormGroup;
|
|
1673
|
+
group(controls: { [key: string]: any }, options: AbstractControlOptions | { [key: string]: any } | null = null): FormGroup {
|
|
1674
|
+
const reducedControls = this._reduceControls(controls);
|
|
1675
|
+
let newOptions: FormControlOptions = {};
|
|
1676
|
+
if (isAbstractControlOptions(options)) {
|
|
1677
|
+
newOptions = options;
|
|
1678
|
+
} else if (options !== null) {
|
|
1679
|
+
const legacy: any = options;
|
|
1680
|
+
newOptions.validators = legacy.validator;
|
|
1681
|
+
newOptions.asyncValidators = legacy.asyncValidator;
|
|
1682
|
+
}
|
|
1683
|
+
return new FormGroup(reducedControls, newOptions);
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
record<T>(controls: { [key: string]: T }, options: AbstractControlOptions | null = null): FormRecord<ɵElement<T, null>> {
|
|
1687
|
+
const reducedControls = this._reduceControls(controls);
|
|
1688
|
+
return new FormRecord(reducedControls, options) as any;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { initialValueIsDefault: true }): FormControl<T>;
|
|
1692
|
+
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions & { nonNullable: true }): FormControl<T>;
|
|
1693
|
+
control<T>(formState: T | FormControlState<T>, opts: FormControlOptions, asyncValidator: AsyncValidatorFn | AsyncValidatorFn[]): FormControl<T | null>;
|
|
1694
|
+
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl<T | null>;
|
|
1695
|
+
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl {
|
|
1696
|
+
let newOptions: FormControlOptions = {};
|
|
1697
|
+
if (!this.useNonNullable) {
|
|
1698
|
+
return new FormControl(formState, validatorOrOpts, asyncValidator);
|
|
1699
|
+
}
|
|
1700
|
+
if (isAbstractControlOptions(validatorOrOpts)) {
|
|
1701
|
+
newOptions = validatorOrOpts;
|
|
1702
|
+
} else {
|
|
1703
|
+
newOptions.validators = validatorOrOpts as ValidatorFn | ValidatorFn[] | null | undefined;
|
|
1704
|
+
newOptions.asyncValidators = asyncValidator;
|
|
1705
|
+
}
|
|
1706
|
+
return new FormControl<T>(formState, { ...newOptions, nonNullable: true });
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
array<T>(controls: Array<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArray<ɵElement<T, null>> {
|
|
1710
|
+
const createdControls: any[] = controls.map((c) => this._createControl(c));
|
|
1711
|
+
return new FormArray(createdControls, validatorOrOpts, asyncValidator) as any;
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
_reduceControls(controls: { [k: string]: any }): { [key: string]: AbstractControl } {
|
|
1715
|
+
const createdControls: { [key: string]: AbstractControl } = {};
|
|
1716
|
+
Object.keys(controls).forEach((controlName) => {
|
|
1717
|
+
createdControls[controlName] = this._createControl(controls[controlName]);
|
|
1718
|
+
});
|
|
1719
|
+
return createdControls;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
_createControl(controls: any): AbstractControl {
|
|
1723
|
+
if (controls instanceof FormControl) {
|
|
1724
|
+
return controls;
|
|
1725
|
+
} else if (controls instanceof AbstractControl) {
|
|
1726
|
+
return controls;
|
|
1727
|
+
} else if (Array.isArray(controls)) {
|
|
1728
|
+
const config: any[] = controls;
|
|
1729
|
+
const value: any = config[0];
|
|
1730
|
+
const validator: ValidatorFn | ValidatorFn[] | null = config.length > 1 ? config[1]! : null;
|
|
1731
|
+
const asyncValidator: AsyncValidatorFn | AsyncValidatorFn[] | null = config.length > 2 ? config[2]! : null;
|
|
1732
|
+
return this.control(value, validator, asyncValidator);
|
|
1733
|
+
} else {
|
|
1734
|
+
return this.control(controls);
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
export class NonNullableFormBuilder {
|
|
1740
|
+
private static _shared: NonNullableFormBuilder | undefined;
|
|
1741
|
+
|
|
1742
|
+
/** The root injector's NonNullableFormBuilder, which `inject(NonNullableFormBuilder)` gives. */
|
|
1743
|
+
static get shared(): NonNullableFormBuilder {
|
|
1744
|
+
if (!NonNullableFormBuilder._shared) NonNullableFormBuilder._shared = new NonNullableFormBuilder();
|
|
1745
|
+
return NonNullableFormBuilder._shared;
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
private readonly builder = FormBuilder._nonNullable();
|
|
1749
|
+
|
|
1750
|
+
group<T extends {}>(controls: T, options?: AbstractControlOptions | null): FormGroup<ɵNonNullableFormControls<T>> {
|
|
1751
|
+
return this.builder.group(controls, options) as any;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
record<T>(controls: { [key: string]: T }, options: AbstractControlOptions | null = null): FormRecord<ɵElement<T, never>> {
|
|
1755
|
+
return this.builder.record(controls, options) as any;
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
array<T>(controls: Array<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArray<ɵElement<T, never>> {
|
|
1759
|
+
return this.builder.array(controls, validatorOrOpts, asyncValidator) as any;
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
control<T>(formState: T | FormControlState<T>, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl<T> {
|
|
1763
|
+
return this.builder.control(formState, validatorOrOpts, asyncValidator) as any;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
export class UntypedFormBuilder {
|
|
1768
|
+
private static _shared: UntypedFormBuilder | undefined;
|
|
1769
|
+
|
|
1770
|
+
/** The root injector's UntypedFormBuilder, which `inject(UntypedFormBuilder)` gives. */
|
|
1771
|
+
static get shared(): UntypedFormBuilder {
|
|
1772
|
+
if (!UntypedFormBuilder._shared) UntypedFormBuilder._shared = new UntypedFormBuilder();
|
|
1773
|
+
return UntypedFormBuilder._shared;
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
private readonly builder = new FormBuilder();
|
|
1777
|
+
|
|
1778
|
+
get nonNullable(): NonNullableFormBuilder {
|
|
1779
|
+
return new NonNullableFormBuilder();
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
group(controlsConfig: { [key: string]: any }, options?: AbstractControlOptions | null): FormGroup<any>;
|
|
1783
|
+
group(controlsConfig: { [key: string]: any }, options: { [key: string]: any }): FormGroup<any>;
|
|
1784
|
+
group(controlsConfig: { [key: string]: any }, options: AbstractControlOptions | { [key: string]: any } | null = null): FormGroup<any> {
|
|
1785
|
+
return this.builder.group(controlsConfig, options);
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
record(controls: { [key: string]: any }, options: AbstractControlOptions | null = null): FormRecord<any> {
|
|
1789
|
+
return this.builder.record(controls, options) as any;
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
control(formState: any, validatorOrOpts?: ValidatorFn | ValidatorFn[] | FormControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormControl<any> {
|
|
1793
|
+
return this.builder.control(formState, validatorOrOpts, asyncValidator);
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
array(controlsConfig: any[], validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): FormArray<any> {
|
|
1797
|
+
return this.builder.array(controlsConfig, validatorOrOpts, asyncValidator);
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
// ---- directives (packages/forms/src/directives) and @nativescript/angular's value accessors ----
|
|
1802
|
+
//
|
|
1803
|
+
// What the release build's template compiler makes for the forms directives an element selects. Each is given what
|
|
1804
|
+
// Angular's injector would give it, which the template compiler resolves from the template: its view, its view's
|
|
1805
|
+
// ChangeDetectorRef (as its markForCheck), the form directive it is under, and the value accessor and validator directives its element selects.
|
|
1806
|
+
// Its inputs come in one call per check (`apply`), which runs ngOnChanges with the inputs that changed, as Angular does.
|
|
1807
|
+
// What Angular reports from a template's structure (a formControlName with no form above it) is a build error instead.
|
|
1808
|
+
|
|
1809
|
+
const resolvedPromise = Promise.resolve(null);
|
|
1810
|
+
|
|
1811
|
+
export function booleanAttribute(value: unknown): boolean {
|
|
1812
|
+
return typeof value === 'boolean' ? value : value != null && value !== 'false';
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
/** What @nativescript/angular's renderer keeps of a view's classes: an ordered set on the view, written whole to className. */
|
|
1816
|
+
function viewClasses(view: any): Map<string, boolean> {
|
|
1817
|
+
if (!view.ngCssClasses) {
|
|
1818
|
+
const classes = new Map<string, boolean>();
|
|
1819
|
+
for (const name of String(view.className ?? '').split(/\s+/)) if (name) classes.set(name, true);
|
|
1820
|
+
view.ngCssClasses = classes;
|
|
1821
|
+
}
|
|
1822
|
+
return view.ngCssClasses;
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
/** The renderer's addClass or removeClass on a view. */
|
|
1826
|
+
export function setViewClass(view: any, name: string, on: boolean): void {
|
|
1827
|
+
const classes = viewClasses(view);
|
|
1828
|
+
if (on) classes.set(name, true);
|
|
1829
|
+
else classes.delete(name);
|
|
1830
|
+
view.className = Array.from(classes.keys()).join(' ');
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
/** One input's change, as ngOnChanges receives it. */
|
|
1834
|
+
export class InputChange {
|
|
1835
|
+
constructor(public readonly previousValue: any, public readonly currentValue: any, public readonly firstChange: boolean) {}
|
|
1836
|
+
|
|
1837
|
+
isFirstChange(): boolean {
|
|
1838
|
+
return this.firstChange;
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
/** A directive's inputs as Angular checks them: each bound one is changed when its value is not Object.is the last. */
|
|
1843
|
+
class Inputs {
|
|
1844
|
+
private readonly previous: any[] = [];
|
|
1845
|
+
private started = false;
|
|
1846
|
+
|
|
1847
|
+
constructor(private readonly names: string[], private readonly bound: boolean[]) {}
|
|
1848
|
+
|
|
1849
|
+
changes(values: any[]): Map<string, InputChange> {
|
|
1850
|
+
const out = new Map<string, InputChange>();
|
|
1851
|
+
for (let k = 0; k < this.names.length; k++) {
|
|
1852
|
+
if (!this.bound[k]) continue;
|
|
1853
|
+
if (this.started && Object.is(this.previous[k], values[k])) continue;
|
|
1854
|
+
out.set(this.names[k], new InputChange(this.previous[k], values[k], !this.started));
|
|
1855
|
+
this.previous[k] = values[k];
|
|
1856
|
+
}
|
|
1857
|
+
this.started = true;
|
|
1858
|
+
return out;
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
/** Notified when a control's status, pristine or touched state changes. */
|
|
1863
|
+
export abstract class ControlStateObserver {
|
|
1864
|
+
abstract controlStateChanged(): void;
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
/**
|
|
1868
|
+
* @nativescript/angular's value accessors: TextValueAccessor (TextField, TextView, SearchBar: `text`, its textChange
|
|
1869
|
+
* and blur), CheckedValueAccessor (Switch: `checked`), DateValueAccessor (DatePicker: `date`), TimeValueAccessor
|
|
1870
|
+
* (TimePicker: `time`), NumberValueAccessor (Slider: `value`) and SelectedIndexValueAccessor (SegmentedBar, ListPicker,
|
|
1871
|
+
* TabView: `selectedIndex`, written once the view is made). A change reaches the control 20 ms after its event, the
|
|
1872
|
+
* last of several in that time only, and a blank value is written as core's unsetValue, which the component passes.
|
|
1873
|
+
*/
|
|
1874
|
+
export class NsValueAccessor {
|
|
1875
|
+
private pendingChangeNotification: any = 0;
|
|
1876
|
+
private value: any = undefined;
|
|
1877
|
+
private viewInitialized = false;
|
|
1878
|
+
onChange: (value: any) => void = () => {};
|
|
1879
|
+
onTouched: () => void = () => {};
|
|
1880
|
+
|
|
1881
|
+
constructor(private readonly view: any, readonly kind: string, private readonly unset: any) {}
|
|
1882
|
+
|
|
1883
|
+
registerOnChange(fn: (value: any) => void): void {
|
|
1884
|
+
this.onChange = (arg: any) => {
|
|
1885
|
+
if (this.pendingChangeNotification) clearTimeout(this.pendingChangeNotification);
|
|
1886
|
+
this.pendingChangeNotification = setTimeout(() => {
|
|
1887
|
+
this.pendingChangeNotification = 0;
|
|
1888
|
+
fn(arg);
|
|
1889
|
+
}, 20);
|
|
1890
|
+
};
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
registerOnTouched(fn: () => void): void {
|
|
1894
|
+
this.onTouched = fn;
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
setDisabledState(isDisabled: boolean): void {
|
|
1898
|
+
this.view.isEnabled = !isDisabled;
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
writeValue(value: any): void {
|
|
1902
|
+
if (this.kind === 'text') {
|
|
1903
|
+
this.view.text = this.normalizeValue(value ? `${value}` : value);
|
|
1904
|
+
} else if (this.kind === 'selectedIndex') {
|
|
1905
|
+
this.value = this.normalizeValue(value);
|
|
1906
|
+
if (this.viewInitialized) this.view.selectedIndex = this.value;
|
|
1907
|
+
} else {
|
|
1908
|
+
this.view[this.kind] = this.normalizeValue(value);
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
ngAfterViewInit(): void {
|
|
1913
|
+
if (this.kind !== 'selectedIndex') return;
|
|
1914
|
+
this.viewInitialized = true;
|
|
1915
|
+
this.view.selectedIndex = this.value;
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
private normalizeValue(value: any): any {
|
|
1919
|
+
return value === undefined || value === null ? this.unset : value;
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
export abstract class AbstractControlDirective {
|
|
1924
|
+
get control(): AbstractControl | null {
|
|
1925
|
+
return null;
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
get value(): any {
|
|
1929
|
+
const control = this.control;
|
|
1930
|
+
return control ? control.value : null;
|
|
1931
|
+
}
|
|
1932
|
+
|
|
1933
|
+
get valid(): boolean | null {
|
|
1934
|
+
const control = this.control;
|
|
1935
|
+
return control ? control.valid : null;
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
get invalid(): boolean | null {
|
|
1939
|
+
const control = this.control;
|
|
1940
|
+
return control ? control.invalid : null;
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
get pending(): boolean | null {
|
|
1944
|
+
const control = this.control;
|
|
1945
|
+
return control ? control.pending : null;
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
get disabled(): boolean | null {
|
|
1949
|
+
const control = this.control;
|
|
1950
|
+
return control ? control.disabled : null;
|
|
1951
|
+
}
|
|
1952
|
+
|
|
1953
|
+
get enabled(): boolean | null {
|
|
1954
|
+
const control = this.control;
|
|
1955
|
+
return control ? control.enabled : null;
|
|
1956
|
+
}
|
|
1957
|
+
|
|
1958
|
+
get errors(): ValidationErrors | null {
|
|
1959
|
+
const control = this.control;
|
|
1960
|
+
return control ? control.errors : null;
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
get pristine(): boolean | null {
|
|
1964
|
+
const control = this.control;
|
|
1965
|
+
return control ? control.pristine : null;
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
get dirty(): boolean | null {
|
|
1969
|
+
const control = this.control;
|
|
1970
|
+
return control ? control.dirty : null;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
get touched(): boolean | null {
|
|
1974
|
+
const control = this.control;
|
|
1975
|
+
return control ? control.touched : null;
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
get status(): string | null {
|
|
1979
|
+
const control = this.control;
|
|
1980
|
+
return control ? control.status : null;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
get untouched(): boolean | null {
|
|
1984
|
+
const control = this.control;
|
|
1985
|
+
return control ? control.untouched : null;
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
get statusChanges(): EventEmitter<FormControlStatus> | null {
|
|
1989
|
+
const control = this.control;
|
|
1990
|
+
return control ? control.statusChanges : null;
|
|
1991
|
+
}
|
|
1992
|
+
|
|
1993
|
+
get valueChanges(): EventEmitter<any> | null {
|
|
1994
|
+
const control = this.control;
|
|
1995
|
+
return control ? control.valueChanges : null;
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
get path(): string[] | null {
|
|
1999
|
+
return null;
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
_composedValidatorFn: any = null;
|
|
2003
|
+
_composedAsyncValidatorFn: any = null;
|
|
2004
|
+
_rawValidators: any[] = [];
|
|
2005
|
+
_rawAsyncValidators: any[] = [];
|
|
2006
|
+
|
|
2007
|
+
_setValidators(validators: any[] | undefined): void {
|
|
2008
|
+
this._rawValidators = validators || [];
|
|
2009
|
+
this._composedValidatorFn = composeValidators(this._rawValidators);
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
_setAsyncValidators(validators: any[] | undefined): void {
|
|
2013
|
+
this._rawAsyncValidators = validators || [];
|
|
2014
|
+
this._composedAsyncValidatorFn = composeAsyncValidators(this._rawAsyncValidators);
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
get validator(): ValidatorFn | null {
|
|
2018
|
+
return this._composedValidatorFn || null;
|
|
2019
|
+
}
|
|
2020
|
+
|
|
2021
|
+
get asyncValidator(): AsyncValidatorFn | null {
|
|
2022
|
+
return this._composedAsyncValidatorFn || null;
|
|
2023
|
+
}
|
|
2024
|
+
|
|
2025
|
+
private _onDestroyCallbacks: any[] = [];
|
|
2026
|
+
|
|
2027
|
+
_registerOnDestroy(fn: () => void): void {
|
|
2028
|
+
this._onDestroyCallbacks.push(fn);
|
|
2029
|
+
}
|
|
2030
|
+
|
|
2031
|
+
_invokeOnDestroyCallbacks(): void {
|
|
2032
|
+
this._onDestroyCallbacks.forEach((fn: any) => fn());
|
|
2033
|
+
this._onDestroyCallbacks = [];
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
reset(value: any = undefined): void {
|
|
2037
|
+
this.control?.reset(value);
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
hasError(errorCode: string, path?: Array<string | number> | string): boolean {
|
|
2041
|
+
const control = this.control;
|
|
2042
|
+
return control ? control.hasError(errorCode, path) : false;
|
|
2043
|
+
}
|
|
2044
|
+
|
|
2045
|
+
getError(errorCode: string, path?: Array<string | number> | string): any {
|
|
2046
|
+
const control = this.control;
|
|
2047
|
+
return control ? control.getError(errorCode, path) : null;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
/**
|
|
2052
|
+
* A form directive or a named group or array under one. What NgForm and the reactive form directives do for the
|
|
2053
|
+
* directives below them (Angular's `Form`) is declared here, done by those two.
|
|
2054
|
+
*/
|
|
2055
|
+
export abstract class ControlContainer extends AbstractControlDirective {
|
|
2056
|
+
name: string | number | null = null;
|
|
2057
|
+
|
|
2058
|
+
get formDirective(): ControlContainer | null {
|
|
2059
|
+
return null;
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
override get path(): string[] | null {
|
|
2063
|
+
return null;
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
get submitted(): boolean {
|
|
2067
|
+
return false;
|
|
2068
|
+
}
|
|
2069
|
+
|
|
2070
|
+
addControl(dir: NgControl): FormControl | null {
|
|
2071
|
+
return null;
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
removeControl(dir: NgControl): void {}
|
|
2075
|
+
|
|
2076
|
+
addFormGroup(dir: ControlContainer): void {}
|
|
2077
|
+
|
|
2078
|
+
removeFormGroup(dir: ControlContainer): void {}
|
|
2079
|
+
|
|
2080
|
+
getFormGroup(dir: ControlContainer): FormGroup | null {
|
|
2081
|
+
return null;
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
addFormArray(dir: ControlContainer): void {}
|
|
2085
|
+
|
|
2086
|
+
removeFormArray(dir: ControlContainer): void {}
|
|
2087
|
+
|
|
2088
|
+
getFormArray(dir: ControlContainer): AbstractControl | null {
|
|
2089
|
+
return null;
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
updateModel(dir: NgControl, value: any): void {}
|
|
2093
|
+
}
|
|
2094
|
+
|
|
2095
|
+
export abstract class NgControl extends AbstractControlDirective {
|
|
2096
|
+
_parent: ControlContainer | null = null;
|
|
2097
|
+
name: string | number | null = null;
|
|
2098
|
+
_accessor: NsValueAccessor | null = null;
|
|
2099
|
+
|
|
2100
|
+
get valueAccessor(): any {
|
|
2101
|
+
return this._accessor;
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
abstract viewToModelUpdate(newValue: any): void;
|
|
2105
|
+
}
|
|
2106
|
+
|
|
2107
|
+
function controlPath(name: string | null, parent: ControlContainer): string[] {
|
|
2108
|
+
const path = parent.path!.slice();
|
|
2109
|
+
path.push(name!);
|
|
2110
|
+
return path;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
function describeControlLocation(dir: AbstractControlDirective): string {
|
|
2114
|
+
const path = dir.path;
|
|
2115
|
+
if (path && path.length > 1) return `path: '${path.join(' -> ')}'`;
|
|
2116
|
+
if (path?.[0]) return `name: '${path}'`;
|
|
2117
|
+
return 'unspecified name attribute';
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
function throwControlError(dir: AbstractControlDirective, message: string): void {
|
|
2121
|
+
throw new Error(`${message} ${describeControlLocation(dir)}`);
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
export function setUpControl(control: FormControl, dir: NgControl, callSetDisabledState: string = 'always'): void {
|
|
2125
|
+
if (!control) throwControlError(dir, 'Cannot find control with');
|
|
2126
|
+
const accessor = dir._accessor!;
|
|
2127
|
+
setUpValidators(control, dir);
|
|
2128
|
+
accessor.writeValue(control.value);
|
|
2129
|
+
if (control.disabled || callSetDisabledState === 'always') accessor.setDisabledState(control.disabled);
|
|
2130
|
+
setUpViewChangePipeline(control, dir);
|
|
2131
|
+
setUpModelChangePipeline(control, dir);
|
|
2132
|
+
setUpBlurPipeline(control, dir);
|
|
2133
|
+
setUpDisabledChangeHandler(control, dir);
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
export function cleanUpControl(control: FormControl | null, dir: NgControl, validateControlPresenceOnChange: boolean = true): void {
|
|
2137
|
+
const noop = () => {
|
|
2138
|
+
if (validateControlPresenceOnChange) throwControlError(dir, 'There is no FormControl instance attached to form control element with');
|
|
2139
|
+
};
|
|
2140
|
+
dir._accessor?.registerOnChange(noop);
|
|
2141
|
+
dir._accessor?.registerOnTouched(noop);
|
|
2142
|
+
cleanUpValidators(control, dir);
|
|
2143
|
+
if (control) {
|
|
2144
|
+
dir._invokeOnDestroyCallbacks();
|
|
2145
|
+
control._registerOnCollectionChange(() => {});
|
|
2146
|
+
}
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
function registerOnValidatorChange(validators: any[], onChange: () => void): void {
|
|
2150
|
+
for (const validator of validators) if (validator instanceof AbstractValidatorDirective) validator.registerOnValidatorChange(onChange);
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
function setUpDisabledChangeHandler(control: FormControl, dir: NgControl): void {
|
|
2154
|
+
const onDisabledChange = (isDisabled: boolean) => {
|
|
2155
|
+
dir._accessor!.setDisabledState(isDisabled);
|
|
2156
|
+
};
|
|
2157
|
+
control.registerOnDisabledChange(onDisabledChange);
|
|
2158
|
+
dir._registerOnDestroy(() => {
|
|
2159
|
+
control._unregisterOnDisabledChange(onDisabledChange);
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
// A composed validator is null or a function: compared loosely, as the native build may hold null as undefined.
|
|
2164
|
+
export function setUpValidators(control: AbstractControl, dir: AbstractControlDirective): void {
|
|
2165
|
+
const validators = getControlValidators(control);
|
|
2166
|
+
if (dir._composedValidatorFn != null) {
|
|
2167
|
+
control._assignValidators(mergeValidators(validators, dir._composedValidatorFn));
|
|
2168
|
+
} else if (typeof validators === 'function') {
|
|
2169
|
+
control._assignValidators([validators]);
|
|
2170
|
+
}
|
|
2171
|
+
const asyncValidators = getControlAsyncValidators(control);
|
|
2172
|
+
if (dir._composedAsyncValidatorFn != null) {
|
|
2173
|
+
control._assignAsyncValidators(mergeValidators(asyncValidators, dir._composedAsyncValidatorFn));
|
|
2174
|
+
} else if (typeof asyncValidators === 'function') {
|
|
2175
|
+
control._assignAsyncValidators([asyncValidators]);
|
|
2176
|
+
}
|
|
2177
|
+
const onValidatorChange = () => control.updateValueAndValidity();
|
|
2178
|
+
registerOnValidatorChange(dir._rawValidators, onValidatorChange);
|
|
2179
|
+
registerOnValidatorChange(dir._rawAsyncValidators, onValidatorChange);
|
|
2180
|
+
}
|
|
2181
|
+
|
|
2182
|
+
export function cleanUpValidators(control: AbstractControl | null, dir: AbstractControlDirective): boolean {
|
|
2183
|
+
let isControlUpdated = false;
|
|
2184
|
+
if (control !== null) {
|
|
2185
|
+
if (dir._composedValidatorFn != null) {
|
|
2186
|
+
const validators = getControlValidators(control);
|
|
2187
|
+
if (Array.isArray(validators) && validators.length > 0) {
|
|
2188
|
+
const updatedValidators = validators.filter((validator: any) => validator !== dir._composedValidatorFn);
|
|
2189
|
+
if (updatedValidators.length !== validators.length) {
|
|
2190
|
+
isControlUpdated = true;
|
|
2191
|
+
control._assignValidators(updatedValidators);
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
if (dir._composedAsyncValidatorFn != null) {
|
|
2196
|
+
const asyncValidators = getControlAsyncValidators(control);
|
|
2197
|
+
if (Array.isArray(asyncValidators) && asyncValidators.length > 0) {
|
|
2198
|
+
const updatedAsyncValidators = asyncValidators.filter((asyncValidator: any) => asyncValidator !== dir._composedAsyncValidatorFn);
|
|
2199
|
+
if (updatedAsyncValidators.length !== asyncValidators.length) {
|
|
2200
|
+
isControlUpdated = true;
|
|
2201
|
+
control._assignAsyncValidators(updatedAsyncValidators);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
const noop = () => {};
|
|
2207
|
+
registerOnValidatorChange(dir._rawValidators, noop);
|
|
2208
|
+
registerOnValidatorChange(dir._rawAsyncValidators, noop);
|
|
2209
|
+
return isControlUpdated;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
function setUpViewChangePipeline(control: FormControl, dir: NgControl): void {
|
|
2213
|
+
dir._accessor!.registerOnChange((newValue: any) => {
|
|
2214
|
+
control._pendingValue = newValue;
|
|
2215
|
+
control._pendingChange = true;
|
|
2216
|
+
control._pendingDirty = true;
|
|
2217
|
+
if (control.updateOn === 'change') updateControl(control, dir);
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
function setUpBlurPipeline(control: FormControl, dir: NgControl): void {
|
|
2222
|
+
dir._accessor!.registerOnTouched(() => {
|
|
2223
|
+
control._pendingTouched = true;
|
|
2224
|
+
if (control.updateOn === 'blur' && control._pendingChange) updateControl(control, dir);
|
|
2225
|
+
if (control.updateOn !== 'submit') control.markAsTouched();
|
|
2226
|
+
});
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
function updateControl(control: FormControl, dir: NgControl): void {
|
|
2230
|
+
if (control._pendingDirty) control.markAsDirty();
|
|
2231
|
+
control.setValue(control._pendingValue, { emitModelToViewChange: false });
|
|
2232
|
+
dir.viewToModelUpdate(control._pendingValue);
|
|
2233
|
+
control._pendingChange = false;
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
function setUpModelChangePipeline(control: FormControl, dir: NgControl): void {
|
|
2237
|
+
const onChange = (newValue?: any, emitModelEvent?: boolean) => {
|
|
2238
|
+
dir._accessor!.writeValue(newValue);
|
|
2239
|
+
if (emitModelEvent) dir.viewToModelUpdate(newValue);
|
|
2240
|
+
};
|
|
2241
|
+
control.registerOnChange(onChange);
|
|
2242
|
+
dir._registerOnDestroy(() => {
|
|
2243
|
+
control._unregisterOnChange(onChange);
|
|
2244
|
+
});
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
export function setUpFormContainer(control: AbstractControl, dir: ControlContainer): void {
|
|
2248
|
+
if (control == null) throwControlError(dir, 'Cannot find control with');
|
|
2249
|
+
setUpValidators(control, dir);
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
export function cleanUpFormContainer(control: AbstractControl, dir: ControlContainer): boolean {
|
|
2253
|
+
return cleanUpValidators(control, dir);
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
function isPropertyUpdated(changes: Map<string, InputChange>, viewModel: any): boolean {
|
|
2257
|
+
const change = changes.get('model');
|
|
2258
|
+
if (!change) return false;
|
|
2259
|
+
if (change.isFirstChange()) return true;
|
|
2260
|
+
return !Object.is(viewModel, change.currentValue);
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
function syncPendingControls(form: AbstractControl, directives: NgControl[]): void {
|
|
2264
|
+
form._syncPendingControls();
|
|
2265
|
+
directives.forEach((dir: NgControl) => {
|
|
2266
|
+
const control = dir.control as FormControl;
|
|
2267
|
+
if (control.updateOn === 'submit' && control._pendingChange) {
|
|
2268
|
+
dir.viewToModelUpdate(control._pendingValue);
|
|
2269
|
+
control._pendingChange = false;
|
|
2270
|
+
}
|
|
2271
|
+
});
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
// ---- validator directives (directives/validators.ts): the ones whose selectors a NativeScript element can match ----
|
|
2275
|
+
|
|
2276
|
+
export abstract class AbstractValidatorDirective {
|
|
2277
|
+
private _validator: ValidatorFn = nullValidator;
|
|
2278
|
+
private _onChange: (() => void) | null = null;
|
|
2279
|
+
private readonly inputs: Inputs;
|
|
2280
|
+
_enabled = false;
|
|
2281
|
+
|
|
2282
|
+
constructor(private readonly inputName: string, bound: boolean) {
|
|
2283
|
+
this.inputs = new Inputs([inputName], [bound]);
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
apply(input: any[]): void {
|
|
2287
|
+
const changes = this.inputs.changes(input);
|
|
2288
|
+
const change = changes.get(this.inputName);
|
|
2289
|
+
if (change) {
|
|
2290
|
+
const value = this.normalizeInput(change.currentValue);
|
|
2291
|
+
this._enabled = this.enabled(value);
|
|
2292
|
+
this._validator = this._enabled ? this.createValidator(value) : nullValidator;
|
|
2293
|
+
const onChange = this._onChange;
|
|
2294
|
+
if (onChange) onChange();
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
|
|
2298
|
+
validate(control: AbstractControl): ValidationErrors | null {
|
|
2299
|
+
return this._validator(control);
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
registerOnValidatorChange(fn: () => void): void {
|
|
2303
|
+
this._onChange = fn;
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
enabled(input: any): boolean {
|
|
2307
|
+
return input != null;
|
|
2308
|
+
}
|
|
2309
|
+
|
|
2310
|
+
abstract normalizeInput(input: any): any;
|
|
2311
|
+
|
|
2312
|
+
abstract createValidator(input: any): ValidatorFn;
|
|
2313
|
+
}
|
|
2314
|
+
|
|
2315
|
+
export class RequiredValidator extends AbstractValidatorDirective {
|
|
2316
|
+
constructor(bound: boolean) {
|
|
2317
|
+
super('required', bound);
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
override normalizeInput(input: any): any {
|
|
2321
|
+
return booleanAttribute(input);
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
override createValidator(input: any): ValidatorFn {
|
|
2325
|
+
return requiredValidator;
|
|
2326
|
+
}
|
|
2327
|
+
|
|
2328
|
+
override enabled(input: any): boolean {
|
|
2329
|
+
return input;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
|
|
2333
|
+
export class MinLengthValidator extends AbstractValidatorDirective {
|
|
2334
|
+
constructor(bound: boolean) {
|
|
2335
|
+
super('minlength', bound);
|
|
2336
|
+
}
|
|
2337
|
+
|
|
2338
|
+
override normalizeInput(input: any): any {
|
|
2339
|
+
return toInteger(input);
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
override createValidator(minlength: any): ValidatorFn {
|
|
2343
|
+
return minLengthValidator(minlength);
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
|
|
2347
|
+
export class MaxLengthValidator extends AbstractValidatorDirective {
|
|
2348
|
+
constructor(bound: boolean) {
|
|
2349
|
+
super('maxlength', bound);
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
override normalizeInput(input: any): any {
|
|
2353
|
+
return toInteger(input);
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
override createValidator(maxlength: any): ValidatorFn {
|
|
2357
|
+
return maxLengthValidator(maxlength);
|
|
2358
|
+
}
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
export class PatternValidator extends AbstractValidatorDirective {
|
|
2362
|
+
constructor(bound: boolean) {
|
|
2363
|
+
super('pattern', bound);
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
override normalizeInput(input: any): any {
|
|
2367
|
+
return input;
|
|
2368
|
+
}
|
|
2369
|
+
|
|
2370
|
+
override createValidator(input: any): ValidatorFn {
|
|
2371
|
+
return patternValidator(input);
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
export class EmailValidator extends AbstractValidatorDirective {
|
|
2376
|
+
constructor(bound: boolean) {
|
|
2377
|
+
super('email', bound);
|
|
2378
|
+
}
|
|
2379
|
+
|
|
2380
|
+
override normalizeInput(input: any): any {
|
|
2381
|
+
return input === '' || input === true || input === 'true';
|
|
2382
|
+
}
|
|
2383
|
+
|
|
2384
|
+
override createValidator(input: any): ValidatorFn {
|
|
2385
|
+
return emailValidator;
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2388
|
+
override enabled(input: any): boolean {
|
|
2389
|
+
return input;
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
function toInteger(value: any): number {
|
|
2394
|
+
return typeof value === 'number' ? value : parseInt(value, 10);
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
// ---- NgControlStatus and NgControlStatusGroup ----
|
|
2398
|
+
|
|
2399
|
+
const STATUS_CLASSES = ['ng-untouched', 'ng-touched', 'ng-pristine', 'ng-dirty', 'ng-valid', 'ng-invalid', 'ng-pending', 'ng-submitted'];
|
|
2400
|
+
|
|
2401
|
+
/**
|
|
2402
|
+
* NgControlStatus (on a control's element) and NgControlStatusGroup (`group`, on a container's): the `ng-*` classes of
|
|
2403
|
+
* its directive's control, a class written only when it changes, and the view checked again when the control's
|
|
2404
|
+
* status, pristine or touched state, or the form's submitted state, changes.
|
|
2405
|
+
*/
|
|
2406
|
+
export class NgControlStatus extends ControlStateObserver {
|
|
2407
|
+
private readonly applied: boolean[] = [];
|
|
2408
|
+
private watched: AbstractControl | null = null;
|
|
2409
|
+
|
|
2410
|
+
constructor(private readonly view: any, private readonly markForCheck: () => void, private readonly dir: AbstractControlDirective, private readonly group: boolean) {
|
|
2411
|
+
super();
|
|
2412
|
+
}
|
|
2413
|
+
|
|
2414
|
+
/** The classes as the control is now: run when its view is checked. */
|
|
2415
|
+
check(): void {
|
|
2416
|
+
const control = this.dir.control;
|
|
2417
|
+
if (control !== this.watched) {
|
|
2418
|
+
if (this.watched) this.unwatch(this.watched);
|
|
2419
|
+
this.watched = control;
|
|
2420
|
+
if (control) control._stateListeners.push(this);
|
|
2421
|
+
}
|
|
2422
|
+
const flags = [!!control?.untouched, !!control?.touched, !!control?.pristine, !!control?.dirty, !!control?.valid, !!control?.invalid, !!control?.pending];
|
|
2423
|
+
if (this.group) flags.push(this.dir instanceof ControlContainer && this.dir.submitted);
|
|
2424
|
+
for (let k = 0; k < flags.length; k++) {
|
|
2425
|
+
const first = this.applied.length <= k;
|
|
2426
|
+
if (!first && this.applied[k] === flags[k]) continue;
|
|
2427
|
+
this.applied[k] = flags[k];
|
|
2428
|
+
if (!first || flags[k]) setViewClass(this.view, STATUS_CLASSES[k], flags[k]);
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
|
|
2432
|
+
override controlStateChanged(): void {
|
|
2433
|
+
this.markForCheck();
|
|
2434
|
+
this.check();
|
|
2435
|
+
}
|
|
2436
|
+
|
|
2437
|
+
destroy(): void {
|
|
2438
|
+
if (this.watched) this.unwatch(this.watched);
|
|
2439
|
+
this.watched = null;
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
private unwatch(control: AbstractControl): void {
|
|
2443
|
+
const at = control._stateListeners.indexOf(this);
|
|
2444
|
+
if (at > -1) control._stateListeners.splice(at, 1);
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
// ---- template-driven directives ----
|
|
2449
|
+
|
|
2450
|
+
export class NgModel extends NgControl {
|
|
2451
|
+
_control: FormControl = new FormControl();
|
|
2452
|
+
_registered = false;
|
|
2453
|
+
viewModel: any;
|
|
2454
|
+
isDisabled: any;
|
|
2455
|
+
model: any;
|
|
2456
|
+
options: { name?: string; standalone?: boolean; updateOn?: FormHooks } | undefined;
|
|
2457
|
+
readonly update = new EventEmitter<any>();
|
|
2458
|
+
private readonly inputs: Inputs;
|
|
2459
|
+
|
|
2460
|
+
/** `bound` says which of ngModel, name, ngModelOptions and disabled the element binds or sets. */
|
|
2461
|
+
constructor(parent: ControlContainer | null, validators: any[], asyncValidators: any[], accessor: NsValueAccessor, private readonly markForCheck: () => void, bound: boolean[]) {
|
|
2462
|
+
super();
|
|
2463
|
+
this._parent = parent;
|
|
2464
|
+
this._accessor = accessor;
|
|
2465
|
+
this._setValidators(validators);
|
|
2466
|
+
this._setAsyncValidators(asyncValidators);
|
|
2467
|
+
this.inputs = new Inputs(['model', 'name', 'options', 'isDisabled'], bound);
|
|
2468
|
+
}
|
|
2469
|
+
|
|
2470
|
+
/** The element's ngModel, name, ngModelOptions and disabled, at each check. */
|
|
2471
|
+
apply(values: any[]): void {
|
|
2472
|
+
const changes = this.inputs.changes(values);
|
|
2473
|
+
if (changes.has('model')) this.model = values[0];
|
|
2474
|
+
if (changes.has('name')) this.name = values[1];
|
|
2475
|
+
if (changes.has('options')) this.options = values[2];
|
|
2476
|
+
if (changes.has('isDisabled')) this.isDisabled = values[3];
|
|
2477
|
+
if (changes.size) this.ngOnChanges(changes);
|
|
2478
|
+
}
|
|
2479
|
+
|
|
2480
|
+
ngOnChanges(changes: Map<string, InputChange>): void {
|
|
2481
|
+
this._checkName();
|
|
2482
|
+
if (!this._registered || changes.has('name')) {
|
|
2483
|
+
if (this._registered) {
|
|
2484
|
+
this._checkName();
|
|
2485
|
+
const formDirective = this.formDirective;
|
|
2486
|
+
if (formDirective) {
|
|
2487
|
+
const oldName = changes.get('name')!.previousValue;
|
|
2488
|
+
(formDirective as NgForm).removeControlAt(this._getPath(oldName), oldName);
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
this._setUpControl();
|
|
2492
|
+
}
|
|
2493
|
+
if (changes.has('isDisabled')) this._updateDisabled(changes.get('isDisabled')!);
|
|
2494
|
+
if (isPropertyUpdated(changes, this.viewModel)) {
|
|
2495
|
+
this._updateValue(this.model);
|
|
2496
|
+
this.viewModel = this.model;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
ngOnDestroy(): void {
|
|
2501
|
+
this.formDirective?.removeControl(this);
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
override get control(): FormControl {
|
|
2505
|
+
return this._control;
|
|
2506
|
+
}
|
|
2507
|
+
|
|
2508
|
+
override get path(): string[] {
|
|
2509
|
+
return this._getPath(this.name);
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
get formDirective(): ControlContainer | null {
|
|
2513
|
+
const parent = this._parent;
|
|
2514
|
+
return parent ? parent.formDirective : null;
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
override viewToModelUpdate(newValue: any): void {
|
|
2518
|
+
this.viewModel = newValue;
|
|
2519
|
+
this.update.emit(newValue);
|
|
2520
|
+
}
|
|
2521
|
+
|
|
2522
|
+
private _setUpControl(): void {
|
|
2523
|
+
this._setUpdateStrategy();
|
|
2524
|
+
if (this._isStandalone()) this._setUpStandalone();
|
|
2525
|
+
else this.formDirective!.addControl(this);
|
|
2526
|
+
this._registered = true;
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
private _setUpdateStrategy(): void {
|
|
2530
|
+
const options = this.options;
|
|
2531
|
+
if (options && options.updateOn != null) this.control._updateOn = options.updateOn;
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2534
|
+
_isStandalone(): boolean {
|
|
2535
|
+
const options = this.options;
|
|
2536
|
+
return !this._parent || !!(options && options.standalone);
|
|
2537
|
+
}
|
|
2538
|
+
|
|
2539
|
+
private _setUpStandalone(): void {
|
|
2540
|
+
setUpControl(this.control, this);
|
|
2541
|
+
this.control.updateValueAndValidity({ emitEvent: false });
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
_setupWithForm(): void {
|
|
2545
|
+
setUpControl(this.control, this);
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
private _checkName(): void {
|
|
2549
|
+
const options = this.options;
|
|
2550
|
+
if (options && options.name) this.name = options.name;
|
|
2551
|
+
if (!this._isStandalone() && !this.name) {
|
|
2552
|
+
throw new Error(`NG01352: If ngModel is used within a form tag, either the name attribute must be set or the form
|
|
2553
|
+
control must be defined as 'standalone' in ngModelOptions.
|
|
2554
|
+
|
|
2555
|
+
Example 1: <input [(ngModel)]="person.firstName" name="first">
|
|
2556
|
+
Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">`);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
private _updateValue(value: any): void {
|
|
2561
|
+
resolvedPromise.then(() => {
|
|
2562
|
+
this.control.setValue(value, { emitViewToModelChange: false });
|
|
2563
|
+
this.markForCheck();
|
|
2564
|
+
});
|
|
2565
|
+
}
|
|
2566
|
+
|
|
2567
|
+
private _updateDisabled(change: InputChange): void {
|
|
2568
|
+
const disabledValue = change.currentValue;
|
|
2569
|
+
const isDisabled = disabledValue !== 0 && booleanAttribute(disabledValue);
|
|
2570
|
+
resolvedPromise.then(() => {
|
|
2571
|
+
if (isDisabled && !this.control.disabled) this.control.disable();
|
|
2572
|
+
else if (!isDisabled && this.control.disabled) this.control.enable();
|
|
2573
|
+
this.markForCheck();
|
|
2574
|
+
});
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
private _getPath(controlName: string): string[] {
|
|
2578
|
+
const parent = this._parent;
|
|
2579
|
+
return parent ? controlPath(controlName, parent) : [controlName];
|
|
2580
|
+
}
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
export class NgForm extends ControlContainer {
|
|
2584
|
+
private _submitted = false;
|
|
2585
|
+
private readonly _directives: NgModel[] = [];
|
|
2586
|
+
readonly form: FormGroup;
|
|
2587
|
+
readonly ngSubmit = new EventEmitter<any>();
|
|
2588
|
+
options: { updateOn?: FormHooks } | undefined;
|
|
2589
|
+
private readonly inputs: Inputs;
|
|
2590
|
+
|
|
2591
|
+
private readonly callSetDisabledState: string;
|
|
2592
|
+
|
|
2593
|
+
constructor(validators: any[], asyncValidators: any[], callSetDisabledState: string, bound: boolean[]) {
|
|
2594
|
+
super();
|
|
2595
|
+
this.callSetDisabledState = callSetDisabledState;
|
|
2596
|
+
this.form = new FormGroup({}, composeValidators(validators), composeAsyncValidators(asyncValidators));
|
|
2597
|
+
this.inputs = new Inputs(['options'], bound);
|
|
2598
|
+
}
|
|
2599
|
+
|
|
2600
|
+
/** The element's ngFormOptions at each check. */
|
|
2601
|
+
apply(values: any[]): void {
|
|
2602
|
+
if (this.inputs.changes(values).has('options')) this.options = values[0];
|
|
2603
|
+
}
|
|
2604
|
+
|
|
2605
|
+
override get submitted(): boolean {
|
|
2606
|
+
return this._submitted;
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
private _setSubmitted(value: boolean): void {
|
|
2610
|
+
if (this._submitted === value) return;
|
|
2611
|
+
this._submitted = value;
|
|
2612
|
+
this.form._stateChangedOutside();
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
ngAfterViewInit(): void {
|
|
2616
|
+
this._setUpdateStrategy();
|
|
2617
|
+
}
|
|
2618
|
+
|
|
2619
|
+
override get formDirective(): ControlContainer {
|
|
2620
|
+
return this;
|
|
2621
|
+
}
|
|
2622
|
+
|
|
2623
|
+
override get control(): FormGroup {
|
|
2624
|
+
return this.form;
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2627
|
+
override get path(): string[] {
|
|
2628
|
+
return [];
|
|
2629
|
+
}
|
|
2630
|
+
|
|
2631
|
+
get controls(): { [key: string]: AbstractControl } {
|
|
2632
|
+
const controls: any = this.form.controls;
|
|
2633
|
+
return controls;
|
|
2634
|
+
}
|
|
2635
|
+
|
|
2636
|
+
override addControl(dir: NgControl): FormControl | null {
|
|
2637
|
+
const model = dir as NgModel;
|
|
2638
|
+
resolvedPromise.then(() => {
|
|
2639
|
+
const container = this._findContainer(model.path);
|
|
2640
|
+
model._control = container.registerControl(model.name, model.control) as FormControl;
|
|
2641
|
+
model._setupWithForm();
|
|
2642
|
+
model.control.updateValueAndValidity({ emitEvent: false });
|
|
2643
|
+
this._directives.push(model);
|
|
2644
|
+
});
|
|
2645
|
+
return null;
|
|
2646
|
+
}
|
|
2647
|
+
|
|
2648
|
+
getControl(dir: NgControl): FormControl | null {
|
|
2649
|
+
return this.form.get(dir.path!) as FormControl;
|
|
2650
|
+
}
|
|
2651
|
+
|
|
2652
|
+
override removeControl(dir: NgControl): void {
|
|
2653
|
+
const model = dir as NgModel;
|
|
2654
|
+
resolvedPromise.then(() => {
|
|
2655
|
+
const container = this._findContainer(model.path);
|
|
2656
|
+
container?.removeControl(model.name);
|
|
2657
|
+
const at = this._directives.indexOf(model);
|
|
2658
|
+
if (at > -1) this._directives.splice(at, 1);
|
|
2659
|
+
});
|
|
2660
|
+
}
|
|
2661
|
+
|
|
2662
|
+
/** removeControl of the control a directive had at `path` under `name`, before its name changed. */
|
|
2663
|
+
removeControlAt(path: string[], name: string): void {
|
|
2664
|
+
resolvedPromise.then(() => {
|
|
2665
|
+
const container = this._findContainer(path);
|
|
2666
|
+
container?.removeControl(name);
|
|
2667
|
+
});
|
|
2668
|
+
}
|
|
2669
|
+
|
|
2670
|
+
override addFormGroup(dir: ControlContainer): void {
|
|
2671
|
+
resolvedPromise.then(() => {
|
|
2672
|
+
const container = this._findContainer(dir.path!);
|
|
2673
|
+
const group = new FormGroup({});
|
|
2674
|
+
setUpFormContainer(group, dir);
|
|
2675
|
+
container.registerControl(String(dir.name), group);
|
|
2676
|
+
group.updateValueAndValidity({ emitEvent: false });
|
|
2677
|
+
});
|
|
2678
|
+
}
|
|
2679
|
+
|
|
2680
|
+
override removeFormGroup(dir: ControlContainer): void {
|
|
2681
|
+
resolvedPromise.then(() => {
|
|
2682
|
+
const container = this._findContainer(dir.path!);
|
|
2683
|
+
container?.removeControl(String(dir.name));
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
override getFormGroup(dir: ControlContainer): FormGroup | null {
|
|
2688
|
+
return this.form.get(dir.path!) as FormGroup;
|
|
2689
|
+
}
|
|
2690
|
+
|
|
2691
|
+
override addFormArray(dir: ControlContainer): void {}
|
|
2692
|
+
|
|
2693
|
+
override removeFormArray(dir: ControlContainer): void {}
|
|
2694
|
+
|
|
2695
|
+
override getFormArray(dir: ControlContainer): AbstractControl | null {
|
|
2696
|
+
return this.form.get(dir.path!);
|
|
2697
|
+
}
|
|
2698
|
+
|
|
2699
|
+
override updateModel(dir: NgControl, value: any): void {
|
|
2700
|
+
resolvedPromise.then(() => {
|
|
2701
|
+
const ctrl = this.form.get(dir.path!) as FormControl;
|
|
2702
|
+
ctrl.setValue(value);
|
|
2703
|
+
});
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2706
|
+
setValue(value: { [key: string]: any }): void {
|
|
2707
|
+
this.control.setValue(value);
|
|
2708
|
+
}
|
|
2709
|
+
|
|
2710
|
+
onSubmit($event?: any): boolean {
|
|
2711
|
+
this._setSubmitted(true);
|
|
2712
|
+
syncPendingControls(this.form, this._directives);
|
|
2713
|
+
this.ngSubmit.emit($event);
|
|
2714
|
+
this.form._events.next(new FormSubmittedEvent(this.control));
|
|
2715
|
+
return false;
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
onReset(): void {
|
|
2719
|
+
this.resetForm();
|
|
2720
|
+
}
|
|
2721
|
+
|
|
2722
|
+
resetForm(value: any = undefined): void {
|
|
2723
|
+
this.form.reset(value);
|
|
2724
|
+
this._setSubmitted(false);
|
|
2725
|
+
}
|
|
2726
|
+
|
|
2727
|
+
private _setUpdateStrategy(): void {
|
|
2728
|
+
const options = this.options;
|
|
2729
|
+
if (options && options.updateOn != null) this.form._updateOn = options.updateOn;
|
|
2730
|
+
}
|
|
2731
|
+
|
|
2732
|
+
private _findContainer(path: string[]): FormGroup {
|
|
2733
|
+
path.pop();
|
|
2734
|
+
return path.length ? (this.form.get(path) as FormGroup) : this.form;
|
|
2735
|
+
}
|
|
2736
|
+
}
|
|
2737
|
+
|
|
2738
|
+
/** NgModelGroup, FormGroupName (a group, `formGroupName`) and FormArrayName (`array`): a named control of the form above. */
|
|
2739
|
+
export class AbstractFormGroupDirective extends ControlContainer {
|
|
2740
|
+
_parent: ControlContainer;
|
|
2741
|
+
private readonly inputs: Inputs;
|
|
2742
|
+
|
|
2743
|
+
constructor(parent: ControlContainer, validators: any[], asyncValidators: any[], private readonly array: boolean, bound: boolean[]) {
|
|
2744
|
+
super();
|
|
2745
|
+
this._parent = parent;
|
|
2746
|
+
this._setValidators(validators);
|
|
2747
|
+
this._setAsyncValidators(asyncValidators);
|
|
2748
|
+
this.inputs = new Inputs(['name'], bound);
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
/** The element's ngModelGroup, formGroupName or formArrayName at each check. */
|
|
2752
|
+
apply(values: any[]): void {
|
|
2753
|
+
if (this.inputs.changes(values).has('name')) this.name = values[0];
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
ngOnInit(): void {
|
|
2757
|
+
if (this.array) this.formDirective!.addFormArray(this);
|
|
2758
|
+
else this.formDirective!.addFormGroup(this);
|
|
2759
|
+
}
|
|
2760
|
+
|
|
2761
|
+
ngOnDestroy(): void {
|
|
2762
|
+
const formDirective = this.formDirective;
|
|
2763
|
+
if (!formDirective) return;
|
|
2764
|
+
if (this.array) formDirective.removeFormArray(this);
|
|
2765
|
+
else formDirective.removeFormGroup(this);
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
override get control(): AbstractControl | null {
|
|
2769
|
+
const formDirective = this.formDirective;
|
|
2770
|
+
if (!formDirective) return null;
|
|
2771
|
+
return this.array ? formDirective.getFormArray(this) : formDirective.getFormGroup(this);
|
|
2772
|
+
}
|
|
2773
|
+
|
|
2774
|
+
override get path(): string[] {
|
|
2775
|
+
const name = this.name;
|
|
2776
|
+
return controlPath(name == null ? name : String(name), this._parent);
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
override get formDirective(): ControlContainer | null {
|
|
2780
|
+
const parent = this._parent;
|
|
2781
|
+
return parent ? parent.formDirective : null;
|
|
2782
|
+
}
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
export class NgModelGroup extends AbstractFormGroupDirective {}
|
|
2786
|
+
export class FormGroupName extends AbstractFormGroupDirective {}
|
|
2787
|
+
export class FormArrayName extends AbstractFormGroupDirective {}
|
|
2788
|
+
|
|
2789
|
+
// ---- reactive directives ----
|
|
2790
|
+
|
|
2791
|
+
/** FormGroupDirective (`[formGroup]`) and FormArrayDirective (`[formArray]`). */
|
|
2792
|
+
export class AbstractFormDirective extends ControlContainer {
|
|
2793
|
+
private _submitted = false;
|
|
2794
|
+
private _oldForm: AbstractControl | undefined;
|
|
2795
|
+
private readonly _onCollectionChange = () => this._updateDomValue();
|
|
2796
|
+
directives: FormControlName[] = [];
|
|
2797
|
+
form!: AbstractControl;
|
|
2798
|
+
readonly ngSubmit = new EventEmitter<any>();
|
|
2799
|
+
private readonly inputs: Inputs;
|
|
2800
|
+
|
|
2801
|
+
private readonly callSetDisabledState: string;
|
|
2802
|
+
private readonly inputName: string;
|
|
2803
|
+
|
|
2804
|
+
constructor(validators: any[], asyncValidators: any[], callSetDisabledState: string, inputName: string, bound: boolean[]) {
|
|
2805
|
+
super();
|
|
2806
|
+
this.callSetDisabledState = callSetDisabledState;
|
|
2807
|
+
this.inputName = inputName;
|
|
2808
|
+
this._setValidators(validators);
|
|
2809
|
+
this._setAsyncValidators(asyncValidators);
|
|
2810
|
+
this.inputs = new Inputs(['form'], bound);
|
|
2811
|
+
}
|
|
2812
|
+
|
|
2813
|
+
/** The element's formGroup or formArray at each check. */
|
|
2814
|
+
apply(values: any[]): void {
|
|
2815
|
+
const changes = this.inputs.changes(values);
|
|
2816
|
+
if (changes.has('form')) this.form = values[0];
|
|
2817
|
+
if (changes.size) this.onChanges(changes);
|
|
2818
|
+
}
|
|
2819
|
+
|
|
2820
|
+
override get submitted(): boolean {
|
|
2821
|
+
return this._submitted;
|
|
2822
|
+
}
|
|
2823
|
+
|
|
2824
|
+
private _setSubmitted(value: boolean): void {
|
|
2825
|
+
if (this._submitted === value) return;
|
|
2826
|
+
this._submitted = value;
|
|
2827
|
+
this.form._stateChangedOutside();
|
|
2828
|
+
}
|
|
2829
|
+
|
|
2830
|
+
private onChanges(changes: Map<string, InputChange>): void {
|
|
2831
|
+
if (!this.form) {
|
|
2832
|
+
throw new Error(`NG01050: ${this.inputName} expects a ${this.inputName === 'formGroup' ? 'FormGroup' : 'FormArray'} instance. Please pass one in.`);
|
|
2833
|
+
}
|
|
2834
|
+
if (changes.has('form')) {
|
|
2835
|
+
this._updateValidators();
|
|
2836
|
+
this._updateDomValue();
|
|
2837
|
+
this._updateRegistrations();
|
|
2838
|
+
this._oldForm = this.form;
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
|
|
2842
|
+
ngOnDestroy(): void {
|
|
2843
|
+
if (this.form) {
|
|
2844
|
+
cleanUpValidators(this.form, this);
|
|
2845
|
+
if (this.form._onCollectionChange === this._onCollectionChange) this.form._registerOnCollectionChange(() => {});
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
|
|
2849
|
+
override get formDirective(): ControlContainer {
|
|
2850
|
+
return this;
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
override get control(): AbstractControl {
|
|
2854
|
+
return this.form;
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
override get path(): string[] {
|
|
2858
|
+
return [];
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2861
|
+
override addControl(dir: NgControl): FormControl | null {
|
|
2862
|
+
const named = dir as FormControlName;
|
|
2863
|
+
const ctrl = this.form.get(named.path) as FormControl;
|
|
2864
|
+
named._setupWithForm(ctrl, this.callSetDisabledState);
|
|
2865
|
+
ctrl.updateValueAndValidity({ emitEvent: false });
|
|
2866
|
+
this.directives.push(named);
|
|
2867
|
+
return ctrl;
|
|
2868
|
+
}
|
|
2869
|
+
|
|
2870
|
+
getControl(dir: NgControl): FormControl | null {
|
|
2871
|
+
return this.form.get(dir.path!) as FormControl;
|
|
2872
|
+
}
|
|
2873
|
+
|
|
2874
|
+
override removeControl(dir: NgControl): void {
|
|
2875
|
+
const named = dir as FormControlName;
|
|
2876
|
+
cleanUpControl(named._controlOrNull(), named, false);
|
|
2877
|
+
const at = this.directives.indexOf(named);
|
|
2878
|
+
if (at > -1) this.directives.splice(at, 1);
|
|
2879
|
+
}
|
|
2880
|
+
|
|
2881
|
+
override addFormGroup(dir: ControlContainer): void {
|
|
2882
|
+
this._setUpFormContainer(dir);
|
|
2883
|
+
}
|
|
2884
|
+
|
|
2885
|
+
override removeFormGroup(dir: ControlContainer): void {
|
|
2886
|
+
this._cleanUpFormContainer(dir);
|
|
2887
|
+
}
|
|
2888
|
+
|
|
2889
|
+
override getFormGroup(dir: ControlContainer): FormGroup | null {
|
|
2890
|
+
return this.form.get(dir.path!) as FormGroup;
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
override getFormArray(dir: ControlContainer): AbstractControl | null {
|
|
2894
|
+
return this.form.get(dir.path!);
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
override addFormArray(dir: ControlContainer): void {
|
|
2898
|
+
this._setUpFormContainer(dir);
|
|
2899
|
+
}
|
|
2900
|
+
|
|
2901
|
+
override removeFormArray(dir: ControlContainer): void {
|
|
2902
|
+
this._cleanUpFormContainer(dir);
|
|
2903
|
+
}
|
|
2904
|
+
|
|
2905
|
+
override updateModel(dir: NgControl, value: any): void {
|
|
2906
|
+
const ctrl = this.form.get(dir.path!) as FormControl;
|
|
2907
|
+
ctrl.setValue(value);
|
|
2908
|
+
}
|
|
2909
|
+
|
|
2910
|
+
onReset(): void {
|
|
2911
|
+
this.resetForm();
|
|
2912
|
+
}
|
|
2913
|
+
|
|
2914
|
+
resetForm(value: any = undefined, options: { onlySelf?: boolean; emitEvent?: boolean } = {}): void {
|
|
2915
|
+
this.form.reset(value, options);
|
|
2916
|
+
this._setSubmitted(false);
|
|
2917
|
+
}
|
|
2918
|
+
|
|
2919
|
+
onSubmit($event?: any): boolean {
|
|
2920
|
+
this._setSubmitted(true);
|
|
2921
|
+
syncPendingControls(this.form, this.directives);
|
|
2922
|
+
this.ngSubmit.emit($event);
|
|
2923
|
+
this.form._events.next(new FormSubmittedEvent(this.control));
|
|
2924
|
+
return false;
|
|
2925
|
+
}
|
|
2926
|
+
|
|
2927
|
+
_updateDomValue(): void {
|
|
2928
|
+
this.directives.forEach((dir: FormControlName) => {
|
|
2929
|
+
const oldCtrl = dir._controlOrNull();
|
|
2930
|
+
const newCtrl = this.form.get(dir.path);
|
|
2931
|
+
if (oldCtrl !== newCtrl) {
|
|
2932
|
+
cleanUpControl(oldCtrl || null, dir);
|
|
2933
|
+
if (newCtrl instanceof FormControl) dir._setupWithForm(newCtrl, this.callSetDisabledState);
|
|
2934
|
+
}
|
|
2935
|
+
});
|
|
2936
|
+
this.form._updateTreeValidity({ emitEvent: false });
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
private _setUpFormContainer(dir: ControlContainer): void {
|
|
2940
|
+
const ctrl = this.form.get(dir.path!)!;
|
|
2941
|
+
setUpFormContainer(ctrl, dir);
|
|
2942
|
+
ctrl.updateValueAndValidity({ emitEvent: false });
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
private _cleanUpFormContainer(dir: ControlContainer): void {
|
|
2946
|
+
const ctrl = this.form?.get(dir.path!);
|
|
2947
|
+
if (ctrl) {
|
|
2948
|
+
const isControlUpdated = cleanUpFormContainer(ctrl, dir);
|
|
2949
|
+
if (isControlUpdated) ctrl.updateValueAndValidity({ emitEvent: false });
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
private _updateRegistrations(): void {
|
|
2954
|
+
this.form._registerOnCollectionChange(this._onCollectionChange);
|
|
2955
|
+
this._oldForm?._registerOnCollectionChange(() => {});
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
private _updateValidators(): void {
|
|
2959
|
+
setUpValidators(this.form, this);
|
|
2960
|
+
if (this._oldForm) cleanUpValidators(this._oldForm, this);
|
|
2961
|
+
}
|
|
2962
|
+
}
|
|
2963
|
+
|
|
2964
|
+
export class FormGroupDirective extends AbstractFormDirective {}
|
|
2965
|
+
export class FormArrayDirective extends AbstractFormDirective {}
|
|
2966
|
+
|
|
2967
|
+
export class FormControlName extends NgControl {
|
|
2968
|
+
private _added = false;
|
|
2969
|
+
viewModel: any;
|
|
2970
|
+
private _control: FormControl | null = null;
|
|
2971
|
+
model: any;
|
|
2972
|
+
readonly update = new EventEmitter<any>();
|
|
2973
|
+
private readonly inputs: Inputs;
|
|
2974
|
+
|
|
2975
|
+
/** `bound` says which of formControlName and ngModel the element binds or sets. */
|
|
2976
|
+
constructor(parent: ControlContainer, validators: any[], asyncValidators: any[], accessor: NsValueAccessor, bound: boolean[]) {
|
|
2977
|
+
super();
|
|
2978
|
+
this._parent = parent;
|
|
2979
|
+
this._accessor = accessor;
|
|
2980
|
+
this._setValidators(validators);
|
|
2981
|
+
this._setAsyncValidators(asyncValidators);
|
|
2982
|
+
this.inputs = new Inputs(['name', 'model'], bound);
|
|
2983
|
+
}
|
|
2984
|
+
|
|
2985
|
+
apply(values: any[]): void {
|
|
2986
|
+
const changes = this.inputs.changes(values);
|
|
2987
|
+
if (changes.has('name')) this.name = values[0];
|
|
2988
|
+
if (changes.has('model')) this.model = values[1];
|
|
2989
|
+
if (changes.size) this.ngOnChanges(changes);
|
|
2990
|
+
}
|
|
2991
|
+
|
|
2992
|
+
override get control(): FormControl {
|
|
2993
|
+
return this._control!;
|
|
2994
|
+
}
|
|
2995
|
+
|
|
2996
|
+
_controlOrNull(): FormControl | null {
|
|
2997
|
+
return this._control;
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
_setupWithForm(control: FormControl, callSetDisabledState?: string): void {
|
|
3001
|
+
this._control = control;
|
|
3002
|
+
setUpControl(control, this, callSetDisabledState);
|
|
3003
|
+
}
|
|
3004
|
+
|
|
3005
|
+
ngOnChanges(changes: Map<string, InputChange>): void {
|
|
3006
|
+
if (!this._added) this._setUpControl();
|
|
3007
|
+
if (isPropertyUpdated(changes, this.viewModel)) {
|
|
3008
|
+
this.viewModel = this.model;
|
|
3009
|
+
this.formDirective!.updateModel(this, this.model);
|
|
3010
|
+
}
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
ngOnDestroy(): void {
|
|
3014
|
+
this.formDirective?.removeControl(this);
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
override viewToModelUpdate(newValue: any): void {
|
|
3018
|
+
this.viewModel = newValue;
|
|
3019
|
+
this.update.emit(newValue);
|
|
3020
|
+
}
|
|
3021
|
+
|
|
3022
|
+
override get path(): string[] {
|
|
3023
|
+
const name = this.name;
|
|
3024
|
+
return controlPath(name == null ? name : String(name), this._parent!);
|
|
3025
|
+
}
|
|
3026
|
+
|
|
3027
|
+
get formDirective(): ControlContainer | null {
|
|
3028
|
+
const parent = this._parent;
|
|
3029
|
+
return parent ? parent.formDirective : null;
|
|
3030
|
+
}
|
|
3031
|
+
|
|
3032
|
+
private _setUpControl(): void {
|
|
3033
|
+
this._control = this.formDirective!.addControl(this);
|
|
3034
|
+
this._added = true;
|
|
3035
|
+
}
|
|
3036
|
+
}
|
|
3037
|
+
|
|
3038
|
+
export class FormControlDirective extends NgControl {
|
|
3039
|
+
viewModel: any;
|
|
3040
|
+
form!: FormControl;
|
|
3041
|
+
model: any;
|
|
3042
|
+
readonly update = new EventEmitter<any>();
|
|
3043
|
+
private readonly inputs: Inputs;
|
|
3044
|
+
|
|
3045
|
+
/** `bound` says which of formControl and ngModel the element binds. */
|
|
3046
|
+
private readonly callSetDisabledState: string;
|
|
3047
|
+
|
|
3048
|
+
constructor(validators: any[], asyncValidators: any[], accessor: NsValueAccessor, callSetDisabledState: string, bound: boolean[]) {
|
|
3049
|
+
super();
|
|
3050
|
+
this.callSetDisabledState = callSetDisabledState;
|
|
3051
|
+
this._accessor = accessor;
|
|
3052
|
+
this._setValidators(validators);
|
|
3053
|
+
this._setAsyncValidators(asyncValidators);
|
|
3054
|
+
this.inputs = new Inputs(['form', 'model'], bound);
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
apply(values: any[]): void {
|
|
3058
|
+
const changes = this.inputs.changes(values);
|
|
3059
|
+
if (changes.has('form')) this.form = values[0];
|
|
3060
|
+
if (changes.has('model')) this.model = values[1];
|
|
3061
|
+
if (changes.size) this.ngOnChanges(changes);
|
|
3062
|
+
}
|
|
3063
|
+
|
|
3064
|
+
ngOnChanges(changes: Map<string, InputChange>): void {
|
|
3065
|
+
const formChange = changes.get('form');
|
|
3066
|
+
if (formChange) {
|
|
3067
|
+
const previousForm: FormControl | null = formChange.previousValue;
|
|
3068
|
+
if (previousForm) cleanUpControl(previousForm, this, false);
|
|
3069
|
+
setUpControl(this.form, this, this.callSetDisabledState);
|
|
3070
|
+
this.form.updateValueAndValidity({ emitEvent: false });
|
|
3071
|
+
}
|
|
3072
|
+
if (isPropertyUpdated(changes, this.viewModel)) {
|
|
3073
|
+
this.form.setValue(this.model);
|
|
3074
|
+
this.viewModel = this.model;
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
ngOnDestroy(): void {
|
|
3079
|
+
if (this.form) cleanUpControl(this.form, this, false);
|
|
3080
|
+
}
|
|
3081
|
+
|
|
3082
|
+
override get path(): string[] {
|
|
3083
|
+
return [];
|
|
3084
|
+
}
|
|
3085
|
+
|
|
3086
|
+
override get control(): FormControl {
|
|
3087
|
+
return this.form;
|
|
3088
|
+
}
|
|
3089
|
+
|
|
3090
|
+
override viewToModelUpdate(newValue: any): void {
|
|
3091
|
+
this.viewModel = newValue;
|
|
3092
|
+
this.update.emit(newValue);
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
|
|
3096
|
+
// ---- What a component imports: the template compiler runs the directives they declare. ----
|
|
3097
|
+
|
|
3098
|
+
export const FormsModule: any = undefined;
|
|
3099
|
+
export const ReactiveFormsModule: any = undefined;
|