-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleContextMenu.js
78 lines (74 loc) · 2.98 KB
/
simpleContextMenu.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
/*
* jQuery Simple Context Menu
* Original author: Niraj Chauhan
* Further changes, comments: @nirajmchauhan
*/;
(function ($, window, document, undefined) {
var simpleContextMenu = 'simpleContextMenu',
defaults = {
options: {
'Home': 'http://webstutorial.com/',
'About Us': 'http://www.webstutorial.com/work-with-us',
'Contact Us': 'http://www.webstutorial.com/work-with-us'
}
},
elementClass = '.SimpleContextMenu';
var multiElements = {};
function Plugin(element, options) {
this.element = element;
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = simpleContextMenu;
elementID = $(element).attr('id');
elementValue = $(element).attr('id') + Math.floor((Math.random() * 10) + 1);
multiElements[elementID] = elementValue;
this.init();
}
Plugin.prototype = {
init: function () {
var menu = '<div id="' + multiElements[$(this.element).attr('id')] + '" class="SimpleContextMenu">';
this.simpleContextMenuTrigger('#' + multiElements[$(this.element).attr('id')]);
if (this.options.html != undefined) {
menu += this.options.html;
} else {
menu += '<ul>';
$.each(this.options.options, function (text, link) {
menu += Plugin.prototype.renderMenu(link, text);
});
menu += '</ul>';
}
menu += '</div>';
$('body').append(menu);
},
renderMenu: function (link, text) {
return '<li class="' + text.replace(/\s+/, "") + '"><a href="' + link + '">' + text + '</a></li>';
},
simpleContextMenuTrigger: function (currentElement) {
$(this.element).bind('contextmenu', function (e) {
e.preventDefault();
$('.SimpleContextMenu').hide();
yPosition = ((e.pageY + $(currentElement).height()) > $(window).height()) ? e.pageY - $(currentElement).height() : e.pageY;
xPosition = ((e.pageX + $(currentElement).width()) > $(window).width()) ? e.pageX - $(currentElement).width() : e.pageX;
$(currentElement).show();
$(currentElement).css({
left: xPosition,
top: yPosition
});
});
$('*').bind('click', function (e) {
$(currentElement).hide();
});
$('.SimpleContextMenu').bind('contextmenu', function (e) {
$(this).hide();
});
}
};
$.fn[simpleContextMenu] = function (options) {
return this.each(function () {
if (!$.data(this, 'plugin_' + simpleContextMenu)) {
$.data(this, 'plugin_' + simpleContextMenu,
new Plugin(this, options));
}
});
}
})(jQuery, window, document);