Skip to content

Commit 7dcb74d

Browse files
committed
Add base api for context menu
1 parent 2064fd2 commit 7dcb74d

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

client/utils/contextmenu.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
define([
2+
'jQuery',
3+
'underscore'
4+
], function ($, _) {
5+
/*
6+
Adding a contextmenu to a view
7+
8+
ContextMenu.add(this.$el, [
9+
{
10+
'type': "action",
11+
'text': "Test 1",
12+
'action': function() {
13+
alert("test")
14+
}
15+
}
16+
]);
17+
*/
18+
19+
var ContextMenu = {
20+
/*
21+
* Clear context menus
22+
*/
23+
clear: function() {
24+
$("#ui-context-menu").remove();
25+
},
26+
27+
/*
28+
* Create a new context menu
29+
*/
30+
open: function(menuItems, pos) {
31+
ContextMenu.clear();
32+
33+
var $menu = $("<ul>", {
34+
'id': "ui-context-menu",
35+
'class': "dropdown-menu",
36+
'css': _.extend({
37+
'position': "fixed",
38+
'z-index': 100
39+
}, pos)
40+
}).appendTo($("body"));
41+
42+
var addItems = function($subMenu, items) {
43+
_.each(items, _.partial(addItem, $subMenu));
44+
};
45+
46+
var addItem = function($subMenu, item) {
47+
var $li = $("<li>");
48+
49+
if (item.type == "action") {
50+
var $a = $("<a>", {
51+
'text': item.text,
52+
'href': "#",
53+
'click': function(e) {
54+
e.preventDefault();
55+
ContextMenu.clear();
56+
item.action(item);
57+
}
58+
});
59+
$a.appendTo($li);
60+
}
61+
62+
$li.appendTo($subMenu);
63+
};
64+
65+
addItems($menu, menuItems);
66+
$menu.show();
67+
},
68+
69+
/*
70+
* Add a context menu to an element
71+
*
72+
*/
73+
add: function(el, menu, options) {
74+
var $el = $(el);
75+
76+
options = _.defaults({}, options, {
77+
// Menu accessible in textinput
78+
'textinput': false
79+
});
80+
81+
var handler = function(e) {
82+
var target = e.target || e.srcElement;
83+
84+
// Ignore contextmenu on textinput
85+
if (!options.textinput &&
86+
(target.tagName == 'INPUT' || target.tagName == 'SELECT' || target.tagName == 'TEXTAREA' || target.isContentEditable)) {
87+
return;
88+
}
89+
90+
91+
ContextMenu.open(menu, {
92+
left: e.pageX,
93+
top: e.pageY
94+
});
95+
return false;
96+
}
97+
98+
$el.on("contextmenu", handler);
99+
}
100+
};
101+
102+
$(document).click(function () {
103+
ContextMenu.clear();
104+
});
105+
$(document).on("contextmenu", function() {
106+
ContextMenu.clear();
107+
});
108+
109+
$.fn.contextmenu = function (menu) {
110+
var $this = this;
111+
return (function () {
112+
ContextMenu.add($this, menu);
113+
})();
114+
};
115+
return ContextMenu;
116+
});

client/views/layouts/lateralbar.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ define([
55
"core/box",
66
"core/commands",
77
"core/files",
8-
"utils/dialogs"
9-
], function(_, $, hr, box, commands, files, Dialogs) {
8+
"utils/dialogs",
9+
"utils/contextmenu"
10+
], function(_, $, hr, box, commands, files, Dialogs, ContextMenu) {
1011

1112
var LateralBarView = hr.View.extend({
1213
className: "layout-lateralbar",

0 commit comments

Comments
 (0)