forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nav.view.ts
109 lines (73 loc) · 2.72 KB
/
nav.view.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace $.$$ {
export class $mol_nav extends $.$mol_nav {
event_key( event? : KeyboardEvent ) {
if( !event ) return event
if( event.defaultPrevented ) return
if( this.mod_ctrl() && !event.ctrlKey ) return
if( this.mod_shift() && !event.shiftKey ) return
if( this.mod_alt() && !event.altKey ) return
switch( event.keyCode ) {
case $mol_keyboard_code.up : return this.event_up( event )
case $mol_keyboard_code.down : return this.event_down( event )
case $mol_keyboard_code.left : return this.event_left( event )
case $mol_keyboard_code.right : return this.event_right( event )
case $mol_keyboard_code.pageUp : return this.event_up( event )
case $mol_keyboard_code.pageDown : return this.event_down( event )
}
}
event_up( event? : KeyboardEvent ) {
if( !event ) return event
const keys = this.keys_y()
if( keys.length < 2 ) return
const index_y = this.index_y();
const index_old = index_y === null ? 0 : index_y
const index_new = ( index_old + keys.length - 1 ) % keys.length
event.preventDefault()
if( index_old === 0 && !this.cycle() ) return
this.current_y( this.keys_y()[ index_new ] )
}
event_down( event? : KeyboardEvent ) {
if( !event ) return event
const keys = this.keys_y()
if( keys.length < 2 ) return
const index_y = this.index_y();
const index_old = index_y === null ? keys.length - 1 : index_y;
const index_new = ( index_old + 1 ) % keys.length
event.preventDefault()
if( index_new === 0 && !this.cycle() ) return
this.current_y( this.keys_y()[ index_new ] )
}
event_left( event? : KeyboardEvent ) {
if( !event ) return event
const keys = this.keys_x()
if( keys.length < 2 ) return
const index_x = this.index_x();
const index_old = index_x === null ? 0 : index_x
const index_new = ( index_old + keys.length - 1 ) % keys.length
event.preventDefault()
if( index_old === 0 && !this.cycle() ) return
this.current_x( this.keys_x()[ index_new ] )
}
event_right( event? : KeyboardEvent ) {
if( !event ) return event
const keys = this.keys_x()
if( keys.length < 2 ) return
const index_x = this.index_x();
const index_old = index_x === null ? keys.length - 1 : index_x
const index_new = ( index_old + 1 ) % keys.length
event.preventDefault()
if( index_new === 0 && !this.cycle() ) return
this.current_x( this.keys_x()[ index_new ] )
}
index_y() {
let index = this.keys_y().indexOf( this.current_y() )
if( index < 0 ) return null
return index
}
index_x() {
let index = this.keys_x().indexOf( this.current_x() )
if( index < 0 ) return null
return index
}
}
}