-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
nav-pop.ts
90 lines (75 loc) · 1.9 KB
/
nav-pop.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
import { AfterContentInit, Directive, HostListener, Optional } from '@angular/core';
import { DeepLinker } from '../../navigation/deep-linker';
import { NavController } from '../../navigation/nav-controller';
import { ViewController } from '../../navigation/view-controller';
/**
* @name NavPop
* @description
* Directive to declaratively pop the current page off from the
* navigation stack.
*
* @usage
* ```html
* <ion-content>
*
* <button ion-button navPop>Go Back</button>
*
* </ion-content>
* ```
*
* Similar to {@link /docs/v2/api/components/nav/NavPush/ `NavPush` }
* @demo /docs/v2/demos/src/navigation/
* @see {@link /docs/v2/components#navigation Navigation Component Docs}
* @see {@link ../NavPush NavPush API Docs}
*/
@Directive({
selector: '[navPop]'
})
export class NavPop {
constructor(@Optional() public _nav: NavController) {
if (!_nav) {
console.error('navPop must be within a NavController');
}
}
/**
* @private
*/
@HostListener('click')
onClick(): boolean {
// If no target, or if target is _self, prevent default browser behavior
if (this._nav) {
this._nav.pop().catch(() => {
console.debug('navPop was rejected');
});
return false;
}
return true;
}
}
/**
* @private
*/
@Directive({
selector: 'a[navPop]',
host: {
'[attr.href]': '_href'
}
})
export class NavPopAnchor implements AfterContentInit {
_href: string;
constructor(
@Optional() public host: NavPop,
public linker: DeepLinker,
@Optional() public viewCtrl: ViewController) {}
updateHref() {
if (this.host && this.viewCtrl) {
const previousView = this.host._nav.getPrevious(this.viewCtrl);
this._href = (previousView && this.linker.createUrl(this.host._nav, this.viewCtrl.component, this.viewCtrl.data)) || '#';
} else {
this._href = '#';
}
}
ngAfterContentInit() {
this.updateHref();
}
}