2015-11-06 17:34:07 -08:00
|
|
|
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {isBlank, isPresent, looseIdentical} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
2015-05-30 11:56:00 -07:00
|
|
|
|
2015-06-10 13:51:44 -07:00
|
|
|
import {ControlContainer} from './control_container';
|
|
|
|
import {NgControl} from './ng_control';
|
2015-10-27 14:36:13 -07:00
|
|
|
import {AbstractControlDirective} from './abstract_control_directive';
|
|
|
|
import {NgControlGroup} from './ng_control_group';
|
|
|
|
import {Control, ControlGroup} from '../model';
|
2015-05-30 11:56:00 -07:00
|
|
|
import {Validators} from '../validators';
|
2015-09-30 17:52:33 -07:00
|
|
|
import {ControlValueAccessor} from './control_value_accessor';
|
|
|
|
import {DefaultValueAccessor} from './default_value_accessor';
|
2015-10-15 11:38:34 -07:00
|
|
|
import {NumberValueAccessor} from './number_value_accessor';
|
2015-09-30 17:52:33 -07:00
|
|
|
import {CheckboxControlValueAccessor} from './checkbox_value_accessor';
|
|
|
|
import {SelectControlValueAccessor} from './select_control_value_accessor';
|
2015-10-28 16:54:27 -07:00
|
|
|
import {normalizeValidator} from './normalize_validator';
|
2015-06-13 12:15:42 -07:00
|
|
|
|
2015-05-30 11:56:00 -07:00
|
|
|
|
2015-06-26 11:10:52 -07:00
|
|
|
export function controlPath(name: string, parent: ControlContainer): string[] {
|
2015-05-30 11:56:00 -07:00
|
|
|
var p = ListWrapper.clone(parent.path);
|
2015-06-17 11:17:21 -07:00
|
|
|
p.push(name);
|
2015-05-30 11:56:00 -07:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-09-01 20:59:43 -07:00
|
|
|
export function setUpControl(control: Control, dir: NgControl): void {
|
|
|
|
if (isBlank(control)) _throwError(dir, "Cannot find control");
|
2015-05-30 11:56:00 -07:00
|
|
|
if (isBlank(dir.valueAccessor)) _throwError(dir, "No value accessor for");
|
|
|
|
|
2015-09-01 20:59:43 -07:00
|
|
|
control.validator = Validators.compose([control.validator, dir.validator]);
|
2015-11-02 10:00:42 -08:00
|
|
|
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
|
2015-09-01 20:59:43 -07:00
|
|
|
dir.valueAccessor.writeValue(control.value);
|
2015-06-01 10:41:50 -07:00
|
|
|
|
|
|
|
// view -> model
|
2015-05-31 12:24:34 -07:00
|
|
|
dir.valueAccessor.registerOnChange(newValue => {
|
|
|
|
dir.viewToModelUpdate(newValue);
|
2015-09-01 20:59:43 -07:00
|
|
|
control.updateValue(newValue, {emitModelToViewChange: false});
|
|
|
|
control.markAsDirty();
|
2015-05-31 12:24:34 -07:00
|
|
|
});
|
2015-06-01 10:41:50 -07:00
|
|
|
|
|
|
|
// model -> view
|
2015-09-01 20:59:43 -07:00
|
|
|
control.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
|
2015-06-02 08:41:33 -07:00
|
|
|
|
|
|
|
// touched
|
2015-09-01 20:59:43 -07:00
|
|
|
dir.valueAccessor.registerOnTouched(() => control.markAsTouched());
|
2015-05-30 11:56:00 -07:00
|
|
|
}
|
|
|
|
|
2015-10-27 14:36:13 -07:00
|
|
|
export function setUpControlGroup(control: ControlGroup, dir: NgControlGroup) {
|
|
|
|
if (isBlank(control)) _throwError(dir, "Cannot find control");
|
|
|
|
control.validator = Validators.compose([control.validator, dir.validator]);
|
2015-11-02 10:00:42 -08:00
|
|
|
control.asyncValidator = Validators.composeAsync([control.asyncValidator, dir.asyncValidator]);
|
2015-10-27 14:36:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function _throwError(dir: AbstractControlDirective, message: string): void {
|
2015-10-06 17:11:10 -07:00
|
|
|
var path = dir.path.join(" -> ");
|
2015-05-30 11:56:00 -07:00
|
|
|
throw new BaseException(`${message} '${path}'`);
|
2015-06-13 12:15:42 -07:00
|
|
|
}
|
|
|
|
|
2015-10-28 17:00:14 -07:00
|
|
|
export function composeValidators(validators: /* Array<Validator|Function> */ any[]): Function {
|
2015-11-02 10:00:42 -08:00
|
|
|
return isPresent(validators) ? Validators.compose(validators.map(normalizeValidator)) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function composeAsyncValidators(
|
|
|
|
validators: /* Array<Validator|Function> */ any[]): Function {
|
|
|
|
return isPresent(validators) ? Validators.composeAsync(validators.map(normalizeValidator)) : null;
|
2015-10-28 16:54:27 -07:00
|
|
|
}
|
|
|
|
|
2015-10-02 16:47:54 -07:00
|
|
|
export function isPropertyUpdated(changes: {[key: string]: any}, viewModel: any): boolean {
|
2015-07-15 18:16:50 -07:00
|
|
|
if (!StringMapWrapper.contains(changes, "model")) return false;
|
|
|
|
var change = changes["model"];
|
|
|
|
|
|
|
|
if (change.isFirstChange()) return true;
|
|
|
|
return !looseIdentical(viewModel, change.currentValue);
|
2015-09-01 20:59:43 -07:00
|
|
|
}
|
2015-09-30 17:52:33 -07:00
|
|
|
|
|
|
|
// TODO: vsavkin remove it once https://github.com/angular/angular/issues/3011 is implemented
|
2015-10-28 08:59:19 +01:00
|
|
|
export function selectValueAccessor(dir: NgControl,
|
|
|
|
valueAccessors: ControlValueAccessor[]): ControlValueAccessor {
|
2015-09-30 17:52:33 -07:00
|
|
|
if (isBlank(valueAccessors)) return null;
|
|
|
|
|
|
|
|
var defaultAccessor;
|
|
|
|
var builtinAccessor;
|
|
|
|
var customAccessor;
|
|
|
|
valueAccessors.forEach(v => {
|
|
|
|
if (v instanceof DefaultValueAccessor) {
|
|
|
|
defaultAccessor = v;
|
|
|
|
|
2015-10-15 11:38:34 -07:00
|
|
|
} else if (v instanceof CheckboxControlValueAccessor || v instanceof NumberValueAccessor ||
|
2015-09-30 17:52:33 -07:00
|
|
|
v instanceof SelectControlValueAccessor) {
|
|
|
|
if (isPresent(builtinAccessor))
|
|
|
|
_throwError(dir, "More than one built-in value accessor matches");
|
|
|
|
builtinAccessor = v;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (isPresent(customAccessor))
|
|
|
|
_throwError(dir, "More than one custom value accessor matches");
|
|
|
|
customAccessor = v;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isPresent(customAccessor)) return customAccessor;
|
|
|
|
if (isPresent(builtinAccessor)) return builtinAccessor;
|
|
|
|
if (isPresent(defaultAccessor)) return defaultAccessor;
|
|
|
|
|
|
|
|
_throwError(dir, "No valid value accessor for");
|
|
|
|
return null;
|
2015-10-02 07:37:23 -07:00
|
|
|
}
|