2018-03-26 08:08:43 +00:00
|
|
|
"use strict";
|
2018-07-12 19:06:17 +00:00
|
|
|
|
2019-11-11 21:39:09 +00:00
|
|
|
import Vue from "vue";
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
const $ = require("jquery");
|
|
|
|
const socket = require("./socket");
|
|
|
|
const utils = require("./utils");
|
|
|
|
const ContextMenu = require("./contextMenu");
|
|
|
|
const contextMenuActions = [];
|
|
|
|
const contextMenuItems = [];
|
2019-11-11 19:18:55 +00:00
|
|
|
const {switchToChannel, navigate} = require("./router");
|
2019-11-03 11:23:03 +00:00
|
|
|
const store = require("./store").default;
|
2018-03-26 08:08:43 +00:00
|
|
|
|
|
|
|
addDefaultItems();
|
2019-10-17 15:32:59 +00:00
|
|
|
registerEvents();
|
2018-03-26 08:08:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for adding context menu items. eg:
|
|
|
|
*
|
|
|
|
* addContextMenuItem({
|
|
|
|
* check: (target) => target.hasClass("user"),
|
|
|
|
* className: "customItemName",
|
2018-04-28 08:19:49 +00:00
|
|
|
* data: (target) => target.attr("data-name"),
|
2018-03-26 08:08:43 +00:00
|
|
|
* displayName: "Do something",
|
|
|
|
* callback: (name) => console.log(name), // print the name of the user to console
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* @param opts
|
|
|
|
* @param {function(Object)} [opts.check] - Function to check whether item should show on the context menu, called with the target jquery element, shows if return is truthy
|
|
|
|
* @param {string|function(Object)} opts.className - class name for the menu item, should be prefixed for non-default menu items (if function, called with jquery element, and uses return value)
|
|
|
|
* @param {string|function(Object)} opts.data - data that will be sent to the callback function (if function, called with jquery element, and uses return value)
|
|
|
|
* @param {string|function(Object)} opts.displayName - text to display on the menu item (if function, called with jquery element, and uses return value)
|
|
|
|
* @param {function(Object)} opts.callback - Function to call when the context menu item is clicked, called with the data requested in opts.data
|
|
|
|
*/
|
|
|
|
function addContextMenuItem(opts) {
|
|
|
|
opts.check = opts.check || (() => true);
|
|
|
|
opts.actionId = contextMenuActions.push(opts.callback) - 1;
|
|
|
|
contextMenuItems.push(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addContextDivider(opts) {
|
|
|
|
opts.check = opts.check || (() => true);
|
|
|
|
opts.divider = true;
|
|
|
|
contextMenuItems.push(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createContextMenu(that, event) {
|
|
|
|
return new ContextMenu(contextMenuItems, contextMenuActions, that, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addWhoisItem() {
|
|
|
|
function whois(itemData) {
|
2019-11-03 11:23:03 +00:00
|
|
|
const chan = store.getters.findChannelOnCurrentNetwork(itemData);
|
2018-03-26 08:08:43 +00:00
|
|
|
|
2018-07-09 17:31:48 +00:00
|
|
|
if (chan) {
|
2019-11-11 19:18:55 +00:00
|
|
|
switchToChannel(chan);
|
2018-03-26 08:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-03-26 08:08:43 +00:00
|
|
|
text: "/whois " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("user"),
|
|
|
|
className: "user",
|
2018-04-28 08:19:49 +00:00
|
|
|
displayName: (target) => target.attr("data-name"),
|
|
|
|
data: (target) => target.attr("data-name"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: whois,
|
|
|
|
});
|
|
|
|
|
|
|
|
addContextDivider({
|
|
|
|
check: (target) => target.hasClass("user"),
|
|
|
|
});
|
|
|
|
|
|
|
|
addContextMenuItem({
|
2018-06-24 15:52:55 +00:00
|
|
|
check: (target) => target.hasClass("user") || target.hasClass("query"),
|
2018-03-26 08:08:43 +00:00
|
|
|
className: "action-whois",
|
|
|
|
displayName: "User information",
|
2018-06-24 15:52:55 +00:00
|
|
|
data: (target) => target.attr("data-name") || target.attr("aria-label"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: whois,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addQueryItem() {
|
|
|
|
function query(itemData) {
|
2019-11-03 11:23:03 +00:00
|
|
|
const chan = store.getters.findChannelOnCurrentNetwork(itemData);
|
2018-03-26 08:08:43 +00:00
|
|
|
|
2018-07-09 17:31:48 +00:00
|
|
|
if (chan) {
|
2019-11-11 19:18:55 +00:00
|
|
|
switchToChannel(chan);
|
2018-03-26 08:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-03-26 08:08:43 +00:00
|
|
|
text: "/query " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("user"),
|
|
|
|
className: "action-query",
|
|
|
|
displayName: "Direct messages",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: query,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:31:44 +00:00
|
|
|
function addCloseItem() {
|
|
|
|
function getCloseDisplay(target) {
|
|
|
|
if (target.hasClass("lobby")) {
|
|
|
|
return "Remove";
|
|
|
|
} else if (target.hasClass("channel")) {
|
|
|
|
return "Leave";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Close";
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("chan"),
|
|
|
|
className: "close",
|
|
|
|
displayName: getCloseDisplay,
|
|
|
|
data: (target) => target.attr("data-target"),
|
2019-03-03 19:43:57 +00:00
|
|
|
callback(itemData) {
|
|
|
|
const close = document.querySelector(
|
|
|
|
`.networks .chan[data-target="${itemData}"] .close`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (close) {
|
|
|
|
// TODO: After context menus are ported to Vue, figure out a direct api
|
|
|
|
close.click();
|
|
|
|
}
|
|
|
|
},
|
2018-04-19 06:31:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addConnectItem() {
|
2018-07-06 18:21:57 +00:00
|
|
|
function connect(itemData) {
|
2018-04-19 06:31:44 +00:00
|
|
|
socket.emit("input", {
|
2018-07-06 18:21:57 +00:00
|
|
|
target: Number(itemData),
|
2018-04-19 06:31:44 +00:00
|
|
|
text: "/connect",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
2018-07-06 18:21:57 +00:00
|
|
|
check: (target) => target.hasClass("lobby") && target.parent().hasClass("not-connected"),
|
2018-04-19 06:31:44 +00:00
|
|
|
className: "connect",
|
|
|
|
displayName: "Connect",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-04-19 06:31:44 +00:00
|
|
|
callback: connect,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addDisconnectItem() {
|
2018-07-06 18:21:57 +00:00
|
|
|
function disconnect(itemData) {
|
2018-04-19 06:31:44 +00:00
|
|
|
socket.emit("input", {
|
2018-07-06 18:21:57 +00:00
|
|
|
target: Number(itemData),
|
2018-04-19 06:31:44 +00:00
|
|
|
text: "/disconnect",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
2018-07-06 18:21:57 +00:00
|
|
|
check: (target) => target.hasClass("lobby") && !target.parent().hasClass("not-connected"),
|
2018-04-19 06:31:44 +00:00
|
|
|
className: "disconnect",
|
|
|
|
displayName: "Disconnect",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-04-19 06:31:44 +00:00
|
|
|
callback: disconnect,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
function addKickItem() {
|
|
|
|
function kick(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-03-26 08:08:43 +00:00
|
|
|
text: "/kick " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
2019-07-17 09:33:59 +00:00
|
|
|
check: (target) =>
|
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"]) &&
|
|
|
|
target.closest(".chan").attr("data-type") === "channel",
|
2018-03-26 08:08:43 +00:00
|
|
|
className: "action-kick",
|
|
|
|
displayName: "Kick",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: kick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:53:41 +00:00
|
|
|
function addOpItem() {
|
|
|
|
function op(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-04-13 15:53:41 +00:00
|
|
|
text: "/op " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) =>
|
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"]) &&
|
2018-04-28 08:19:49 +00:00
|
|
|
!utils.hasRoleInChannel(target.closest(".chan"), ["op"], target.attr("data-name")),
|
2018-04-13 15:53:41 +00:00
|
|
|
className: "action-op",
|
|
|
|
displayName: "Give operator (+o)",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-04-13 15:53:41 +00:00
|
|
|
callback: op,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addDeopItem() {
|
|
|
|
function deop(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-04-13 15:53:41 +00:00
|
|
|
text: "/deop " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) =>
|
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"]) &&
|
2018-04-28 08:19:49 +00:00
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"], target.attr("data-name")),
|
2018-04-13 15:53:41 +00:00
|
|
|
className: "action-op",
|
|
|
|
displayName: "Revoke operator (-o)",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-04-13 15:53:41 +00:00
|
|
|
callback: deop,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVoiceItem() {
|
|
|
|
function voice(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-04-13 15:53:41 +00:00
|
|
|
text: "/voice " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) =>
|
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"]) &&
|
2018-04-28 08:19:49 +00:00
|
|
|
!utils.hasRoleInChannel(target.closest(".chan"), ["voice"], target.attr("data-name")),
|
2018-04-13 15:53:41 +00:00
|
|
|
className: "action-voice",
|
|
|
|
displayName: "Give voice (+v)",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-04-13 15:53:41 +00:00
|
|
|
callback: voice,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addDevoiceItem() {
|
|
|
|
function devoice(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-07-19 10:46:30 +00:00
|
|
|
target: Number($("#chat").attr("data-id")),
|
2018-04-13 15:53:41 +00:00
|
|
|
text: "/devoice " + itemData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) =>
|
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["op"]) &&
|
2018-04-28 08:19:49 +00:00
|
|
|
utils.hasRoleInChannel(target.closest(".chan"), ["voice"], target.attr("data-name")),
|
2018-04-13 15:53:41 +00:00
|
|
|
className: "action-voice",
|
|
|
|
displayName: "Revoke voice (-v)",
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-name"),
|
2018-04-13 15:53:41 +00:00
|
|
|
callback: devoice,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
function addFocusItem() {
|
|
|
|
function focusChan(itemData) {
|
|
|
|
$(`.networks .chan[data-target="${itemData}"]`).click();
|
|
|
|
}
|
|
|
|
|
|
|
|
const getClass = (target) => {
|
|
|
|
if (target.hasClass("lobby")) {
|
|
|
|
return "network";
|
|
|
|
} else if (target.hasClass("query")) {
|
|
|
|
return "query";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "chan";
|
|
|
|
};
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("chan"),
|
|
|
|
className: getClass,
|
2019-01-02 12:09:50 +00:00
|
|
|
displayName: (target) => target.attr("data-name") || target.attr("aria-label"),
|
2018-04-28 08:19:49 +00:00
|
|
|
data: (target) => target.attr("data-target"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: focusChan,
|
|
|
|
});
|
|
|
|
|
|
|
|
addContextDivider({
|
|
|
|
check: (target) => target.hasClass("chan"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-15 08:37:32 +00:00
|
|
|
function addEditNetworkItem() {
|
2019-10-28 13:48:13 +00:00
|
|
|
function edit(networkUuid) {
|
2019-11-11 19:18:55 +00:00
|
|
|
navigate("NetworkEdit", {uuid: networkUuid});
|
2018-03-15 08:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("lobby"),
|
|
|
|
className: "edit",
|
|
|
|
displayName: "Edit this network…",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.closest(".network").attr("data-uuid"),
|
2018-03-15 08:37:32 +00:00
|
|
|
callback: edit,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
function addChannelListItem() {
|
|
|
|
function list(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-05-01 08:36:10 +00:00
|
|
|
target: parseInt(itemData, 10),
|
2018-03-26 08:08:43 +00:00
|
|
|
text: "/list",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("lobby"),
|
|
|
|
className: "list",
|
|
|
|
displayName: "List all channels",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: list,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-03 10:09:55 +00:00
|
|
|
function addEditTopicItem() {
|
|
|
|
function setEditTopic(itemData) {
|
2019-11-03 14:59:43 +00:00
|
|
|
store.getters.findChannel(Number(itemData)).channel.editTopic = true;
|
2019-08-03 10:09:55 +00:00
|
|
|
document.querySelector(`#sidebar .chan[data-id="${Number(itemData)}"]`).click();
|
|
|
|
|
2019-11-11 21:39:09 +00:00
|
|
|
Vue.nextTick(() => {
|
2019-08-11 13:59:06 +00:00
|
|
|
document.querySelector(`#chan-${Number(itemData)} .topic-input`).focus();
|
2019-08-03 10:09:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("channel"),
|
|
|
|
className: "edit",
|
|
|
|
displayName: "Edit topic",
|
|
|
|
data: (target) => target.attr("data-id"),
|
|
|
|
callback: setEditTopic,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
function addBanListItem() {
|
|
|
|
function banlist(itemData) {
|
|
|
|
socket.emit("input", {
|
2018-05-01 08:36:10 +00:00
|
|
|
target: parseInt(itemData, 10),
|
2018-03-26 08:08:43 +00:00
|
|
|
text: "/banlist",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("channel"),
|
|
|
|
className: "list",
|
|
|
|
displayName: "List banned users",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: banlist,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addJoinItem() {
|
|
|
|
function openJoinForm(itemData) {
|
2019-11-03 14:59:43 +00:00
|
|
|
store.getters.findChannel(Number(itemData)).network.isJoinChannelShown = true;
|
2018-03-26 08:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("lobby"),
|
|
|
|
className: "join",
|
|
|
|
displayName: "Join a channel…",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-03-26 08:08:43 +00:00
|
|
|
callback: openJoinForm,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:17:57 +00:00
|
|
|
function addIgnoreListItem() {
|
|
|
|
function ignorelist(itemData) {
|
|
|
|
socket.emit("input", {
|
|
|
|
target: parseInt(itemData, 10),
|
|
|
|
text: "/ignorelist",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addContextMenuItem({
|
|
|
|
check: (target) => target.hasClass("lobby"),
|
|
|
|
className: "list",
|
|
|
|
displayName: "List ignored users",
|
2018-07-19 10:46:30 +00:00
|
|
|
data: (target) => target.attr("data-id"),
|
2018-03-11 18:17:57 +00:00
|
|
|
callback: ignorelist,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-26 08:08:43 +00:00
|
|
|
function addDefaultItems() {
|
2018-06-24 15:52:55 +00:00
|
|
|
addFocusItem();
|
2018-03-26 08:08:43 +00:00
|
|
|
addWhoisItem();
|
|
|
|
addQueryItem();
|
|
|
|
addKickItem();
|
2018-04-13 15:53:41 +00:00
|
|
|
addOpItem();
|
|
|
|
addDeopItem();
|
|
|
|
addVoiceItem();
|
|
|
|
addDevoiceItem();
|
2018-03-15 08:37:32 +00:00
|
|
|
addEditNetworkItem();
|
2018-06-01 09:13:40 +00:00
|
|
|
addJoinItem();
|
2018-03-26 08:08:43 +00:00
|
|
|
addChannelListItem();
|
2019-08-03 10:09:55 +00:00
|
|
|
addEditTopicItem();
|
2018-03-26 08:08:43 +00:00
|
|
|
addBanListItem();
|
2018-03-11 18:17:57 +00:00
|
|
|
addIgnoreListItem();
|
2018-04-19 06:31:44 +00:00
|
|
|
addConnectItem();
|
|
|
|
addDisconnectItem();
|
|
|
|
addCloseItem();
|
2018-03-26 08:08:43 +00:00
|
|
|
}
|
2019-10-17 15:32:59 +00:00
|
|
|
|
|
|
|
function registerEvents() {
|
|
|
|
const viewport = $("#viewport");
|
|
|
|
|
|
|
|
viewport.on("contextmenu", ".network .chan", function(e) {
|
|
|
|
return createContextMenu($(this), e).show();
|
|
|
|
});
|
|
|
|
|
|
|
|
viewport.on("click contextmenu", ".user", function(e) {
|
|
|
|
// If user is selecting text, do not open context menu
|
|
|
|
// This primarily only targets mobile devices where selection is performed with touch
|
|
|
|
if (!window.getSelection().isCollapsed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return createContextMenu($(this), e).show();
|
|
|
|
});
|
|
|
|
|
|
|
|
viewport.on("click", "#chat .menu", function(e) {
|
|
|
|
e.currentTarget = $(
|
|
|
|
`#sidebar .chan[data-id="${$(this)
|
|
|
|
.closest(".chan")
|
|
|
|
.attr("data-id")}"]`
|
|
|
|
)[0];
|
|
|
|
return createContextMenu($(this), e).show();
|
|
|
|
});
|
|
|
|
}
|