forked from vakata/jstree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstree.hotkeys.js
138 lines (137 loc) · 4.35 KB
/
jstree.hotkeys.js
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
/* File: jstree.hotkeys.js
Enables keyboard shortcuts. Depends on jQuery.hotkeys (included).
*/
/* Group: jstree hotkeys plugin */
(function ($) {
if(typeof $.hotkeys === "undefined") { throw "jsTree hotkeys: jQuery hotkeys plugin not included."; }
var bound = [];
function exec(i, event) {
var f = $.jstree._focused(), tmp;
if(f && f.data && f.data.hotkeys && f.data.hotkeys.enabled) {
tmp = f.get_settings(true).hotkeys[i];
if(tmp) { return tmp.call(f, event); }
}
}
$.jstree.plugin("hotkeys", {
__construct : function () {
if(!this.data.ui) { throw "jsTree hotkeys: jsTree UI plugin not included."; }
$.each(this.get_settings(true).hotkeys, function (i, v) {
if(v !== false && $.inArray(i, bound) == -1) {
$(document).bind("keydown", i, function (event) { return exec(i, event); });
bound.push(i);
}
});
this.get_container()
.bind("lock.jstree", $.proxy(function () {
if(this.data.hotkeys.enabled) { this.data.hotkeys.enabled = false; this.data.hotkeys.revert = true; }
}, this))
.bind("unlock.jstree", $.proxy(function () {
if(this.data.hotkeys.revert) { this.data.hotkeys.enabled = true; }
}, this));
this.enable_hotkeys();
},
defaults : {
"up" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_prev(o));
return false;
},
"ctrl+up" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_prev(o));
return false;
},
"shift+up" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_prev(o));
return false;
},
"down" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_next(o));
return false;
},
"ctrl+down" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_next(o));
return false;
},
"shift+down" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected || -1;
this.hover_node(this.get_next(o));
return false;
},
"left" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o) {
if(o.hasClass("jstree-open")) { this.close_node(o); }
else { this.hover_node(this.get_prev(o)); }
}
return false;
},
"ctrl+left" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o) {
if(o.hasClass("jstree-open")) { this.close_node(o); }
else { this.hover_node(this.get_prev(o)); }
}
return false;
},
"shift+left" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o) {
if(o.hasClass("jstree-open")) { this.close_node(o); }
else { this.hover_node(this.get_prev(o)); }
}
return false;
},
"right" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o && o.length) {
if(o.hasClass("jstree-closed")) { this.open_node(o); }
else { this.hover_node(this.get_next(o)); }
}
return false;
},
"ctrl+right" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o && o.length) {
if(o.hasClass("jstree-closed")) { this.open_node(o); }
else { this.hover_node(this.get_next(o)); }
}
return false;
},
"shift+right" : function () {
var o = this.data.ui.hovered || this.data.ui.last_selected;
if(o && o.length) {
if(o.hasClass("jstree-closed")) { this.open_node(o); }
else { this.hover_node(this.get_next(o)); }
}
return false;
},
"space" : function () {
if(this.data.ui.hovered) { this.data.ui.hovered.children("a:eq(0)").click(); }
return false;
},
"ctrl+space" : function (event) {
event.type = "click";
if(this.data.ui.hovered) { this.data.ui.hovered.children("a:eq(0)").trigger(event); }
return false;
},
"shift+space" : function (event) {
event.type = "click";
if(this.data.ui.hovered) { this.data.ui.hovered.children("a:eq(0)").trigger(event); }
return false;
}
},
_fn : {
enable_hotkeys : function () {
this.data.hotkeys.enabled = true;
},
disable_hotkeys : function () {
this.data.hotkeys.enabled = false;
}
}
});
$.jstree.defaults.plugins.push("hotkeys");
})(jQuery);