2019-02-17 16:08:10 +01:00
|
|
|
let context_menu: JQuery;
|
|
|
|
|
2019-04-04 21:47:52 +02:00
|
|
|
$(document).bind("click", function (e) {
|
2019-02-17 16:08:10 +01:00
|
|
|
let menu = context_menu || (context_menu = $(".context-menu"));
|
|
|
|
|
2019-01-20 18:43:14 +01:00
|
|
|
if(!menu.is(":visible")) return;
|
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
if ($(e.target).parents(".context-menu").length == 0) {
|
|
|
|
despawn_context_menu();
|
2019-04-04 21:47:52 +02:00
|
|
|
e.preventDefault();
|
2018-02-27 17:20:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let contextMenuCloseFn = undefined;
|
2018-09-30 21:50:59 +02:00
|
|
|
function despawn_context_menu() {
|
2019-02-17 16:08:10 +01:00
|
|
|
let menu = context_menu || (context_menu = $(".context-menu"));
|
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
if(!menu.is(":visible")) return;
|
2019-03-25 20:04:04 +01:00
|
|
|
menu.animate({opacity: 0}, 100, () => menu.css("display", "none"));
|
2018-02-27 17:20:49 +01:00
|
|
|
if(contextMenuCloseFn) contextMenuCloseFn();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum MenuEntryType {
|
|
|
|
CLOSE,
|
|
|
|
ENTRY,
|
|
|
|
HR,
|
2018-09-30 21:50:59 +02:00
|
|
|
SUB_MENU
|
2018-02-27 17:20:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class MenuEntry {
|
|
|
|
static HR() {
|
|
|
|
return {
|
|
|
|
callback: () => {},
|
|
|
|
type: MenuEntryType.HR,
|
|
|
|
name: "",
|
|
|
|
icon: ""
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
static CLOSE(callback: () => void) {
|
|
|
|
return {
|
|
|
|
callback: callback,
|
2018-09-30 21:50:59 +02:00
|
|
|
type: MenuEntryType.CLOSE,
|
2018-02-27 17:20:49 +01:00
|
|
|
name: "",
|
|
|
|
icon: ""
|
|
|
|
};
|
|
|
|
}
|
2018-04-11 17:56:09 +02:00
|
|
|
}
|
2018-02-27 17:20:49 +01:00
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
interface ContextMenuEntry {
|
|
|
|
callback?: () => void;
|
2018-02-27 17:20:49 +01:00
|
|
|
type: MenuEntryType;
|
|
|
|
name: (() => string) | string;
|
2018-09-30 21:50:59 +02:00
|
|
|
icon?: (() => string) | string | JQuery;
|
2018-04-11 17:56:09 +02:00
|
|
|
disabled?: boolean;
|
2019-03-07 15:30:53 +01:00
|
|
|
visible?: boolean;
|
2018-02-27 17:20:49 +01:00
|
|
|
|
2019-03-07 15:30:53 +01:00
|
|
|
invalidPermission?: boolean;
|
2018-09-30 21:50:59 +02:00
|
|
|
sub_menu?: ContextMenuEntry[];
|
|
|
|
}
|
2018-02-27 17:20:49 +01:00
|
|
|
|
2018-09-30 21:50:59 +02: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 17:20:49 +01:00
|
|
|
|
2018-09-30 21:50:59 +02: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 17:20:49 +01:00
|
|
|
else icon = "icon " + icon;
|
2018-09-30 21:50:59 +02:00
|
|
|
}
|
2018-02-27 17:20:49 +01:00
|
|
|
|
2018-09-30 21:50:59 +02: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 17:56:09 +02:00
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
tag.append($.spawn("div").addClass("arrow right"));
|
2018-02-27 17:20:49 +01:00
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
if(entry.disabled || entry.invalidPermission) tag.addClass("disabled");
|
|
|
|
else {
|
2019-04-15 15:33:51 +02:00
|
|
|
let menu = $.spawn("div").addClass("sub-menu").addClass("context-menu-container");
|
2019-03-07 15:30:53 +01:00
|
|
|
for(const e of entry.sub_menu) {
|
|
|
|
if(typeof(entry.visible) === 'boolean' && !entry.visible)
|
|
|
|
continue;
|
2018-09-30 21:50:59 +02:00
|
|
|
menu.append(generate_tag(e));
|
2019-03-07 15:30:53 +01:00
|
|
|
}
|
2018-09-30 21:50:59 +02:00
|
|
|
menu.appendTo(tag);
|
2018-02-27 17:20:49 +01:00
|
|
|
}
|
2018-09-30 21:50:59 +02:00
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
return $.spawn("div").text("undefined");
|
|
|
|
}
|
|
|
|
|
|
|
|
function spawn_context_menu(x, y, ...entries: ContextMenuEntry[]) {
|
2019-03-25 20:04:04 +01:00
|
|
|
let menu_tag = context_menu || (context_menu = $(".context-menu"));
|
|
|
|
menu_tag.finish().empty().css("opacity", "0");
|
2019-02-17 16:08:10 +01:00
|
|
|
|
2019-03-25 20:04:04 +01:00
|
|
|
const menu_container = $.spawn("div").addClass("context-menu-container");
|
2018-09-30 21:50:59 +02:00
|
|
|
contextMenuCloseFn = undefined;
|
|
|
|
|
2019-03-07 15:30:53 +01:00
|
|
|
for(const entry of entries){
|
|
|
|
if(typeof(entry.visible) === 'boolean' && !entry.visible)
|
|
|
|
continue;
|
|
|
|
|
2018-09-30 21:50:59 +02:00
|
|
|
if(entry.type == MenuEntryType.CLOSE) {
|
|
|
|
contextMenuCloseFn = entry.callback;
|
|
|
|
} else
|
2019-03-25 20:04:04 +01:00
|
|
|
menu_container.append(generate_tag(entry));
|
2018-02-27 17:20:49 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 20:04:04 +01:00
|
|
|
menu_tag.append(menu_container);
|
|
|
|
menu_tag.animate({opacity: 1}, 100).css("display", "block");
|
|
|
|
|
|
|
|
const width = menu_container.visible_width();
|
|
|
|
if(x + width + 5 > window.innerWidth)
|
|
|
|
menu_container.addClass("left");
|
|
|
|
|
2018-02-27 17:20:49 +01:00
|
|
|
// In the right position (the mouse)
|
2019-03-25 20:04:04 +01:00
|
|
|
menu_tag.css({
|
2018-02-27 17:20:49 +01:00
|
|
|
"top": y + "px",
|
|
|
|
"left": x + "px"
|
|
|
|
});
|
|
|
|
}
|