|
1 | | -import {Decorator, NgElement, Ancestor} from 'angular2/core'; |
| 1 | +import {TemplateConfig, Component, Decorator, NgElement, Ancestor} from 'angular2/core'; |
2 | 2 | import {DOM} from 'angular2/src/facade/dom'; |
3 | | -import {isPresent} from 'angular2/src/facade/lang'; |
| 3 | +import {isBlank, isPresent} from 'angular2/src/facade/lang'; |
4 | 4 | import {ListWrapper} from 'angular2/src/facade/collection'; |
5 | 5 | import {ControlGroup, Control} from './model'; |
6 | 6 |
|
7 | | -@Decorator({ |
8 | | - selector: '[control-name]', |
9 | | - bind: { |
10 | | - 'control-name' : 'controlName' |
11 | | - } |
12 | | -}) |
13 | | -export class ControlDecorator { |
14 | | - _groupDecorator:ControlGroupDecorator; |
| 7 | +export class ControlDirectiveBase { |
| 8 | + _groupDecorator:ControlGroupDirectiveBase; |
15 | 9 | _el:NgElement; |
16 | | - _controlName:String; |
| 10 | + _controlName:string; |
17 | 11 |
|
18 | | - constructor(@Ancestor() groupDecorator:ControlGroupDecorator, el:NgElement) { |
| 12 | + constructor(groupDecorator, el:NgElement) { |
19 | 13 | this._groupDecorator = groupDecorator; |
20 | | - groupDecorator.addControlDecorator(this); |
21 | | - |
22 | 14 | this._el = el; |
23 | 15 | DOM.on(el.domElement, "change", (_) => this._updateControl()); |
24 | 16 | } |
25 | 17 |
|
26 | 18 | set controlName(name:string) { |
27 | 19 | this._controlName = name; |
| 20 | + this._groupDecorator.addDirective(this); |
28 | 21 | this._updateDOM(); |
29 | 22 | } |
30 | 23 |
|
| 24 | + get controlName() { |
| 25 | + return this._controlName; |
| 26 | + } |
| 27 | + |
| 28 | + //TODO:vsavkin: Remove it once change detection lifecycle callbacks are available |
| 29 | + isInitialized():boolean { |
| 30 | + return isPresent(this._controlName); |
| 31 | + } |
| 32 | + |
31 | 33 | _updateDOM() { |
32 | | - // remove it once all DOM write go throuh a queue |
33 | | - if (isPresent(this._controlName)) { |
34 | | - var inputElem: any = this._el.domElement; |
35 | | - inputElem.value = this._control().value; |
| 34 | + // remove it once all DOM write go through a queue |
| 35 | + if (this.isInitialized()) { |
| 36 | + var inputElement:any = this._el.domElement; |
| 37 | + inputElement.value = this._control().value; |
36 | 38 | } |
37 | 39 | } |
38 | 40 |
|
39 | 41 | _updateControl() { |
40 | | - var inputElem: any = this._el.domElement; |
41 | | - this._control().value = inputElem.value; |
| 42 | + var inputElement:any = this._el.domElement; |
| 43 | + this._control().value = inputElement.value; |
42 | 44 | } |
43 | 45 |
|
44 | 46 | _control() { |
45 | 47 | return this._groupDecorator.findControl(this._controlName); |
46 | 48 | } |
47 | 49 | } |
48 | 50 |
|
| 51 | +class ControlGroupDirectiveBase { |
| 52 | + addDirective(c:ControlNameDirective):void {} |
| 53 | + findControl(name:string):Control {} |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +@Decorator({ |
| 58 | + selector: '[control-name]', |
| 59 | + bind: { |
| 60 | + 'control-name' : 'controlName' |
| 61 | + } |
| 62 | +}) |
| 63 | +export class ControlNameDirective extends ControlDirectiveBase { |
| 64 | + _groupDecorator:ControlGroupDirective; |
| 65 | + _el:NgElement; |
| 66 | + _controlName:String; |
| 67 | + |
| 68 | + constructor(@Ancestor() groupDecorator:ControlGroupDirective, el:NgElement) { |
| 69 | + super(groupDecorator, el); |
| 70 | +} |
| 71 | +} |
| 72 | + |
| 73 | +@Decorator({ |
| 74 | + selector: '[control]', |
| 75 | + bind: { |
| 76 | + 'control' : 'controlName' |
| 77 | + } |
| 78 | +}) |
| 79 | +export class ControlDirective extends ControlDirectiveBase { |
| 80 | + _groupDecorator:ControlGroupDirective; |
| 81 | + _el:NgElement; |
| 82 | + _controlName:String; |
| 83 | + |
| 84 | + constructor(@Ancestor() groupDecorator:NewControlGroupDirective, el:NgElement) { |
| 85 | + super(groupDecorator, el); |
| 86 | +} |
| 87 | +} |
| 88 | + |
49 | 89 | @Decorator({ |
50 | 90 | selector: '[control-group]', |
51 | 91 | bind: { |
52 | 92 | 'control-group' : 'controlGroup' |
53 | 93 | } |
54 | 94 | }) |
55 | | -export class ControlGroupDecorator { |
| 95 | +export class ControlGroupDirective extends ControlGroupDirectiveBase { |
56 | 96 | _controlGroup:ControlGroup; |
57 | | - _controlDecorators:List<ControlDecorator>; |
| 97 | + _directives:List<ControlNameDirective>; |
58 | 98 |
|
59 | 99 | constructor() { |
60 | | - this._controlDecorators = ListWrapper.create(); |
| 100 | + this._directives = ListWrapper.create(); |
61 | 101 | } |
62 | 102 |
|
63 | 103 | set controlGroup(controlGroup:ControlGroup) { |
64 | 104 | this._controlGroup = controlGroup; |
65 | | - ListWrapper.forEach(this._controlDecorators, (cd) => cd._updateDOM()); |
| 105 | + ListWrapper.forEach(this._directives, (cd) => cd._updateDOM()); |
66 | 106 | } |
67 | 107 |
|
68 | | - addControlDecorator(c:ControlDecorator) { |
69 | | - ListWrapper.push(this._controlDecorators, c); |
| 108 | + addDirective(c:ControlNameDirective) { |
| 109 | + ListWrapper.push(this._directives, c); |
70 | 110 | } |
71 | 111 |
|
72 | 112 | findControl(name:string):Control { |
73 | 113 | return this._controlGroup.controls[name]; |
74 | 114 | } |
75 | 115 | } |
| 116 | + |
| 117 | +@Component({ |
| 118 | + selector: '[new-control-group]', |
| 119 | + bind: { |
| 120 | + 'new-control-group' : 'initData' |
| 121 | + }, |
| 122 | + template: new TemplateConfig({ |
| 123 | + inline: '<content>' |
| 124 | + }) |
| 125 | +}) |
| 126 | +export class NewControlGroupDirective extends ControlGroupDirectiveBase { |
| 127 | + _initData:any; |
| 128 | + _controlGroup:ControlGroup; |
| 129 | + _directives:List<ControlNameDirective>; |
| 130 | + |
| 131 | + constructor() { |
| 132 | + this._directives = ListWrapper.create(); |
| 133 | + } |
| 134 | + |
| 135 | + set initData(value) { |
| 136 | + this._initData = value; |
| 137 | + } |
| 138 | + |
| 139 | + addDirective(c:ControlDirective) { |
| 140 | + ListWrapper.push(this._directives, c); |
| 141 | + this._controlGroup = null; |
| 142 | + } |
| 143 | + |
| 144 | + findControl(name:string):Control { |
| 145 | + if (isBlank(this._controlGroup)) { |
| 146 | + this._controlGroup = this._createControlGroup(); |
| 147 | + } |
| 148 | + return this._controlGroup.controls[name]; |
| 149 | + } |
| 150 | + |
| 151 | + _createControlGroup():ControlGroup { |
| 152 | + var controls = ListWrapper.reduce(this._directives, (memo, cd) => { |
| 153 | + if (cd.isInitialized()) { |
| 154 | + var initControlValue = this._initData[cd.controlName]; |
| 155 | + memo[cd.controlName] = new Control(initControlValue); |
| 156 | + } |
| 157 | + return memo; |
| 158 | + }, {}); |
| 159 | + return new ControlGroup(controls); |
| 160 | + } |
| 161 | + |
| 162 | + get value() { |
| 163 | + return this._controlGroup.value; |
| 164 | + } |
| 165 | +} |
0 commit comments