forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-view-nested-segmented-bar-page.component.ts
127 lines (111 loc) · 4.1 KB
/
list-view-nested-segmented-bar-page.component.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
import { Component, ViewChild, ElementRef, OnInit } from "@angular/core";
import { SegmentedBarItem, SegmentedBar } from "tns-core-modules/ui/segmented-bar/segmented-bar";
import { ListView } from "tns-core-modules/ui/list-view/list-view";
import { EventData } from "tns-core-modules/ui/page/page";
interface DataItem {
id: number;
name: string;
type: string;
}
@Component({
moduleId: module.id,
selector: "segmented-bar-list-test",
template: `
<GridLayout automationText="mainView" iosOverflowSafeArea="false" style="height: 100%;">
<ListView
#listViewTest
[itemTemplateSelector]="templateSelector"
[items]="displayedItems"
>
<ng-template let-item="item">
<label [text]="'Unsupported Element ' + item.type" color="red"></label>
</ng-template>
<ng-template nsTemplateKey="segmentedBarTemplate" let-item="item">
<SegmentedBar
[items]="segmentedBarItems"
(selectedIndexChange)="onSegmentedBarPress($event)"
></SegmentedBar>
</ng-template>
<ng-template nsTemplateKey="dataItemTemplate" let-item="item">
<StackLayout>
<Label [text]="'Item ID: ' + item.id" height="50"></Label>
<Label [text]="item.name" height="50"></Label>
</StackLayout>
</ng-template>
<ng-template nsTemplateKey="buttonTemplate" let-item="item">
<button text="Pushing me shouldn't crash!" (tap)="onButtonPress()"></button>
</ng-template>
</ListView>
</GridLayout>
`,
})
export class ListViewSegmentedBarPageComponent implements OnInit {
public displayedItems: Array<DataItem> = [];
public items: Array<DataItem>;
public segmentedBarItems: SegmentedBarItem[] = this.createSegmentedBarItems();
@ViewChild("listViewTest", { static: false })
private listViewTest?: ElementRef;
constructor() {
this.items = [];
for (let i = 0; i < 20; i++) {
const type = "dataItemTemplate";
this.items.push({
id: i,
name: `data item ${i}`,
type: type,
});
}
}
public ngOnInit() {
this.displayedItems = this.updateItems(true);
}
public onButtonPress() {
// tslint:disable-next-line: no-unused-expression
new Promise((resolve) => {
setTimeout(() => {
if (this.listViewTest) {
console.log("Scrolling to the top of the list...");
const listView = this.listViewTest.nativeElement as ListView;
listView.scrollToIndex(0);
}
resolve();
}, 150);
});
this.displayedItems = this.updateItems(false);
}
public onSegmentedBarPress(args: EventData) {
if (args && args.object) {
const segmentBar = args.object as SegmentedBar;
const selectedOdd = segmentBar.selectedIndex === 0;
this.displayedItems = this.updateItems(selectedOdd);
}
}
public createSegmentedBarItems() {
const itemOdd = new SegmentedBarItem();
itemOdd.title = "Odd Items";
const itemEven = new SegmentedBarItem();
itemEven.title = "Even Items";
return [itemOdd, itemEven];
}
public templateSelector(item: DataItem): string {
return item.type;
}
private updateItems(odd: boolean) {
const items = [
{
id: -1,
name: "Segmented Bar",
type: "segmentedBarTemplate",
},
...(odd
? this.items.filter((item) => item.id % 2 === 1)
: this.items.filter((item) => item.id % 2 === 0)),
{
id: 999,
name: "Refresh test",
type: "buttonTemplate",
},
];
return items;
}
}