forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu-toggle.ts
More file actions
143 lines (134 loc) · 3.49 KB
/
menu-toggle.ts
File metadata and controls
143 lines (134 loc) · 3.49 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
import { Directive, ElementRef, Input, HostListener, Optional } from '@angular/core';
import { MenuController } from './menu-controller';
import { Navbar } from '../navbar/navbar';
import { ViewController } from '../../navigation/view-controller';
/**
* @name MenuToggle
* @description
* The `menuToggle` directive can be placed on any button to toggle a menu open or closed.
* If it is added to the [NavBar](../../nav/NavBar) of a page, the button will only appear
* when the page it's in is currently a root page. See the [Menu Navigation Bar Behavior](../Menu#navigation-bar-behavior)
* docs for more information.
*
*
* @usage
*
* A simple `menuToggle` button can be added using the following markup:
*
* ```html
* <button ion-button menuToggle>Toggle Menu</button>
* ```
*
* To toggle a specific menu by its id or side, give the `menuToggle`
* directive a value.
*
* ```html
* <button ion-button menuToggle="right">Toggle Right Menu</button>
* ```
*
* If placing the `menuToggle` in a navbar or toolbar, it should be
* placed as a child of the `<ion-navbar>` or `<ion-toolbar>`, and not in
* the `<ion-buttons>` element:
*
* ```html
* <ion-header>
*
* <ion-navbar>
* <ion-buttons start>
* <button ion-button>
* <ion-icon name="contact"></ion-icon>
* </button>
* </ion-buttons>
* <button ion-button menuToggle>
* <ion-icon name="menu"></ion-icon>
* </button>
* <ion-title>
* Title
* </ion-title>
* <ion-buttons end>
* <button ion-button (click)="doClick()">
* <ion-icon name="more"></ion-icon>
* </button>
* </ion-buttons>
* </ion-navbar>
*
* </ion-header>
* ```
*
* Similar to `<ion-buttons>`, the `menuToggle` can be positioned using
* `start`, `end`, `left`, or `right`:
*
* ```html
* <ion-toolbar>
* <button ion-button menuToggle right>
* <ion-icon name="menu"></ion-icon>
* </button>
* <ion-title>
* Title
* </ion-title>
* <ion-buttons end>
* <button ion-button (click)="doClick()">
* <ion-icon name="more"></ion-icon>
* </button>
* </ion-buttons>
* </ion-toolbar>
* ```
*
* See the [Toolbar API docs](../../toolbar/Toolbar) for more information
* on the different positions.
*
* @demo /docs/v2/demos/menu/
* @see {@link /docs/v2/components#menus Menu Component Docs}
* @see {@link ../../menu/Menu Menu API Docs}
*/
@Directive({
selector: '[menuToggle]',
host: {
'[hidden]': 'isHidden',
'menuToggle': '' // ensures the attr is there for css when using [menuToggle]
}
})
export class MenuToggle {
/**
* @private
*/
@Input() menuToggle: string;
/**
* @private
*/
private _inNavbar: boolean;
constructor(
private _menu: MenuController,
elementRef: ElementRef,
@Optional() private _viewCtrl: ViewController,
@Optional() private _navbar: Navbar
) {
this._inNavbar = !!_navbar;
}
/**
* @private
*/
@HostListener('click')
toggle() {
let menu = this._menu.get(this.menuToggle);
menu && menu.toggle();
}
/**
* @private
*/
get isHidden() {
if (this._inNavbar && this._viewCtrl) {
if (this._viewCtrl.isFirst()) {
// this is the first view, so it should always show
return false;
}
let menu = this._menu.get(this.menuToggle);
if (menu) {
// this is not the root view, so see if this menu
// is configured to still be enabled if it's not the root view
return !menu.persistent;
}
}
return false;
}
}