-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathjquery.contextmenu-ui.js
251 lines (236 loc) · 9.89 KB
/
jquery.contextmenu-ui.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* ContextMenu - jQuery plugin for right-click context menus
*
* Author: Chris Domigan
* Contributors: Dan G. Switzer, II
* Parts of this plugin are inspired by Joern Zaefferer's Tooltip plugin
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Version: r2
* Date: 16 July 2007
*
* For documentation visit http://www.trendskitchens.co.nz/jquery/contextmenu/
*
* Updated: include support jQuery UI CSS classes existing starting with version 1.8
* and the currents modified CSS classes of version jQuery UI 1.9
* by Oleg Kiriljuk, [email protected]
* Date: 24 December 2011
*
* Updated by Oleg Kiriljuk to support jQuery UI 1.10 and 1.11
* Date: 17 March 2015
*/
/*global jQuery, $, Testportal */
/*jslint devel: true, browser: true, plusplus: true, eqeq: true */
(function ($) {
"use strict";
var menu, shadow, content, hash, currentTarget,
versionParts = $.ui != null && typeof $.ui.version === "string" ? /^([0-9]+)\.([0-9]+)\.([0-9]+)$/.exec($.ui.version) : [],
isAncorRequired = versionParts != null && versionParts.length === 4 && versionParts[1] === "1" && versionParts[2] < 11,
defaults = {
menuClasses: "ui-menu ui-widget ui-widget-content ui-corner-all",
menuIconClasses: "ui-menu-icons ui-menu ui-widget ui-widget-content ui-corner-all",
menuDivStyle: {
position: "absolute",
zIndex: "500"
},
menuStyle: {
width: "100%"
},
itemClasses: "ui-menu-item",
itemStyle: {},
itemHoverStyle: {},
itemAnchorClasses: "ui-corner-all",
itemAnchorStyle: {
position: "relative",
paddingRight: "0"
},
itemIconAnchorStyle: {
paddingLeft: "2em"
},
itemIconSpanStyle: {
left: ".2em",
top: ".3em",
marginRight: ".5em",
position: "absolute",
"float": "left"
},
itemHoverAnchorClasses: "ui-state-hover",
eventPosX: "pageX",
eventPosY: "pageY",
shadow: true,
menuShadowClasses: "ui-widget-shadow",
menuShadowStyle: {
position: "absolute",
zIndex: "499",
margin: "0",
padding: "1px 0 0 6px"
},
onContextMenu: null,
onShowMenu: null
};
$.fn.contextMenu = function (id, options) {
hash = hash || [];
hash.push({
id: id,
menuDivStyle: $.extend({}, defaults.menuDivStyle, options.menuDivStyle || {}),
menuStyle: $.extend({}, defaults.menuStyle, options.menuStyle || {}),
menuShadowStyle: $.extend({}, defaults.menuShadowStyle, options.menuShadowStyle || {}),
itemStyle: $.extend({}, defaults.itemStyle, options.itemStyle || {}),
itemHoverStyle: $.extend({}, defaults.itemHoverStyle, options.itemHoverStyle || {}),
menuClasses: options.menuClasses || defaults.menuClasses,
menuIconClasses: options.menuIconClasses || defaults.menuIconClasses,
menuShadowClasses: options.menuShadowClasses || defaults.menuShadowClasses,
itemClasses: options.itemClasses || defaults.itemClasses,
itemAnchorClasses: options.itemAnchorClasses || defaults.itemAnchorClasses,
itemAnchorStyle: $.extend({}, defaults.itemAnchorStyle, options.itemAnchorStyle || {}),
itemIconSpanStyle: $.extend({}, defaults.itemIconSpanStyle, options.itemIconSpanStyle || {}),
itemIconAnchorStyle: $.extend({}, defaults.itemIconAnchorStyle, options.itemIconAnchorStyle || {}),
itemHoverAnchorClasses: options.itemHoverAnchorClasses || defaults.itemHoverAnchorClasses,
bindings: options.bindings || {},
shadow: options.shadow || options.shadow === false ? options.shadow : defaults.shadow,
onContextMenu: options.onContextMenu || defaults.onContextMenu,
onShowMenu: options.onShowMenu || defaults.onShowMenu,
eventPosX: options.eventPosX || defaults.eventPosX,
eventPosY: options.eventPosY || defaults.eventPosY
});
function hide() {
menu.hide().attr("aria-hidden", "true");
shadow.hide().attr("aria-hidden", "true");
}
function display(i, trigger, e) {
var cur = hash[i], items;
content = $("#" + cur.id).find("ul:first").clone(true);
// Send the content to the menu
menu.html(content);
// if there's an onShowMenu, run it now -- must run after content has been added
// if you try to alter the content variable before the menu.html(), IE6 has issues
// updating the content
if (!!cur.onShowMenu) { menu = cur.onShowMenu(e, menu); }
if (cur.menuClasses) {
if (cur.menuIconClasses && content.find(".ui-icon").length > 0) {
content.addClass(cur.menuIconClasses);
} else {
content.addClass(cur.menuClasses);
}
}
if (!$.isEmptyObject(cur.menuStyle)) {
content.css(cur.menuStyle);
}
items = content.attr("role", "menu").find("li");
if (cur.itemClasses) {
items.addClass(cur.itemClasses).attr("role", isAncorRequired ? "presentation" : "menuitem");
}
if (!$.isEmptyObject(cur.itemStyle)) {
items.css(cur.itemStyle);
}
if (cur.itemAnchorClasses) {
if (isAncorRequired) {
items.children("a").addClass(cur.itemAnchorClasses).filter(":not([role])").attr("role", "menuitem");
} else {
items.addClass(cur.itemAnchorClasses);
}
}
if (!$.isEmptyObject(cur.itemAnchorStyle)) {
items.children("a").css(cur.itemAnchorStyle);
}
if (!$.isEmptyObject(cur.itemIconSpanStyle)) {
items.children("a").children("span.ui-icon").css(cur.itemIconSpanStyle).parent("a").css(cur.itemIconAnchorStyle);
}
if ($.isEmptyObject(cur.itemHoverStyle)) {
items.hover(
function () {
//$(this).siblings().children(".ui-state-active").removeClass("ui-state-active");
var menuItem = isAncorRequired ? $(this).children("a") : $(this);
menuItem.addClass(cur.itemHoverAnchorClasses);
},
function () {
var menuItem = isAncorRequired ? $(this).children("a") : $(this);
menuItem.removeClass(cur.itemHoverAnchorClasses);
}
);
} else if (!$.isEmptyObject(cur.itemHoverStyle)) {
items.hover(
function () {
$(this).css(cur.itemHoverStyle);
},
function () {
$(this).css(cur.itemStyle);
}
);
}
items.find("img").css({ verticalAlign: "middle", paddingRight: "2px" });
$.each(cur.bindings, function (menuItemId, func) {
$("#" + menuItemId, menu).bind("click", function () {
hide();
func(trigger, currentTarget);
});
});
menu.css({
left: e[cur.eventPosX],
top: e[cur.eventPosY]
}).show().removeAttr("aria-hidden");
if (cur.shadow) {
shadow.css({
width: menu.width(),
height: menu.height(),
left: e.pageX + 2,
top: e.pageY + 2
}).show().removeAttr("aria-hidden");
}
$(document).one("click", hide);
}
var index = hash.length - 1;
if (!menu) { // Create singleton menu
menu = $("<div class=\"jqContextMenu\"></div>")
.hide()
.attr("aria-hidden", "true")
.css(hash[index].menuDivStyle)
.appendTo("body")
.bind("click", function (e) {
e.stopPropagation();
}).mouseleave(function (e) {
if (e.pageX === -1 && e.pageY === -1) {
return; // over tooltip
}
hide();
});
}
if (!shadow) {
shadow = $("<div></div>")
.addClass(hash[index].menuShadowClasses)
.css(hash[index].menuShadowStyle)
.appendTo("body")
.hide().attr("aria-hidden", "true");
}
$(this).bind("contextmenu", function (e) {
// Check if onContextMenu() defined
var bShowContext = (!!hash[index].onContextMenu) ? hash[index].onContextMenu(e) : true;
currentTarget = e.target;
if (bShowContext) {
display(index, this, e, options);
return false;
}
hide();
return true;
});
return this;
};
// Apply defaults
$.contextMenu = {
defaults: function (userDefaults) {
$.each(userDefaults, function (i, val) {
if (typeof val === "object" && defaults[i]) {
$.extend(defaults[i], val);
} else {
defaults[i] = val;
}
});
}
};
$(function () {
$("div.contextMenu").hide().attr("aria-hidden", "true");
});
}(jQuery));