2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-25 03:50:16 -04:00
|
|
|
import {Directive, Host, Inject, Input, OnChanges, OnDestroy, Optional, Output, Self, SimpleChanges, forwardRef} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {EventEmitter} from '../facade/async';
|
2016-06-10 14:15:59 -04:00
|
|
|
import {FormControl} from '../model';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators';
|
|
|
|
|
2016-07-27 13:59:40 -04:00
|
|
|
import {AbstractFormGroupDirective} from './abstract_form_group_directive';
|
2016-06-10 13:09:50 -04:00
|
|
|
import {ControlContainer} from './control_container';
|
2016-06-08 18:36:24 -04:00
|
|
|
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
|
|
|
import {NgControl} from './ng_control';
|
2016-07-27 13:59:40 -04:00
|
|
|
import {NgForm} from './ng_form';
|
|
|
|
import {NgModelGroup} from './ng_model_group';
|
2016-06-10 13:09:50 -04:00
|
|
|
import {composeAsyncValidators, composeValidators, controlPath, isPropertyUpdated, selectValueAccessor, setUpControl} from './shared';
|
2016-07-27 13:59:40 -04:00
|
|
|
import {TemplateDrivenErrors} from './template_driven_errors';
|
2016-08-29 14:33:49 -04:00
|
|
|
import {AsyncValidatorFn, Validator, ValidatorFn} from './validators';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-07-30 22:18:14 -04:00
|
|
|
export const formControlBinding: any = {
|
|
|
|
provide: NgControl,
|
|
|
|
useExisting: forwardRef(() => NgModel)
|
|
|
|
};
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
const resolvedPromise = Promise.resolve(null);
|
|
|
|
|
2016-06-08 18:36:24 -04:00
|
|
|
/**
|
|
|
|
* Binds a domain model to a form control.
|
|
|
|
*
|
|
|
|
* ### Usage
|
|
|
|
*
|
|
|
|
* `ngModel` binds an existing domain model to a form control. For a
|
|
|
|
* two-way binding, use `[(ngModel)]` to ensure the model updates in
|
|
|
|
* both directions.
|
|
|
|
*
|
|
|
|
* ```typescript
|
|
|
|
* @Component({
|
|
|
|
* selector: "search-comp",
|
2016-06-13 19:32:45 -04:00
|
|
|
* directives: [],
|
2016-06-08 18:36:24 -04:00
|
|
|
* template: `<input type='text' [(ngModel)]="searchQuery">`
|
|
|
|
* })
|
|
|
|
* class SearchComp {
|
|
|
|
* searchQuery: string;
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2016-08-17 10:44:39 -04:00
|
|
|
* @stable
|
2016-06-08 18:36:24 -04:00
|
|
|
*/
|
|
|
|
@Directive({
|
2016-06-12 16:17:07 -04:00
|
|
|
selector: '[ngModel]:not([formControlName]):not([formControl])',
|
2016-06-08 18:36:24 -04:00
|
|
|
providers: [formControlBinding],
|
2016-06-15 20:46:45 -04:00
|
|
|
exportAs: 'ngModel'
|
2016-06-08 18:36:24 -04:00
|
|
|
})
|
2016-06-10 13:09:50 -04:00
|
|
|
export class NgModel extends NgControl implements OnChanges,
|
|
|
|
OnDestroy {
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-23 12:55:26 -04:00
|
|
|
_control = new FormControl();
|
2016-06-08 18:36:24 -04:00
|
|
|
/** @internal */
|
2016-06-23 12:55:26 -04:00
|
|
|
_registered = false;
|
2016-06-08 18:36:24 -04:00
|
|
|
viewModel: any;
|
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
@Input() name: string;
|
2016-08-25 17:56:31 -04:00
|
|
|
@Input('disabled') isDisabled: boolean;
|
2016-08-24 19:58:43 -04:00
|
|
|
@Input('ngModel') model: any;
|
2016-06-23 12:55:26 -04:00
|
|
|
@Input('ngModelOptions') options: {name?: string, standalone?: boolean};
|
2016-08-24 19:58:43 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
@Output('ngModelChange') update = new EventEmitter();
|
|
|
|
|
2016-08-29 20:49:42 -04:00
|
|
|
constructor(@Optional() @Host() parent: ControlContainer,
|
2016-08-29 14:33:49 -04:00
|
|
|
@Optional() @Self() @Inject(NG_VALIDATORS) validators: Array<Validator|ValidatorFn>,
|
|
|
|
@Optional() @Self() @Inject(NG_ASYNC_VALIDATORS) asyncValidators: Array<Validator|AsyncValidatorFn>,
|
2016-06-08 18:36:24 -04:00
|
|
|
@Optional() @Self() @Inject(NG_VALUE_ACCESSOR)
|
|
|
|
valueAccessors: ControlValueAccessor[]) {
|
2016-06-08 19:38:52 -04:00
|
|
|
super();
|
2016-08-29 20:49:42 -04:00
|
|
|
this._parent = parent;
|
2016-08-29 14:33:49 -04:00
|
|
|
this._rawValidators = validators || [];
|
|
|
|
this._rawAsyncValidators = asyncValidators || [];
|
2016-06-08 19:38:52 -04:00
|
|
|
this.valueAccessor = selectValueAccessor(this, valueAccessors);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
2016-07-27 13:59:40 -04:00
|
|
|
this._checkForErrors();
|
2016-06-23 12:55:26 -04:00
|
|
|
if (!this._registered) this._setUpControl();
|
2016-08-25 17:56:31 -04:00
|
|
|
if ('isDisabled' in changes) {
|
2016-08-24 19:58:43 -04:00
|
|
|
this._updateDisabled(changes);
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
if (isPropertyUpdated(changes, this.viewModel)) {
|
2016-06-23 21:07:56 -04:00
|
|
|
this._updateValue(this.model);
|
2016-06-08 19:38:52 -04:00
|
|
|
this.viewModel = this.model;
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
ngOnDestroy(): void { this.formDirective && this.formDirective.removeControl(this); }
|
|
|
|
|
2016-06-10 14:15:59 -04:00
|
|
|
get control(): FormControl { return this._control; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-10 13:09:50 -04:00
|
|
|
get path(): string[] {
|
2016-07-13 17:13:02 -04:00
|
|
|
return this._parent ? controlPath(this.name, this._parent) : [this.name];
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-08-29 14:33:49 -04:00
|
|
|
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
get asyncValidator(): AsyncValidatorFn {
|
2016-08-29 14:33:49 -04:00
|
|
|
return composeAsyncValidators(this._rawAsyncValidators);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
viewToModelUpdate(newValue: any): void {
|
|
|
|
this.viewModel = newValue;
|
2016-08-02 18:53:34 -04:00
|
|
|
this.update.emit(newValue);
|
2016-06-08 19:38:52 -04:00
|
|
|
}
|
2016-06-10 13:09:50 -04:00
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
private _setUpControl(): void {
|
|
|
|
this._isStandalone() ? this._setUpStandalone() :
|
|
|
|
this.formDirective.addControl(this);
|
|
|
|
this._registered = true;
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
private _isStandalone(): boolean {
|
|
|
|
return !this._parent || (this.options && this.options.standalone);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _setUpStandalone(): void {
|
2016-06-10 13:09:50 -04:00
|
|
|
setUpControl(this._control, this);
|
|
|
|
this._control.updateValueAndValidity({emitEvent: false});
|
|
|
|
}
|
|
|
|
|
2016-07-27 13:59:40 -04:00
|
|
|
private _checkForErrors(): void {
|
|
|
|
if (!this._isStandalone()) {
|
|
|
|
this._checkParentType();
|
|
|
|
}
|
|
|
|
this._checkName();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _checkParentType(): void {
|
|
|
|
if (!(this._parent instanceof NgModelGroup) &&
|
|
|
|
this._parent instanceof AbstractFormGroupDirective) {
|
|
|
|
TemplateDrivenErrors.formGroupNameException();
|
|
|
|
} else if (
|
|
|
|
!(this._parent instanceof NgModelGroup) && !(this._parent instanceof NgForm)) {
|
|
|
|
TemplateDrivenErrors.modelParentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 21:07:56 -04:00
|
|
|
private _checkName(): void {
|
2016-06-13 19:32:45 -04:00
|
|
|
if (this.options && this.options.name) this.name = this.options.name;
|
|
|
|
|
2016-06-23 12:55:26 -04:00
|
|
|
if (!this._isStandalone() && !this.name) {
|
2016-07-27 13:59:40 -04:00
|
|
|
TemplateDrivenErrors.missingNameException();
|
2016-06-10 13:09:50 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-23 21:07:56 -04:00
|
|
|
|
|
|
|
private _updateValue(value: any): void {
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then(
|
2016-08-05 16:35:17 -04:00
|
|
|
() => { this.control.setValue(value, {emitViewToModelChange: false}); });
|
2016-06-23 21:07:56 -04:00
|
|
|
}
|
2016-08-24 19:58:43 -04:00
|
|
|
|
|
|
|
private _updateDisabled(changes: SimpleChanges) {
|
2016-08-25 17:56:31 -04:00
|
|
|
const disabledValue = changes['isDisabled'].currentValue;
|
2016-08-24 19:58:43 -04:00
|
|
|
const isDisabled = disabledValue != null && disabledValue != false;
|
|
|
|
|
|
|
|
resolvedPromise.then(() => {
|
|
|
|
if (isDisabled && !this.control.disabled) {
|
|
|
|
this.control.disable();
|
|
|
|
} else if (!isDisabled && this.control.disabled) {
|
|
|
|
this.control.enable();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|