feat(forms): remove support for declaring forms in html

This commit is contained in:
vsavkin
2015-02-23 15:26:53 -08:00
parent ded83e589b
commit a73c643322
3 changed files with 53 additions and 156 deletions

View File

@ -5,11 +5,6 @@ import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {ControlGroup, Control} from './model';
import * as validators from './validators';
class ControlGroupDirectiveBase {
addDirective(directive):void {}
findControl(name:string):Control { return null; }
}
@CONST()
export class ControlValueAccessor {
readValue(el){}
@ -60,9 +55,16 @@ function controlValueAccessorFor(controlType:string):ControlValueAccessor {
}
}
export class ControlDirectiveBase {
_groupDecorator:ControlGroupDirectiveBase;
@Decorator({
lifecycle: [onChange],
selector: '[control]',
bind: {
'controlName' : 'control',
'type' : 'type'
}
})
export class ControlDirective {
_groupDecorator:ControlGroupDirective;
_el:NgElement;
controlName:string;
@ -71,12 +73,18 @@ export class ControlDirectiveBase {
validator:Function;
constructor(groupDecorator, el:NgElement) {
constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) {
this._groupDecorator = groupDecorator;
this._el = el;
this.validator = validators.nullValidator;
}
// TODO: vsavkin this should be moved into the constructor once static bindings
// are implemented
onChange(_) {
this._initialize();
}
_initialize() {
this._groupDecorator.addDirective(this);
@ -105,51 +113,15 @@ export class ControlDirectiveBase {
}
}
@Decorator({
lifecycle: [onChange],
selector: '[control-name]',
bind: {
'controlName' : 'control-name',
'type' : 'type'
}
})
export class ControlNameDirective extends ControlDirectiveBase {
constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) {
super(groupDecorator, el);
}
onChange(_) {
this._initialize();
}
}
@Decorator({
lifecycle: [onChange],
selector: '[control]',
bind: {
'controlName' : 'control',
'type' : 'type'
}
})
export class ControlDirective extends ControlDirectiveBase {
constructor(@Ancestor() groupDecorator:NewControlGroupDirective, el:NgElement) {
super(groupDecorator, el);
}
onChange(_) {
this._initialize();
}
}
@Decorator({
selector: '[control-group]',
bind: {
'controlGroup' : 'control-group'
}
})
export class ControlGroupDirective extends ControlGroupDirectiveBase {
export class ControlGroupDirective {
_controlGroup:ControlGroup;
_directives:List<ControlNameDirective>;
_directives:List<ControlDirective>;
constructor() {
super();
@ -161,71 +133,15 @@ export class ControlGroupDirective extends ControlGroupDirectiveBase {
ListWrapper.forEach(this._directives, (cd) => cd._updateDomValue());
}
addDirective(c:ControlNameDirective) {
ListWrapper.push(this._directives, c);
}
findControl(name:string):Control {
return this._controlGroup.controls[name];
}
}
@Component({
selector: '[new-control-group]',
bind: {
'initData' : 'new-control-group'
}
})
@Template({inline: '<content>'})
export class NewControlGroupDirective extends ControlGroupDirectiveBase {
_initData:any;
_controlGroup:ControlGroup;
_directives:List<ControlNameDirective>;
constructor() {
super();
this._directives = ListWrapper.create();
}
set initData(value) {
this._initData = value;
}
addDirective(c:ControlDirective) {
ListWrapper.push(this._directives, c);
this._controlGroup = null;
}
findControl(name:string):Control {
if (isBlank(this._controlGroup)) {
this._controlGroup = this._createControlGroup();
}
return this._controlGroup.controls[name];
}
_createControlGroup():ControlGroup {
var controls = ListWrapper.reduce(this._directives, (memo, cd) => {
var initControlValue = this._initData[cd.controlName];
memo[cd.controlName] = new Control(initControlValue);
return memo;
}, {});
return new ControlGroup(controls);
}
get value() {
return this._controlGroup.value;
}
get errors() {
return this._controlGroup.errors;
}
get valid() {
return this._controlGroup.valid;
}
}
export var FormDirectives = [
ControlGroupDirective, ControlNameDirective,
ControlDirective, NewControlGroupDirective
ControlGroupDirective, ControlDirective
];