2015-05-20 18:10:30 -07:00
|
|
|
import {Directive, Ancestor} from 'angular2/src/core/annotations/decorators';
|
|
|
|
import {Optional} from 'angular2/src/di/decorators';
|
2015-04-28 11:20:01 -07:00
|
|
|
import {ElementRef} from 'angular2/src/core/compiler/element_ref';
|
2015-04-21 11:47:53 -07:00
|
|
|
import {Renderer} from 'angular2/src/render/api';
|
2015-05-20 18:10:30 -07:00
|
|
|
import {
|
|
|
|
isPresent,
|
|
|
|
isString,
|
|
|
|
CONST_EXPR,
|
|
|
|
isBlank,
|
|
|
|
BaseException,
|
|
|
|
Type
|
|
|
|
} from 'angular2/src/facade/lang';
|
2015-04-28 00:23:39 +02:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2015-05-11 12:00:56 -07:00
|
|
|
import {ControlGroup, Control, isControl} from './model';
|
2015-03-19 14:21:40 -07:00
|
|
|
import {Validators} from './validators';
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
function _lookupControl(groupDirective: ControlGroupDirective, controlOrName: any): any {
|
2015-05-11 12:00:56 -07:00
|
|
|
if (isControl(controlOrName)) {
|
|
|
|
return controlOrName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBlank(groupDirective)) {
|
|
|
|
throw new BaseException(`No control group found for "${controlOrName}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
var control = groupDirective.findControl(controlOrName);
|
|
|
|
|
|
|
|
if (isBlank(control)) {
|
|
|
|
throw new BaseException(`Cannot find control "${controlOrName}"`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return control;
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-05-20 18:10:30 -07:00
|
|
|
* Binds a control group to a DOM element.
|
2015-04-10 11:15:01 -07:00
|
|
|
*
|
2015-05-20 18:10:30 -07:00
|
|
|
* # 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.
|
|
|
|
*
|
|
|
|
* Here we use {@link formDirectives}, rather than importing each form directive individually, e.g.
|
|
|
|
* `ControlDirective`, `ControlGroupDirective`. This is just a shorthand for the same end result.
|
2015-04-10 11:15:01 -07:00
|
|
|
*
|
|
|
|
* ```
|
2015-05-20 18:10:30 -07:00
|
|
|
* @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 11:15:01 -07:00
|
|
|
* ```
|
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-05-26 15:54:10 +02:00
|
|
|
@Directive({selector: '[control-group]', properties: ['controlOrName: control-group']})
|
2015-05-20 18:10:30 -07:00
|
|
|
export class ControlGroupDirective {
|
|
|
|
_groupDirective: ControlGroupDirective;
|
|
|
|
_directives: List<ControlDirective>;
|
|
|
|
_controlOrName: any;
|
2015-04-21 11:47:53 -07:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
constructor(@Optional() @Ancestor() groupDirective: ControlGroupDirective) {
|
|
|
|
this._groupDirective = groupDirective;
|
|
|
|
this._directives = ListWrapper.create();
|
|
|
|
}
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
set controlOrName(controlOrName) {
|
|
|
|
this._controlOrName = controlOrName;
|
|
|
|
this._updateDomValue();
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
_updateDomValue() { ListWrapper.forEach(this._directives, (cd) => cd._updateDomValue()); }
|
|
|
|
|
|
|
|
addDirective(c: ControlDirective) { ListWrapper.push(this._directives, c); }
|
|
|
|
|
|
|
|
findControl(name: string): any { return this._getControlGroup().controls[name]; }
|
|
|
|
|
|
|
|
_getControlGroup(): ControlGroup {
|
|
|
|
return _lookupControl(this._groupDirective, this._controlOrName);
|
2015-02-07 14:14:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07: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
|
|
|
|
*
|
2015-05-20 18:10:30 -07:00
|
|
|
* 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
|
2015-04-10 11:15:01 -07:00
|
|
|
* change.
|
|
|
|
*
|
2015-05-11 15:20:04 +02: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({
|
2015-05-11 15:20:04 +02:00
|
|
|
* directives: [formDirectives],
|
2015-04-10 11:15:01 -07:00
|
|
|
* 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-05-26 15:54:10 +02:00
|
|
|
@Directive({selector: '[control]', properties: ['controlOrName: control']})
|
2015-02-23 15:26:53 -08:00
|
|
|
export class ControlDirective {
|
2015-05-20 18:10:30 -07:00
|
|
|
_groupDirective: ControlGroupDirective;
|
2015-02-07 14:14:07 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
_controlOrName: any;
|
|
|
|
valueAccessor: any; // ControlValueAccessor
|
2015-02-03 07:27:09 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
validator: Function;
|
2015-02-11 11:10:31 -08:00
|
|
|
|
2015-05-19 21:10:23 -07:00
|
|
|
constructor(@Optional() @Ancestor() groupDirective: ControlGroupDirective) {
|
2015-02-25 15:10:27 -08:00
|
|
|
this._groupDirective = groupDirective;
|
2015-05-11 12:00:56 -07:00
|
|
|
this._controlOrName = null;
|
2015-03-19 14:21:40 -07:00
|
|
|
this.validator = Validators.nullValidator;
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-05-11 12:00:56 -07:00
|
|
|
set controlOrName(controlOrName) {
|
|
|
|
this._controlOrName = controlOrName;
|
2015-02-23 15:26:53 -08:00
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
if (isPresent(this._groupDirective)) {
|
2015-03-25 14:21:57 -07:00
|
|
|
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-05-19 21:10:23 -07:00
|
|
|
if (isBlank(this.valueAccessor)) {
|
|
|
|
throw new BaseException(`Cannot find value accessor for control "${controlOrName}"`);
|
|
|
|
}
|
|
|
|
|
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-05-20 18:10:30 -07:00
|
|
|
_updateDomValue() { this.valueAccessor.writeValue(this._control().value); }
|
2015-03-19 13:12:16 -07:00
|
|
|
|
|
|
|
_setUpUpdateControlValue() {
|
|
|
|
this.valueAccessor.onChange = (newValue) => this._control().updateValue(newValue);
|
2015-02-04 11:45:33 -08:00
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
_control() { return _lookupControl(this._groupDirective, this._controlOrName); }
|
2015-02-03 07:27:09 -08:00
|
|
|
}
|
|
|
|
|
2015-05-19 21:10:23 -07:00
|
|
|
/**
|
|
|
|
* The default accessor for writing a value and listening to changes that is used by a {@link
|
|
|
|
* Control} directive.
|
|
|
|
*
|
|
|
|
* This is the default strategy that Angular uses when no other accessor is applied.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* <input type="text" [control]="loginControl">
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/forms
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: 'input:not([type=checkbox])[control],textarea[control]',
|
|
|
|
hostListeners:
|
|
|
|
{'change': 'onChange($event.target.value)', 'input': 'onChange($event.target.value)'},
|
|
|
|
hostProperties: {'value': 'value'}
|
|
|
|
})
|
|
|
|
export class DefaultValueAccessor {
|
|
|
|
value = null;
|
|
|
|
onChange: Function;
|
|
|
|
|
|
|
|
constructor(cd: ControlDirective, private _elementRef: ElementRef, private _renderer: Renderer) {
|
|
|
|
this.onChange = (_) => {};
|
|
|
|
cd.valueAccessor = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeValue(value) {
|
|
|
|
this._renderer.setElementProperty(this._elementRef.parentView.render,
|
|
|
|
this._elementRef.boundElementIndex, 'value', value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The accessor for writing a value and listening to changes that is used by a {@link
|
|
|
|
* Control} directive.
|
|
|
|
*
|
|
|
|
* This is the default strategy that Angular uses when no other accessor is applied.
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
* ```
|
|
|
|
* <input type="text" [control]="loginControl">
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @exportedAs angular2/forms
|
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector: 'select[control]',
|
|
|
|
hostListeners:
|
|
|
|
{'change': 'onChange($event.target.value)', 'input': 'onChange($event.target.value)'},
|
|
|
|
hostProperties: {'value': 'value'}
|
|
|
|
})
|
|
|
|
export class SelectControlValueAccessor {
|
|
|
|
value = null;
|
|
|
|
onChange: Function;
|
|
|
|
|
|
|
|
constructor(cd: ControlDirective, private _elementRef: ElementRef, private _renderer: Renderer) {
|
|
|
|
this.onChange = (_) => {};
|
|
|
|
this.value = '';
|
|
|
|
cd.valueAccessor = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeValue(value) {
|
|
|
|
this._renderer.setElementProperty(this._elementRef.parentView.render,
|
|
|
|
this._elementRef.boundElementIndex, 'value', value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-05-20 18:10:30 -07:00
|
|
|
* The accessor for writing a value and listening to changes on a checkbox input element.
|
2015-04-10 11:15:01 -07:00
|
|
|
*
|
|
|
|
*
|
2015-05-20 18:10:30 -07:00
|
|
|
* # Example
|
2015-04-10 11:15:01 -07:00
|
|
|
* ```
|
2015-05-20 18:10:30 -07:00
|
|
|
* <input type="checkbox" [control]="rememberLogin">
|
2015-04-10 11:15:01 -07:00
|
|
|
* ```
|
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/forms
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-04-30 13:38:40 -07:00
|
|
|
@Directive({
|
2015-05-20 18:10:30 -07:00
|
|
|
selector: 'input[type=checkbox][control]',
|
|
|
|
hostListeners: {'change': 'onChange($event.target.checked)'},
|
|
|
|
hostProperties: {'checked': 'checked'}
|
2015-02-03 07:27:09 -08:00
|
|
|
})
|
2015-05-20 18:10:30 -07:00
|
|
|
export class CheckboxControlValueAccessor {
|
|
|
|
checked: boolean;
|
|
|
|
onChange: Function;
|
2015-02-04 11:45:33 -08:00
|
|
|
|
2015-05-19 21:10:23 -07:00
|
|
|
constructor(cd: ControlDirective, private _elementRef: ElementRef, private _renderer: Renderer) {
|
2015-05-20 18:10:30 -07:00
|
|
|
this.onChange = (_) => {};
|
2015-05-19 21:10:23 -07:00
|
|
|
cd.valueAccessor = this;
|
2015-02-25 15:10:27 -08:00
|
|
|
}
|
|
|
|
|
2015-05-20 18:10:30 -07:00
|
|
|
writeValue(value) {
|
|
|
|
this._renderer.setElementProperty(this._elementRef.parentView.render,
|
|
|
|
this._elementRef.boundElementIndex, 'checked', value)
|
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
|
|
|
*/
|
2015-05-19 21:10:23 -07:00
|
|
|
export const formDirectives: List<Type> = CONST_EXPR([
|
|
|
|
ControlGroupDirective,
|
|
|
|
ControlDirective,
|
|
|
|
CheckboxControlValueAccessor,
|
|
|
|
DefaultValueAccessor,
|
|
|
|
SelectControlValueAccessor
|
|
|
|
]);
|