TeaWeb/shared/js/contextMenu.ts
WolverinDEV 664f8b2abd
Implemented the Material Design and fixed some bugs (#33)
* cleaned up some files

* Fundamental style update

* Redesigned some style

* fixed hostbanner popup

* Removed old identity stuff

* fixed close listener

* Fixed changelog date

* fixed release chat icons

* fixed url

* Fixed hostbanner

* Uploaded missing images

* Improved update handling

* Improved script files

* Fixed loading error and icon error

* fixed Yes/No modal

* Fixed loader issues with MS Edge

* fixed modal style bug

* Fixed control bar overflow for small devices

* Improved error handling on identity creation

* Logging generate error to terminal

* fixed possible php error

* fixed some possible loading errors when other files have'nt been already loaded.

* removed debug message

* Changed emsrcypten flags

* Improved codec error handling

* removed webassembly as required dependency

* Improved and fixed channel tree issues

* Improved the sliders

* Removed unneeded files

* fixed loader versions cache

* second slight performance improved (dont animate elements anymore if they are not shown)

* Fixed query visibility setting

* not showing useless client infos for query clients

* Added an auto reconnect system

* Added a canceled message and increased reconnect interval

* removed implemented todo

* fixed repetitive channel names

* Reworked the channel tree selected lines

* Fixed channel tree names

* Fixed name alignment

* fixed the native client

* added min width to the server select groups to avoid a disappearing effect on shrink

* fixed bugged downloaded icons
2019-02-17 16:08:10 +01:00

127 lines
No EOL
3.6 KiB
TypeScript

let context_menu: JQuery;
$(document).bind("mousedown", function (e) {
let menu = context_menu || (context_menu = $(".context-menu"));
if(!menu.is(":visible")) return;
if ($(e.target).parents(".context-menu").length == 0) {
despawn_context_menu();
}
});
let contextMenuCloseFn = undefined;
function despawn_context_menu() {
let menu = context_menu || (context_menu = $(".context-menu"));
if(!menu.is(":visible")) return;
menu.hide(100);
if(contextMenuCloseFn) contextMenuCloseFn();
}
enum MenuEntryType {
CLOSE,
ENTRY,
HR,
SUB_MENU
}
class MenuEntry {
static HR() {
return {
callback: () => {},
type: MenuEntryType.HR,
name: "",
icon: ""
};
};
static CLOSE(callback: () => void) {
return {
callback: callback,
type: MenuEntryType.CLOSE,
name: "",
icon: ""
};
}
}
interface ContextMenuEntry {
callback?: () => void;
type: MenuEntryType;
name: (() => string) | string;
icon?: (() => string) | string | JQuery;
disabled?: boolean;
invalidPermission?: boolean;
sub_menu?: ContextMenuEntry[];
}
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;
}
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";
else icon = "icon " + icon;
}
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));
tag.append($.spawn("div").addClass("arrow right"));
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);
}
return tag;
}
return $.spawn("div").text("undefined");
}
function spawn_context_menu(x, y, ...entries: ContextMenuEntry[]) {
let menu = context_menu || (context_menu = $(".context-menu"));
menu.finish().empty();
contextMenuCloseFn = undefined;
for(let entry of entries){
if(entry.type == MenuEntryType.CLOSE) {
contextMenuCloseFn = entry.callback;
} else
menu.append(generate_tag(entry));
}
menu.show(100);
// In the right position (the mouse)
menu.css({
"top": y + "px",
"left": x + "px"
});
}