TeaWeb/shared/js/contextMenu.ts

118 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-02-27 16:20:49 +00:00
$(document).bind("mousedown", function (e) {
2018-09-30 19:50:59 +00:00
if ($(e.target).parents(".context-menu").length == 0) {
despawn_context_menu();
2018-02-27 16:20:49 +00:00
}
});
let contextMenuCloseFn = undefined;
2018-09-30 19:50:59 +00:00
function despawn_context_menu() {
let menu = $(".context-menu");
if(!menu.is(":visible")) return;
menu.hide(100);
2018-02-27 16:20:49 +00:00
if(contextMenuCloseFn) contextMenuCloseFn();
}
enum MenuEntryType {
CLOSE,
ENTRY,
HR,
2018-09-30 19:50:59 +00:00
SUB_MENU
2018-02-27 16:20:49 +00:00
}
class MenuEntry {
static HR() {
return {
callback: () => {},
type: MenuEntryType.HR,
name: "",
icon: ""
};
};
static CLOSE(callback: () => void) {
return {
callback: callback,
2018-09-30 19:50:59 +00:00
type: MenuEntryType.CLOSE,
2018-02-27 16:20:49 +00:00
name: "",
icon: ""
};
}
2018-04-11 15:56:09 +00:00
}
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
interface ContextMenuEntry {
callback?: () => void;
2018-02-27 16:20:49 +00:00
type: MenuEntryType;
name: (() => string) | string;
2018-09-30 19:50:59 +00:00
icon?: (() => string) | string | JQuery;
2018-04-11 15:56:09 +00:00
disabled?: boolean;
2018-04-16 18:38:35 +00:00
invalidPermission?: boolean;
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
sub_menu?: ContextMenuEntry[];
}
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
function generate_tag(entry: ContextMenuEntry) : JQuery {
if(entry.type == MenuEntryType.HR) {
return $.spawn("hr");
} else if(entry.type == MenuEntryType.ENTRY) {
console.log(entry.icon);
let icon = $.isFunction(entry.icon) ? entry.icon() : entry.icon;
if(typeof(icon) === "string") {
if(!icon || icon.length == 0) icon = "icon_empty";
else icon = "icon " + icon;
}
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
let tag = $.spawn("div").addClass("entry");
tag.append(typeof(icon) === "string" ? $.spawn("div").addClass(icon) : icon);
tag.append($.spawn("div").html($.isFunction(entry.name) ? entry.name() : entry.name));
if(entry.disabled || entry.invalidPermission) tag.addClass("disabled");
else {
tag.click(function () {
if($.isFunction(entry.callback)) entry.callback();
despawn_context_menu();
});
}
return tag;
} else if(entry.type == MenuEntryType.SUB_MENU) {
let icon = $.isFunction(entry.icon) ? entry.icon() : entry.icon;
if(typeof(icon) === "string") {
if(!icon || icon.length == 0) icon = "icon_empty";
2018-02-27 16:20:49 +00:00
else icon = "icon " + icon;
2018-09-30 19:50:59 +00:00
}
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
let tag = $.spawn("div").addClass("entry").addClass("sub-container");
tag.append(typeof(icon) === "string" ? $.spawn("div").addClass(icon) : icon);
tag.append($.spawn("div").html($.isFunction(entry.name) ? entry.name() : entry.name));
2018-04-11 15:56:09 +00:00
2018-09-30 19:50:59 +00:00
tag.append($.spawn("div").addClass("arrow right"));
2018-02-27 16:20:49 +00:00
2018-09-30 19:50:59 +00:00
if(entry.disabled || entry.invalidPermission) tag.addClass("disabled");
else {
let menu = $.spawn("div").addClass("sub-menu").addClass("context-menu");
for(let e of entry.sub_menu)
menu.append(generate_tag(e));
menu.appendTo(tag);
2018-02-27 16:20:49 +00:00
}
2018-09-30 19:50:59 +00:00
return tag;
}
return $.spawn("div").text("undefined");
}
function spawn_context_menu(x, y, ...entries: ContextMenuEntry[]) {
const menu = $("#contextMenu").finish().empty();
contextMenuCloseFn = undefined;
for(let entry of entries){
if(entry.type == MenuEntryType.CLOSE) {
contextMenuCloseFn = entry.callback;
} else
menu.append(generate_tag(entry));
2018-02-27 16:20:49 +00:00
}
menu.show(100);
// In the right position (the mouse)
menu.css({
"top": y + "px",
"left": x + "px"
});
}