2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-06-14 21:23:40 -04:00
|
|
|
import {ListWrapper, StringMapWrapper} from '../facade/collection';
|
|
|
|
import {BaseException} from '../facade/exceptions';
|
|
|
|
import {hasConstructor, isBlank, isPresent, looseIdentical} from '../facade/lang';
|
2016-06-25 16:27:29 -04:00
|
|
|
import {FormArray, FormControl, FormGroup} from '../model';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {Validators} from '../validators';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
|
|
|
import {AbstractControlDirective} from './abstract_control_directive';
|
2016-06-12 19:37:42 -04:00
|
|
|
import {AbstractFormGroupDirective} from './abstract_form_group_directive';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {CheckboxControlValueAccessor} from './checkbox_value_accessor';
|
|
|
|
import {ControlContainer} from './control_container';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {ControlValueAccessor} from './control_value_accessor';
|
|
|
|
import {DefaultValueAccessor} from './default_value_accessor';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NgControl} from './ng_control';
|
|
|
|
import {normalizeAsyncValidator, normalizeValidator} from './normalize_validator';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {NumberValueAccessor} from './number_value_accessor';
|
|
|
|
import {RadioControlValueAccessor} from './radio_control_value_accessor';
|
2016-06-25 16:27:29 -04:00
|
|
|
import {FormArrayName} from './reactive_directives/form_array_name';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {SelectControlValueAccessor} from './select_control_value_accessor';
|
2016-06-26 18:24:27 -04:00
|
|
|
import {SelectMultipleControlValueAccessor} from './select_multiple_control_value_accessor';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {AsyncValidatorFn, ValidatorFn} from './validators';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
export function controlPath(name: string, parent: ControlContainer): string[] {
|
|
|
|
var p = ListWrapper.clone(parent.path);
|
|
|
|
p.push(name);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
export function setUpControl(control: FormControl, dir: NgControl): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
if (isBlank(control)) _throwError(dir, 'Cannot find control');
|
|
|
|
if (isBlank(dir.valueAccessor)) _throwError(dir, 'No value accessor for');
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
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((newValue: any) => {
|
|
|
|
dir.viewToModelUpdate(newValue);
|
|
|
|
control.updateValue(newValue, {emitModelToViewChange: false});
|
|
|
|
control.markAsDirty();
|
|
|
|
});
|
|
|
|
|
|
|
|
// model -> view
|
|
|
|
control.registerOnChange((newValue: any) => dir.valueAccessor.writeValue(newValue));
|
|
|
|
|
|
|
|
// touched
|
|
|
|
dir.valueAccessor.registerOnTouched(() => control.markAsTouched());
|
|
|
|
}
|
|
|
|
|
2016-06-25 16:27:29 -04:00
|
|
|
export function setUpFormContainer(
|
|
|
|
control: FormGroup | FormArray, dir: AbstractFormGroupDirective | FormArrayName) {
|
2016-06-08 19:38:52 -04:00
|
|
|
if (isBlank(control)) _throwError(dir, 'Cannot find control');
|
2016-06-08 18:36:24 -04:00
|
|
|
control.validator = Validators.compose([control.validator, dir.validator]);
|
|
|
|
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _throwError(dir: AbstractControlDirective, message: string): void {
|
2016-06-08 19:38:52 -04:00
|
|
|
var path = dir.path.join(' -> ');
|
2016-06-08 18:36:24 -04:00
|
|
|
throw new BaseException(`${message} '${path}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function composeValidators(validators: /* Array<Validator|Function> */ any[]): ValidatorFn {
|
|
|
|
return isPresent(validators) ? Validators.compose(validators.map(normalizeValidator)) : null;
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
export function composeAsyncValidators(validators: /* Array<Validator|Function> */ any[]):
|
|
|
|
AsyncValidatorFn {
|
2016-06-08 18:36:24 -04:00
|
|
|
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeAsyncValidator)) :
|
|
|
|
null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any): boolean {
|
2016-06-08 19:38:52 -04:00
|
|
|
if (!StringMapWrapper.contains(changes, 'model')) return false;
|
|
|
|
var change = changes['model'];
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
if (change.isFirstChange()) return true;
|
|
|
|
return !looseIdentical(viewModel, change.currentValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
|
2016-06-08 19:38:52 -04:00
|
|
|
export function selectValueAccessor(
|
|
|
|
dir: NgControl, valueAccessors: ControlValueAccessor[]): ControlValueAccessor {
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isBlank(valueAccessors)) return null;
|
|
|
|
|
|
|
|
var defaultAccessor: ControlValueAccessor;
|
|
|
|
var builtinAccessor: ControlValueAccessor;
|
|
|
|
var customAccessor: ControlValueAccessor;
|
|
|
|
valueAccessors.forEach((v: ControlValueAccessor) => {
|
|
|
|
if (hasConstructor(v, DefaultValueAccessor)) {
|
|
|
|
defaultAccessor = v;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
} else if (
|
|
|
|
hasConstructor(v, CheckboxControlValueAccessor) || hasConstructor(v, NumberValueAccessor) ||
|
|
|
|
hasConstructor(v, SelectControlValueAccessor) ||
|
2016-06-26 18:24:27 -04:00
|
|
|
hasConstructor(v, SelectMultipleControlValueAccessor) ||
|
2016-06-08 19:38:52 -04:00
|
|
|
hasConstructor(v, RadioControlValueAccessor)) {
|
2016-06-08 18:36:24 -04:00
|
|
|
if (isPresent(builtinAccessor))
|
2016-06-08 19:38:52 -04:00
|
|
|
_throwError(dir, 'More than one built-in value accessor matches');
|
2016-06-08 18:36:24 -04:00
|
|
|
builtinAccessor = v;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (isPresent(customAccessor))
|
2016-06-08 19:38:52 -04:00
|
|
|
_throwError(dir, 'More than one custom value accessor matches');
|
2016-06-08 18:36:24 -04:00
|
|
|
customAccessor = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isPresent(customAccessor)) return customAccessor;
|
|
|
|
if (isPresent(builtinAccessor)) return builtinAccessor;
|
|
|
|
if (isPresent(defaultAccessor)) return defaultAccessor;
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
_throwError(dir, 'No valid value accessor for');
|
2016-06-08 18:36:24 -04:00
|
|
|
return null;
|
|
|
|
}
|