| 
									
										
										
										
											2016-06-23 09:47:54 -07: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-06-24 08:46:43 -07:00
										 |  |  | import {AnimationEntryMetadata, BaseException, Compiler, Injectable, Injector, Type, ViewMetadata, resolveForwardRef} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {ViewResolver} from '../index'; | 
					
						
							|  |  |  | import {Map} from '../src/facade/collection'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {isArray, isBlank, isPresent, stringify} from '../src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 09:06:42 -08:00
										 |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2015-06-24 10:54:04 +02:00
										 |  |  | export class MockViewResolver extends ViewResolver { | 
					
						
							| 
									
										
										
										
											2015-10-09 17:21:25 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |   _views = new Map<Type, ViewMetadata>(); | 
					
						
							| 
									
										
										
										
											2015-10-09 17:21:25 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |   _inlineTemplates = new Map<Type, string>(); | 
					
						
							| 
									
										
										
										
											2015-10-09 17:21:25 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |   _animations = new Map<Type, AnimationEntryMetadata[]>(); | 
					
						
							|  |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |   _directiveOverrides = new Map<Type, Map<Type, Type>>(); | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |   constructor(private _injector: Injector) { super(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private get _compiler(): Compiler { return this._injector.get(Compiler); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); } | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-08-14 10:03:45 -07:00
										 |  |  |    * Overrides the {@link ViewMetadata} for a component. | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-14 10:03:45 -07:00
										 |  |  |   setView(component: Type, view: ViewMetadata): void { | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |     this._views.set(component, view); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     this._clearCacheFor(component); | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Overrides the inline template for a component - other configuration remains unchanged. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   setInlineTemplate(component: Type, template: string): void { | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |     this._inlineTemplates.set(component, template); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     this._clearCacheFor(component); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |   setAnimations(component: Type, animations: AnimationEntryMetadata[]): void { | 
					
						
							|  |  |  |     this._animations.set(component, animations); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     this._clearCacheFor(component); | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-08-14 10:03:45 -07:00
										 |  |  |    * Overrides a directive from the component {@link ViewMetadata}. | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-05-15 16:42:52 -07:00
										 |  |  |   overrideViewDirective(component: Type, from: Type, to: Type): void { | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |     var overrides = this._directiveOverrides.get(component); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (isBlank(overrides)) { | 
					
						
							| 
									
										
										
										
											2015-09-29 11:11:06 -07:00
										 |  |  |       overrides = new Map<Type, Type>(); | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |       this._directiveOverrides.set(component, overrides); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |     overrides.set(from, to); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     this._clearCacheFor(component); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-08-14 10:03:45 -07:00
										 |  |  |    * Returns the {@link ViewMetadata} for a component: | 
					
						
							|  |  |  |    * - Set the {@link ViewMetadata} to the overridden view when it exists or fallback to the default | 
					
						
							| 
									
										
										
										
											2015-06-24 10:54:04 +02:00
										 |  |  |    * `ViewResolver`, | 
					
						
							| 
									
										
										
										
											2015-04-17 13:01:07 -07:00
										 |  |  |    *   see `setView`. | 
					
						
							| 
									
										
										
										
											2015-05-15 16:42:52 -07:00
										 |  |  |    * - Override the directives, see `overrideViewDirective`. | 
					
						
							| 
									
										
										
										
											2015-04-17 13:01:07 -07:00
										 |  |  |    * - Override the @View definition, see `setInlineTemplate`. | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-14 10:03:45 -07:00
										 |  |  |   resolve(component: Type): ViewMetadata { | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     var view = this._views.get(component); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |     if (isBlank(view)) { | 
					
						
							|  |  |  |       view = super.resolve(component); | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |     var directives: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |     if (isPresent(view.directives)) { | 
					
						
							|  |  |  |       flattenArray(view.directives, directives); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var animations = view.animations; | 
					
						
							|  |  |  |     var templateUrl = view.templateUrl; | 
					
						
							| 
									
										
										
										
											2015-06-17 16:21:40 -07:00
										 |  |  |     var overrides = this._directiveOverrides.get(component); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |     var inlineAnimations = this._animations.get(component); | 
					
						
							|  |  |  |     if (isPresent(inlineAnimations)) { | 
					
						
							|  |  |  |       animations = inlineAnimations; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var inlineTemplate = this._inlineTemplates.get(component); | 
					
						
							|  |  |  |     if (isPresent(inlineTemplate)) { | 
					
						
							|  |  |  |       templateUrl = null; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       inlineTemplate = view.template; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-24 23:11:30 -07:00
										 |  |  |     if (isPresent(overrides) && isPresent(view.directives)) { | 
					
						
							| 
									
										
										
										
											2015-10-08 16:01:18 -07:00
										 |  |  |       overrides.forEach((to, from) => { | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         var srcIndex = directives.indexOf(from); | 
					
						
							|  |  |  |         if (srcIndex == -1) { | 
					
						
							| 
									
										
										
										
											2015-05-20 17:19:46 -07:00
										 |  |  |           throw new BaseException( | 
					
						
							|  |  |  |               `Overriden directive ${stringify(from)} not found in the template of ${stringify(component)}`); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         directives[srcIndex] = to; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |     view = new ViewMetadata({ | 
					
						
							|  |  |  |       template: inlineTemplate, | 
					
						
							|  |  |  |       templateUrl: templateUrl, | 
					
						
							|  |  |  |       directives: directives.length > 0 ? directives : null, | 
					
						
							|  |  |  |       animations: animations, | 
					
						
							|  |  |  |       styles: view.styles, | 
					
						
							|  |  |  |       styleUrls: view.styleUrls, | 
					
						
							|  |  |  |       pipes: view.pipes, | 
					
						
							| 
									
										
										
										
											2016-06-20 09:52:41 -07:00
										 |  |  |       encapsulation: view.encapsulation, | 
					
						
							|  |  |  |       interpolation: view.interpolation | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |     return view; | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-24 15:32:44 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-24 23:11:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | function flattenArray(tree: any[], out: Array<Type|any[]>): void { | 
					
						
							| 
									
										
										
										
											2016-05-25 12:46:22 -07:00
										 |  |  |   if (!isPresent(tree)) return; | 
					
						
							| 
									
										
										
										
											2016-04-24 23:11:30 -07:00
										 |  |  |   for (var i = 0; i < tree.length; i++) { | 
					
						
							|  |  |  |     var item = resolveForwardRef(tree[i]); | 
					
						
							|  |  |  |     if (isArray(item)) { | 
					
						
							|  |  |  |       flattenArray(item, out); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       out.push(item); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |