2015-06-17 14:45:40 -07:00
|
|
|
import {ListWrapper, iterableToList} from 'angular2/src/facade/collection';
|
2015-05-30 11:56:00 -07:00
|
|
|
import {isBlank, BaseException} from 'angular2/src/facade/lang';
|
|
|
|
|
2015-06-10 13:51:44 -07:00
|
|
|
import {ControlContainer} from './control_container';
|
|
|
|
import {NgControl} from './ng_control';
|
2015-06-17 14:45:40 -07:00
|
|
|
import {NgValidator} from './validators';
|
2015-05-30 11:56:00 -07:00
|
|
|
import {Control} from '../model';
|
|
|
|
import {Validators} from '../validators';
|
2015-07-02 14:20:50 -07:00
|
|
|
import {Renderer} from 'angular2/render';
|
|
|
|
import {ElementRef, QueryList} from 'angular2/core';
|
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-06-10 13:51:44 -07:00
|
|
|
export function setUpControl(c: Control, dir: NgControl) {
|
2015-05-30 11:56:00 -07:00
|
|
|
if (isBlank(c)) _throwError(dir, "Cannot find control");
|
|
|
|
if (isBlank(dir.valueAccessor)) _throwError(dir, "No value accessor for");
|
|
|
|
|
|
|
|
c.validator = Validators.compose([c.validator, dir.validator]);
|
|
|
|
dir.valueAccessor.writeValue(c.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);
|
|
|
|
c.updateValue(newValue);
|
2015-06-03 11:54:39 -07:00
|
|
|
c.markAsDirty();
|
2015-05-31 12:24:34 -07:00
|
|
|
});
|
2015-06-01 10:41:50 -07:00
|
|
|
|
|
|
|
// model -> view
|
|
|
|
c.registerOnChange(newValue => dir.valueAccessor.writeValue(newValue));
|
2015-06-02 08:41:33 -07:00
|
|
|
|
|
|
|
// touched
|
2015-06-03 11:54:39 -07:00
|
|
|
dir.valueAccessor.registerOnTouched(() => c.markAsTouched());
|
2015-05-30 11:56:00 -07:00
|
|
|
}
|
|
|
|
|
2015-06-17 14:45:40 -07:00
|
|
|
export function composeNgValidator(ngValidators: QueryList<NgValidator>): Function {
|
|
|
|
if (isBlank(ngValidators)) return Validators.nullValidator;
|
2015-06-18 18:11:20 -07:00
|
|
|
return Validators.compose(
|
|
|
|
(<List<NgValidator>>iterableToList(ngValidators)).map(v => v.validator));
|
2015-06-17 14:45:40 -07:00
|
|
|
}
|
|
|
|
|
2015-06-10 13:51:44 -07:00
|
|
|
function _throwError(dir: NgControl, message: string): void {
|
2015-05-30 11:56:00 -07:00
|
|
|
var path = ListWrapper.join(dir.path, " -> ");
|
|
|
|
throw new BaseException(`${message} '${path}'`);
|
2015-06-13 12:15:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setProperty(renderer: Renderer, elementRef: ElementRef, propName: string,
|
|
|
|
propValue: any) {
|
2015-06-23 11:21:56 -07:00
|
|
|
renderer.setElementProperty(elementRef, propName, propValue);
|
|
|
|
}
|