ultimix
jquery.tree.contextmenu.js
Go to the documentation of this file.
1 (function ($) {
2  $.extend($.tree.plugins, {
3  "contextmenu" : {
4  object : $("<ul id='jstree-contextmenu' class='tree-context' />"),
5  data : {
6  t : false,
7  a : false,
8  r : false
9  },
10 
11  defaults : {
12  class_name : "hover",
13  items : {
14  create : {
15  label : "Create",
16  icon : "create",
17  visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return 0; return TREE_OBJ.check("creatable", NODE); },
18  action : function (NODE, TREE_OBJ) { TREE_OBJ.create(false, TREE_OBJ.get_node(NODE[0])); },
19  separator_after : true
20  },
21  rename : {
22  label : "Rename",
23  icon : "rename",
24  visible : function (NODE, TREE_OBJ) { if(NODE.length != 1) return false; return TREE_OBJ.check("renameable", NODE); },
25  action : function (NODE, TREE_OBJ) { TREE_OBJ.rename(NODE); }
26  },
27  remove : {
28  label : "Delete",
29  icon : "remove",
30  visible : function (NODE, TREE_OBJ) { var ok = true; $.each(NODE, function () { if(TREE_OBJ.check("deletable", this) == false) ok = false; return false; }); return ok; },
31  action : function (NODE, TREE_OBJ) { $.each(NODE, function () { TREE_OBJ.remove(this); }); }
32  }
33  }
34  },
35  show : function(obj, t) {
36  var opts = $.extend(true, {}, $.tree.plugins.contextmenu.defaults, t.settings.plugins.contextmenu);
37  obj = $(obj);
38  $.tree.plugins.contextmenu.object.empty();
39  var str = "";
40  var cnt = 0;
41  for(var i in opts.items) {
42  if(!opts.items.hasOwnProperty(i)) continue;
43  if(opts.items[i] === false) continue;
44  var r = 1;
45  if(typeof opts.items[i].visible == "function") r = opts.items[i].visible.call(null, $.tree.plugins.contextmenu.data.a, t);
46  if(r == -1) continue;
47  else cnt ++;
48  if(opts.items[i].separator_before === true) str += "<li class='separator'><span>&nbsp;</span></li>";
49  str += '<li><a href="#" rel="' + i + '" class="' + i + ' ' + (r == 0 ? 'disabled' : '') + '">';
50  if(opts.items[i].icon) str += "<ins " + (opts.items[i].icon.indexOf("/") == -1 ? " class='" + opts.items[i].icon + "' " : " style='background-image:url(\"" + opts.items[i].icon + "\");' " ) + ">&nbsp;</ins>";
51  else str += "<ins>&nbsp;</ins>";
52  str += "<span>" + opts.items[i].label + '</span></a></li>';
53  if(opts.items[i].separator_after === true) str += "<li class='separator'><span>&nbsp;</span></li>";
54  }
55  var tmp = obj.children("a:visible").offset();
56  $.tree.plugins.contextmenu.object.attr("class","tree-context tree-" + t.settings.ui.theme_name.toString() + "-context").html(str);
57  var h = $.tree.plugins.contextmenu.object.height();
58  var w = $.tree.plugins.contextmenu.object.width();
59  var x = tmp.left;
60  var y = tmp.top + parseInt(obj.children("a:visible").height()) + 2;
61  var max_y = $(window).height() + $(window).scrollTop();
62  var max_x = $(window).width() + $(window).scrollLeft();
63  if(y + h > max_y) y = Math.max( (max_y - h - 2), 0);
64  if(x + w > max_x) x = Math.max( (max_x - w - 2), 0);
65  $.tree.plugins.contextmenu.object.css({ "left" : (x), "top" : (y) }).fadeIn("fast");
66  },
67  hide : function () {
68  if(!$.tree.plugins.contextmenu.data.t) return;
69  var opts = $.extend(true, {}, $.tree.plugins.contextmenu.defaults, $.tree.plugins.contextmenu.data.t.settings.plugins.contextmenu);
70  if($.tree.plugins.contextmenu.data.r && $.tree.plugins.contextmenu.data.a) {
71  $.tree.plugins.contextmenu.data.a.children("a, span").removeClass(opts.class_name);
72  }
73  $.tree.plugins.contextmenu.data = { a : false, r : false, t : false };
74  $.tree.plugins.contextmenu.object.fadeOut("fast");
75  },
76  exec : function (cmd) {
77  if($.tree.plugins.contextmenu.data.t == false) return;
78  var opts = $.extend(true, {}, $.tree.plugins.contextmenu.defaults, $.tree.plugins.contextmenu.data.t.settings.plugins.contextmenu);
79  try { opts.items[cmd].action.apply(null, [$.tree.plugins.contextmenu.data.a, $.tree.plugins.contextmenu.data.t]); } catch(e) { };
80  },
81 
82  callbacks : {
83  oninit : function () {
84  if(!$.tree.plugins.contextmenu.css) {
85  var css = '#jstree-contextmenu { display:none; position:absolute; z-index:2000; list-style-type:none; margin:0; padding:0; left:-2000px; top:-2000px; } .tree-context { margin:20px; padding:0; width:180px; border:1px solid #979797; padding:2px; background:#f5f5f5; list-style-type:none; }.tree-context li { height:22px; margin:0 0 0 27px; padding:0; background:#ffffff; border-left:1px solid #e0e0e0; }.tree-context li a { position:relative; display:block; height:22px; line-height:22px; margin:0 0 0 -28px; text-decoration:none; color:black; padding:0; }.tree-context li a ins { text-decoration:none; float:left; width:16px; height:16px; margin:0 0 0 0; background-color:#f0f0f0; border:1px solid #f0f0f0; border-width:3px 5px 3px 6px; line-height:16px; }.tree-context li a span { display:block; background:#f0f0f0; margin:0 0 0 29px; padding-left:5px; }.tree-context li.separator { background:#f0f0f0; height:2px; line-height:2px; font-size:1px; border:0; margin:0; padding:0; }.tree-context li.separator span { display:block; margin:0px 0 0px 27px; height:1px; border-top:1px solid #e0e0e0; border-left:1px solid #e0e0e0; line-height:1px; font-size:1px; background:white; }.tree-context li a:hover { border:1px solid #d8f0fa; height:20px; line-height:20px; }.tree-context li a:hover span { background:#e7f4f9; margin-left:28px; }.tree-context li a:hover ins { background-color:#e7f4f9; border-color:#e7f4f9; border-width:2px 5px 2px 5px; }.tree-context li a.disabled { color:gray; }.tree-context li a.disabled ins { }.tree-context li a.disabled:hover { border:0; height:22px; line-height:22px; }.tree-context li a.disabled:hover span { background:#f0f0f0; margin-left:29px; }.tree-context li a.disabled:hover ins { border-color:#f0f0f0; background-color:#f0f0f0; border-width:3px 5px 3px 6px; }';
86  $.tree.plugins.contextmenu.css = this.add_sheet({ str : css });
87  }
88  },
89  onrgtclk : function (n, t, e) {
90  var opts = $.extend(true, {}, $.tree.plugins.contextmenu.defaults, t.settings.plugins.contextmenu);
91  n = $(n);
92  if(n.size() == 0) return;
93  $.tree.plugins.contextmenu.data.t = t;
94  if(!n.children("a:eq(0)").hasClass("clicked")) {
95  $.tree.plugins.contextmenu.data.a = n;
96  $.tree.plugins.contextmenu.data.r = true;
97  n.children("a").addClass(opts.class_name);
98  e.target.blur();
99  }
100  else {
101  $.tree.plugins.contextmenu.data.r = false;
102  $.tree.plugins.contextmenu.data.a = (t.selected_arr && t.selected_arr.length > 1) ? t.selected_arr : t.selected;
103  }
104  $.tree.plugins.contextmenu.show(n, t);
105  e.preventDefault();
106  e.stopPropagation();
107  // return false; // commented out because you might want to do something in your own callback
108  },
109  onchange : function () { $.tree.plugins.contextmenu.hide(); },
110  beforedata : function () { $.tree.plugins.contextmenu.hide(); },
111  ondestroy : function () { $.tree.plugins.contextmenu.hide(); }
112  }
113  }
114  });
115  $(function () {
116  $.tree.plugins.contextmenu.object.hide().appendTo("body");
117  $("a", $.tree.plugins.contextmenu.object[0])
118  .live("click", function (event) {
119  if(!$(this).hasClass("disabled")) {
120  $.tree.plugins.contextmenu.exec.apply(null, [$(this).attr("rel")]);
121  $.tree.plugins.contextmenu.hide();
122  }
123  event.stopPropagation();
124  event.preventDefault();
125  return false;
126  })
127  $(document).bind("mousedown", function(event) { if($(event.target).parents("#jstree-contextmenu").size() == 0) $.tree.plugins.contextmenu.hide(); });
128  });
129 })(jQuery);