forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.ts
More file actions
625 lines (554 loc) · 17.4 KB
/
menu.ts
File metadata and controls
625 lines (554 loc) · 17.4 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import { ChangeDetectionStrategy, Component, ElementRef, EventEmitter, Input, NgZone, Output, Renderer, ViewChild, ViewEncapsulation } from '@angular/core';
import { Backdrop } from '../backdrop/backdrop';
import { Config } from '../../config/config';
import { isTrueProperty } from '../../util/util';
import { Keyboard } from '../../util/keyboard';
import { MenuContentGesture } from './menu-gestures';
import { MenuController } from './menu-controller';
import { MenuType } from './menu-types';
import { Platform } from '../../platform/platform';
import { GestureController } from '../../gestures/gesture-controller';
/**
* @name Menu
* @description
* The Menu component is a navigation drawer that slides in from the side of the current
* view. By default, it slides in from the left, but the side can be overridden. The menu
* will be displayed differently based on the mode, however the display type can be changed
* to any of the available [menu types](#menu-types). The menu element should be a sibling
* to the app's content element. There can be any number of menus attached to the content.
* These can be controlled from the templates, or programmatically using the [MenuController](../MenuController).
*
*
* ### Opening/Closing Menus
*
* There are several ways to open or close a menu. The menu can be **toggled** open or closed
* from the template using the [MenuToggle](../MenuToggle) directive. It can also be
* **closed** from the template using the [MenuClose](../MenuClose) directive. To display a menu
* programmatically, inject the [MenuController](../MenuController) provider and call any of the
* `MenuController` methods.
*
*
* ### Menu Types
*
* The menu supports several display types: `overlay`, `reveal` and `push`. By default,
* it will use the correct type based on the mode, but this can be changed. The default
* type for both Material Design and Windows mode is `overlay`, and `reveal` is the default
* type for iOS mode. The menu type can be changed in the app's [config](../../config/Config)
* via the `menuType` property, or passed in the `type` property on the `<ion-menu>` element.
* See [usage](#usage) below for examples of changing the menu type.
*
*
* ### Navigation Bar Behavior
*
* If a [MenuToggle](../MenuToggle) button 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. The
* root page is the initial page loaded in the app, or a page that has been set as the root
* using the [setRoot](../../nav/NavController/#setRoot) method on the [NavController](../../nav/NavController).
*
* For example, say the application has two pages, `Page1` and `Page2`, and both have a
* `MenuToggle` button in their navigation bars. Assume the initial page loaded into the app
* is `Page1`, making it the root page. `Page1` will display the `MenuToggle` button, but once
* `Page2` is pushed onto the navigation stack, the `MenuToggle` will not be displayed.
*
*
* ### Persistent Menus
*
* Persistent menus display the [MenuToggle](../MenuToggle) button in the [NavBar](../../nav/NavBar)
* on all pages in the navigation stack. To make a menu persistent set `persistent` to `true` on the
* `<ion-menu>` element. Note that this will only affect the `MenuToggle` button in the `NavBar` attached
* to the `Menu` with `persistent` set to true, any other `MenuToggle` buttons will not be affected.
*
*
* @usage
*
* To add a menu to an application, the `<ion-menu>` element should be added as a sibling to
* the content it belongs to. A [local variable](https://angular.io/docs/ts/latest/guide/user-input.html#local-variables)
* should be added to the content element and passed to the menu element in the `content` property.
* This tells the menu which content it is attached to, so it knows which element to watch for
* gestures. In the below example, `content` is using [property binding](https://angular.io/docs/ts/latest/guide/template-syntax.html#!#property-binding)
* because `mycontent` is a reference to the `<ion-nav>` element, and not a string.
*
* ```html
* <ion-menu [content]="mycontent">
* <ion-content>
* <ion-list>
* ...
* </ion-list>
* </ion-content>
* </ion-menu>
*
* <ion-nav #mycontent [root]="rootPage"></ion-nav>
* ```
*
* ### Menu Side
*
* By default, menus slide in from the left, but this can be overridden by passing `right`
* to the `side` property:
*
* ```html
* <ion-menu side="right" [content]="mycontent">...</ion-menu>
* ```
*
*
* ### Menu Type
*
* The menu type can be changed by passing the value to `type` on the `<ion-menu>`:
*
* ```html
* <ion-menu type="overlay" [content]="mycontent">...</ion-menu>
* ```
*
* It can also be set in the app's config. The below will set the menu type to
* `push` for all modes, and then set the type to `overlay` for the `ios` mode.
*
* ```ts
* import { ionicBootstrap } from 'ionic-angular';
*
* ionicBootstrap(MyApp, customProviders, {
* menuType: 'push',
* platforms: {
* ios: {
* menuType: 'overlay',
* }
* }
* });
* ```
*
*
* ### Displaying the Menu
*
* To toggle a menu from the template, add a button with the `menuToggle`
* directive anywhere in the page's template:
*
* ```html
* <button ion-button menuToggle>Toggle Menu</button>
* ```
*
* To close a menu, add the `menuClose` button. It can be added anywhere
* in the content, or even the menu itself. Below it is added to the menu's
* content:
*
* ```html
* <ion-menu [content]="mycontent">
* <ion-content>
* <ion-list>
* <button ion-button menuClose ion-item detail-none>Close Menu</button>
* </ion-list>
* </ion-content>
* </ion-menu>
* ```
*
* See the [MenuToggle](../MenuToggle) and [MenuClose](../MenuClose) docs
* for more information on these directives.
*
* The menu can also be controlled from the Page by using the `MenuController`.
* Inject the `MenuController` provider into the page and then call any of its
* methods. In the below example, the `openMenu` method will open the menu
* when it is called.
*
* ```ts
* import { Component } from '@angular/core';
* import { MenuController } from 'ionic-angular';
*
* @Component({...})
* export class MyPage {
* constructor(public menuCtrl: MenuController) {}
*
* openMenu() {
* this.menuCtrl.open();
* }
* }
* ```
*
* See the [MenuController](../MenuController) API docs for all of the methods
* and usage information.
*
*
* @demo /docs/v2/demos/src/menu/
*
* @see {@link /docs/v2/components#menus Menu Component Docs}
* @see {@link ../MenuController MenuController API Docs}
* @see {@link ../../nav/Nav Nav API Docs}
* @see {@link ../../nav/NavController NavController API Docs}
*/
@Component({
selector: 'ion-menu',
template:
'<div class="menu-inner"><ng-content></ng-content></div>' +
'<ion-backdrop (click)="bdClick($event)" disableScroll="false"></ion-backdrop>',
host: {
'role': 'navigation'
},
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class Menu {
private _cntEle: HTMLElement;
private _cntGesture: MenuContentGesture;
private _type: MenuType;
private _resizeUnreg: Function;
private _isEnabled: boolean = true;
private _isSwipeEnabled: boolean = true;
private _isAnimating: boolean = false;
private _isPers: boolean = false;
private _init: boolean = false;
/**
* @private
*/
isOpen: boolean = false;
/**
* @private
*/
@ViewChild(Backdrop) backdrop: Backdrop;
/**
* @private
*/
onContentClick: EventListener;
/**
* @input {any} A reference to the content element the menu should use.
*/
@Input() content: any;
/**
* @input {string} An id for the menu.
*/
@Input() id: string;
/**
* @input {string} Which side of the view the menu should be placed. Default `"left"`.
*/
@Input() side: string;
/**
* @input {string} The display type of the menu. Default varies based on the mode,
* see the `menuType` in the [config](../../config/Config). Available options:
* `"overlay"`, `"reveal"`, `"push"`.
*/
@Input() type: string;
/**
* @input {boolean} Whether or not the menu should be enabled. Default `true`.
*/
@Input()
get enabled(): boolean {
return this._isEnabled;
}
set enabled(val: boolean) {
this._isEnabled = isTrueProperty(val);
this._setListeners();
}
/**
* @input {boolean} Whether or not swiping the menu should be enabled. Default `true`.
*/
@Input()
get swipeEnabled(): boolean {
return this._isSwipeEnabled;
}
set swipeEnabled(val: boolean) {
this._isSwipeEnabled = isTrueProperty(val);
this._setListeners();
}
/**
* @input {string} Whether or not the menu should persist on child pages. Default `false`.
*/
@Input()
get persistent(): boolean {
return this._isPers;
}
set persistent(val: boolean) {
this._isPers = isTrueProperty(val);
}
/**
* @private
*/
@Input() maxEdgeStart: number;
/**
* @output {event} When the menu is being dragged open.
*/
@Output() ionDrag: EventEmitter<number> = new EventEmitter<number>();
/**
* @output {event} When the menu has been opened.
*/
@Output() ionOpen: EventEmitter<boolean> = new EventEmitter<boolean>();
/**
* @output {event} When the menu has been closed.
*/
@Output() ionClose: EventEmitter<boolean> = new EventEmitter<boolean>();
/** @private */
_menuCtrl: MenuController;
constructor(
_menuCtrl: MenuController,
private _elementRef: ElementRef,
private _config: Config,
private _platform: Platform,
private _renderer: Renderer,
private _keyboard: Keyboard,
private _zone: NgZone,
public gestureCtrl: GestureController
) {
this._menuCtrl = _menuCtrl;
}
/**
* @private
*/
ngOnInit() {
let self = this;
self._init = true;
let content = self.content;
self._cntEle = (content instanceof Node) ? content : content && content.getNativeElement && content.getNativeElement();
// requires content element
if (!self._cntEle) {
return console.error('Menu: must have a [content] element to listen for drag events on. Example:\n\n<ion-menu [content]="content"></ion-menu>\n\n<ion-nav #content></ion-nav>');
}
// normalize the "side"
if (self.side !== 'left' && self.side !== 'right') {
self.side = 'left';
}
self._renderer.setElementAttribute(self._elementRef.nativeElement, 'side', self.side);
// normalize the "type"
if (!self.type) {
self.type = self._config.get('menuType');
}
self._renderer.setElementAttribute(self._elementRef.nativeElement, 'type', self.type);
// add the gestures
self._cntGesture = new MenuContentGesture(self, document.body);
// register listeners if this menu is enabled
// check if more than one menu is on the same side
let hasEnabledSameSideMenu = self._menuCtrl.getMenus().some(m => {
return m.side === self.side && m.enabled;
});
if (hasEnabledSameSideMenu) {
// auto-disable if another menu on the same side is already enabled
self._isEnabled = false;
}
self._setListeners();
// create a reusable click handler on this instance, but don't assign yet
self.onContentClick = function(ev: UIEvent) {
if (self._isEnabled) {
ev.preventDefault();
ev.stopPropagation();
self.close();
}
};
self._cntEle.classList.add('menu-content');
self._cntEle.classList.add('menu-content-' + self.type);
// register this menu with the app's menu controller
self._menuCtrl.register(self);
}
/**
* @private
*/
bdClick(ev: Event) {
console.debug('backdrop clicked');
ev.preventDefault();
ev.stopPropagation();
this._menuCtrl.close();
}
/**
* @private
*/
private _setListeners() {
if (!this._init) {
return;
}
// only listen/unlisten if the menu has initialized
if (this._isEnabled && this._isSwipeEnabled && !this._cntGesture.isListening) {
// should listen, but is not currently listening
console.debug('menu, gesture listen', this.side);
this._cntGesture.listen();
} else if (this._cntGesture.isListening && (!this._isEnabled || !this._isSwipeEnabled)) {
// should not listen, but is currently listening
console.debug('menu, gesture unlisten', this.side);
this._cntGesture.unlisten();
}
}
/**
* @private
*/
private _getType(): MenuType {
if (!this._type) {
this._type = MenuController.create(this.type, this, this._platform);
if (this._config.get('animate') === false) {
this._type.ani.duration(0);
}
}
return this._type;
}
/**
* @private
*/
setOpen(shouldOpen: boolean, animated: boolean = true): Promise<boolean> {
// _isPrevented is used to prevent unwanted opening/closing after swiping open/close
// or swiping open the menu while pressing down on the MenuToggle button
if ((shouldOpen && this.isOpen) || !this._isEnabled || this._isAnimating) {
return Promise.resolve(this.isOpen);
}
this._before();
return new Promise(resolve => {
this._getType().setOpen(shouldOpen, animated, () => {
this._after(shouldOpen);
resolve(this.isOpen);
});
});
}
/**
* @private
*/
canSwipe(): boolean {
return this._isEnabled && this._isSwipeEnabled && !this._isAnimating;
}
/**
* @private
*/
swipeStart() {
// user started swiping the menu open/close
if (this.canSwipe()) {
this._before();
this._getType().setProgressStart(this.isOpen);
}
}
/**
* @private
*/
swipeProgress(stepValue: number) {
// user actively dragging the menu
if (!this._isAnimating) {
return;
}
this._getType().setProgessStep(stepValue);
this.ionDrag.emit(stepValue);
}
/**
* @private
*/
swipeEnd(shouldCompleteLeft: boolean, shouldCompleteRight: boolean, stepValue: number) {
if (!this._isAnimating) {
return;
}
// user has finished dragging the menu
let opening = !this.isOpen;
let shouldComplete = false;
if (opening) {
shouldComplete = (this.side === 'right') ? shouldCompleteLeft : shouldCompleteRight;
} else {
shouldComplete = (this.side === 'right') ? shouldCompleteRight : shouldCompleteLeft;
}
this._getType().setProgressEnd(shouldComplete, stepValue, (isOpen: boolean) => {
console.debug('menu, swipeEnd', this.side);
this._after(isOpen);
});
}
private _before() {
// this places the menu into the correct location before it animates in
// this css class doesn't actually kick off any animations
this.getNativeElement().classList.add('show-menu');
this.getBackdropElement().classList.add('show-backdrop');
this._keyboard.close();
this._isAnimating = true;
}
private _after(isOpen: boolean) {
// keep opening/closing the menu disabled for a touch more yet
// only add listeners/css if it's enabled and isOpen
// and only remove listeners/css if it's not open
// emit opened/closed events
this.isOpen = isOpen;
this._isAnimating = false;
(<any>this._cntEle.classList)[isOpen ? 'add' : 'remove']('menu-content-open');
this._cntEle.removeEventListener('click', this.onContentClick);
if (isOpen) {
this._cntEle.addEventListener('click', this.onContentClick);
this.ionOpen.emit(true);
} else {
this.getNativeElement().classList.remove('show-menu');
this.getBackdropElement().classList.remove('show-backdrop');
this.ionClose.emit(true);
}
}
/**
* @private
*/
open() {
return this.setOpen(true);
}
/**
* @private
*/
close() {
return this.setOpen(false);
}
/**
* @private
*/
toggle() {
return this.setOpen(!this.isOpen);
}
/**
* @private
*/
enable(shouldEnable: boolean): Menu {
this.enabled = shouldEnable;
if (!shouldEnable && this.isOpen) {
// close if this menu is open, and should not be enabled
this.close();
}
if (shouldEnable) {
// if this menu should be enabled
// then find all the other menus on this same side
// and automatically disable other same side menus
this._menuCtrl.getMenus()
.filter(m => m.side === this.side && m !== this)
.map(m => m.enabled = false);
}
// TODO
// what happens if menu is disabled while swipping?
return this;
}
/**
* @private
*/
swipeEnable(shouldEnable: boolean): Menu {
this.swipeEnabled = shouldEnable;
// TODO
// what happens if menu swipe is disabled while swipping?
return this;
}
getNativeElement(): HTMLElement {
return this._elementRef.nativeElement;
}
/**
* @private
*/
getMenuElement(): HTMLElement {
return <HTMLElement>this.getNativeElement().querySelector('.menu-inner');
}
/**
* @private
*/
getContentElement(): HTMLElement {
return this._cntEle;
}
/**
* @private
*/
getBackdropElement(): HTMLElement {
return this.backdrop.getNativeElement();
}
width(): number {
return this.getMenuElement().offsetWidth;
}
/**
* @private
*/
getMenuController(): MenuController {
return this._menuCtrl;
}
/**
* @private
*/
ngOnDestroy() {
this._menuCtrl.unregister(this);
this._cntGesture && this._cntGesture.destroy();
this._type && this._type.destroy();
this._resizeUnreg && this._resizeUnreg();
this._cntGesture = null;
this._type = null;
this._cntEle = null;
this._resizeUnreg = null;
}
}