2015-04-09 21:20:11 +02:00
|
|
|
import {View, Component, Decorator, Ancestor, onChange, PropertySetter} from 'angular2/angular2';
|
2015-02-25 15:10:27 -08:00
|
|
|
import {Optional} from 'angular2/di';
|
|
|
|
import {isBlank, isPresent, isString, CONST} from 'angular2/src/facade/lang';
|
2015-02-07 14:14:07 -08:00
|
|
|
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
2015-02-03 07:27:09 -08:00
|
|
|
import {ControlGroup, Control} from './model';
|
2015-03-19 14:21:40 -07:00
|
|
|
import {Validators} from './validators';
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-03-19 14:01:11 -07:00
|
|
|
//export interface ControlValueAccessor {
|
|
|
|
// writeValue(value):void{}
|
|
|
|
// set onChange(fn){}
|
|
|
|
//}
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-03-19 13:12:16 -07:00
|
|
|
@Decorator({
|
|
|
|
selector: '[control]',
|
2015-04-09 21:20:11 +02:00
|
|
|
hostListeners: {
|
2015-03-19 14:01:11 -07:00
|
|
|
'change' : 'onChange($event.target.value)',
|
|
|
|
'input' : 'onChange($event.target.value)'
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
2015-03-19 13:12:16 -07:00
|
|
|
})
|
2015-03-19 14:01:11 -07:00
|
|
|
export class DefaultValueAccessor {
|
2015-03-19 13:12:16 -07:00
|
|
|
_setValueProperty:Function;
|
|
|
|
onChange:Function;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-03-19 13:12:16 -07:00
|
|
|
constructor(@PropertySetter('value') setValueProperty:Function) {
|
|
|
|
this._setValueProperty = setValueProperty;
|
|
|
|
this.onChange = (_) => {};
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
|
2015-03-19 13:12:16 -07:00
|
|
|
writeValue(value) {
|
|
|
|
this._setValueProperty(value);
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-03-19 13:12:16 -07:00
|
|
|
@Decorator({
|
2015-03-30 20:52:37 -07:00
|
|
|
selector: 'input[type=checkbox][control]',
|
2015-04-09 21:20:11 +02:00
|
|
|
hostListeners: {
|
2015-03-19 13:12:16 -07:00
|
|
|
'change' : 'onChange($event.target.checked)'
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
2015-03-19 13:12:16 -07:00
|
|
|
})
|
2015-03-19 14:01:11 -07:00
|
|
|
export class CheckboxControlValueAccessor {
|
2015-03-19 13:12:16 -07:00
|
|
|
_setCheckedProperty:Function;
|
|
|
|
onChange:Function;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-03-19 13:12:16 -07:00
|
|
|
constructor(cd:ControlDirective, @PropertySetter('checked') setCheckedProperty:Function) {
|
|
|
|
this._setCheckedProperty = setCheckedProperty;
|
|
|
|
this.onChange = (_) => {};
|
2015-03-19 14:01:11 -07:00
|
|
|
cd.valueAccessor = this; //ControlDirective should inject CheckboxControlDirective
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
|
2015-03-19 13:12:16 -07:00
|
|
|
writeValue(value) {
|
|
|
|
this._setCheckedProperty(value);
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-02-23 15:26:53 -08:00
|
|
|
@Decorator({
|
|
|
|
lifecycle: [onChange],
|
|
|
|
selector: '[control]',
|
2015-04-09 21:20:11 +02:00
|
|
|
properties: {
|
2015-03-25 14:21:57 -07:00
|
|
|
'controlOrName' : 'control'
|
2015-02-23 15:26:53 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
export class ControlDirective {
|
2015-02-25 15:10:27 -08:00
|
|
|
_groupDirective:ControlGroupDirective;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-03-25 14:21:57 -07:00
|
|
|
controlOrName:any;
|
2015-03-19 14:01:11 -07:00
|
|
|
valueAccessor:any; //ControlValueAccessor
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-02-11 11:10:31 -08:00
|
|
|
validator:Function;
|
|
|
|
|
2015-03-25 14:21:57 -07:00
|
|
|
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
|
2015-02-25 15:10:27 -08:00
|
|
|
this._groupDirective = groupDirective;
|
2015-03-25 14:21:57 -07:00
|
|
|
this.controlOrName = null;
|
2015-03-19 13:12:16 -07:00
|
|
|
this.valueAccessor = valueAccessor;
|
2015-03-19 14:21:40 -07:00
|
|
|
this.validator = Validators.nullValidator;
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-02-23 15:26:53 -08:00
|
|
|
// TODO: vsavkin this should be moved into the constructor once static bindings
|
|
|
|
// are implemented
|
|
|
|
onChange(_) {
|
|
|
|
this._initialize();
|
|
|
|
}
|
|
|
|
|
2015-02-07 14:14:07 -08:00
|
|
|
_initialize() {
|
2015-03-25 14:21:57 -07:00
|
|
|
if(isPresent(this._groupDirective)) {
|
|
|
|
this._groupDirective.addDirective(this);
|
|
|
|
}
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-02-24 11:59:10 -08:00
|
|
|
var c = this._control();
|
2015-03-19 14:21:40 -07:00
|
|
|
c.validator = Validators.compose([c.validator, this.validator]);
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-02-07 14:14:07 -08:00
|
|
|
this._updateDomValue();
|
2015-03-19 13:12:16 -07:00
|
|
|
this._setUpUpdateControlValue();
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-02-07 14:14:07 -08:00
|
|
|
_updateDomValue() {
|
2015-03-19 13:12:16 -07:00
|
|
|
this.valueAccessor.writeValue(this._control().value);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setUpUpdateControlValue() {
|
|
|
|
this.valueAccessor.onChange = (newValue) => this._control().updateValue(newValue);
|
2015-02-04 11:45:33 -08:00
|
|
|
}
|
|
|
|
|
2015-02-03 07:27:09 -08:00
|
|
|
_control() {
|
2015-03-25 14:21:57 -07:00
|
|
|
if (isString(this.controlOrName)) {
|
|
|
|
return this._groupDirective.findControl(this.controlOrName);
|
|
|
|
} else {
|
|
|
|
return this.controlOrName;
|
|
|
|
}
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-02-03 07:27:09 -08:00
|
|
|
@Decorator({
|
|
|
|
selector: '[control-group]',
|
2015-04-09 21:20:11 +02:00
|
|
|
properties: {
|
2015-02-17 21:55:18 +01:00
|
|
|
'controlGroup' : 'control-group'
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
})
|
2015-02-23 15:26:53 -08:00
|
|
|
export class ControlGroupDirective {
|
2015-02-25 15:10:27 -08:00
|
|
|
_groupDirective:ControlGroupDirective;
|
|
|
|
_controlGroupName:string;
|
|
|
|
|
2015-02-03 07:27:09 -08:00
|
|
|
_controlGroup:ControlGroup;
|
2015-02-23 15:26:53 -08:00
|
|
|
_directives:List<ControlDirective>;
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-02-25 15:10:27 -08:00
|
|
|
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective) {
|
|
|
|
this._groupDirective = groupDirective;
|
2015-02-04 11:45:33 -08:00
|
|
|
this._directives = ListWrapper.create();
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-02-25 15:10:27 -08:00
|
|
|
set controlGroup(controlGroup) {
|
|
|
|
if (isString(controlGroup)) {
|
|
|
|
this._controlGroupName = controlGroup;
|
|
|
|
} else {
|
|
|
|
this._controlGroup = controlGroup;
|
|
|
|
}
|
|
|
|
this._updateDomValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateDomValue() {
|
2015-02-07 14:14:07 -08:00
|
|
|
ListWrapper.forEach(this._directives, (cd) => cd._updateDomValue());
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
addDirective(c:ControlDirective) {
|
|
|
|
ListWrapper.push(this._directives, c);
|
|
|
|
}
|
|
|
|
|
2015-02-25 15:10:27 -08:00
|
|
|
findControl(name:string):any {
|
|
|
|
return this._getControlGroup().controls[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
_getControlGroup():ControlGroup {
|
|
|
|
if (isPresent(this._controlGroupName)) {
|
|
|
|
return this._groupDirective.findControl(this._controlGroupName)
|
|
|
|
} else {
|
|
|
|
return this._controlGroup;
|
|
|
|
}
|
2015-02-04 11:45:33 -08:00
|
|
|
}
|
|
|
|
}
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
|
|
|
// todo(misko): rename to lover case as it is not a Type but a var.
|
2015-02-07 14:14:07 -08:00
|
|
|
export var FormDirectives = [
|
2015-03-19 14:01:11 -07:00
|
|
|
ControlGroupDirective, ControlDirective, CheckboxControlValueAccessor, DefaultValueAccessor
|
2015-02-07 14:14:07 -08:00
|
|
|
];
|