shared.js
6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { isBlank, isPresent, looseIdentical } from '../facade/lang';
import { Validators } from '../validators';
import { CheckboxControlValueAccessor } from './checkbox_value_accessor';
import { DefaultValueAccessor } from './default_value_accessor';
import { normalizeAsyncValidator, normalizeValidator } from './normalize_validator';
import { NumberValueAccessor } from './number_value_accessor';
import { RadioControlValueAccessor } from './radio_control_value_accessor';
import { RangeValueAccessor } from './range_value_accessor';
import { SelectControlValueAccessor } from './select_control_value_accessor';
import { SelectMultipleControlValueAccessor } from './select_multiple_control_value_accessor';
/**
* @param {?} name
* @param {?} parent
* @return {?}
*/
export function controlPath(name, parent) {
return parent.path.concat([name]);
}
/**
* @param {?} control
* @param {?} dir
* @return {?}
*/
export function setUpControl(control, dir) {
if (!control)
_throwError(dir, 'Cannot find control with');
if (!dir.valueAccessor)
_throwError(dir, 'No value accessor for form control with');
control.validator = Validators.compose([control.validator, dir.validator]);
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
dir.valueAccessor.writeValue(control.value);
// view -> model
dir.valueAccessor.registerOnChange(function (newValue) {
dir.viewToModelUpdate(newValue);
control.markAsDirty();
control.setValue(newValue, { emitModelToViewChange: false });
});
// touched
dir.valueAccessor.registerOnTouched(function () { return control.markAsTouched(); });
control.registerOnChange(function (newValue, emitModelEvent) {
// control -> view
dir.valueAccessor.writeValue(newValue);
// control -> ngModel
if (emitModelEvent)
dir.viewToModelUpdate(newValue);
});
if (dir.valueAccessor.setDisabledState) {
control.registerOnDisabledChange(function (isDisabled) { dir.valueAccessor.setDisabledState(isDisabled); });
}
// re-run validation when validator binding changes, e.g. minlength=3 -> minlength=4
dir._rawValidators.forEach(function (validator) {
if (((validator)).registerOnValidatorChange)
((validator)).registerOnValidatorChange(function () { return control.updateValueAndValidity(); });
});
dir._rawAsyncValidators.forEach(function (validator) {
if (((validator)).registerOnValidatorChange)
((validator)).registerOnValidatorChange(function () { return control.updateValueAndValidity(); });
});
}
/**
* @param {?} control
* @param {?} dir
* @return {?}
*/
export function cleanUpControl(control, dir) {
dir.valueAccessor.registerOnChange(function () { return _noControlError(dir); });
dir.valueAccessor.registerOnTouched(function () { return _noControlError(dir); });
dir._rawValidators.forEach(function (validator) {
if (validator.registerOnValidatorChange) {
validator.registerOnValidatorChange(null);
}
});
dir._rawAsyncValidators.forEach(function (validator) {
if (validator.registerOnValidatorChange) {
validator.registerOnValidatorChange(null);
}
});
if (control)
control._clearChangeFns();
}
/**
* @param {?} control
* @param {?} dir
* @return {?}
*/
export function setUpFormContainer(control, dir) {
if (isBlank(control))
_throwError(dir, 'Cannot find control with');
control.validator = Validators.compose([control.validator, dir.validator]);
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
}
/**
* @param {?} dir
* @return {?}
*/
function _noControlError(dir) {
return _throwError(dir, 'There is no FormControl instance attached to form control element with');
}
/**
* @param {?} dir
* @param {?} message
* @return {?}
*/
function _throwError(dir, message) {
var /** @type {?} */ messageEnd;
if (dir.path.length > 1) {
messageEnd = "path: '" + dir.path.join(' -> ') + "'";
}
else if (dir.path[0]) {
messageEnd = "name: '" + dir.path + "'";
}
else {
messageEnd = 'unspecified name attribute';
}
throw new Error(message + " " + messageEnd);
}
/**
* @param {?} validators
* @return {?}
*/
export function composeValidators(validators) {
return isPresent(validators) ? Validators.compose(validators.map(normalizeValidator)) : null;
}
/**
* @param {?} validators
* @return {?}
*/
export function composeAsyncValidators(validators) {
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
null;
}
/**
* @param {?} changes
* @param {?} viewModel
* @return {?}
*/
export function isPropertyUpdated(changes, viewModel) {
if (!changes.hasOwnProperty('model'))
return false;
var /** @type {?} */ change = changes['model'];
if (change.isFirstChange())
return true;
return !looseIdentical(viewModel, change.currentValue);
}
var /** @type {?} */ BUILTIN_ACCESSORS = [
CheckboxControlValueAccessor,
RangeValueAccessor,
NumberValueAccessor,
SelectControlValueAccessor,
SelectMultipleControlValueAccessor,
RadioControlValueAccessor,
];
/**
* @param {?} valueAccessor
* @return {?}
*/
export function isBuiltInAccessor(valueAccessor) {
return BUILTIN_ACCESSORS.some(function (a) { return valueAccessor.constructor === a; });
}
/**
* @param {?} dir
* @param {?} valueAccessors
* @return {?}
*/
export function selectValueAccessor(dir, valueAccessors) {
if (!valueAccessors)
return null;
var /** @type {?} */ defaultAccessor;
var /** @type {?} */ builtinAccessor;
var /** @type {?} */ customAccessor;
valueAccessors.forEach(function (v) {
if (v.constructor === DefaultValueAccessor) {
defaultAccessor = v;
}
else if (isBuiltInAccessor(v)) {
if (builtinAccessor)
_throwError(dir, 'More than one built-in value accessor matches form control with');
builtinAccessor = v;
}
else {
if (customAccessor)
_throwError(dir, 'More than one custom value accessor matches form control with');
customAccessor = v;
}
});
if (customAccessor)
return customAccessor;
if (builtinAccessor)
return builtinAccessor;
if (defaultAccessor)
return defaultAccessor;
_throwError(dir, 'No valid value accessor for form control with');
return null;
}
//# sourceMappingURL=shared.js.map