2015-05-30 11:56:00 -07:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
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-05-30 11:56:00 -07:00
|
|
|
import {Control} from '../model';
|
|
|
|
import {Validators} from '../validators';
|
|
|
|
|
2015-06-10 13:51:44 -07:00
|
|
|
export function controlPath(name, parent: ControlContainer) {
|
2015-05-30 11:56:00 -07:00
|
|
|
var p = ListWrapper.clone(parent.path);
|
|
|
|
ListWrapper.push(p, name);
|
|
|
|
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-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}'`);
|
|
|
|
}
|