2015-04-28 18:17:00 -07:00
|
|
|
import {Decorator, onChange} from 'angular2/src/core/annotations_impl/annotations';
|
|
|
|
import {Ancestor} from 'angular2/src/core/annotations_impl/visibility';
|
2015-04-28 11:20:01 -07:00
|
|
|
import {ElementRef} from 'angular2/src/core/compiler/element_ref';
|
2015-02-25 15:10:27 -08:00
|
|
|
import {Optional} from 'angular2/di';
|
2015-04-21 11:47:53 -07:00
|
|
|
import {Renderer} from 'angular2/src/render/api';
|
2015-04-28 00:23:39 +02:00
|
|
|
import {isPresent, isString} from 'angular2/src/facade/lang';
|
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {ControlGroup} 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-17 13:01:07 -07:00
|
|
|
* The default accessor for writing a value and listening to changes that is used by a {@link Control} directive.
|
2015-04-10 11:15:01 -07:00
|
|
|
*
|
|
|
|
* This is the default strategy that Angular uses when no other accessor is applied.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* <input type="text" [control]="loginControl">
|
|
|
|
* ```
|
|
|
|
*
|
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-04-21 11:47:53 -07:00
|
|
|
},
|
|
|
|
hostProperties: {
|
|
|
|
'value' : '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-04-21 11:47:53 -07:00
|
|
|
value;
|
2015-03-19 13:12:16 -07:00
|
|
|
onChange:Function;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-04-21 11:47:53 -07:00
|
|
|
constructor() {
|
2015-03-19 13:12:16 -07:00
|
|
|
this.onChange = (_) => {};
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
|
2015-03-19 13:12:16 -07:00
|
|
|
writeValue(value) {
|
2015-04-21 11:47:53 -07:00
|
|
|
this.value = value
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 11:15:01 -07:00
|
|
|
* The accessor for writing a value and listening to changes on a checkbox input element.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* <input type="checkbox" [control]="rememberLogin">
|
|
|
|
* ```
|
|
|
|
*
|
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-04-21 11:47:53 -07:00
|
|
|
},
|
|
|
|
hostProperties: {
|
|
|
|
'checked' : '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-04-21 11:47:53 -07:00
|
|
|
_elementRef:ElementRef;
|
|
|
|
_renderer:Renderer;
|
|
|
|
|
|
|
|
checked:boolean;
|
2015-03-19 13:12:16 -07:00
|
|
|
onChange:Function;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-04-21 11:47:53 -07:00
|
|
|
constructor(cd:ControlDirective, elementRef:ElementRef, renderer:Renderer) {
|
2015-03-19 13:12:16 -07:00
|
|
|
this.onChange = (_) => {};
|
2015-04-21 11:47:53 -07:00
|
|
|
this._elementRef = elementRef;
|
|
|
|
this._renderer = renderer;
|
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) {
|
2015-04-28 11:20:01 -07:00
|
|
|
this._renderer.setElementProperty(this._elementRef.parentView.render, this._elementRef.boundElementIndex,
|
2015-04-21 11:47:53 -07:00
|
|
|
'checked', value)
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 11:15:01 -07:00
|
|
|
* Binds a control to a DOM element.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* In this example, we bind the control to an input element. When the value of the input element changes, the value of
|
|
|
|
* the control will reflect that change. Likewise, if the value of the control changes, the input element reflects that
|
|
|
|
* change.
|
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* Here we use {@link FormDirectives}, rather than importing each form directive individually, e.g.
|
2015-04-10 11:15:01 -07:00
|
|
|
* `ControlDirective`, `ControlGroupDirective`. This is just a shorthand for the same end result.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({selector: "login-comp"})
|
|
|
|
* @View({
|
|
|
|
* directives: [FormDirectives],
|
|
|
|
* inline: "<input type='text' [control]='loginControl'>"
|
|
|
|
* })
|
|
|
|
* class LoginComp {
|
|
|
|
* loginControl:Control;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.loginControl = new Control('');
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*
|
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 11:15:01 -07:00
|
|
|
* Binds a control group to a DOM element.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* In this example, we bind the control group to the form element, and we bind the login and password controls to the
|
|
|
|
* login and password elements.
|
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* Here we use {@link FormDirectives}, rather than importing each form directive individually, e.g.
|
2015-04-10 11:15:01 -07:00
|
|
|
* `ControlDirective`, `ControlGroupDirective`. This is just a shorthand for the same end result.
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* @Component({selector: "login-comp"})
|
|
|
|
* @View({
|
|
|
|
* directives: [FormDirectives],
|
|
|
|
* inline: "<form [control-group]='loginForm'>" +
|
|
|
|
* "Login <input type='text' control='login'>" +
|
|
|
|
* "Password <input type='password' control='password'>" +
|
|
|
|
* "<button (click)="onLogin()">Login</button>" +
|
|
|
|
* "</form>"
|
|
|
|
* })
|
|
|
|
* class LoginComp {
|
|
|
|
* loginForm:ControlGroup;
|
|
|
|
*
|
|
|
|
* constructor() {
|
|
|
|
* this.loginForm = new ControlGroup({
|
|
|
|
* login: new Control(""),
|
|
|
|
* password: new Control("")
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* onLogin() {
|
|
|
|
* // this.loginForm.value
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
*
|
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 11:15:01 -07:00
|
|
|
*
|
|
|
|
* A list of all the form directives used as part of a `@View` annotation.
|
|
|
|
*
|
|
|
|
* This is a shorthand for importing them each individually.
|
|
|
|
*
|
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
|
|
|
];
|