| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | import {CONST_EXPR} from 'angular2/src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  | import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; | 
					
						
							|  |  |  | import {List, StringMapWrapper, StringMap} from 'angular2/src/facade/collection'; | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-04 15:04:50 +02:00
										 |  |  | import {Directive, LifecycleEvent, Query, QueryList} from 'angular2/angular2'; | 
					
						
							| 
									
										
										
										
											2015-06-26 15:59:18 -07:00
										 |  |  | import {forwardRef, Ancestor, Binding, Inject} from 'angular2/di'; | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  | import {ControlContainer} from './control_container'; | 
					
						
							|  |  |  | import {NgControl} from './ng_control'; | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  | import {NgValidator} from './validators'; | 
					
						
							|  |  |  | import {controlPath, composeNgValidator} from './shared'; | 
					
						
							| 
									
										
										
										
											2015-06-03 11:56:01 -07:00
										 |  |  | import {Control} from '../model'; | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | const controlNameBinding = | 
					
						
							| 
									
										
										
										
											2015-06-11 18:49:07 -07:00
										 |  |  |     CONST_EXPR(new Binding(NgControl, {toAlias: forwardRef(() => NgControlName)})); | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  * Creates and binds a control with a specified name to a DOM element. | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  * This directive can only be used as a child of {@link NgForm} or {@link NgFormModel}. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |  * # Example | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  * In this example, we create the login and password controls. | 
					
						
							|  |  |  |  * We can work with each control separately: check its validity, get its value, listen to its | 
					
						
							|  |  |  |  changes. | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  ```
 | 
					
						
							|  |  |  |  * @Component({selector: "login-comp"}) | 
					
						
							|  |  |  |  * @View({ | 
					
						
							|  |  |  |  *      directives: [formDirectives], | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  *      template: `
 | 
					
						
							|  |  |  |  *              <form #f="form" (submit)='onLogIn(f.value)'> | 
					
						
							|  |  |  |  *                Login <input type='text' ng-control='login' #l="form"> | 
					
						
							|  |  |  |  *                <div *ng-if="!l.valid">Login is invalid</div> | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  *                Password <input type='password' ng-control='password'> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  *                <button type='submit'>Log in!</button> | 
					
						
							|  |  |  |  *              </form> | 
					
						
							|  |  |  |  *      `})
 | 
					
						
							|  |  |  |  * class LoginComp { | 
					
						
							|  |  |  |  *  onLogIn(value) { | 
					
						
							|  |  |  |  *    // value === {login: 'some login', password: 'some password'}
 | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  |  *  } | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  * } | 
					
						
							|  |  |  |  *  ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * We can also use ng-model to bind a domain model to the form. | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-11 09:58:53 -07:00
										 |  |  |  *  ```
 | 
					
						
							|  |  |  |  * @Component({selector: "login-comp"}) | 
					
						
							|  |  |  |  * @View({ | 
					
						
							|  |  |  |  *      directives: [formDirectives], | 
					
						
							|  |  |  |  *      template: `
 | 
					
						
							|  |  |  |  *              <form (submit)='onLogIn()'> | 
					
						
							|  |  |  |  *                Login <input type='text' ng-control='login' [(ng-model)]="credentials.login"> | 
					
						
							|  |  |  |  *                Password <input type='password' ng-control='password' | 
					
						
							|  |  |  |  [(ng-model)]="credentials.password"> | 
					
						
							|  |  |  |  *                <button type='submit'>Log in!</button> | 
					
						
							|  |  |  |  *              </form> | 
					
						
							|  |  |  |  *      `})
 | 
					
						
							|  |  |  |  * class LoginComp { | 
					
						
							|  |  |  |  *  credentials: {login:string, password:string}; | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  onLogIn() { | 
					
						
							|  |  |  |  *    // this.credentials.login === "some login"
 | 
					
						
							|  |  |  |  *    // this.credentials.password === "some password"
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |  *  } | 
					
						
							|  |  |  |  * } | 
					
						
							|  |  |  |  *  ```
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | @Directive({ | 
					
						
							| 
									
										
										
										
											2015-06-02 14:53:49 -07:00
										 |  |  |   selector: '[ng-control]', | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |   hostInjector: [controlNameBinding], | 
					
						
							| 
									
										
										
										
											2015-06-17 15:42:07 -07:00
										 |  |  |   properties: ['name: ngControl', 'model: ngModel'], | 
					
						
							|  |  |  |   events: ['update: ngModel'], | 
					
						
							| 
									
										
										
										
											2015-07-04 15:04:50 +02:00
										 |  |  |   lifecycle: [LifecycleEvent.onDestroy, LifecycleEvent.onChange], | 
					
						
							| 
									
										
										
										
											2015-06-05 14:33:57 -07:00
										 |  |  |   exportAs: 'form' | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2015-06-10 13:51:44 -07:00
										 |  |  | export class NgControlName extends NgControl { | 
					
						
							|  |  |  |   _parent: ControlContainer; | 
					
						
							| 
									
										
										
										
											2015-06-17 15:42:07 -07:00
										 |  |  |   update = new EventEmitter(); | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  |   model: any; | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |   ngValidators: QueryList<NgValidator>; | 
					
						
							| 
									
										
										
										
											2015-06-17 15:42:07 -07:00
										 |  |  |   _added = false; | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |   // Scope the query once https://github.com/angular/angular/issues/2603 is fixed
 | 
					
						
							|  |  |  |   constructor(@Ancestor() parent: ControlContainer, | 
					
						
							|  |  |  |               @Query(NgValidator) ngValidators: QueryList<NgValidator>) { | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |     super(); | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  |     this._parent = parent; | 
					
						
							|  |  |  |     this.ngValidators = ngValidators; | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  |   onChange(c: StringMap<string, any>) { | 
					
						
							|  |  |  |     if (!this._added) { | 
					
						
							|  |  |  |       this.formDirective.addControl(this); | 
					
						
							|  |  |  |       this._added = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (StringMapWrapper.contains(c, "model")) { | 
					
						
							|  |  |  |       this.formDirective.updateModel(this, this.model); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   onDestroy() { this.formDirective.removeControl(this); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 15:42:07 -07:00
										 |  |  |   viewToModelUpdate(newValue: any): void { ObservableWrapper.callNext(this.update, newValue); } | 
					
						
							| 
									
										
										
										
											2015-05-31 12:24:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-30 11:56:00 -07:00
										 |  |  |   get path(): List<string> { return controlPath(this.name, this._parent); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get formDirective(): any { return this._parent.formDirective; } | 
					
						
							| 
									
										
										
										
											2015-06-03 11:56:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   get control(): Control { return this.formDirective.getControl(this); } | 
					
						
							| 
									
										
										
										
											2015-06-17 14:45:40 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   get validator(): Function { return composeNgValidator(this.ngValidators); } | 
					
						
							| 
									
										
										
										
											2015-07-04 15:04:50 +02:00
										 |  |  | } |