ultimix
jquery.tree.hotkeys.js
Go to the documentation of this file.
1 (function ($) {
2  if(typeof window.hotkeys == "undefined") throw "jsTree hotkeys: jQuery hotkeys plugin not included.";
3 
4  $.extend($.tree.plugins, {
5  "hotkeys" : {
6  bound : [],
7  disabled : false,
8  defaults : {
9  hover_mode : false,
10  functions : {
11  "up" : function () { $.tree.plugins.hotkeys.get_prev.apply(this); return false; },
12  "down" : function () { $.tree.plugins.hotkeys.get_next.apply(this); return false; },
13  "left" : function () { $.tree.plugins.hotkeys.get_left.apply(this); return false; },
14  "right" : function () { $.tree.plugins.hotkeys.get_right.apply(this); return false; },
15  "f2" : function () { if(this.selected) this.rename(); return false; },
16  "del" : function () { if(this.selected) this.remove(); return false; },
17  "ctrl+c": function () { if(this.selected) this.copy(); return false; },
18  "ctrl+x": function () { if(this.selected) this.cut(); return false; },
19  "ctrl+v": function () { if(this.selected) this.paste(); return false; }
20  }
21  },
22  exec : function(key) {
23  if($.tree.plugins.hotkeys.disabled) return false;
24 
25  var t = $.tree.focused();
26  if(typeof t.settings.plugins.hotkeys == "undefined") return;
27  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, t.settings.plugins.hotkeys);
28  if(typeof opts.functions[key] == "function") return opts.functions[key].apply(t);
29  },
30  get_next : function() {
31  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, this.settings.plugins.hotkeys);
32  var obj = this.hovered || this.selected;
33  return opts.hover_mode ? this.hover_branch(this.next(obj)) : this.select_branch(this.next(obj));
34  },
35  get_prev : function() {
36  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, this.settings.plugins.hotkeys);
37  var obj = this.hovered || this.selected;
38  return opts.hover_mode ? this.hover_branch(this.prev(obj)) : this.select_branch(this.prev(obj));
39  },
40  get_left : function() {
41  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, this.settings.plugins.hotkeys);
42  var obj = this.hovered || this.selected;
43  if(obj) {
44  if(obj.hasClass("open")) this.close_branch(obj);
45  else {
46  return opts.hover_mode ? this.hover_branch(this.parent(obj)) : this.select_branch(this.parent(obj));
47  }
48  }
49  },
50  get_right : function() {
51  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, this.settings.plugins.hotkeys);
52  var obj = this.hovered || this.selected;
53  if(obj) {
54  if(obj.hasClass("closed")) this.open_branch(obj);
55  else {
56  return opts.hover_mode ? this.hover_branch(obj.find("li:eq(0)")) : this.select_branch(obj.find("li:eq(0)"));
57  }
58  }
59  },
60 
61  callbacks : {
62  oninit : function (t) {
63  var opts = $.extend(true, {}, $.tree.plugins.hotkeys.defaults, this.settings.plugins.hotkeys);
64  for(var i in opts.functions) {
65  if(opts.functions.hasOwnProperty(i) && $.inArray(i, $.tree.plugins.hotkeys.bound) == -1) {
66  (function (k) {
67  $(document).bind("keydown", { combi : k, disableInInput: true }, function (event) {
68  return $.tree.plugins.hotkeys.exec(k);
69  });
70  })(i);
71  $.tree.plugins.hotkeys.bound.push(i);
72  }
73  }
74  }
75  }
76  }
77  });
78 })(jQuery);