forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.ts
More file actions
362 lines (327 loc) · 8.41 KB
/
tab.ts
File metadata and controls
362 lines (327 loc) · 8.41 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ElementRef, EventEmitter, Input, NgZone, Optional, Output, Renderer, ViewChild, ViewEncapsulation, ViewContainerRef } from '@angular/core';
import { App } from '../app/app';
import { Config } from '../../config/config';
import { DeepLinker } from '../../navigation/deep-linker';
import { GestureController } from '../../gestures/gesture-controller';
import { isTrueProperty } from '../../util/util';
import { Keyboard } from '../../util/keyboard';
import { NavControllerBase } from '../../navigation/nav-controller-base';
import { NavOptions } from '../../navigation/nav-util';
import { TabButton } from './tab-button';
import { Tabs } from './tabs';
import { TransitionController } from '../../transitions/transition-controller';
import { ViewController } from '../../navigation/view-controller';
/**
* @name Tab
* @description
* The Tab component, written `<ion-tab>`, is styled based on the mode and should
* be used in conjunction with the [Tabs](../Tabs/) component.
*
* Each `ion-tab` is a declarative component for a [NavController](../NavController/).
* Basically, each tab is a `NavController`. For more information on using
* navigation controllers take a look at the [NavController API Docs](../../nav/NavController/).
*
* See the [Tabs API Docs](../Tabs/) for more details on configuring Tabs.
*
* @usage
*
* To add a basic tab, you can use the following markup where the `root` property
* is the page you want to load for that tab, `tabTitle` is the optional text to
* display on the tab, and `tabIcon` is the optional [icon](../../icon/Icon/).
*
* ```html
* <ion-tabs>
* <ion-tab [root]="chatRoot" tabTitle="Chat" tabIcon="chat"><ion-tab>
* </ion-tabs>
* ```
*
* Then, in your class you can set `chatRoot` to an imported class:
*
* ```ts
* import { ChatPage } from '../chat/chat';
*
* export class Tabs {
* // here we'll set the property of chatRoot to
* // the imported class of ChatPage
* chatRoot = ChatPage;
*
* constructor() {
*
* }
* }
* ```
*
* You can also pass some parameters to the root page of the tab through
* `rootParams`. Below we pass `chatParams` to the Chat tab:
*
* ```html
* <ion-tabs>
* <ion-tab [root]="chatRoot" [rootParams]="chatParams" tabTitle="Chat" tabIcon="chat"><ion-tab>
* </ion-tabs>
* ```
*
* ```ts
* export class Tabs {
* chatRoot = ChatPage;
*
* // set some user information on chatParams
* chatParams = {
* user1: "admin",
* user2: "ionic"
* };
*
* constructor() {
*
* }
* }
* ```
*
* And in `ChatPage` you can get the data from `NavParams`:
*
* ```ts
* export class ChatPage {
* constructor(navParams: NavParams) {
* console.log("Passed params", navParams.data);
* }
* }
* ```
*
* Sometimes you may want to call a method instead of navigating to a new
* page. You can use the `(ionSelect)` event to call a method on your class when
* the tab is selected. Below is an example of presenting a modal from one of
* the tabs.
*
* ```html
* <ion-tabs>
* <ion-tab (ionSelect)="chat()"></ion-tab>
* </ion-tabs>
* ```
*
* ```ts
* export class Tabs {
* constructor(public modalCtrl: ModalController) {
*
* }
*
* chat() {
* let modal = this.modalCtrl.create(ChatPage);
* modal.present();
* }
* }
* ```
*
*
* @demo /docs/v2/demos/tabs/
* @see {@link /docs/v2/components#tabs Tabs Component Docs}
* @see {@link ../../tabs/Tabs Tabs API Docs}
* @see {@link ../../nav/Nav Nav API Docs}
* @see {@link ../../nav/NavController NavController API Docs}
*/
@Component({
selector: 'ion-tab',
template:
'<div #viewport></div><div class="nav-decor"></div>',
host: {
'[attr.id]': '_tabId',
'[attr.aria-labelledby]': '_btnId',
'role': 'tabpanel'
},
encapsulation: ViewEncapsulation.None,
})
export class Tab extends NavControllerBase {
/**
* @private
*/
_isInitial: boolean;
/**
* @private
*/
_isEnabled: boolean = true;
/**
* @private
*/
_isShown: boolean = true;
/**
* @private
*/
_tabId: string;
/**
* @private
*/
_btnId: string;
/**
* @private
*/
_loaded: boolean;
/**
* @private
*/
isSelected: boolean;
/**
* @private
*/
btn: TabButton;
/**
* @input {Page} Set the root page for this tab.
*/
@Input() root: any;
/**
* @input {object} Any nav-params to pass to the root page of this tab.
*/
@Input() rootParams: any;
/**
* @input {string} The URL path name to represent this tab within the URL.
*/
@Input() tabUrlPath: string;
/**
* @input {string} The title of the tab button.
*/
@Input() tabTitle: string;
/**
* @input {string} The icon for the tab button.
*/
@Input() tabIcon: string;
/**
* @input {string} The badge for the tab button.
*/
@Input() tabBadge: string;
/**
* @input {string} The badge color for the tab button.
*/
@Input() tabBadgeStyle: string;
/**
* @input {boolean} If the tab is enabled or not. If the tab
* is not enabled then the tab button will still show, however,
* the button will appear grayed out and will not be clickable.
* Defaults to `true`.
*/
@Input()
get enabled(): boolean {
return this._isEnabled;
}
set enabled(val: boolean) {
this._isEnabled = isTrueProperty(val);
}
/**
* @input {boolean} If the tab button is visible within the
* tabbar or not. Defaults to `true`.
*/
@Input()
get show(): boolean {
return this._isShown;
}
set show(val: boolean) {
this._isShown = isTrueProperty(val);
}
/**
* @input {boolean} Whether it's possible to swipe-to-go-back on this tab or not.
*/
@Input()
get swipeBackEnabled(): boolean {
return this._sbEnabled;
}
set swipeBackEnabled(val: boolean) {
this._sbEnabled = isTrueProperty(val);
}
/**
* @output {Tab} Method to call when the current tab is selected
*/
@Output() ionSelect: EventEmitter<Tab> = new EventEmitter<Tab>();
constructor(
parent: Tabs,
app: App,
config: Config,
keyboard: Keyboard,
elementRef: ElementRef,
zone: NgZone,
renderer: Renderer,
cfr: ComponentFactoryResolver,
private _cd: ChangeDetectorRef,
gestureCtrl: GestureController,
transCtrl: TransitionController,
@Optional() private linker: DeepLinker
) {
// A Tab is a NavController for its child pages
super(parent, app, config, keyboard, elementRef, zone, renderer, cfr, gestureCtrl, transCtrl, linker);
this.id = parent.add(this);
this._tabId = 'tabpanel-' + this.id;
this._btnId = 'tab-' + this.id;
}
/**
* @private
*/
@ViewChild('viewport', {read: ViewContainerRef})
set _vp(val: ViewContainerRef) {
this.setViewport(val);
}
/**
* @private
*/
ngOnInit() {
this.tabBadgeStyle = this.tabBadgeStyle ? this.tabBadgeStyle : 'default';
}
/**
* @private
*/
load(opts: NavOptions, done?: Function) {
if (!this._loaded && this.root) {
this.push(this.root, this.rootParams, opts, done);
this._loaded = true;
} else {
done(true);
}
}
/**
* @private
*/
_viewInsert(viewCtrl: ViewController, componentRef: ComponentRef<any>, viewport: ViewContainerRef) {
const isTabSubPage = (this.parent._subPages && viewCtrl.index > 0);
if (isTabSubPage) {
viewport = this.parent.portal;
}
super._viewInsert(viewCtrl, componentRef, viewport);
if (isTabSubPage) {
// add the .tab-subpage css class to tabs pages that should act like subpages
const pageEleRef = viewCtrl.pageRef();
if (pageEleRef) {
this._renderer.setElementClass(pageEleRef.nativeElement, 'tab-subpage', true);
}
}
}
/**
* @private
*/
setSelected(isSelected: boolean) {
this.isSelected = isSelected;
this.setElementClass('show-tab', isSelected);
this.setElementAttribute('aria-hidden', (!isSelected).toString());
if (isSelected) {
// this is the selected tab, detect changes
this._cd.reattach();
} else {
// this tab is not selected, do not detect changes
this._cd.detach();
}
}
/**
* @private
*/
get index(): number {
return this.parent.getIndex(this);
}
/**
* @private
*/
updateHref(component: any, data: any) {
if (this.btn && this.linker) {
let href = this.linker.createUrl(this, component, data) || '#';
this.btn.updateHref(href);
}
}
/**
* @private
*/
destroy() {
this.destroy();
}
}