|  | 
|  | 1 | +import { Component, ViewChild, ElementRef, OnInit } from "@angular/core"; | 
|  | 2 | +import { SegmentedBarItem, SegmentedBar } from "tns-core-modules/ui/segmented-bar/segmented-bar"; | 
|  | 3 | +import { ListView } from "tns-core-modules/ui/list-view/list-view"; | 
|  | 4 | +import { EventData } from "tns-core-modules/ui/page/page"; | 
|  | 5 | + | 
|  | 6 | +interface DataItem { | 
|  | 7 | + id: number; | 
|  | 8 | + name: string; | 
|  | 9 | + type: string; | 
|  | 10 | +} | 
|  | 11 | + | 
|  | 12 | +@Component({ | 
|  | 13 | + moduleId: module.id, | 
|  | 14 | + selector: "segmented-bar-list-test", | 
|  | 15 | + template: ` | 
|  | 16 | + <GridLayout automationText="mainView" iosOverflowSafeArea="false" style="height: 100%;"> | 
|  | 17 | + <ListView | 
|  | 18 | + #listViewTest | 
|  | 19 | + [itemTemplateSelector]="templateSelector" | 
|  | 20 | + [items]="displayedItems" | 
|  | 21 | + > | 
|  | 22 | + <ng-template let-item="item"> | 
|  | 23 | + <label [text]="'Unsupported Element ' + item.type" color="red"></label> | 
|  | 24 | + </ng-template> | 
|  | 25 | + | 
|  | 26 | + <ng-template nsTemplateKey="segmentedBarTemplate" let-item="item"> | 
|  | 27 | + <SegmentedBar | 
|  | 28 | + [items]="segmentedBarItems" | 
|  | 29 | + (selectedIndexChange)="onSegmentedBarPress($event)" | 
|  | 30 | + ></SegmentedBar> | 
|  | 31 | + </ng-template> | 
|  | 32 | + | 
|  | 33 | + <ng-template nsTemplateKey="dataItemTemplate" let-item="item"> | 
|  | 34 | + <StackLayout> | 
|  | 35 | + <Label [text]="'Item ID: ' + item.id" height="50"></Label> | 
|  | 36 | + <Label [text]="item.name" height="50"></Label> | 
|  | 37 | + </StackLayout> | 
|  | 38 | + </ng-template> | 
|  | 39 | + | 
|  | 40 | + <ng-template nsTemplateKey="buttonTemplate" let-item="item"> | 
|  | 41 | + <button text="Pushing me shouldn't crash!" (tap)="onButtonPress()"></button> | 
|  | 42 | + </ng-template> | 
|  | 43 | + </ListView> | 
|  | 44 | + </GridLayout> | 
|  | 45 | + `, | 
|  | 46 | +}) | 
|  | 47 | +export class ListViewSegmentedBarPageComponent implements OnInit { | 
|  | 48 | + public displayedItems: Array<DataItem> = []; | 
|  | 49 | + public items: Array<DataItem>; | 
|  | 50 | + public segmentedBarItems: SegmentedBarItem[] = this.createSegmentedBarItems(); | 
|  | 51 | + | 
|  | 52 | + @ViewChild("listViewTest", { static: false }) | 
|  | 53 | + private listViewTest?: ElementRef; | 
|  | 54 | + | 
|  | 55 | + constructor() { | 
|  | 56 | + this.items = []; | 
|  | 57 | + | 
|  | 58 | + for (let i = 0; i < 20; i++) { | 
|  | 59 | + const type = "dataItemTemplate"; | 
|  | 60 | + | 
|  | 61 | + this.items.push({ | 
|  | 62 | + id: i, | 
|  | 63 | + name: `data item ${i}`, | 
|  | 64 | + type: type, | 
|  | 65 | + }); | 
|  | 66 | + } | 
|  | 67 | + } | 
|  | 68 | + | 
|  | 69 | + public ngOnInit() { | 
|  | 70 | + this.displayedItems = this.updateItems(true); | 
|  | 71 | + } | 
|  | 72 | + | 
|  | 73 | + public onButtonPress() { | 
|  | 74 | + // tslint:disable-next-line: no-unused-expression | 
|  | 75 | + new Promise((resolve) => { | 
|  | 76 | + setTimeout(() => { | 
|  | 77 | + if (this.listViewTest) { | 
|  | 78 | + console.log("Scrolling to the top of the list..."); | 
|  | 79 | + const listView = this.listViewTest.nativeElement as ListView; | 
|  | 80 | + listView.scrollToIndex(0); | 
|  | 81 | + } | 
|  | 82 | + resolve(); | 
|  | 83 | + }, 150); | 
|  | 84 | + }); | 
|  | 85 | + | 
|  | 86 | + this.displayedItems = this.updateItems(false); | 
|  | 87 | + } | 
|  | 88 | + | 
|  | 89 | + public onSegmentedBarPress(args: EventData) { | 
|  | 90 | + if (args && args.object) { | 
|  | 91 | + const segmentBar = args.object as SegmentedBar; | 
|  | 92 | + const selectedOdd = segmentBar.selectedIndex === 0; | 
|  | 93 | + this.displayedItems = this.updateItems(selectedOdd); | 
|  | 94 | + } | 
|  | 95 | + } | 
|  | 96 | + | 
|  | 97 | + public createSegmentedBarItems() { | 
|  | 98 | + const itemOdd = new SegmentedBarItem(); | 
|  | 99 | + itemOdd.title = "Odd Items"; | 
|  | 100 | + const itemEven = new SegmentedBarItem(); | 
|  | 101 | + itemEven.title = "Even Items"; | 
|  | 102 | + return [itemOdd, itemEven]; | 
|  | 103 | + } | 
|  | 104 | + | 
|  | 105 | + public templateSelector(item: DataItem): string { | 
|  | 106 | + return item.type; | 
|  | 107 | + } | 
|  | 108 | + | 
|  | 109 | + private updateItems(odd: boolean) { | 
|  | 110 | + const items = [ | 
|  | 111 | + { | 
|  | 112 | + id: -1, | 
|  | 113 | + name: "Segmented Bar", | 
|  | 114 | + type: "segmentedBarTemplate", | 
|  | 115 | + }, | 
|  | 116 | + ...(odd | 
|  | 117 | + ? this.items.filter((item) => item.id % 2 === 1) | 
|  | 118 | + : this.items.filter((item) => item.id % 2 === 0)), | 
|  | 119 | + { | 
|  | 120 | + id: 999, | 
|  | 121 | + name: "Refresh test", | 
|  | 122 | + type: "buttonTemplate", | 
|  | 123 | + }, | 
|  | 124 | + ]; | 
|  | 125 | + return items; | 
|  | 126 | + } | 
|  | 127 | +} | 
0 commit comments