-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextMenu.js
83 lines (80 loc) · 2.62 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
72
73
74
75
76
77
78
79
80
81
82
83
/**
script: ContextMenu.js
name: ContextMenu
description: Class that converts anchor tags inside the target element into a ContextMenu
license: MIT-style license
requires:
- Core/MooTools
- Core/More
- Bootstrap.css
Known limitations:
- Doesn't work with nested element with nested Context menu
*/
var ContextMenu = new Class({
Implements: [Events, Options],
Binds: ["show", "hide"],
evt: null,
initialize: function(context, options) {
var self = this;
self.setOptions(options);
self.context = context;
self.context.getElements('.actions').setStyle('display', 'none');
self.evt = 'contextmenu' + (self.options.relay ? ':relay(' + self.options.relay + ')': '');
self.context.addEvent(self.evt, self.show);
document.body.addEvent('click', self.hide);
},
show: function(evt, elem) {
document.body.fireEvent('click');
//Firing click will close other existing instances of ContextMenu
evt.stop();
var self = this;
if (!elem) {
elem = self.context;
}
var actions = elem.getElements('.actions a');
//Get previous sibling of .action element and attach Object to it.
elem.getElement('.actions !~').adopt(self.getContainer());
self.menuList.empty();
actions.each(function(a) {
var li = new Element('li');
li.adopt(a.clone());
self.menuList.adopt(li);
});
self.container.setStyle('opacity', 0);
self.container.setPosition(evt.page);
self.container.fade('in');
self.fireEvent('show', [evt, elem]);
},
hide: function(e) {
var self = this;
if (self.container) {
self.container.fade("out");
}
},
getContainer: function() {
var self = this;
if (!self.container) {
self.container = new Element('div.dropdown', {
'styles': {
'position': 'absolute'
}
});
self.menuList = new Element('ul.dropdown-menu', {
'styles': {
'display': 'block'
}
});
self.container.adopt(self.menuList);
self.container.get('tween').options.duration = 110;
}
return this.container;
},
dispose: function() {
var self = this;
if (self.container) self.container.dispose();
self.context.removeEvent(self.evt, self.show);
document.body.removeEvent('click', self.hide);
self.context.getElements('.actions').setStyle('display', 'inherit');
delete this;
}
});