| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {ViewType} from '../../core_private'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {isPresent, isBlank} from '../../src/facade/lang'; | 
					
						
							|  |  |  | import {ListWrapper} from '../../src/facade/collection'; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import * as o from '../output/output_ast'; | 
					
						
							|  |  |  | import {EventHandlerVars} from './constants'; | 
					
						
							|  |  |  | import {CompileQuery, createQueryList, addQueryToTokenMap} from './compile_query'; | 
					
						
							|  |  |  | import {NameResolver} from './expression_converter'; | 
					
						
							|  |  |  | import {CompileElement, CompileNode} from './compile_element'; | 
					
						
							|  |  |  | import {CompileMethod} from './compile_method'; | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  | import {CompilePipe} from './compile_pipe'; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | import { | 
					
						
							|  |  |  |   CompileDirectiveMetadata, | 
					
						
							|  |  |  |   CompilePipeMetadata, | 
					
						
							|  |  |  |   CompileIdentifierMetadata, | 
					
						
							|  |  |  |   CompileTokenMap | 
					
						
							|  |  |  | } from '../compile_metadata'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   getViewFactoryName, | 
					
						
							|  |  |  |   injectFromViewParentInjector, | 
					
						
							|  |  |  |   createDiTokenExpression, | 
					
						
							| 
									
										
										
										
											2016-05-01 22:50:37 -07:00
										 |  |  |   getPropertyInView, | 
					
						
							|  |  |  |   createPureProxy | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | } from './util'; | 
					
						
							|  |  |  | import {CompilerConfig} from '../config'; | 
					
						
							|  |  |  | import {CompileBinding} from './compile_binding'; | 
					
						
							| 
									
										
										
										
											2016-04-29 10:43:26 -07:00
										 |  |  | import {Identifiers} from '../identifiers'; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class CompileView implements NameResolver { | 
					
						
							|  |  |  |   public viewType: ViewType; | 
					
						
							|  |  |  |   public viewQueries: CompileTokenMap<CompileQuery[]>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public nodes: CompileNode[] = []; | 
					
						
							| 
									
										
										
										
											2016-04-18 13:24:42 -07:00
										 |  |  |   // root nodes or AppElements for ViewContainers
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   public rootNodesOrAppElements: o.Expression[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public bindings: CompileBinding[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public classStatements: o.Statement[] = []; | 
					
						
							|  |  |  |   public createMethod: CompileMethod; | 
					
						
							|  |  |  |   public injectorGetMethod: CompileMethod; | 
					
						
							|  |  |  |   public updateContentQueriesMethod: CompileMethod; | 
					
						
							|  |  |  |   public dirtyParentQueriesMethod: CompileMethod; | 
					
						
							|  |  |  |   public updateViewQueriesMethod: CompileMethod; | 
					
						
							|  |  |  |   public detectChangesInInputsMethod: CompileMethod; | 
					
						
							| 
									
										
										
										
											2016-04-20 18:10:19 -07:00
										 |  |  |   public detectChangesRenderPropertiesMethod: CompileMethod; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   public afterContentLifecycleCallbacksMethod: CompileMethod; | 
					
						
							|  |  |  |   public afterViewLifecycleCallbacksMethod: CompileMethod; | 
					
						
							|  |  |  |   public destroyMethod: CompileMethod; | 
					
						
							|  |  |  |   public eventHandlerMethods: o.ClassMethod[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public fields: o.ClassField[] = []; | 
					
						
							|  |  |  |   public getters: o.ClassGetter[] = []; | 
					
						
							|  |  |  |   public disposables: o.Expression[] = []; | 
					
						
							|  |  |  |   public subscriptions: o.Expression[] = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public componentView: CompileView; | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |   public purePipes = new Map<string, CompilePipe>(); | 
					
						
							|  |  |  |   public pipes: CompilePipe[] = []; | 
					
						
							| 
									
										
										
										
											2016-04-25 19:52:24 -07:00
										 |  |  |   public locals = new Map<string, o.Expression>(); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   public className: string; | 
					
						
							|  |  |  |   public classType: o.Type; | 
					
						
							|  |  |  |   public viewFactory: o.ReadVarExpr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public literalArrayCount = 0; | 
					
						
							|  |  |  |   public literalMapCount = 0; | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |   public pipeCount = 0; | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 14:00:31 -07:00
										 |  |  |   public componentContext: o.Expression; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   constructor(public component: CompileDirectiveMetadata, public genConfig: CompilerConfig, | 
					
						
							|  |  |  |               public pipeMetas: CompilePipeMetadata[], public styles: o.Expression, | 
					
						
							|  |  |  |               public viewIndex: number, public declarationElement: CompileElement, | 
					
						
							|  |  |  |               public templateVariableBindings: string[][]) { | 
					
						
							|  |  |  |     this.createMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.injectorGetMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.updateContentQueriesMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.dirtyParentQueriesMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.updateViewQueriesMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.detectChangesInInputsMethod = new CompileMethod(this); | 
					
						
							| 
									
										
										
										
											2016-04-20 18:10:19 -07:00
										 |  |  |     this.detectChangesRenderPropertiesMethod = new CompileMethod(this); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     this.afterContentLifecycleCallbacksMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.afterViewLifecycleCallbacksMethod = new CompileMethod(this); | 
					
						
							|  |  |  |     this.destroyMethod = new CompileMethod(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.viewType = getViewType(component, viewIndex); | 
					
						
							|  |  |  |     this.className = `_View_${component.type.name}${viewIndex}`; | 
					
						
							|  |  |  |     this.classType = o.importType(new CompileIdentifierMetadata({name: this.className})); | 
					
						
							|  |  |  |     this.viewFactory = o.variable(getViewFactoryName(component, viewIndex)); | 
					
						
							|  |  |  |     if (this.viewType === ViewType.COMPONENT || this.viewType === ViewType.HOST) { | 
					
						
							|  |  |  |       this.componentView = this; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       this.componentView = this.declarationElement.view.componentView; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-28 14:00:31 -07:00
										 |  |  |     this.componentContext = | 
					
						
							|  |  |  |         getPropertyInView(o.THIS_EXPR.prop('context'), this, this.componentView); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     var viewQueries = new CompileTokenMap<CompileQuery[]>(); | 
					
						
							|  |  |  |     if (this.viewType === ViewType.COMPONENT) { | 
					
						
							|  |  |  |       var directiveInstance = o.THIS_EXPR.prop('context'); | 
					
						
							|  |  |  |       ListWrapper.forEachWithIndex(this.component.viewQueries, (queryMeta, queryIndex) => { | 
					
						
							|  |  |  |         var propName = `_viewQuery_${queryMeta.selectors[0].name}_${queryIndex}`; | 
					
						
							|  |  |  |         var queryList = createQueryList(queryMeta, directiveInstance, propName, this); | 
					
						
							|  |  |  |         var query = new CompileQuery(queryMeta, queryList, directiveInstance, this); | 
					
						
							|  |  |  |         addQueryToTokenMap(viewQueries, query); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       var constructorViewQueryCount = 0; | 
					
						
							|  |  |  |       this.component.type.diDeps.forEach((dep) => { | 
					
						
							|  |  |  |         if (isPresent(dep.viewQuery)) { | 
					
						
							|  |  |  |           var queryList = o.THIS_EXPR.prop('declarationAppElement') | 
					
						
							|  |  |  |                               .prop('componentConstructorViewQueries') | 
					
						
							|  |  |  |                               .key(o.literal(constructorViewQueryCount++)); | 
					
						
							|  |  |  |           var query = new CompileQuery(dep.viewQuery, queryList, null, this); | 
					
						
							|  |  |  |           addQueryToTokenMap(viewQueries, query); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     this.viewQueries = viewQueries; | 
					
						
							| 
									
										
										
										
											2016-04-28 14:00:31 -07:00
										 |  |  |     templateVariableBindings.forEach( | 
					
						
							|  |  |  |         (entry) => { this.locals.set(entry[1], o.THIS_EXPR.prop('context').prop(entry[0])); }); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!this.declarationElement.isNull()) { | 
					
						
							|  |  |  |       this.declarationElement.setEmbeddedView(this); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |   callPipe(name: string, input: o.Expression, args: o.Expression[]): o.Expression { | 
					
						
							| 
									
										
										
										
											2016-05-03 08:59:25 -07:00
										 |  |  |     return CompilePipe.call(this, name, [input].concat(args)); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 19:52:24 -07:00
										 |  |  |   getLocal(name: string): o.Expression { | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     if (name == EventHandlerVars.event.name) { | 
					
						
							|  |  |  |       return EventHandlerVars.event; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     var currView: CompileView = this; | 
					
						
							| 
									
										
										
										
											2016-04-25 19:52:24 -07:00
										 |  |  |     var result = currView.locals.get(name); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     while (isBlank(result) && isPresent(currView.declarationElement.view)) { | 
					
						
							|  |  |  |       currView = currView.declarationElement.view; | 
					
						
							| 
									
										
										
										
											2016-04-25 19:52:24 -07:00
										 |  |  |       result = currView.locals.get(name); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (isPresent(result)) { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |       return getPropertyInView(result, this, currView); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   createLiteralArray(values: o.Expression[]): o.Expression { | 
					
						
							| 
									
										
										
										
											2016-04-29 10:43:26 -07:00
										 |  |  |     if (values.length === 0) { | 
					
						
							|  |  |  |       return o.importExpr(Identifiers.EMPTY_ARRAY); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |     var proxyExpr = o.THIS_EXPR.prop(`_arr_${this.literalArrayCount++}`); | 
					
						
							|  |  |  |     var proxyParams: o.FnParam[] = []; | 
					
						
							|  |  |  |     var proxyReturnEntries: o.Expression[] = []; | 
					
						
							|  |  |  |     for (var i = 0; i < values.length; i++) { | 
					
						
							|  |  |  |       var paramName = `p${i}`; | 
					
						
							|  |  |  |       proxyParams.push(new o.FnParam(paramName)); | 
					
						
							|  |  |  |       proxyReturnEntries.push(o.variable(paramName)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     createPureProxy(o.fn(proxyParams, [new o.ReturnStatement(o.literalArr(proxyReturnEntries))]), | 
					
						
							|  |  |  |                     values.length, proxyExpr, this); | 
					
						
							|  |  |  |     return proxyExpr.callFn(values); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   createLiteralMap(entries: Array<Array<string | o.Expression>>): o.Expression { | 
					
						
							| 
									
										
										
										
											2016-04-29 10:43:26 -07:00
										 |  |  |     if (entries.length === 0) { | 
					
						
							|  |  |  |       return o.importExpr(Identifiers.EMPTY_MAP); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |     var proxyExpr = o.THIS_EXPR.prop(`_map_${this.literalMapCount++}`); | 
					
						
							|  |  |  |     var proxyParams: o.FnParam[] = []; | 
					
						
							|  |  |  |     var proxyReturnEntries: Array<Array<string | o.Expression>> = []; | 
					
						
							|  |  |  |     var values: o.Expression[] = []; | 
					
						
							|  |  |  |     for (var i = 0; i < entries.length; i++) { | 
					
						
							|  |  |  |       var paramName = `p${i}`; | 
					
						
							|  |  |  |       proxyParams.push(new o.FnParam(paramName)); | 
					
						
							|  |  |  |       proxyReturnEntries.push([entries[i][0], o.variable(paramName)]); | 
					
						
							|  |  |  |       values.push(<o.Expression>entries[i][1]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     createPureProxy(o.fn(proxyParams, [new o.ReturnStatement(o.literalMap(proxyReturnEntries))]), | 
					
						
							|  |  |  |                     entries.length, proxyExpr, this); | 
					
						
							|  |  |  |     return proxyExpr.callFn(values); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterNodes() { | 
					
						
							| 
									
										
										
										
											2016-04-22 15:33:32 -07:00
										 |  |  |     this.pipes.forEach((pipe) => pipe.create()); | 
					
						
							| 
									
										
										
										
											2016-01-06 14:13:44 -08:00
										 |  |  |     this.viewQueries.values().forEach( | 
					
						
							|  |  |  |         (queries) => queries.forEach((query) => query.afterChildren(this.updateViewQueriesMethod))); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getViewType(component: CompileDirectiveMetadata, embeddedTemplateIndex: number): ViewType { | 
					
						
							|  |  |  |   if (embeddedTemplateIndex > 0) { | 
					
						
							|  |  |  |     return ViewType.EMBEDDED; | 
					
						
							|  |  |  |   } else if (component.type.isHost) { | 
					
						
							|  |  |  |     return ViewType.HOST; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     return ViewType.COMPONENT; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |