import { AfterContentInit, ContentChild, Directive, DoCheck, ElementRef, EmbeddedViewRef, EventEmitter, Host, Inject, InjectionToken, Input, IterableDiffer, IterableDiffers, OnDestroy, Output, TemplateRef, ViewChild, ViewContainerRef, ɵisListLikeIterable as isListLikeIterable, Injectable, NgZone } from '@angular/core'; import { ObservableArray, View, KeyedTemplate, LayoutBase, ItemEventData, TemplatedItemsView, profile } from '@nativescript/core'; import { getSingleViewRecursive } from '../element-registry'; import { NativeScriptDebug } from '../trace'; const NG_VIEW = '_ngViewRef'; export class ItemContext { constructor(public $implicit?: any, public item?: any, public index?: number, public even?: boolean, public odd?: boolean) {} } export interface SetupItemViewArgs { view: EmbeddedViewRef; data: any; index: number; context: ItemContext; } @Directive() export abstract class TemplatedItemsComponent implements DoCheck, OnDestroy, AfterContentInit { public abstract get nativeElement(): TemplatedItemsView; protected templatedItemsView: TemplatedItemsView; protected _items: any; protected _differ: IterableDiffer; protected _templateMap: Map; @ViewChild('loader', { read: ViewContainerRef, static: false }) loader: ViewContainerRef; @Output() public setupItemView = new EventEmitter(); @ContentChild(TemplateRef, { read: TemplateRef, static: false }) itemTemplateQuery: TemplateRef; itemTemplate: TemplateRef; @Input() get items() { return this._items; } set items(value: any) { this._items = value; let needDiffer = true; if (value instanceof ObservableArray) { needDiffer = false; } if (needDiffer && !this._differ && isListLikeIterable(value)) { this._differ = this._iterableDiffers.find(this._items).create((_index, item) => { return item; }); } this.templatedItemsView.items = this._items; } constructor(_elementRef: ElementRef, private _iterableDiffers: IterableDiffers, private zone: NgZone) { this.templatedItemsView = _elementRef.nativeElement; this.templatedItemsView.on('itemLoading', this.onItemLoading, this); } ngAfterContentInit() { if (NativeScriptDebug.isLogEnabled()) { NativeScriptDebug.listViewLog('TemplatedItemsView.ngAfterContentInit()'); } this.setItemTemplates(); } ngOnDestroy() { this.templatedItemsView.off('itemLoading', this.onItemLoading, this); this.templatedItemsView = null; if (this._templateMap) { this._templateMap.clear(); } } private setItemTemplates() { // The itemTemplateQuery may be changed after list items are added that contain