chore(merge): forms, compiler
This commit is contained in:
@ -7,9 +7,9 @@ import {Parser} from 'angular2/change_detection';
|
||||
import {CompileStep} from './compile_step';
|
||||
import {CompileElement} from './compile_element';
|
||||
import {CompileControl} from './compile_control';
|
||||
import {StringWrapper} from 'facade/src/lang';
|
||||
import {StringWrapper} from 'angular2/src/facade/lang';
|
||||
|
||||
import {$BANG} from 'change_detection/src/parser/lexer';
|
||||
import {$BANG} from 'angular2/src/change_detection/parser/lexer';
|
||||
|
||||
/**
|
||||
* Splits views at `<template>` elements or elements with `template` attribute:
|
||||
|
73
modules/angular2/src/forms/decorators.js
vendored
Normal file
73
modules/angular2/src/forms/decorators.js
vendored
Normal file
@ -0,0 +1,73 @@
|
||||
import {Decorator, NgElement, Ancestor} from 'angular2/core';
|
||||
import {DOM} from 'angular2/src/facade/dom';
|
||||
import {isPresent} from 'angular2/src/facade/lang';
|
||||
import {ListWrapper} from 'angular2/src/facade/collection';
|
||||
import {ControlGroup, Control} from './model';
|
||||
|
||||
@Decorator({
|
||||
selector: '[control-name]',
|
||||
bind: {
|
||||
'control-name' : 'controlName'
|
||||
}
|
||||
})
|
||||
export class ControlDecorator {
|
||||
_groupDecorator:ControlGroupDecorator;
|
||||
_el:NgElement;
|
||||
_controlName:String;
|
||||
|
||||
constructor(@Ancestor() groupDecorator:ControlGroupDecorator, el:NgElement) {
|
||||
this._groupDecorator = groupDecorator;
|
||||
groupDecorator.addControlDecorator(this);
|
||||
|
||||
this._el = el;
|
||||
DOM.on(el.domElement, "change", (_) => this._updateControl());
|
||||
}
|
||||
|
||||
set controlName(name:string) {
|
||||
this._controlName = name;
|
||||
this._updateDOM();
|
||||
}
|
||||
|
||||
_updateDOM() {
|
||||
// remove it once all DOM write go throuh a queue
|
||||
if (isPresent(this._controlName)) {
|
||||
this._el.domElement.value = this._control().value;
|
||||
}
|
||||
}
|
||||
|
||||
_updateControl() {
|
||||
this._control().value = this._el.domElement.value;
|
||||
}
|
||||
|
||||
_control() {
|
||||
return this._groupDecorator.findControl(this._controlName);
|
||||
}
|
||||
}
|
||||
|
||||
@Decorator({
|
||||
selector: '[control-group]',
|
||||
bind: {
|
||||
'control-group' : 'controlGroup'
|
||||
}
|
||||
})
|
||||
export class ControlGroupDecorator {
|
||||
_controlGroup:ControlGroup;
|
||||
_controlDecorators:List<ControlDecorator>;
|
||||
|
||||
constructor() {
|
||||
this._controlDecorators = ListWrapper.create();
|
||||
}
|
||||
|
||||
set controlGroup(controlGroup:ControlGroup) {
|
||||
this._controlGroup = controlGroup;
|
||||
ListWrapper.forEach(this._controlDecorators, (cd) => cd._updateDOM());
|
||||
}
|
||||
|
||||
addControlDecorator(c:ControlDecorator) {
|
||||
ListWrapper.push(this._controlDecorators, c);
|
||||
}
|
||||
|
||||
findControl(name:string):Control {
|
||||
return this._controlGroup.controls[name];
|
||||
}
|
||||
}
|
25
modules/angular2/src/forms/model.js
vendored
Normal file
25
modules/angular2/src/forms/model.js
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
import {StringMapWrapper} from 'angular2/src/facade/collection';
|
||||
|
||||
export class Control {
|
||||
value:any;
|
||||
|
||||
constructor(value:any) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
export class ControlGroup {
|
||||
controls;
|
||||
|
||||
constructor(controls) {
|
||||
this.controls = controls;
|
||||
}
|
||||
|
||||
get value() {
|
||||
var res = {};
|
||||
StringMapWrapper.forEach(this.controls, (control, name) => {
|
||||
res[name] = control.value;
|
||||
});
|
||||
return res;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user