UNPKG

26 kBTypeScriptView Raw
1import { EventEmitter } from '@angular/core';
2import { Config } from '../config/config';
3import { NavOptions, TransitionDoneFn } from './nav-util';
4import { Page } from './nav-util';
5import { ViewController } from './view-controller';
6import { NavigationContainer } from './navigation-container';
7/**
8 * @name NavController
9 * @description
10 *
11 * NavController is the base class for navigation controller components like
12 * [`Nav`](../../components/nav/Nav/) and [`Tab`](../../components/tabs/Tab/). You use navigation controllers
13 * to navigate to [pages](#view-creation) in your app. At a basic level, a
14 * navigation controller is an array of pages representing a particular history
15 * (of a Tab for example). This array can be manipulated to navigate throughout
16 * an app by pushing and popping pages or inserting and removing them at
17 * arbitrary locations in history.
18 *
19 * The current page is the last one in the array, or the top of the stack if we
20 * think of it that way. [Pushing](#push) a new page onto the top of the
21 * navigation stack causes the new page to be animated in, while [popping](#pop)
22 * the current page will navigate to the previous page in the stack.
23 *
24 * Unless you are using a directive like [NavPush](../../components/nav/NavPush/), or need a
25 * specific NavController, most times you will inject and use a reference to the
26 * nearest NavController to manipulate the navigation stack.
27 *
28 * ## Basic usage
29 * The simplest way to navigate through an app is to create and initialize a new
30 * nav controller using the `<ion-nav>` component. `ion-nav` extends the `NavController`
31 * class.
32 *
33 * ```typescript
34 * import { Component } from `@angular/core`;
35 * import { StartPage } from './start-page';
36 *
37 * @Component(
38 * template: `<ion-nav [root]="rootPage"></ion-nav>`
39 * })
40 * class MyApp {
41 * // set the rootPage to the first page we want displayed
42 * public rootPage: any = StartPage;
43 *
44 * constructor(){
45 * }
46 * }
47 *
48 * ```
49 *
50 * ### Injecting NavController
51 * Injecting NavController will always get you an instance of the nearest
52 * NavController, regardless of whether it is a Tab or a Nav.
53 *
54 * Behind the scenes, when Ionic instantiates a new NavController, it creates an
55 * injector with NavController bound to that instance (usually either a Nav or
56 * Tab) and adds the injector to its own providers. For more information on
57 * providers and dependency injection, see [Dependency Injection](https://angular.io/docs/ts/latest/guide/dependency-injection.html).
58 *
59 * Instead, you can inject NavController and know that it is the correct
60 * navigation controller for most situations (for more advanced situations, see
61 * [Menu](../../menu/Menu/) and [Tab](../../tab/Tab/)).
62 *
63 * ```ts
64 * import { NavController } from 'ionic-angular';
65 *
66 * class MyComponent {
67 * constructor(public navCtrl: NavController) {
68 *
69 * }
70 * }
71 * ```
72 *
73 * ### Navigating from the Root component
74 * What if you want to control navigation from your root app component?
75 * You can't inject `NavController` because any components that are navigation
76 * controllers are _children_ of the root component so they aren't available
77 * to be injected.
78 *
79 * By adding a reference variable to the `ion-nav`, you can use `@ViewChild` to
80 * get an instance of the `Nav` component, which is a navigation controller
81 * (it extends `NavController`):
82 *
83 * ```typescript
84 *
85 * import { Component, ViewChild } from '@angular/core';
86 * import { NavController } from 'ionic-angular';
87 *
88 * @Component({
89 * template: '<ion-nav #myNav [root]="rootPage"></ion-nav>'
90 * })
91 * export class MyApp {
92 * @ViewChild('myNav') nav: NavController
93 * public rootPage: any = TabsPage;
94 *
95 * // Wait for the components in MyApp's template to be initialized
96 * // In this case, we are waiting for the Nav with reference variable of "#myNav"
97 * ngOnInit() {
98 * // Let's navigate from TabsPage to Page1
99 * this.nav.push(Page1);
100 * }
101 * }
102 * ```
103 *
104 * ### Navigating from an Overlay Component
105 * What if you wanted to navigate from an overlay component (popover, modal, alert, etc)?
106 * In this example, we've displayed a popover in our app. From the popover, we'll get a
107 * reference of the root `NavController` in our app, using the `getRootNav()` method.
108 *
109 *
110 * ```typescript
111 * import { Component } from '@angular/core';
112 * import { App, ViewController } from 'ionic-angular';
113 *
114 * @Component({
115 * template: `
116 * <ion-content>
117 * <h1>My PopoverPage</h1>
118 * <button ion-button (click)="pushPage()">Call pushPage</button>
119 * </ion-content>
120 * `
121 * })
122 * class PopoverPage {
123 * constructor(
124 * public viewCtrl: ViewController
125 * public appCtrl: App
126 * ) {}
127 *
128 * pushPage() {
129 * this.viewCtrl.dismiss();
130 * this.appCtrl.getRootNav().push(SecondPage);
131 * }
132 * }
133 *```
134 *
135 *
136 * ## View creation
137 * Views are created when they are added to the navigation stack. For methods
138 * like [push()](#push), the NavController takes any component class that is
139 * decorated with `@Component` as its first argument. The NavController then
140 * compiles that component, adds it to the app and animates it into view.
141 *
142 * By default, pages are cached and left in the DOM if they are navigated away
143 * from but still in the navigation stack (the exiting page on a `push()` for
144 * example). They are destroyed when removed from the navigation stack (on
145 * [pop()](#pop) or [setRoot()](#setRoot)).
146 *
147 * ## Pushing a View
148 * To push a new view onto the navigation stack, use the `push` method.
149 * If the page has an [`<ion-navbar>`](../../navbar/Navbar/),
150 * a back button will automatically be added to the pushed view.
151 *
152 * Data can also be passed to a view by passing an object to the `push` method.
153 * The pushed view can then receive the data by accessing it via the `NavParams`
154 * class.
155 *
156 * ```typescript
157 * import { Component } from '@angular/core';
158 * import { NavController } from 'ionic-angular';
159 * import { OtherPage } from './other-page';
160 * @Component({
161 * template: `
162 * <ion-header>
163 * <ion-navbar>
164 * <ion-title>Login</ion-title>
165 * </ion-navbar>
166 * </ion-header>
167 *
168 * <ion-content>
169 * <button ion-button (click)="pushPage()">
170 * Go to OtherPage
171 * </button>
172 * </ion-content>
173 * `
174 * })
175 * export class StartPage {
176 * constructor(public navCtrl: NavController) {
177 * }
178 *
179 * pushPage(){
180 * // push another page onto the navigation stack
181 * // causing the nav controller to transition to the new page
182 * // optional data can also be passed to the pushed page.
183 * this.navCtrl.push(OtherPage, {
184 * id: "123",
185 * name: "Carl"
186 * });
187 * }
188 * }
189 *
190 * import { NavParams } from 'ionic-angular';
191 *
192 * @Component({
193 * template: `
194 * <ion-header>
195 * <ion-navbar>
196 * <ion-title>Other Page</ion-title>
197 * </ion-navbar>
198 * </ion-header>
199 * <ion-content>I'm the other page!</ion-content>`
200 * })
201 * class OtherPage {
202 * constructor(private navParams: NavParams) {
203 * let id = navParams.get('id');
204 * let name = navParams.get('name');
205 * }
206 * }
207 * ```
208 *
209 * ## Removing a view
210 * To remove a view from the stack, use the `pop` method.
211 * Popping a view will transition to the previous view.
212 *
213 * ```ts
214 * import { Component } from '@angular/core';
215 * import { NavController } from 'ionic-angular';
216 *
217 * @Component({
218 * template: `
219 * <ion-header>
220 * <ion-navbar>
221 * <ion-title>Other Page</ion-title>
222 * </ion-navbar>
223 * </ion-header>
224 * <ion-content>I'm the other page!</ion-content>`
225 * })
226 * class OtherPage {
227 * constructor(public navCtrl: NavController ){
228 * }
229 *
230 * popView(){
231 * this.navCtrl.pop();
232 * }
233 * }
234 * ```
235 *
236 * ## Lifecycle events
237 * Lifecycle events are fired during various stages of navigation. They can be
238 * defined in any component type which is pushed/popped from a `NavController`.
239 *
240 * ```ts
241 * import { Component } from '@angular/core';
242 *
243 * @Component({
244 * template: 'Hello World'
245 * })
246 * class HelloWorld {
247 * ionViewDidLoad() {
248 * console.log("I'm alive!");
249 * }
250 * ionViewWillLeave() {
251 * console.log("Looks like I'm about to leave :(");
252 * }
253 * }
254 * ```
255 *
256 * | Page Event | Returns | Description |
257 * |---------------------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
258 * | `ionViewDidLoad` | void | Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The `ionViewDidLoad` event is good place to put your setup code for the page. |
259 * | `ionViewWillEnter` | void | Runs when the page is about to enter and become the active page. |
260 * | `ionViewDidEnter` | void | Runs when the page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page. |
261 * | `ionViewWillLeave` | void | Runs when the page is about to leave and no longer be the active page. |
262 * | `ionViewDidLeave` | void | Runs when the page has finished leaving and is no longer the active page. |
263 * | `ionViewWillUnload` | void | Runs when the page is about to be destroyed and have its elements removed. |
264 * | `ionViewCanEnter` | boolean/Promise&lt;void&gt; | Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter |
265 * | `ionViewCanLeave` | boolean/Promise&lt;void&gt; | Runs before the view can leave. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can leave |
266 *
267 *
268 * ## Nav Guards
269 *
270 * In some cases, a developer should be able to control views leaving and entering. To allow for this, NavController has the `ionViewCanEnter` and `ionViewCanLeave` methods.
271 * Similar to Angular route guards, but are more integrated with NavController. For example, if you wanted to prevent a user from leaving a view:
272 *
273 * ```ts
274 * export class MyClass{
275 * constructor(
276 * public navCtrl: NavController
277 * ){}
278 *
279 * pushPage(){
280 * this.navCtrl.push(DetailPage);
281 * }
282 *
283 * ionViewCanLeave(): boolean{
284 * // here we can either return true or false
285 * // depending on if we want to leave this view
286 * if(isValid(randomValue)){
287 * return true;
288 * } else {
289 * return false;
290 * }
291 * }
292 * }
293 * ```
294 *
295 * We need to make sure that our `navCtrl.push` has a catch in order to catch the and handle the error.
296 * If you need to prevent a view from entering, you can do the same thing
297 *
298 * ```ts
299 * export class MyClass{
300 * constructor(
301 * public navCtrl: NavController
302 * ){}
303 *
304 * pushPage(){
305 * this.navCtrl.push(DetailPage);
306 * }
307 *
308 * }
309 *
310 * export class DetailPage(){
311 * constructor(
312 * public navCtrl: NavController
313 * ){}
314 * ionViewCanEnter(): boolean{
315 * // here we can either return true or false
316 * // depending on if we want to leave this view
317 * if(isValid(randomValue)){
318 * return true;
319 * } else {
320 * return false;
321 * }
322 * }
323 * }
324 * ```
325 *
326 * Similar to `ionViewCanLeave` we still need a catch on the original `navCtrl.push` in order to handle it properly.
327 * When handling the back button in the `ion-navbar`, the catch is already taken care of for you by the framework.
328 *
329 * ## NavOptions
330 *
331 * Some methods on `NavController` allow for customizing the current transition.
332 * To do this, we can pass an object with the modified properites.
333 *
334 *
335 * | Property | Value | Description |
336 * |-----------|-----------|------------------------------------------------------------------------------------------------------------|
337 * | animate | `boolean` | Whether or not the transition should animate. |
338 * | animation | `string` | What kind of animation should be used. |
339 * | direction | `string` | The conceptual direction the user is navigating. For example, is the user navigating `forward`, or `back`? |
340 * | duration | `number` | The length in milliseconds the animation should take. |
341 * | easing | `string` | The easing for the animation. |
342 *
343 * The property 'animation' understands the following values: `md-transition`, `ios-transition` and `wp-transition`.
344 *
345 * @see {@link /docs/components#navigation Navigation Component Docs}
346 */
347export declare abstract class NavController implements NavigationContainer {
348 /**
349 * Observable to be subscribed to when a component is loaded.
350 * @returns {Observable} Returns an observable
351 */
352 viewDidLoad: EventEmitter<any>;
353 /**
354 * Observable to be subscribed to when a component is about to be loaded.
355 * @returns {Observable} Returns an observable
356 */
357 viewWillEnter: EventEmitter<any>;
358 /**
359 * Observable to be subscribed to when a component has fully become the active component.
360 * @returns {Observable} Returns an observable
361 */
362 viewDidEnter: EventEmitter<any>;
363 /**
364 * Observable to be subscribed to when a component is about to leave, and no longer active.
365 * @returns {Observable} Returns an observable
366 */
367 viewWillLeave: EventEmitter<any>;
368 /**
369 * Observable to be subscribed to when a component has fully left and is no longer active.
370 * @returns {Observable} Returns an observable
371 */
372 viewDidLeave: EventEmitter<any>;
373 /**
374 * Observable to be subscribed to when a component is about to be unloaded and destroyed.
375 * @returns {Observable} Returns an observable
376 */
377 viewWillUnload: EventEmitter<any>;
378 /**
379 * @hidden
380 */
381 id: string;
382 /**
383 * @hidden
384 */
385 name: string;
386 /**
387 * The parent navigation instance. If this is the root nav, then
388 * it'll be `null`. A `Tab` instance's parent is `Tabs`, otherwise
389 * the parent would be another nav, if it's not already the root nav.
390 */
391 parent: any;
392 /**
393 * @hidden
394 */
395 config: Config;
396 /**
397 * @input {boolean} If true, swipe to go back is enabled.
398 */
399 swipeBackEnabled: boolean;
400 /**
401 * Push a new component onto the current navigation stack. Pass any aditional information
402 * along as an object. This additional information is accessible through NavParams
403 *
404 * @param {Page|string} page The component class or deeplink name you want to push onto the navigation stack.
405 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
406 * @param {object} [opts={}] Nav options to go with this transition.
407 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
408 */
409 abstract push(page: Page | string, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
410 /**
411 * Inserts a component into the nav stack at the specified index. This is useful if
412 * you need to add a component at any point in your navigation stack.
413 *
414 *
415 * @param {number} insertIndex The index where to insert the page.
416 * @param {Page|string} page The component class or deeplink name you want to push onto the navigation stack.
417 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
418 * @param {object} [opts={}] Nav options to go with this transition.
419 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
420 */
421 abstract insert(insertIndex: number, page: Page | string, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
422 /**
423 * Inserts an array of components into the nav stack at the specified index.
424 * The last component in the array will become instantiated as a view,
425 * and animate in to become the active view.
426 *
427 * @param {number} insertIndex The index where you want to insert the page.
428 * @param {array} insertPages An array of objects, each with a `page` and optionally `params` property.
429 * @param {object} [opts={}] Nav options to go with this transition.
430 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
431 */
432 abstract insertPages(insertIndex: number, insertPages: Array<{
433 page: Page | string;
434 params?: any;
435 }>, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
436 /**
437 * Call to navigate back from a current component. Similar to `push()`, you
438 * can also pass navigation options.
439 *
440 * @param {object} [opts={}] Nav options to go with this transition.
441 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
442 */
443 abstract pop(opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
444 /**
445 * Navigate back to the root of the stack, no matter how far back that is.
446 *
447 * @param {object} [opts={}] Nav options to go with this transition.
448 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
449 */
450 abstract popToRoot(opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
451 /**
452 * @hidden
453 * Pop to a specific view in the history stack. If an already created
454 * instance of the page is not found in the stack, then it'll `setRoot`
455 * to the nav stack by removing all current pages and pushing on a
456 * new instance of the given page. Note that any params passed to
457 * this method are not used when an existing page instance has already
458 * been found in the stack. Nav params are only used by this method
459 * when a new instance needs to be created.
460 *
461 * @param {Page|string|ViewController} page The component class or deeplink name you want to push onto the navigation stack.
462 * @param {object} [opts={}] Nav options to go with this transition.
463 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
464 */
465 abstract popTo(page: Page | string | ViewController, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
466 /**
467 * @hidden
468 * Pop sequently all the pages in the stack.
469 *
470 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
471 */
472 abstract popAll(): Promise<any[]>;
473 /**
474 * Removes a page from the nav stack at the specified index.
475 *
476 * @param {number} startIndex The starting index to remove pages from the stack. Default is the index of the last page.
477 * @param {number} [removeCount] The number of pages to remove, defaults to remove `1`.
478 * @param {object} [opts={}] Any options you want to use pass to transtion.
479 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
480 */
481 abstract remove(startIndex: number, removeCount?: number, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
482 /**
483 * Removes the specified view controller from the nav stack.
484 *
485 * @param {ViewController} [viewController] The viewcontroller to remove.
486 * @param {object} [opts={}] Any options you want to use pass to transtion.
487 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
488 */
489 abstract removeView(viewController: ViewController, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
490 /**
491 * Set the root for the current navigation stack.
492 * @param {Page|string|ViewController} pageOrViewCtrl The name of the component you want to push on the navigation stack.
493 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
494 * @param {object} [opts={}] Any options you want to use pass to transtion.
495 * @param {Function} done Callback function on done.
496 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
497 */
498 abstract setRoot(pageOrViewCtrl: Page | string | ViewController, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
499 abstract goToRoot(options: NavOptions): Promise<any>;
500 /**
501 * Set the views of the current navigation stack and navigate to the
502 * last view. By default animations are disabled, but they can be enabled
503 * by passing options to the navigation controller.You can also pass any
504 * navigation params to the individual pages in the array.
505 *
506 * @param {Array<{page:any, params: any}>} pages An array of objects, each with a `page` and optionally `params` property to load in the stack.
507 * @param {Object} [opts={}] Nav options to go with this transition.
508 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
509 */
510 abstract setPages(pages: ({
511 page: Page | string;
512 params?: any;
513 } | ViewController)[], opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
514 /**
515 * @param {number} index The index of the page to get.
516 * @returns {ViewController} Returns the view controller that matches the given index.
517 */
518 abstract getByIndex(index: number): ViewController;
519 /**
520 * @returns {ViewController} Returns the active page's view controller.
521 */
522 abstract getActive(includeEntering?: boolean): ViewController;
523 /**
524 * Returns if the given view is the active view or not.
525 * @param {ViewController} view
526 * @returns {boolean}
527 */
528 abstract isActive(view: ViewController): boolean;
529 /**
530 * Returns the view controller which is before the given view controller.
531 * If no view controller is passed in, then it'll default to the active view.
532 * @param {ViewController} view
533 * @returns {viewController}
534 */
535 abstract getPrevious(view?: ViewController): ViewController;
536 /**
537 * Returns the first view controller in this nav controller's stack.
538 * @returns {ViewController}
539 */
540 abstract first(): ViewController;
541 /**
542 * Returns the last page in this nav controller's stack.
543 * @returns {ViewController}
544 */
545 abstract last(): ViewController;
546 /**
547 * Returns the index number of the given view controller.
548 * @param {ViewController} view
549 * @returns {number}
550 */
551 abstract indexOf(view: ViewController): number;
552 /**
553 * Returns the number of views in this nav controller.
554 * @returns {number} The number of views in this stack, including the current view.
555 */
556 abstract length(): number;
557 /**
558 * Returns the current stack of views in this nav controller.
559 * @returns {Array<ViewController>} the stack of view controllers in this nav controller.
560 */
561 abstract getViews(): Array<ViewController>;
562 /**
563 * Returns a list of the active child navigation.
564 */
565 abstract getActiveChildNavs(): any[];
566 /**
567 * Returns the active child navigation.
568 */
569 abstract getActiveChildNav(): any;
570 /**
571 * Returns a list of all child navigation containers
572 */
573 abstract getAllChildNavs(): any[];
574 /**
575 * Returns if the nav controller is actively transitioning or not.
576 * @return {boolean}
577 */
578 abstract isTransitioning(includeAncestors?: boolean): boolean;
579 /**
580 * If it's possible to use swipe back or not. If it's not possible
581 * to go back, or swipe back is not enabled, then this will return `false`.
582 * If it is possible to go back, and swipe back is enabled, then this
583 * will return `true`.
584 * @returns {boolean}
585 */
586 abstract canSwipeBack(): boolean;
587 /**
588 * Returns `true` if there's a valid previous page that we can pop
589 * back to. Otherwise returns `false`.
590 * @returns {boolean}
591 */
592 abstract canGoBack(): boolean;
593 /**
594 * @hidden
595 */
596 abstract registerChildNav(nav: any): void;
597 /**
598 * @hidden
599 */
600 abstract resize(): void;
601 abstract getType(): string;
602 abstract getSecondaryIdentifier(): string;
603}