TeaWeb/shared/js/contextMenu.ts

141 lines
4.2 KiB
TypeScript
Raw Normal View History

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
let context_menu: JQuery;
2018-02-27 17:20:49 +01:00
$(document).bind("mousedown", function (e) {
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
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();
2018-02-27 17:20:49 +01:00
}
});
let contextMenuCloseFn = undefined;
2018-09-30 21:50:59 +02:00
function despawn_context_menu() {
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
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 {
let menu = $.spawn("div").addClass("sub-menu").addClass("context-menu");
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");
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
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"
});
}