-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathcontextmenu.js
71 lines (70 loc) · 2.06 KB
/
contextmenu.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
(function() {
var module = angular.module('PlayerApp');
module.directive('ngContextMenu', function ($parse) {
var renderContextMenu = function ($scope, event, options) {
if (!$) { var $ = angular.element; }
$(event.target).addClass('context');
var $contextMenu = $('<div>');
var $list = $('<div>');
$list.addClass('context-menu');
$list.attr({ 'role': 'menu' });
$list.css({
display: 'block',
position: 'absolute',
left: event.pageX + 'px',
top: event.pageY + 'px'
});
angular.forEach(options, function (item, i) {
var $item = $('<div>');
if (item === null) {
$item.addClass('divider');
} else {
$item.addClass('item');
$a = $('<a>');
$a.attr({ tabindex: '-1', href: '#' });
$a.text(item[0]);
$item.append($a);
$item.on('click', function () {
$scope.$apply(function() {
item[1].call($scope, $scope);
});
});
}
$list.append($item);
});
$contextMenu.append($list);
$contextMenu.css({
width: '100%',
height: '100%',
position: 'absolute',
top: 0,
left: 0,
zIndex: 9999
});
$(document).find('body').append($contextMenu);
$contextMenu.on("click", function (e) {
$(event.target).removeClass('context');
$contextMenu.remove();
e.preventDefault();
}).on('contextmenu', function (event) {
$(event.target).removeClass('context');
event.preventDefault();
$contextMenu.remove();
});
};
return function ($scope, element, attrs) {
element.on('contextmenu', function (event) {
$scope.$apply(function () {
event.preventDefault();
var options = $scope.$eval(attrs.ngContextMenu);
if (options === null) return;
if (options instanceof Array) {
renderContextMenu($scope, event, options);
} else {
throw '"' + attrs.ngContextMenu + '" not an array';
}
});
});
};
});
})();