-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.context.js
52 lines (42 loc) · 1.42 KB
/
jquery.context.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
/* Bootstrap contextmenu
*
* @package bootstrap-contextmenu
* @version 1.0
* @author Jochem Stoel
* @url http://jochemstoel.github.io/
* @license The MIT License (MIT)
*/
$.fn.contextmenu = function($target, $callback) {
function getMenuPosition($mouse, $direction, $scrollDir) {
$win = $(window)[$direction]();
$scroll = $(window)[$scrollDir]();
$menu = $($target)[$direction]();
$position = $mouse + $scroll;
if ($mouse + $menu > $win && $menu < $mouse) {
$position -= $menu;
}
return $position;
}
$(this).on("contextmenu", function ($e) {
if ($e.ctrlKey) return;
$($target)
.data("invokedOn", $($e.target))
.show()
.css({
position: "absolute",
left: getMenuPosition($e.clientX, 'width', 'scrollLeft'),
top: getMenuPosition($e.clientY, 'height', 'scrollTop')
})
.off('click')
.on('click', function ($e) {
$(this).hide();
var $invokedOn = $(this).data("invokedOn");
var $selectedMenu = $($e.target);
$callback.call(this, $invokedOn, $selectedMenu);
});
return false;
});
$(document).click(function () {
$($target).hide();
});
};