2015-02-04 11:45:33 -08:00
|
|
|
import {TemplateConfig, Component, Decorator, NgElement, Ancestor} from 'angular2/core';
|
2015-02-05 14:09:26 -08:00
|
|
|
import {DOM} from 'angular2/src/facade/dom';
|
2015-02-04 11:45:33 -08:00
|
|
|
import {isBlank, isPresent} from 'angular2/src/facade/lang';
|
2015-02-05 14:09:26 -08:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2015-02-03 07:27:09 -08:00
|
|
|
import {ControlGroup, Control} from './model';
|
|
|
|
|
2015-02-06 09:16:55 -08:00
|
|
|
class ControlGroupDirectiveBase {
|
|
|
|
addDirective(directive):void {}
|
|
|
|
findControl(name:string):Control { return null; }
|
|
|
|
}
|
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
export class ControlDirectiveBase {
|
|
|
|
_groupDecorator:ControlGroupDirectiveBase;
|
2015-02-03 07:27:09 -08:00
|
|
|
_el:NgElement;
|
2015-02-04 11:45:33 -08:00
|
|
|
_controlName:string;
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
constructor(groupDecorator, el:NgElement) {
|
2015-02-03 07:27:09 -08:00
|
|
|
this._groupDecorator = groupDecorator;
|
|
|
|
this._el = el;
|
|
|
|
DOM.on(el.domElement, "change", (_) => this._updateControl());
|
|
|
|
}
|
|
|
|
|
|
|
|
set controlName(name:string) {
|
|
|
|
this._controlName = name;
|
2015-02-04 11:45:33 -08:00
|
|
|
this._groupDecorator.addDirective(this);
|
2015-02-03 07:27:09 -08:00
|
|
|
this._updateDOM();
|
|
|
|
}
|
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
get controlName() {
|
|
|
|
return this._controlName;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO:vsavkin: Remove it once change detection lifecycle callbacks are available
|
|
|
|
isInitialized():boolean {
|
|
|
|
return isPresent(this._controlName);
|
|
|
|
}
|
|
|
|
|
2015-02-03 07:27:09 -08:00
|
|
|
_updateDOM() {
|
2015-02-04 11:45:33 -08:00
|
|
|
// remove it once all DOM write go through a queue
|
|
|
|
if (this.isInitialized()) {
|
|
|
|
var inputElement:any = this._el.domElement;
|
|
|
|
inputElement.value = this._control().value;
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateControl() {
|
2015-02-04 11:45:33 -08:00
|
|
|
var inputElement:any = this._el.domElement;
|
|
|
|
this._control().value = inputElement.value;
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_control() {
|
|
|
|
return this._groupDecorator.findControl(this._controlName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
|
|
|
|
@Decorator({
|
|
|
|
selector: '[control-name]',
|
|
|
|
bind: {
|
|
|
|
'control-name' : 'controlName'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export class ControlNameDirective extends ControlDirectiveBase {
|
|
|
|
constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) {
|
2015-02-06 09:16:55 -08:00
|
|
|
super(groupDecorator, el);
|
|
|
|
}
|
2015-02-04 11:45:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Decorator({
|
|
|
|
selector: '[control]',
|
|
|
|
bind: {
|
|
|
|
'control' : 'controlName'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
export class ControlDirective extends ControlDirectiveBase {
|
|
|
|
constructor(@Ancestor() groupDecorator:NewControlGroupDirective, el:NgElement) {
|
2015-02-06 09:16:55 -08:00
|
|
|
super(groupDecorator, el);
|
|
|
|
}
|
2015-02-04 11:45:33 -08:00
|
|
|
}
|
|
|
|
|
2015-02-03 07:27:09 -08:00
|
|
|
@Decorator({
|
|
|
|
selector: '[control-group]',
|
|
|
|
bind: {
|
|
|
|
'control-group' : 'controlGroup'
|
|
|
|
}
|
|
|
|
})
|
2015-02-04 11:45:33 -08:00
|
|
|
export class ControlGroupDirective extends ControlGroupDirectiveBase {
|
2015-02-03 07:27:09 -08:00
|
|
|
_controlGroup:ControlGroup;
|
2015-02-04 11:45:33 -08:00
|
|
|
_directives:List<ControlNameDirective>;
|
2015-02-03 07:27:09 -08:00
|
|
|
|
|
|
|
constructor() {
|
2015-02-06 13:38:52 -08:00
|
|
|
super();
|
2015-02-04 11:45:33 -08:00
|
|
|
this._directives = ListWrapper.create();
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
set controlGroup(controlGroup:ControlGroup) {
|
|
|
|
this._controlGroup = controlGroup;
|
2015-02-04 11:45:33 -08:00
|
|
|
ListWrapper.forEach(this._directives, (cd) => cd._updateDOM());
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-02-04 11:45:33 -08:00
|
|
|
addDirective(c:ControlNameDirective) {
|
|
|
|
ListWrapper.push(this._directives, c);
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
findControl(name:string):Control {
|
|
|
|
return this._controlGroup.controls[name];
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 11:45:33 -08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: '[new-control-group]',
|
|
|
|
bind: {
|
|
|
|
'new-control-group' : 'initData'
|
|
|
|
},
|
|
|
|
template: new TemplateConfig({
|
|
|
|
inline: '<content>'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
export class NewControlGroupDirective extends ControlGroupDirectiveBase {
|
|
|
|
_initData:any;
|
|
|
|
_controlGroup:ControlGroup;
|
|
|
|
_directives:List<ControlNameDirective>;
|
|
|
|
|
|
|
|
constructor() {
|
2015-02-06 13:38:52 -08:00
|
|
|
super();
|
2015-02-04 11:45:33 -08:00
|
|
|
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) => {
|
|
|
|
if (cd.isInitialized()) {
|
|
|
|
var initControlValue = this._initData[cd.controlName];
|
|
|
|
memo[cd.controlName] = new Control(initControlValue);
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, {});
|
|
|
|
return new ControlGroup(controls);
|
|
|
|
}
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this._controlGroup.value;
|
|
|
|
}
|
|
|
|
}
|