-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
tab-navigation-base.ts
243 lines (198 loc) · 6.96 KB
/
tab-navigation-base.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
// Types
import { TabNavigationBase as TabNavigationBaseDefinition, SelectedIndexChangedEventData } from ".";
import { TabContentItem } from "../tab-content-item";
import { TabStrip } from "../tab-strip";
import { TabStripItem } from "../tab-strip-item";
import { ViewBase, AddArrayFromBuilder, AddChildFromBuilder, EventData } from "../../core/view";
// Requires
import { View, Property, CoercibleProperty, isIOS } from "../../core/view";
// TODO: Impl trace
// export const traceCategory = "TabView";
export module knownCollections {
export const items = "items";
}
export class TabNavigationBase extends View implements TabNavigationBaseDefinition, AddChildFromBuilder, AddArrayFromBuilder {
public static selectedIndexChangedEvent = "selectedIndexChanged";
public items: TabContentItem[];
public tabStrip: TabStrip;
public selectedIndex: number;
public _addArrayFromBuilder(name: string, value: Array<any>) {
if (name === "items") {
this.items = value;
}
}
public _addChildFromBuilder(name: string, value: any): void {
if (name === "TabContentItem") {
if (!this.items) {
this.items = new Array<TabContentItem>();
}
this.items.push(<TabContentItem>value);
this._addView(value);
// selectedIndexProperty.coerce(this);
} else if (name === "TabStrip") {
// Setting tabStrip will trigger onTabStripChanged
this.tabStrip = value;
}
}
get _selectedView(): View {
let selectedIndex = this.selectedIndex;
return selectedIndex > -1 ? this.items[selectedIndex].view : null;
}
get _childrenCount(): number {
const items = this.items;
return items ? items.length : 0;
}
public eachChild(callback: (child: ViewBase) => boolean) {
const items = this.items;
if (items) {
items.forEach((item, i) => {
callback(item);
});
}
const tabStrip = this.tabStrip;
if (tabStrip) {
callback(tabStrip);
}
}
public eachChildView(callback: (child: View) => boolean) {
const items = this.items;
if (items) {
items.forEach((item, i) => {
callback(item.view);
});
}
}
public onItemsChanged(oldItems: TabContentItem[], newItems: TabContentItem[]): void {
if (oldItems) {
oldItems.forEach(item => this._removeView(item));
}
if (newItems) {
newItems.forEach(item => {
if (!item.view) {
throw new Error(`TabContentItem must have a view.`);
}
this._addView(item);
});
}
}
public onTabStripChanged(oldTabStrip: TabStrip, newTabStrip: TabStrip) {
if (oldTabStrip && oldTabStrip.parent) {
this._removeView(oldTabStrip);
}
if (newTabStrip) {
this._addView(newTabStrip);
}
}
public onSelectedIndexChanged(oldIndex: number, newIndex: number): void {
// to be overridden in platform specific files
this.notify(<SelectedIndexChangedEventData>{ eventName: TabNavigationBase.selectedIndexChangedEvent, object: this, oldIndex, newIndex });
}
public getTabBarBackgroundColor(): any {
// overridden by inheritors
return null;
}
public setTabBarBackgroundColor(value: any): void {
// overridden by inheritors
}
public getTabBarFontInternal(): any {
// overridden by inheritors
return null;
}
public setTabBarFontInternal(value: any): void {
// overridden by inheritors
}
public getTabBarTextTransform(): any {
// overridden by inheritors
return null;
}
public setTabBarTextTransform(value: any): void {
// overridden by inheritors
}
public getTabBarHighlightColor(): any {
// overridden by inheritors
}
public setTabBarHighlightColor(value: any) {
// overridden by inheritors
}
public getTabBarColor(): any {
// overridden by inheritors
return null;
}
public setTabBarColor(value: any): void {
// overridden by inheritors
}
public getTabBarItemBackgroundColor(tabStripItem: TabStripItem): any {
// overridden by inheritors
return null;
}
public setTabBarItemBackgroundColor(tabStripItem: TabStripItem, value: any): void {
// overridden by inheritors
}
public getTabBarItemColor(tabStripItem: TabStripItem): any {
// overridden by inheritors
return null;
}
public setTabBarItemColor(tabStripItem: TabStripItem, value: any): void {
// overridden by inheritors
}
public getTabBarItemFontSize(tabStripItem: TabStripItem): any {
// overridden by inheritors
return null;
}
public setTabBarItemFontSize(tabStripItem: TabStripItem, value: any): void {
// overridden by inheritors
}
public getTabBarItemFontInternal(tabStripItem: TabStripItem): any {
// overridden by inheritors
return null;
}
public setTabBarItemFontInternal(tabStripItem: TabStripItem, value: any): void {
// overridden by inheritors
}
public getTabBarItemTextTransform(tabStripItem: TabStripItem): any {
// overridden by inheritors
return null;
}
public setTabBarItemTextTransform(tabStripItem: TabStripItem, value: any): void {
// overridden by inheritors
}
}
export interface TabNavigationBase {
on(eventNames: string, callback: (data: EventData) => void, thisArg?: any);
on(event: "selectedIndexChanged", callback: (args: SelectedIndexChangedEventData) => void, thisArg?: any);
}
export const selectedIndexProperty = new CoercibleProperty<TabNavigationBase, number>({
name: "selectedIndex", defaultValue: -1, affectsLayout: isIOS,
valueChanged: (target, oldValue, newValue) => {
target.onSelectedIndexChanged(oldValue, newValue);
},
coerceValue: (target, value) => {
let items = target.items;
if (items) {
let max = items.length - 1;
if (value < 0) {
value = 0;
}
if (value > max) {
value = max;
}
} else {
value = -1;
}
return value;
},
valueConverter: (v) => parseInt(v)
});
selectedIndexProperty.register(TabNavigationBase);
export const itemsProperty = new Property<TabNavigationBase, TabContentItem[]>({
name: "items", valueChanged: (target, oldValue, newValue) => {
target.onItemsChanged(oldValue, newValue);
}
});
itemsProperty.register(TabNavigationBase);
export const tabStripProperty = new Property<TabNavigationBase, TabStrip>({
name: "tabStrip", valueChanged: (target, oldValue, newValue) => {
target.onTabStripChanged(oldValue, newValue);
}
});
tabStripProperty.register(TabNavigationBase);