UNPKG

26.1 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>`](../../components/toolbar/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 * Those events are only fired on IonicPage, for classic Angular Component, use [Angular Lifecycle Hooks](https://angular.io/guide/lifecycle-hooks).
268 *
269 * ## Nav Guards
270 *
271 * 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.
272 * Similar to Angular route guards, but are more integrated with NavController. For example, if you wanted to prevent a user from leaving a view:
273 *
274 * ```ts
275 * export class MyClass{
276 * constructor(
277 * public navCtrl: NavController
278 * ){}
279 *
280 * pushPage(){
281 * this.navCtrl.push(DetailPage);
282 * }
283 *
284 * ionViewCanLeave(): boolean{
285 * // here we can either return true or false
286 * // depending on if we want to leave this view
287 * if(isValid(randomValue)){
288 * return true;
289 * } else {
290 * return false;
291 * }
292 * }
293 * }
294 * ```
295 *
296 * We need to make sure that our `navCtrl.push` has a catch in order to catch the and handle the error.
297 * If you need to prevent a view from entering, you can do the same thing
298 *
299 * ```ts
300 * export class MyClass{
301 * constructor(
302 * public navCtrl: NavController
303 * ){}
304 *
305 * pushPage(){
306 * this.navCtrl.push(DetailPage);
307 * }
308 *
309 * }
310 *
311 * export class DetailPage(){
312 * constructor(
313 * public navCtrl: NavController
314 * ){}
315 * ionViewCanEnter(): boolean{
316 * // here we can either return true or false
317 * // depending on if we want to enter this view
318 * if(isValid(randomValue)){
319 * return true;
320 * } else {
321 * return false;
322 * }
323 * }
324 * }
325 * ```
326 *
327 * Similar to `ionViewCanLeave` we still need a catch on the original `navCtrl.push` in order to handle it properly.
328 * When handling the back button in the `ion-navbar`, the catch is already taken care of for you by the framework.
329 *
330 * ## NavOptions
331 *
332 * Some methods on `NavController` allow for customizing the current transition.
333 * To do this, we can pass an object with the modified properites.
334 *
335 *
336 * | Property | Value | Description |
337 * |-----------|-----------|------------------------------------------------------------------------------------------------------------|
338 * | animate | `boolean` | Whether or not the transition should animate. |
339 * | animation | `string` | What kind of animation should be used. |
340 * | direction | `string` | The conceptual direction the user is navigating. For example, is the user navigating `forward`, or `back`? |
341 * | duration | `number` | The length in milliseconds the animation should take. |
342 * | easing | `string` | The easing for the animation. |
343 *
344 * The property 'animation' understands the following values: `md-transition`, `ios-transition` and `wp-transition`.
345 *
346 * @see {@link /docs/components#navigation Navigation Component Docs}
347 */
348export declare abstract class NavController implements NavigationContainer {
349 /**
350 * Observable to be subscribed to when a component is loaded.
351 * @returns {Observable} Returns an observable
352 */
353 viewDidLoad: EventEmitter<any>;
354 /**
355 * Observable to be subscribed to when a component is about to be loaded.
356 * @returns {Observable} Returns an observable
357 */
358 viewWillEnter: EventEmitter<any>;
359 /**
360 * Observable to be subscribed to when a component has fully become the active component.
361 * @returns {Observable} Returns an observable
362 */
363 viewDidEnter: EventEmitter<any>;
364 /**
365 * Observable to be subscribed to when a component is about to leave, and no longer active.
366 * @returns {Observable} Returns an observable
367 */
368 viewWillLeave: EventEmitter<any>;
369 /**
370 * Observable to be subscribed to when a component has fully left and is no longer active.
371 * @returns {Observable} Returns an observable
372 */
373 viewDidLeave: EventEmitter<any>;
374 /**
375 * Observable to be subscribed to when a component is about to be unloaded and destroyed.
376 * @returns {Observable} Returns an observable
377 */
378 viewWillUnload: EventEmitter<any>;
379 /**
380 * @hidden
381 */
382 id: string;
383 /**
384 * @hidden
385 */
386 name: string;
387 /**
388 * The parent navigation instance. If this is the root nav, then
389 * it'll be `null`. A `Tab` instance's parent is `Tabs`, otherwise
390 * the parent would be another nav, if it's not already the root nav.
391 */
392 parent: any;
393 /**
394 * @hidden
395 */
396 config: Config;
397 /**
398 * @input {boolean} If true, swipe to go back is enabled.
399 */
400 swipeBackEnabled: boolean;
401 /**
402 * Push a new component onto the current navigation stack. Pass any aditional information
403 * along as an object. This additional information is accessible through NavParams
404 *
405 * @param {Page|string} page The component class or deeplink name you want to push onto the navigation stack.
406 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
407 * @param {object} [opts={}] Nav options to go with this transition.
408 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
409 */
410 abstract push(page: Page | string, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
411 /**
412 * Inserts a component into the nav stack at the specified index. This is useful if
413 * you need to add a component at any point in your navigation stack.
414 *
415 *
416 * @param {number} insertIndex The index where to insert the page.
417 * @param {Page|string} page The component class or deeplink name you want to push onto the navigation stack.
418 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
419 * @param {object} [opts={}] Nav options to go with this transition.
420 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
421 */
422 abstract insert(insertIndex: number, page: Page | string, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
423 /**
424 * Inserts an array of components into the nav stack at the specified index.
425 * The last component in the array will become instantiated as a view,
426 * and animate in to become the active view.
427 *
428 * @param {number} insertIndex The index where you want to insert the page.
429 * @param {array} insertPages An array of objects, each with a `page` and optionally `params` property.
430 * @param {object} [opts={}] Nav options to go with this transition.
431 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
432 */
433 abstract insertPages(insertIndex: number, insertPages: Array<{
434 page: Page | string;
435 params?: any;
436 }>, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
437 /**
438 * Call to navigate back from a current component. Similar to `push()`, you
439 * can also pass navigation options.
440 *
441 * @param {object} [opts={}] Nav options to go with this transition.
442 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
443 */
444 abstract pop(opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
445 /**
446 * Navigate back to the root of the stack, no matter how far back that is.
447 *
448 * @param {object} [opts={}] Nav options to go with this transition.
449 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
450 */
451 abstract popToRoot(opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
452 /**
453 * @hidden
454 * Pop to a specific view in the history stack. If an already created
455 * instance of the page is not found in the stack, then it'll `setRoot`
456 * to the nav stack by removing all current pages and pushing on a
457 * new instance of the given page. Note that any params passed to
458 * this method are not used when an existing page instance has already
459 * been found in the stack. Nav params are only used by this method
460 * when a new instance needs to be created.
461 *
462 * @param {Page|string|ViewController} page The component class or deeplink name you want to push onto the navigation stack.
463 * @param {object} [opts={}] Nav options to go with this transition.
464 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
465 */
466 abstract popTo(page: Page | string | ViewController, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
467 /**
468 * @hidden
469 * Pop sequently all the pages in the stack.
470 *
471 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
472 */
473 abstract popAll(): Promise<any[]>;
474 /**
475 * Removes a page from the nav stack at the specified index.
476 *
477 * @param {number} startIndex The starting index to remove pages from the stack. Default is the index of the last page.
478 * @param {number} [removeCount] The number of pages to remove, defaults to remove `1`.
479 * @param {object} [opts={}] Any options you want to use pass to transtion.
480 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
481 */
482 abstract remove(startIndex: number, removeCount?: number, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
483 /**
484 * Removes the specified view controller from the nav stack.
485 *
486 * @param {ViewController} [viewController] The viewcontroller to remove.
487 * @param {object} [opts={}] Any options you want to use pass to transtion.
488 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
489 */
490 abstract removeView(viewController: ViewController, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
491 /**
492 * Set the root for the current navigation stack.
493 * @param {Page|string|ViewController} pageOrViewCtrl The name of the component you want to push on the navigation stack.
494 * @param {object} [params={}] Any NavParams you want to pass along to the next view.
495 * @param {object} [opts={}] Any options you want to use pass to transtion.
496 * @param {Function} done Callback function on done.
497 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
498 */
499 abstract setRoot(pageOrViewCtrl: Page | string | ViewController, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
500 abstract goToRoot(options: NavOptions): Promise<any>;
501 /**
502 * Set the views of the current navigation stack and navigate to the
503 * last view. By default animations are disabled, but they can be enabled
504 * by passing options to the navigation controller.You can also pass any
505 * navigation params to the individual pages in the array.
506 *
507 * @param {Array<{page:any, params: any}>} pages An array of objects, each with a `page` and optionally `params` property to load in the stack.
508 * @param {Object} [opts={}] Nav options to go with this transition.
509 * @returns {Promise} Returns a promise which is resolved when the transition has completed.
510 */
511 abstract setPages(pages: ({
512 page: Page | string;
513 params?: any;
514 } | ViewController)[], opts?: NavOptions, done?: TransitionDoneFn): Promise<any>;
515 /**
516 * @param {number} index The index of the page to get.
517 * @returns {ViewController} Returns the view controller that matches the given index.
518 */
519 abstract getByIndex(index: number): ViewController;
520 /**
521 * @returns {ViewController} Returns the active page's view controller.
522 */
523 abstract getActive(includeEntering?: boolean): ViewController;
524 /**
525 * Returns if the given view is the active view or not.
526 * @param {ViewController} view
527 * @returns {boolean}
528 */
529 abstract isActive(view: ViewController): boolean;
530 /**
531 * Returns the view controller which is before the given view controller.
532 * If no view controller is passed in, then it'll default to the active view.
533 * @param {ViewController} view
534 * @returns {viewController}
535 */
536 abstract getPrevious(view?: ViewController): ViewController;
537 /**
538 * Returns the first view controller in this nav controller's stack.
539 * @returns {ViewController}
540 */
541 abstract first(): ViewController;
542 /**
543 * Returns the last page in this nav controller's stack.
544 * @returns {ViewController}
545 */
546 abstract last(): ViewController;
547 /**
548 * Returns the index number of the given view controller.
549 * @param {ViewController} view
550 * @returns {number}
551 */
552 abstract indexOf(view: ViewController): number;
553 /**
554 * Returns the number of views in this nav controller.
555 * @returns {number} The number of views in this stack, including the current view.
556 */
557 abstract length(): number;
558 /**
559 * Returns the current stack of views in this nav controller.
560 * @returns {Array<ViewController>} the stack of view controllers in this nav controller.
561 */
562 abstract getViews(): Array<ViewController>;
563 /**
564 * Returns a list of the active child navigation.
565 */
566 abstract getActiveChildNavs(): any[];
567 /**
568 * Returns the active child navigation.
569 */
570 abstract getActiveChildNav(): any;
571 /**
572 * Returns a list of all child navigation containers
573 */
574 abstract getAllChildNavs(): any[];
575 /**
576 * Returns if the nav controller is actively transitioning or not.
577 * @return {boolean}
578 */
579 abstract isTransitioning(includeAncestors?: boolean): boolean;
580 /**
581 * If it's possible to use swipe back or not. If it's not possible
582 * to go back, or swipe back is not enabled, then this will return `false`.
583 * If it is possible to go back, and swipe back is enabled, then this
584 * will return `true`.
585 * @returns {boolean}
586 */
587 abstract canSwipeBack(): boolean;
588 /**
589 * Returns `true` if there's a valid previous page that we can pop
590 * back to. Otherwise returns `false`.
591 * @returns {boolean}
592 */
593 abstract canGoBack(): boolean;
594 /**
595 * @hidden
596 */
597 abstract registerChildNav(nav: any): void;
598 /**
599 * @hidden
600 */
601 abstract resize(): void;
602 abstract getType(): string;
603 abstract getSecondaryIdentifier(): string;
604}