FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ionic/ionic/components/menu/menu.ts at loading · Java1024/ionic · GitHub
Java1024
ionic
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ionic
/
ionic
/
components
/
menu
/
menu.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
470 lines (401 loc) · 11.5 KB
Breadcrumbs
ionic
/
ionic
/
components
/
menu
/
menu.ts
Copy path
File metadata and controls
470 lines (401 loc) · 11.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
Component
,
forwardRef
,
Directive
,
Host
,
EventEmitter
,
ElementRef
,
NgZone
,
Input
,
Output
,
Renderer
}
from
'angular2/core'
;
import
{
Ion
}
from
'../ion'
;
import
{
Config
}
from
'../../config/config'
;
import
{
Platform
}
from
'../../platform/platform'
;
import
{
Keyboard
}
from
'../../util/keyboard'
;
import
{
MenuContentGesture
,
MenuTargetGesture
}
from
'./menu-gestures'
;
import
{
MenuController
}
from
'./menu-controller'
;
import
{
MenuType
}
from
'./menu-types'
;
import
{
isTrueProperty
}
from
'../../util/util'
;
/**
*
@private
*/
@
Component
(
{
selector
:
'ion-menu'
,
host
:
{
'role'
:
'navigation'
}
,
template
:
'<ng-content></ng-content>'
+
'<div tappable disable-activated class="backdrop"></div>'
,
directives
:
[
forwardRef
(
(
)
=>
MenuBackdrop
)
]
}
)
export
class
Menu
extends
Ion
{
private
_preventTime
:
number
=
0
;
private
_cntEle
:
HTMLElement
;
private
_cntGesture
:
MenuTargetGesture
;
private
_menuGesture
:
MenuContentGesture
;
private
_type
:
MenuType
;
private
_resizeUnreg
:
Function
;
private
_isEnabled
:
boolean
=
true
;
private
_isSwipeEnabled
:
boolean
=
true
;
private
_isPers
:
boolean
=
false
;
private
_init
:
boolean
=
false
;
/**
*
@private
*/
isOpen
:
boolean
=
false
;
/**
*
@private
*/
backdrop
:
MenuBackdrop
;
/**
*
@private
*/
onContentClick
:
EventListener
;
/**
*
@private
*/
@
Input
(
)
content
:
any
;
/**
*
@private
*/
@
Input
(
)
id
:
string
;
/**
*
@private
*/
@
Input
(
)
side
:
string
;
/**
*
@private
*/
@
Input
(
)
type
:
string
;
/**
*
@private
*/
@
Input
(
)
get
enabled
(
)
:
boolean
{
return
this
.
_isEnabled
;
}
set
enabled
(
val
:
boolean
)
{
this
.
_isEnabled
=
isTrueProperty
(
val
)
;
this
.
_setListeners
(
)
;
}
/**
*
@private
*/
@
Input
(
)
get
swipeEnabled
(
)
:
boolean
{
return
this
.
_isSwipeEnabled
;
}
set
swipeEnabled
(
val
:
boolean
)
{
this
.
_isSwipeEnabled
=
isTrueProperty
(
val
)
;
this
.
_setListeners
(
)
;
}
/**
*
@private
*/
@
Input
(
)
get
persistent
(
)
:
boolean
{
return
this
.
_isPers
;
}
set
persistent
(
val
:
boolean
)
{
this
.
_isPers
=
isTrueProperty
(
val
)
;
}
/**
*
@private
*/
@
Input
(
)
maxEdgeStart
:
number
;
/**
*
@private
*/
@
Output
(
)
opening
:
EventEmitter
<
number
>
=
new
EventEmitter
(
)
;
constructor
(
private
_menuCtrl
:
MenuController
,
private
_elementRef
:
ElementRef
,
private
_config
:
Config
,
private
_platform
:
Platform
,
private
_renderer
:
Renderer
,
private
_keyboard
:
Keyboard
,
private
_zone
:
NgZone
)
{
super
(
_elementRef
)
;
}
/**
*
@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
,
self
.
getContentElement
(
)
)
;
self
.
_menuGesture
=
new
MenuTargetGesture
(
self
,
self
.
getNativeElement
(
)
)
;
// 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
*/
private
_setListeners
(
)
{
let
self
=
this
;
if
(
self
.
_init
)
{
// only listen/unlisten if the menu has initialized
if
(
self
.
_isEnabled
&&
self
.
_isSwipeEnabled
&&
!
self
.
_cntGesture
.
isListening
)
{
// should listen, but is not currently listening
console
.
debug
(
'menu, gesture listen'
,
self
.
side
)
;
self
.
_zone
.
runOutsideAngular
(
function
(
)
{
self
.
_cntGesture
.
listen
(
)
;
self
.
_menuGesture
.
listen
(
)
;
}
)
;
}
else
if
(
self
.
_cntGesture
.
isListening
&&
(
!
self
.
_isEnabled
||
!
self
.
_isSwipeEnabled
)
)
{
// should not listen, but is currently listening
console
.
debug
(
'menu, gesture unlisten'
,
self
.
side
)
;
self
.
_cntGesture
.
unlisten
(
)
;
self
.
_menuGesture
.
unlisten
(
)
;
}
}
}
/**
*
@private
*/
private
_getType
(
)
:
MenuType
{
if
(
!
this
.
_type
)
{
this
.
_type
=
MenuController
.
create
(
this
.
type
,
this
)
;
if
(
this
.
_config
.
get
(
'animate'
)
===
false
)
{
this
.
_type
.
ani
.
duration
(
0
)
;
}
}
return
this
.
_type
;
}
/**
* Sets the state of the Menu to open or not.
*
@param
{
boolean
} shouldOpen If the Menu is open or not.
*
@return
{
Promise
} returns a promise once set
*/
setOpen
(
shouldOpen
:
boolean
)
:
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
.
_isPrevented
(
)
)
{
return
Promise
.
resolve
(
this
.
isOpen
)
;
}
this
.
_before
(
)
;
return
new
Promise
(
resolve
=>
{
this
.
_getType
(
)
.
setOpen
(
shouldOpen
,
(
)
=>
{
this
.
_after
(
shouldOpen
)
;
resolve
(
this
.
isOpen
)
;
}
)
;
}
)
;
}
/**
*
@private
*/
swipeStart
(
)
{
// user started swiping the menu open/close
if
(
this
.
_isPrevented
(
)
||
!
this
.
_isEnabled
||
!
this
.
_isSwipeEnabled
)
return
;
this
.
_before
(
)
;
this
.
_getType
(
)
.
setProgressStart
(
this
.
isOpen
)
;
}
/**
*
@private
*/
swipeProgress
(
stepValue
:
number
)
{
// user actively dragging the menu
if
(
this
.
_isEnabled
&&
this
.
_isSwipeEnabled
)
{
this
.
_prevent
(
)
;
this
.
_getType
(
)
.
setProgessStep
(
stepValue
)
;
this
.
opening
.
next
(
stepValue
)
;
}
}
/**
*
@private
*/
swipeEnd
(
shouldComplete
:
boolean
,
currentStepValue
:
number
)
{
// user has finished dragging the menu
if
(
this
.
_isEnabled
&&
this
.
_isSwipeEnabled
)
{
this
.
_prevent
(
)
;
this
.
_getType
(
)
.
setProgressEnd
(
shouldComplete
,
currentStepValue
,
(
isOpen
)
=>
{
console
.
debug
(
'menu, swipeEnd'
,
this
.
side
)
;
this
.
_after
(
isOpen
)
;
}
)
;
}
}
/**
*
@private
*/
private
_before
(
)
{
// this places the menu into the correct location before it animates in
// this css class doesn't actually kick off any animations
if
(
this
.
_isEnabled
)
{
this
.
getNativeElement
(
)
.
classList
.
add
(
'show-menu'
)
;
this
.
getBackdropElement
(
)
.
classList
.
add
(
'show-backdrop'
)
;
this
.
_prevent
(
)
;
this
.
_keyboard
.
close
(
)
;
}
}
/**
*
@private
*/
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
if
(
(
this
.
_isEnabled
&&
isOpen
)
||
!
isOpen
)
{
this
.
_prevent
(
)
;
this
.
isOpen
=
isOpen
;
this
.
_cntEle
.
classList
[
isOpen
?
'add'
:
'remove'
]
(
'menu-content-open'
)
;
this
.
_cntEle
.
removeEventListener
(
'click'
,
this
.
onContentClick
)
;
if
(
isOpen
)
{
this
.
_cntEle
.
addEventListener
(
'click'
,
this
.
onContentClick
)
;
}
else
{
this
.
getNativeElement
(
)
.
classList
.
remove
(
'show-menu'
)
;
this
.
getBackdropElement
(
)
.
classList
.
remove
(
'show-backdrop'
)
;
}
}
}
/**
*
@private
*/
private
_prevent
(
)
{
// used to prevent unwanted opening/closing after swiping open/close
// or swiping open the menu while pressing down on the menuToggle
this
.
_preventTime
=
Date
.
now
(
)
+
20
;
}
/**
*
@private
*/
private
_isPrevented
(
)
{
return
this
.
_preventTime
>
Date
.
now
(
)
;
}
/**
* Progamatically open the Menu.
*
@return
{
Promise
} returns a promise when the menu is fully opened.
*/
open
(
)
{
return
this
.
setOpen
(
true
)
;
}
/**
* Progamatically close the Menu.
*
@return
{
Promise
} returns a promise when the menu is fully closed.
*/
close
(
)
{
return
this
.
setOpen
(
false
)
;
}
/**
* Toggle the menu. If it's closed, it will open, and if opened, it will close.
*
@return
{
Promise
} returns a promise when the menu has been toggled.
*/
toggle
(
)
{
return
this
.
setOpen
(
!
this
.
isOpen
)
;
}
/**
* Used to enable or disable a menu. For example, there could be multiple
* left menus, but only one of them should be able to be opened at the same
* time. If there are multiple menus on the same side, then enabling one menu
* will also automatically disable all the others that are on the same side.
*
@param
{
boolean
} shouldEnable True if it should be enabled, false if not.
*
@return
{
Menu
} Returns the instance of the menu, which is useful for chaining.
*/
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
let
sameSideMenus
=
this
.
_menuCtrl
.
getMenus
(
)
.
filter
(
m
=>
m
.
side
===
this
.
side
&&
m
!==
this
)
.
map
(
m
=>
m
.
enabled
=
false
)
;
}
return
this
;
}
/**
* Used to enable or disable the ability to swipe open the menu.
*
@param
{
boolean
} shouldEnable True if it should be swipe-able, false if not.
*
@return
{
Menu
} Returns the instance of the menu, which is useful for chaining.
*/
swipeEnable
(
shouldEnable
:
boolean
)
:
Menu
{
this
.
swipeEnabled
=
shouldEnable
;
return
this
;
}
/**
*
@private
*/
getMenuElement
(
)
:
HTMLElement
{
return
this
.
getNativeElement
(
)
;
}
/**
*
@private
*/
getContentElement
(
)
:
HTMLElement
{
return
this
.
_cntEle
;
}
/**
*
@private
*/
getBackdropElement
(
)
:
HTMLElement
{
return
this
.
backdrop
.
elementRef
.
nativeElement
;
}
/**
*
@private
*/
ngOnDestroy
(
)
{
this
.
_menuCtrl
.
unregister
(
this
)
;
this
.
_cntGesture
&&
this
.
_cntGesture
.
destroy
(
)
;
this
.
_menuGesture
&&
this
.
_menuGesture
.
destroy
(
)
;
this
.
_type
&&
this
.
_type
.
destroy
(
)
;
this
.
_resizeUnreg
&&
this
.
_resizeUnreg
(
)
;
this
.
_cntEle
=
null
;
}
}
/**
*
@private
*/
@
Directive
(
{
selector
:
'.backdrop'
,
host
:
{
'(click)'
:
'clicked($event)'
,
}
}
)
export
class
MenuBackdrop
{
constructor
(
@
Host
(
)
private
_menuCtrl
:
Menu
,
public
elementRef
:
ElementRef
)
{
_menuCtrl
.
backdrop
=
this
;
}
/**
*
@private
*/
private
clicked
(
ev
)
{
console
.
debug
(
'backdrop clicked'
)
;
ev
.
preventDefault
(
)
;
ev
.
stopPropagation
(
)
;
this
.
_menuCtrl
.
close
(
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL