forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtab-view.ts
151 lines (125 loc) · 3.89 KB
/
tab-view.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
import {
AfterViewInit,
Directive,
ElementRef,
Input,
OnInit,
TemplateRef,
ViewContainerRef,
} from "@angular/core";
import { TabView, TabViewItem } from "tns-core-modules/ui/tab-view";
import { TextTransform } from "tns-core-modules/ui/text-base";
import { InvisibleNode } from "../element-registry";
import { rendererLog } from "../trace";
import { isBlank } from "../lang-facade";
export interface TabViewItemDef {
title?: string;
iconSource?: string;
textTransform?: TextTransform;
}
@Directive({
selector: "TabView", // tslint:disable-line:directive-selector
})
export class TabViewDirective implements AfterViewInit {
public tabView: TabView;
private _selectedIndex: number;
private viewInitialized: boolean;
@Input()
get selectedIndex(): number {
return this._selectedIndex;
}
set selectedIndex(value) {
this._selectedIndex = value;
if (this.viewInitialized) {
this.tabView.selectedIndex = this._selectedIndex;
}
}
constructor(element: ElementRef) {
this.tabView = element.nativeElement;
}
ngAfterViewInit() {
this.viewInitialized = true;
rendererLog("this._selectedIndex: " + this._selectedIndex);
if (!isBlank(this._selectedIndex)) {
this.tabView.selectedIndex = this._selectedIndex;
}
}
}
@Directive({
selector: "[tabItem]" // tslint:disable-line:directive-selector
})
export class TabViewItemDirective implements OnInit {
private item: TabViewItem;
private _config: TabViewItemDef;
constructor(
private owner: TabViewDirective,
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef
) {
}
@Input("tabItem")
set config(config: TabViewItemDef) {
if (!this._config
|| this._config.iconSource !== config.iconSource
|| this._config.title !== config.title
|| this._config.textTransform !== config.textTransform) {
this._config = config;
this.applyConfig();
}
}
get config(): TabViewItemDef { // tslint:disable-line:no-input-rename
return this._config || {};
}
@Input()
set title(title: string) {
this.config = Object.assign(this.config, { title });
}
get title() {
return this.config.title;
}
@Input()
set iconSource(iconSource: string) {
this.config = Object.assign(this.config, { iconSource });
}
get iconSource() {
return this.config.iconSource;
}
@Input()
set textTransform(textTransform: TextTransform) {
this.config = Object.assign(this.config, { textTransform });
}
get textTransform() {
return this.config.textTransform;
}
private ensureItem() {
if (!this.item) {
this.item = new TabViewItem();
}
}
private applyConfig() {
this.ensureItem();
if (this.config.title) {
this.item.title = this.config.title;
}
if (this.config.iconSource) {
this.item.iconSource = this.config.iconSource;
}
// TabViewItem textTransform has a default value for Android that kick in
// only if no value (even a null value) is set.
if (this.config.textTransform) {
this.item.textTransform = this.config.textTransform;
}
}
ngOnInit() {
this.applyConfig();
const viewRef = this.viewContainer.createEmbeddedView(this.templateRef);
// Filter out text nodes and comments
const realViews = viewRef.rootNodes.filter(node =>
!(node instanceof InvisibleNode));
if (realViews.length > 0) {
this.item.view = realViews[0];
const newItems = (this.owner.tabView.items || []).concat([this.item]);
this.owner.tabView.items = newItems;
}
}
}