-
Notifications
You must be signed in to change notification settings - Fork 12
/
jqTreeContextMenu.js
206 lines (187 loc) · 6.54 KB
/
jqTreeContextMenu.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
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
(function ($) {
if (!$.fn.tree) {
throw "Error jqTree is not loaded.";
}
$.fn.jqTreeContextMenu = function (menuCallback, callbacks) {
//
// TODO:
// * Make sure the useContextMenu option is set in jqTree, either complain or set it automatically
// * Make menu fade in/out
//
var self = this;
var $el = this;
var $menuEl;
// This hash holds all menu items that should be disabled for a specific node.
var nodeToDisabledMenuItems = {};
// Disable system context menu from beeing displayed.
$el.bind("contextmenu", function (e) {
e.preventDefault();
return false;
});
// Handle the contextmenu event sent from jqTree when user clicks right mouse button.
$el.bind('tree.contextmenu', function (event) {
// Hide the menu div if it was initialized.
if (typeof $menuEl !== 'undefined') {
$menuEl.hide();
}
// The jQuery object of the menu div.
$menuEl = menuCallback(event.node);
var x = event.click_event.pageX;
var y = event.click_event.pageY;
var yPadding = 5;
var xPadding = 5;
var menuHeight = $menuEl.height();
var menuWidth = $menuEl.width();
var windowHeight = $(window).height();
var windowWidth = $(window).width();
if (menuHeight + y + yPadding > windowHeight) {
// Make sure the whole menu is rendered within the viewport.
y = y - menuHeight;
}
if (menuWidth + x + xPadding > windowWidth) {
// Make sure the whole menu is rendered within the viewport.
x = x - menuWidth;
}
// Handle disabling and enabling of menu items on specific nodes.
if (Object.keys(nodeToDisabledMenuItems).length > 0) {
if (event.node.name in nodeToDisabledMenuItems) {
var nodeName = event.node.name;
var items = nodeToDisabledMenuItems[nodeName];
if (items.length === 0) {
$menuEl.find('li').addClass('disabled');
$menuEl.find('li > a').unbind('click');
} else {
$menuEl.find('li > a').each(function () {
$(this).closest('li').removeClass('disabled');
var hrefValue = $(this).attr('href');
var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
if ($.inArray(value, items) > -1) {
$(this).closest('li').addClass('disabled');
$(this).unbind('click');
}
});
}
} else {
$menuEl.find('li.disabled').removeClass('disabled');
}
}
// Must call show before we set the offset (offset can not be set on display: none elements).
$menuEl.show();
$menuEl.offset({ left: x, top: y });
var dismissContextMenu = function () {
$(document).unbind('click.jqtreecontextmenu');
$el.unbind('tree.click.jqtreecontextmenu');
$(document).unbind('keydown.jqtreecontextmenu');
$menuEl.hide();
}
// Make it possible to dismiss context menu by clicking somewhere in the document.
$(document).bind('click.jqtreecontextmenu', function () {
dismissContextMenu();
});
// Dismiss context menu if another node in the tree is clicked.
$el.bind('tree.click.jqtreecontextmenu', function (e) {
dismissContextMenu();
});
$(document).bind('keydown.jqtreecontextmenu', function (e) {
if (e.key === "Escape") {
e.preventDefault();
dismissContextMenu();
}
});
// Make selection follow the node that was right clicked on.
var selectedNode = $el.tree('getSelectedNode');
if (selectedNode !== event.node) {
$el.tree('selectNode', event.node);
}
// Handle click on menu items, if it's not disabled.
var menuItems = $menuEl.find('li:not(.disabled) a');
if (menuItems.length !== 0) {
menuItems.unbind('click');
menuItems.click(function (e) {
e.stopImmediatePropagation();
dismissContextMenu();
var hrefAnchor = e.currentTarget.attributes.href.nodeValue;
var funcKey = hrefAnchor.slice(hrefAnchor.indexOf("#") + 1, hrefAnchor.length)
var callbackFn = callbacks[funcKey];
if (callbackFn) {
callbackFn(event.node);
}
return false;
});
}
});
this.disable = function () {
if (arguments.length === 0) {
// Called as: api.disable()
$menuEl.find('li:not(.disabled)').addClass('disabled');
$menuEl.find('li a').unbind('click');
nodeToDisabledMenuItems = {};
} else if (arguments.length === 1) {
// Called as: api.disable(['edit','remove'])
var items = arguments[0];
if (typeof items !== 'object') {
return;
}
$menuEl.find('li > a').each(function () {
var hrefValue = $(this).attr('href');
var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
if ($.inArray(value, items) > -1) {
$(this).closest('li').addClass('disabled');
$(this).unbind('click');
}
});
nodeToDisabledMenuItems = {};
} else if (arguments.length === 2) {
// Called as: api.disable(nodeName, ['edit','remove'])
var nodeName = arguments[0];
var items = arguments[1];
nodeToDisabledMenuItems[nodeName] = items;
}
};
this.enable = function () {
if (arguments.length === 0) {
// Called as: api.enable()
$menuEl.find('li.disabled').removeClass('disabled');
nodeToDisabledMenuItems = {};
} else if (arguments.length === 1) {
// Called as: api.enable(['edit','remove'])
var items = arguments[0];
if (typeof items !== 'object') {
return;
}
$menuEl.find('li > a').each(function () {
var hrefValue = $(this).attr('href');
var value = hrefValue.slice(hrefValue.indexOf("#") + 1, hrefValue.length)
if ($.inArray(value, items) > -1) {
$(this).closest('li').removeClass('disabled');
}
});
nodeToDisabledMenuItems = {};
} else if (arguments.length === 2) {
// Called as: api.enable(nodeName, ['edit','remove'])
var nodeName = arguments[0];
var items = arguments[1];
if (items.length === 0) {
delete nodeToDisabledMenuItems[nodeName];
} else {
var disabledItems = nodeToDisabledMenuItems[nodeName];
for (var i = 0; i < items.length; i++) {
var idx = disabledItems.indexOf(items[i]);
if (idx > -1) {
disabledItems.splice(idx, 1);
}
}
if (disabledItems.length === 0) {
delete nodeToDisabledMenuItems[nodeName];
} else {
nodeToDisabledMenuItems[nodeName] = disabledItems;
}
}
if (Object.keys(nodeToDisabledMenuItems).length === 0) {
$menuEl.find('li.disabled').removeClass('disabled');
}
}
};
return this;
};
} (jQuery));