-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathlist-view-comp.ts
268 lines (222 loc) · 8.14 KB
/
list-view-comp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import {
AfterContentInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ContentChild,
Directive,
DoCheck,
ElementRef,
EmbeddedViewRef,
EventEmitter,
Host,
Input,
IterableDiffer,
IterableDiffers,
OnDestroy,
Output,
TemplateRef,
ViewChild,
ViewContainerRef,
} from "@angular/core";
import { ListView, ItemEventData } from "tns-core-modules/ui/list-view";
import { View, KeyedTemplate } from "tns-core-modules/ui/core/view";
import { ObservableArray } from "tns-core-modules/data/observable-array";
import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base";
import { profile } from "tns-core-modules/profiling";
import { InvisibleNode } from "../element-registry";
import { isListLikeIterable } from "../collection-facade";
import { listViewLog, listViewError } from "../trace";
const NG_VIEW = "_ngViewRef";
export class ListItemContext {
constructor(
public $implicit?: any,
public item?: any,
public index?: number,
public even?: boolean,
public odd?: boolean
) {
}
}
export interface SetupItemViewArgs {
view: EmbeddedViewRef<any>;
data: any;
index: number;
context: ListItemContext;
}
@Component({
selector: "ListView",
template: `
<DetachedContainer>
<Placeholder #loader></Placeholder>
</DetachedContainer>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
public get nativeElement(): ListView {
return this.listView;
}
private listView: ListView;
private _items: any;
private _differ: IterableDiffer<KeyedTemplate>;
private _templateMap: Map<string, KeyedTemplate>;
@ViewChild("loader", { read: ViewContainerRef }) loader: ViewContainerRef;
@Output() public setupItemView = new EventEmitter<SetupItemViewArgs>();
@ContentChild(TemplateRef) itemTemplateQuery: TemplateRef<ListItemContext>;
itemTemplate: TemplateRef<ListItemContext>;
@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(this._cdr, (_index, item) => { return item; });
}
this.listView.items = this._items;
}
constructor(_elementRef: ElementRef,
private _iterableDiffers: IterableDiffers,
private _cdr: ChangeDetectorRef) {
this.listView = _elementRef.nativeElement;
this.listView.on("itemLoading", this.onItemLoading, this);
}
ngAfterContentInit() {
listViewLog("ListView.ngAfterContentInit()");
this.setItemTemplates();
}
ngOnDestroy() {
this.listView.off("itemLoading", this.onItemLoading, this);
}
private setItemTemplates() {
// The itemTemplateQuery may be changed after list items are added that contain <template> inside,
// so cache and use only the original template to avoid errors.
this.itemTemplate = this.itemTemplateQuery;
if (this._templateMap) {
listViewLog("Setting templates");
const templates: KeyedTemplate[] = [];
this._templateMap.forEach(value => {
templates.push(value);
});
this.listView.itemTemplates = templates;
}
}
public registerTemplate(key: string, template: TemplateRef<ListItemContext>) {
listViewLog("registerTemplate for key: " + key);
if (!this._templateMap) {
this._templateMap = new Map<string, KeyedTemplate>();
}
const keyedTemplate = {
key,
createView: () => {
listViewLog("registerTemplate for key: " + key);
const viewRef = this.loader.createEmbeddedView(template, new ListItemContext(), 0);
const resultView = getItemViewRoot(viewRef);
resultView[NG_VIEW] = viewRef;
return resultView;
}
};
this._templateMap.set(key, keyedTemplate);
}
@profile
public onItemLoading(args: ItemEventData) {
if (!args.view && !this.itemTemplate) {
return;
}
const index = args.index;
const items = (<any>args.object).items;
const currentItem = typeof items.getItem === "function" ? items.getItem(index) : items[index];
let viewRef: EmbeddedViewRef<ListItemContext>;
if (args.view) {
listViewLog("onItemLoading: " + index + " - Reusing existing view");
viewRef = args.view[NG_VIEW];
// Getting angular view from original element (in cases when ProxyViewContainer
// is used NativeScript internally wraps it in a StackLayout)
if (!viewRef && args.view instanceof LayoutBase && args.view.getChildrenCount() > 0) {
viewRef = args.view.getChildAt(0)[NG_VIEW];
}
if (!viewRef) {
listViewError("ViewReference not found for item " + index + ". View recycling is not working");
}
}
if (!viewRef) {
listViewLog("onItemLoading: " + index + " - Creating view from template");
viewRef = this.loader.createEmbeddedView(this.itemTemplate, new ListItemContext(), 0);
args.view = getItemViewRoot(viewRef);
args.view[NG_VIEW] = viewRef;
}
this.setupViewRef(viewRef, currentItem, index);
this.detectChangesOnChild(viewRef, index);
}
public setupViewRef(viewRef: EmbeddedViewRef<ListItemContext>, data: any, index: number): void {
const context = viewRef.context;
context.$implicit = data;
context.item = data;
context.index = index;
context.even = (index % 2 === 0);
context.odd = !context.even;
this.setupItemView.next({ view: viewRef, data: data, index: index, context: context });
}
@profile
private detectChangesOnChild(viewRef: EmbeddedViewRef<ListItemContext>, index: number) {
listViewLog("Manually detect changes in child: " + index);
viewRef.markForCheck();
viewRef.detectChanges();
}
ngDoCheck() {
if (this._differ) {
listViewLog("ngDoCheck() - execute differ");
const changes = this._differ.diff(this._items);
if (changes) {
listViewLog("ngDoCheck() - refresh");
this.listView.refresh();
}
}
}
}
function getSingleViewRecursive(nodes: Array<any>, nestLevel: number): View {
const actualNodes = nodes.filter(node => !(node instanceof InvisibleNode));
if (actualNodes.length === 0) {
throw new Error(`No suitable views found in list template! ` +
`Nesting level: ${nestLevel}`);
} else if (actualNodes.length > 1) {
throw new Error(`More than one view found in list template!` +
`Nesting level: ${nestLevel}`);
}
const rootLayout = actualNodes[0];
if (!rootLayout) {
return getSingleViewRecursive(rootLayout.children, nestLevel + 1);
}
let parentLayout = rootLayout.parent;
if (parentLayout instanceof LayoutBase) {
parentLayout.removeChild(rootLayout);
}
return rootLayout;
}
export interface ComponentView {
rootNodes: Array<any>;
destroy(): void;
}
export type RootLocator = (nodes: Array<any>, nestLevel: number) => View;
export function getItemViewRoot(viewRef: ComponentView, rootLocator: RootLocator = getSingleViewRecursive): View {
const rootView = rootLocator(viewRef.rootNodes, 0);
return rootView;
}
@Directive({ selector: "[nsTemplateKey]" })
export class TemplateKeyDirective {
constructor(
private templateRef: TemplateRef<any>,
@Host() private list: ListViewComponent) {
}
@Input()
set nsTemplateKey(value: any) {
if (this.list && this.templateRef) {
this.list.registerTemplate(value, this.templateRef);
}
}
}