2017-03-18 09:21:18 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-18 15:53:28 +00:00
|
|
|
// vendor libraries
|
2017-03-18 09:21:18 +00:00
|
|
|
require("jquery-ui/ui/widgets/sortable");
|
2016-12-11 00:13:26 +00:00
|
|
|
require("jquery-textcomplete");
|
2017-03-18 09:21:18 +00:00
|
|
|
const $ = require("jquery");
|
2017-04-22 04:06:07 +00:00
|
|
|
const moment = require("moment");
|
2017-03-18 09:21:18 +00:00
|
|
|
const Mousetrap = require("mousetrap");
|
|
|
|
const URI = require("urijs");
|
2016-12-30 05:32:27 +00:00
|
|
|
const fuzzy = require("fuzzy");
|
2016-12-18 15:53:28 +00:00
|
|
|
|
|
|
|
// our libraries
|
2016-12-11 00:13:26 +00:00
|
|
|
const emojiMap = require("./libs/simplemap.json");
|
2017-03-18 09:21:18 +00:00
|
|
|
require("./libs/jquery/inputhistory");
|
|
|
|
require("./libs/jquery/stickyscroll");
|
|
|
|
require("./libs/jquery/tabcomplete");
|
|
|
|
const helpers_parse = require("./libs/handlebars/parse");
|
|
|
|
const helpers_roundBadgeNumber = require("./libs/handlebars/roundBadgeNumber");
|
|
|
|
const slideoutMenu = require("./libs/slideout");
|
|
|
|
const templates = require("../views");
|
2017-04-18 07:31:46 +00:00
|
|
|
const socket = require("./socket");
|
2017-04-18 07:42:26 +00:00
|
|
|
const constants = require("./constants");
|
2017-04-22 13:03:00 +00:00
|
|
|
const storage = require("./localStorage");
|
2016-10-09 19:14:02 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
$(function() {
|
2014-09-19 23:12:17 +00:00
|
|
|
var sidebar = $("#sidebar, #footer");
|
2014-08-16 16:15:17 +00:00
|
|
|
var chat = $("#chat");
|
2014-09-10 19:23:56 +00:00
|
|
|
|
2017-06-05 11:40:25 +00:00
|
|
|
$(document.body).data("app-name", document.title);
|
|
|
|
|
2016-11-22 12:14:17 +00:00
|
|
|
var ignoreSortSync = false;
|
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
var pop;
|
2014-09-10 17:04:27 +00:00
|
|
|
try {
|
2015-09-30 22:39:57 +00:00
|
|
|
pop = new Audio();
|
2016-02-13 21:24:43 +00:00
|
|
|
pop.src = "audio/pop.ogg";
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
pop = {
|
2014-09-10 17:04:27 +00:00
|
|
|
play: $.noop
|
|
|
|
};
|
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-05-01 09:41:17 +00:00
|
|
|
$("#play").on("click", function() {
|
|
|
|
pop.play();
|
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-02-24 17:19:05 +00:00
|
|
|
var favicon = $("#favicon");
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-12-11 00:13:26 +00:00
|
|
|
// Autocompletion Strategies
|
|
|
|
|
2017-04-24 23:01:24 +00:00
|
|
|
const emojiStrategy = {
|
2016-12-11 00:13:26 +00:00
|
|
|
id: "emoji",
|
2017-05-05 18:08:41 +00:00
|
|
|
match: /\B:([-+\w]*):?$/,
|
2017-04-24 23:01:24 +00:00
|
|
|
search(term, callback) {
|
2017-04-08 12:34:31 +00:00
|
|
|
callback(Object.keys(emojiMap).filter((name) => name.indexOf(term) === 0));
|
2016-12-11 00:13:26 +00:00
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
template(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return `<span class="emoji">${emojiMap[value]}</span> ${value}`;
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
replace(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return emojiMap[value];
|
|
|
|
},
|
|
|
|
index: 1
|
|
|
|
};
|
|
|
|
|
2017-04-24 23:01:24 +00:00
|
|
|
const nicksStrategy = {
|
2016-12-11 00:13:26 +00:00
|
|
|
id: "nicks",
|
|
|
|
match: /\B(@([a-zA-Z_[\]\\^{}|`@][a-zA-Z0-9_[\]\\^{}|`-]*)?)$/,
|
2017-04-24 23:01:24 +00:00
|
|
|
search(term, callback) {
|
2016-12-11 00:13:26 +00:00
|
|
|
term = term.slice(1);
|
|
|
|
if (term[0] === "@") {
|
2017-04-08 12:34:31 +00:00
|
|
|
callback(completeNicks(term.slice(1)).map((val) => "@" + val));
|
2016-12-11 00:13:26 +00:00
|
|
|
} else {
|
|
|
|
callback(completeNicks(term));
|
|
|
|
}
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
template(value) {
|
|
|
|
return value;
|
2016-12-11 00:13:26 +00:00
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
replace(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
index: 1
|
|
|
|
};
|
|
|
|
|
2017-04-24 23:01:24 +00:00
|
|
|
const chanStrategy = {
|
2016-12-11 00:13:26 +00:00
|
|
|
id: "chans",
|
|
|
|
match: /\B((#|\+|&|![A-Z0-9]{5})([^\x00\x0A\x0D\x20\x2C\x3A]+(:[^\x00\x0A\x0D\x20\x2C\x3A]*)?)?)$/,
|
2017-04-24 23:01:24 +00:00
|
|
|
search(term, callback, match) {
|
2016-12-11 00:13:26 +00:00
|
|
|
callback(completeChans(match[0]));
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
template(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return value;
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
replace(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
index: 1
|
|
|
|
};
|
|
|
|
|
2017-04-24 23:01:24 +00:00
|
|
|
const commandStrategy = {
|
2016-12-11 00:13:26 +00:00
|
|
|
id: "commands",
|
|
|
|
match: /^\/(\w*)$/,
|
2017-04-24 23:01:24 +00:00
|
|
|
search(term, callback) {
|
2016-12-11 00:13:26 +00:00
|
|
|
callback(completeCommands("/" + term));
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
template(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return value;
|
|
|
|
},
|
2017-04-24 23:01:24 +00:00
|
|
|
replace(value) {
|
2016-12-11 00:13:26 +00:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
index: 1
|
|
|
|
};
|
|
|
|
|
2017-04-28 21:40:26 +00:00
|
|
|
const foregroundColorStrategy = {
|
|
|
|
id: "foreground-colors",
|
|
|
|
match: /\x03(\d{0,2}|[A-Za-z ]{0,10})$/,
|
|
|
|
search(term, callback) {
|
|
|
|
term = term.toLowerCase();
|
|
|
|
const matchingColorCodes = constants.colorCodeMap
|
2017-04-08 12:34:31 +00:00
|
|
|
.filter((i) => i[0].startsWith(term) || i[1].toLowerCase().startsWith(term));
|
2017-04-28 21:40:26 +00:00
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
|
|
|
template(value) {
|
|
|
|
return `<span class="irc-fg${parseInt(value[0], 10)}">${value[1]}</span>`;
|
|
|
|
},
|
|
|
|
replace(value) {
|
|
|
|
return "\x03" + value[0];
|
|
|
|
},
|
|
|
|
index: 1
|
|
|
|
};
|
|
|
|
|
2017-04-30 10:18:21 +00:00
|
|
|
const backgroundColorStrategy = {
|
|
|
|
id: "background-colors",
|
|
|
|
match: /\x03(\d{2}),(\d{0,2}|[A-Za-z ]{0,10})$/,
|
|
|
|
search(term, callback, match) {
|
|
|
|
term = term.toLowerCase();
|
|
|
|
const matchingColorCodes = constants.colorCodeMap
|
2017-04-08 12:34:31 +00:00
|
|
|
.filter((i) => i[0].startsWith(term) || i[1].toLowerCase().startsWith(term))
|
|
|
|
.map((pair) => pair.concat(match[1])); // Needed to pass fg color to `template`...
|
2017-04-30 10:18:21 +00:00
|
|
|
|
|
|
|
callback(matchingColorCodes);
|
|
|
|
},
|
|
|
|
template(value) {
|
|
|
|
return `<span class="irc-fg${parseInt(value[2], 10)} irc-bg irc-bg${parseInt(value[0], 10)}">${value[1]}</span>`;
|
|
|
|
},
|
|
|
|
replace(value) {
|
|
|
|
return "\x03$1," + value[0];
|
|
|
|
},
|
|
|
|
index: 2
|
|
|
|
};
|
|
|
|
|
2016-05-31 21:02:10 +00:00
|
|
|
socket.on("auth", function(data) {
|
2014-08-16 16:15:17 +00:00
|
|
|
var login = $("#sign-in");
|
2016-10-09 08:54:44 +00:00
|
|
|
var token;
|
2016-05-31 21:02:10 +00:00
|
|
|
|
2014-10-09 14:35:29 +00:00
|
|
|
login.find(".btn").prop("disabled", false);
|
2016-05-31 21:02:10 +00:00
|
|
|
|
|
|
|
if (!data.success) {
|
2017-04-22 13:03:00 +00:00
|
|
|
storage.remove("token");
|
2016-05-31 21:02:10 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
var error = login.find(".error");
|
|
|
|
error.show().closest("form").one("submit", function() {
|
|
|
|
error.hide();
|
|
|
|
});
|
2016-05-31 21:02:10 +00:00
|
|
|
} else {
|
2017-04-22 13:03:00 +00:00
|
|
|
token = storage.get("token");
|
2016-05-31 21:02:10 +00:00
|
|
|
if (token) {
|
2016-06-07 17:56:49 +00:00
|
|
|
$("#loading-page-message").text("Authorizing…");
|
2016-05-31 21:02:10 +00:00
|
|
|
socket.emit("auth", {token: token});
|
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
2016-05-31 21:02:10 +00:00
|
|
|
|
2014-09-10 19:33:15 +00:00
|
|
|
var input = login.find("input[name='user']");
|
|
|
|
if (input.val() === "") {
|
2017-04-22 13:03:00 +00:00
|
|
|
input.val(storage.get("user") || "");
|
2014-09-10 19:33:15 +00:00
|
|
|
}
|
2014-10-08 20:16:10 +00:00
|
|
|
if (token) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sidebar.find(".sign-in")
|
2017-01-28 17:48:34 +00:00
|
|
|
.trigger("click", {
|
|
|
|
pushState: false,
|
|
|
|
})
|
2014-10-08 20:16:10 +00:00
|
|
|
.end()
|
|
|
|
.find(".networks")
|
|
|
|
.html("")
|
|
|
|
.next()
|
|
|
|
.show();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2016-02-17 00:14:43 +00:00
|
|
|
socket.on("change-password", function(data) {
|
|
|
|
var passwordForm = $("#change-password");
|
|
|
|
if (data.error || data.success) {
|
|
|
|
var message = data.success ? data.success : data.error;
|
|
|
|
var feedback = passwordForm.find(".feedback");
|
|
|
|
|
|
|
|
if (data.success) {
|
|
|
|
feedback.addClass("success").removeClass("error");
|
|
|
|
} else {
|
|
|
|
feedback.addClass("error").removeClass("success");
|
|
|
|
}
|
|
|
|
|
|
|
|
feedback.text(message).show();
|
|
|
|
feedback.closest("form").one("submit", function() {
|
|
|
|
feedback.hide();
|
|
|
|
});
|
|
|
|
}
|
2016-05-31 21:28:31 +00:00
|
|
|
|
2017-04-22 13:03:00 +00:00
|
|
|
if (data.token && storage.get("token") !== null) {
|
|
|
|
storage.set("token", data.token);
|
2016-05-31 21:28:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 00:14:43 +00:00
|
|
|
passwordForm
|
|
|
|
.find("input")
|
|
|
|
.val("")
|
|
|
|
.end()
|
|
|
|
.find(".btn")
|
|
|
|
.prop("disabled", false);
|
|
|
|
});
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
socket.on("init", function(data) {
|
2016-09-25 06:52:16 +00:00
|
|
|
$("#loading-page-message").text("Rendering…");
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
if (data.networks.length === 0) {
|
2017-01-28 17:48:34 +00:00
|
|
|
$("#footer").find(".connect").trigger("click", {
|
|
|
|
pushState: false,
|
|
|
|
});
|
2014-09-14 19:13:34 +00:00
|
|
|
} else {
|
2016-07-02 08:26:23 +00:00
|
|
|
renderNetworks(data);
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 21:28:31 +00:00
|
|
|
if (data.token && $("#sign-in-remember").is(":checked")) {
|
2017-04-22 13:03:00 +00:00
|
|
|
storage.set("token", data.token);
|
2016-05-31 21:28:31 +00:00
|
|
|
} else {
|
2017-04-22 13:03:00 +00:00
|
|
|
storage.remove("token");
|
2014-09-15 21:13:03 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
$("body").removeClass("signed-out");
|
2016-06-21 05:52:18 +00:00
|
|
|
$("#loading").remove();
|
|
|
|
$("#sign-in").remove();
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2014-09-21 16:46:43 +00:00
|
|
|
var id = data.active;
|
2017-04-24 11:20:48 +00:00
|
|
|
var target = sidebar.find("[data-id='" + id + "']").trigger("click", {
|
|
|
|
replaceHistory: true
|
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
if (target.length === 0) {
|
|
|
|
var first = sidebar.find(".chan")
|
|
|
|
.eq(0)
|
|
|
|
.trigger("click");
|
|
|
|
if (first.length === 0) {
|
2017-01-28 17:48:34 +00:00
|
|
|
$("#footer").find(".connect").trigger("click", {
|
|
|
|
pushState: false,
|
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-25 06:41:10 +00:00
|
|
|
socket.on("open", function(id) {
|
|
|
|
// Another client opened the channel, clear the unread counter
|
2016-10-25 11:58:28 +00:00
|
|
|
sidebar.find(".chan[data-id='" + id + "'] .badge")
|
2016-09-25 06:41:10 +00:00
|
|
|
.removeClass("highlight")
|
|
|
|
.empty();
|
|
|
|
});
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
socket.on("join", function(data) {
|
|
|
|
var id = data.network;
|
|
|
|
var network = sidebar.find("#network-" + id);
|
|
|
|
network.append(
|
2016-12-18 15:53:28 +00:00
|
|
|
templates.chan({
|
2014-08-16 16:15:17 +00:00
|
|
|
channels: [data.chan]
|
|
|
|
})
|
|
|
|
);
|
|
|
|
chat.append(
|
2016-12-18 15:53:28 +00:00
|
|
|
templates.chat({
|
2014-08-16 16:15:17 +00:00
|
|
|
channels: [data.chan]
|
|
|
|
})
|
|
|
|
);
|
2016-03-23 10:15:44 +00:00
|
|
|
renderChannel(data.chan);
|
2016-05-30 19:38:53 +00:00
|
|
|
|
|
|
|
// Queries do not automatically focus, unless the user did a whois
|
|
|
|
if (data.chan.type === "query" && !data.shouldOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sidebar.find(".chan")
|
2016-05-01 09:41:17 +00:00
|
|
|
.sort(function(a, b) {
|
|
|
|
return $(a).data("id") - $(b).data("id");
|
|
|
|
})
|
2016-05-30 19:38:53 +00:00
|
|
|
.last()
|
|
|
|
.click();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2016-01-20 13:32:14 +00:00
|
|
|
function buildChatMessage(data) {
|
2016-01-23 14:39:54 +00:00
|
|
|
var type = data.msg.type;
|
2014-10-08 21:21:41 +00:00
|
|
|
var target = "#chan-" + data.chan;
|
2016-01-23 14:39:54 +00:00
|
|
|
if (type === "error") {
|
2014-10-08 21:21:41 +00:00
|
|
|
target = "#chan-" + chat.find(".active").data("id");
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
2014-10-08 21:21:41 +00:00
|
|
|
|
|
|
|
var chan = chat.find(target);
|
2016-04-24 15:12:54 +00:00
|
|
|
var template = "msg";
|
2014-10-08 21:21:41 +00:00
|
|
|
|
2017-04-22 13:03:00 +00:00
|
|
|
if (!data.msg.highlight && !data.msg.self && (type === "message" || type === "notice") && options.highlights.some(function(h) {
|
2016-08-16 04:24:20 +00:00
|
|
|
return data.msg.text.toLocaleLowerCase().indexOf(h.toLocaleLowerCase()) > -1;
|
2016-06-21 19:27:49 +00:00
|
|
|
})) {
|
|
|
|
data.msg.highlight = true;
|
|
|
|
}
|
|
|
|
|
2016-01-23 14:39:54 +00:00
|
|
|
if ([
|
2016-02-12 11:24:13 +00:00
|
|
|
"invite",
|
2016-01-23 14:39:54 +00:00
|
|
|
"join",
|
|
|
|
"mode",
|
|
|
|
"kick",
|
|
|
|
"nick",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"topic",
|
2016-03-08 09:26:43 +00:00
|
|
|
"topic_set_by",
|
2016-01-23 14:39:54 +00:00
|
|
|
"action",
|
2016-03-07 15:10:46 +00:00
|
|
|
"whois",
|
2016-03-27 15:57:59 +00:00
|
|
|
"ctcp",
|
2016-03-09 20:04:07 +00:00
|
|
|
"channel_list",
|
2017-04-22 12:51:21 +00:00
|
|
|
"ban_list",
|
2016-01-23 14:39:54 +00:00
|
|
|
].indexOf(type) !== -1) {
|
2016-04-24 15:12:54 +00:00
|
|
|
template = "msg_action";
|
|
|
|
} else if (type === "unhandled") {
|
|
|
|
template = "msg_unhandled";
|
2016-01-23 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
2016-12-18 15:53:28 +00:00
|
|
|
var msg = $(templates[template](data.msg));
|
2015-03-28 18:47:15 +00:00
|
|
|
var text = msg.find(".text");
|
2016-12-18 15:53:28 +00:00
|
|
|
|
|
|
|
if (template === "msg_action") {
|
|
|
|
text.html(templates.actions[type](data.msg));
|
|
|
|
}
|
|
|
|
|
2016-01-23 14:39:54 +00:00
|
|
|
if ((type === "message" || type === "action") && chan.hasClass("channel")) {
|
|
|
|
var nicks = chan.find(".users").data("nicks");
|
|
|
|
if (nicks) {
|
2016-03-07 15:10:46 +00:00
|
|
|
var find = nicks.indexOf(data.msg.from);
|
2017-04-27 18:32:58 +00:00
|
|
|
if (find !== -1) {
|
|
|
|
nicks.splice(find, 1);
|
|
|
|
nicks.unshift(data.msg.from);
|
2014-10-08 21:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 13:32:14 +00:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildChannelMessages(channel, messages) {
|
|
|
|
return messages.reduce(function(docFragment, message) {
|
|
|
|
docFragment.append(buildChatMessage({
|
|
|
|
chan: channel,
|
|
|
|
msg: message
|
|
|
|
}));
|
|
|
|
return docFragment;
|
|
|
|
}, $(document.createDocumentFragment()));
|
|
|
|
}
|
|
|
|
|
2016-03-23 10:15:44 +00:00
|
|
|
function renderChannel(data) {
|
|
|
|
renderChannelMessages(data);
|
2017-04-22 12:49:01 +00:00
|
|
|
|
|
|
|
if (data.type === "channel") {
|
|
|
|
renderChannelUsers(data);
|
|
|
|
}
|
2016-03-23 10:15:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 13:32:14 +00:00
|
|
|
function renderChannelMessages(data) {
|
|
|
|
var documentFragment = buildChannelMessages(data.id, data.messages);
|
2016-05-13 10:23:05 +00:00
|
|
|
var channel = chat.find("#chan-" + data.id + " .messages").append(documentFragment);
|
|
|
|
|
|
|
|
if (data.firstUnread > 0) {
|
|
|
|
var first = channel.find("#msg-" + data.firstUnread);
|
|
|
|
|
|
|
|
// TODO: If the message is far off in the history, we still need to append the marker into DOM
|
|
|
|
if (!first.length) {
|
2016-12-18 15:53:28 +00:00
|
|
|
channel.prepend(templates.unread_marker());
|
2016-05-13 10:23:05 +00:00
|
|
|
} else {
|
2016-12-18 15:53:28 +00:00
|
|
|
first.before(templates.unread_marker());
|
2016-05-13 10:23:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-18 15:53:28 +00:00
|
|
|
channel.append(templates.unread_marker());
|
2016-05-13 10:23:05 +00:00
|
|
|
}
|
2016-10-03 18:03:19 +00:00
|
|
|
|
|
|
|
if (data.type !== "lobby") {
|
|
|
|
var lastDate;
|
|
|
|
$(chat.find("#chan-" + data.id + " .messages .msg[data-time]")).each(function() {
|
|
|
|
var msg = $(this);
|
|
|
|
var msgDate = new Date(msg.attr("data-time"));
|
|
|
|
|
|
|
|
// Top-most message in a channel
|
|
|
|
if (!lastDate) {
|
|
|
|
lastDate = msgDate;
|
2016-12-18 15:53:28 +00:00
|
|
|
msg.before(templates.date_marker({msgDate: msgDate}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastDate.toDateString() !== msgDate.toDateString()) {
|
2016-12-18 15:53:28 +00:00
|
|
|
msg.before(templates.date_marker({msgDate: msgDate}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastDate = msgDate;
|
|
|
|
});
|
|
|
|
}
|
2016-01-20 13:32:14 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 10:15:44 +00:00
|
|
|
function renderChannelUsers(data) {
|
|
|
|
var users = chat.find("#chan-" + data.id).find(".users");
|
|
|
|
var nicks = users.data("nicks") || [];
|
|
|
|
var i, oldSortOrder = {};
|
|
|
|
|
|
|
|
for (i in nicks) {
|
|
|
|
oldSortOrder[nicks[i]] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
nicks = [];
|
|
|
|
|
|
|
|
for (i in data.users) {
|
2017-06-01 18:54:46 +00:00
|
|
|
nicks.push(data.users[i].nick);
|
2016-03-23 10:15:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nicks = nicks.sort(function(a, b) {
|
|
|
|
return (oldSortOrder[a] || Number.MAX_VALUE) - (oldSortOrder[b] || Number.MAX_VALUE);
|
|
|
|
});
|
|
|
|
|
2017-04-22 12:49:01 +00:00
|
|
|
const search = users
|
|
|
|
.find(".search")
|
|
|
|
.attr("placeholder", nicks.length + " " + (nicks.length === 1 ? "user" : "users"));
|
|
|
|
|
|
|
|
users
|
2017-04-27 18:32:58 +00:00
|
|
|
.data("nicks", nicks)
|
2017-04-22 12:49:01 +00:00
|
|
|
.find(".names-original")
|
2017-04-27 18:32:58 +00:00
|
|
|
.html(templates.user(data));
|
2017-04-22 12:49:01 +00:00
|
|
|
|
|
|
|
// Refresh user search
|
|
|
|
if (search.val().length) {
|
|
|
|
search.trigger("input");
|
|
|
|
}
|
2016-03-23 10:15:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 08:26:23 +00:00
|
|
|
function renderNetworks(data) {
|
|
|
|
sidebar.find(".empty").hide();
|
|
|
|
sidebar.find(".networks").append(
|
2016-12-18 15:53:28 +00:00
|
|
|
templates.network({
|
2016-07-02 08:26:23 +00:00
|
|
|
networks: data.networks
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
var channels = $.map(data.networks, function(n) {
|
|
|
|
return n.channels;
|
|
|
|
});
|
|
|
|
chat.append(
|
2016-12-18 15:53:28 +00:00
|
|
|
templates.chat({
|
2016-07-02 08:26:23 +00:00
|
|
|
channels: channels
|
|
|
|
})
|
|
|
|
);
|
|
|
|
channels.forEach(renderChannel);
|
|
|
|
|
|
|
|
confirmExit();
|
|
|
|
sortable();
|
|
|
|
|
|
|
|
if (sidebar.find(".highlight").length) {
|
|
|
|
toggleNotificationMarkers(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 13:32:14 +00:00
|
|
|
socket.on("msg", function(data) {
|
|
|
|
var msg = buildChatMessage(data);
|
|
|
|
var target = "#chan-" + data.chan;
|
2016-05-13 10:23:05 +00:00
|
|
|
var container = chat.find(target + " .messages");
|
|
|
|
|
2017-04-22 12:51:21 +00:00
|
|
|
if (data.msg.type === "channel_list" || data.msg.type === "ban_list") {
|
2017-04-06 16:22:56 +00:00
|
|
|
$(container).empty();
|
|
|
|
}
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
// Check if date changed
|
2016-10-03 18:03:19 +00:00
|
|
|
var prevMsg = $(container.find(".msg")).last();
|
|
|
|
var prevMsgTime = new Date(prevMsg.attr("data-time"));
|
|
|
|
var msgTime = new Date(msg.attr("data-time"));
|
|
|
|
|
|
|
|
// It's the first message in a channel/query
|
|
|
|
if (prevMsg.length === 0) {
|
2016-12-18 15:53:28 +00:00
|
|
|
container.append(templates.date_marker({msgDate: msgTime}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prevMsgTime.toDateString() !== msgTime.toDateString()) {
|
2017-04-01 08:06:01 +00:00
|
|
|
prevMsg.after(templates.date_marker({msgDate: msgTime}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
// Add message to the container
|
2016-10-14 15:49:08 +00:00
|
|
|
container
|
|
|
|
.append(msg)
|
|
|
|
.trigger("msg", [
|
|
|
|
target,
|
2016-09-25 06:41:10 +00:00
|
|
|
data
|
2016-10-14 15:49:08 +00:00
|
|
|
]);
|
2016-05-13 10:23:05 +00:00
|
|
|
|
|
|
|
if (data.msg.self) {
|
|
|
|
container
|
|
|
|
.find(".unread-marker")
|
|
|
|
.appendTo(container);
|
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2014-09-10 19:23:56 +00:00
|
|
|
socket.on("more", function(data) {
|
2016-01-20 13:32:14 +00:00
|
|
|
var documentFragment = buildChannelMessages(data.chan, data.messages);
|
2014-09-10 19:23:56 +00:00
|
|
|
var chan = chat
|
2016-01-20 13:32:14 +00:00
|
|
|
.find("#chan-" + data.chan)
|
2016-07-12 15:26:55 +00:00
|
|
|
.find(".messages");
|
|
|
|
|
2017-05-03 07:54:07 +00:00
|
|
|
// get the scrollable wrapper around messages
|
|
|
|
var scrollable = chan.closest(".chat");
|
|
|
|
var heightOld = chan.height();
|
|
|
|
|
2016-10-03 18:03:19 +00:00
|
|
|
// Remove the date-change marker we put at the top, because it may
|
|
|
|
// not actually be a date change now
|
2016-11-25 16:05:04 +00:00
|
|
|
var children = $(chan).children();
|
2017-05-03 07:54:07 +00:00
|
|
|
if (children.eq(0).hasClass("date-marker-container")) { // Check top most child
|
2016-11-25 16:05:04 +00:00
|
|
|
children.eq(0).remove();
|
2017-05-03 07:54:07 +00:00
|
|
|
} else if (children.eq(1).hasClass("date-marker-container")) {
|
|
|
|
// The unread-marker could be at index 0, which will cause the date-marker to become "stuck"
|
2016-11-25 16:05:04 +00:00
|
|
|
children.eq(1).remove();
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
2017-05-09 17:19:02 +00:00
|
|
|
// Add the older messages
|
|
|
|
chan.prepend(documentFragment).end();
|
|
|
|
|
2016-07-12 15:26:55 +00:00
|
|
|
// restore scroll position
|
|
|
|
var position = chan.height() - heightOld;
|
|
|
|
scrollable.scrollTop(position);
|
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
if (data.messages.length !== 100) {
|
2016-07-12 15:26:55 +00:00
|
|
|
scrollable.find(".show-more").removeClass("show");
|
2014-09-10 19:23:56 +00:00
|
|
|
}
|
2016-10-03 18:03:19 +00:00
|
|
|
|
|
|
|
// Date change detect
|
|
|
|
// Have to use data instaid of the documentFragment because it's being weird
|
|
|
|
var lastDate;
|
|
|
|
$(data.messages).each(function() {
|
|
|
|
var msgData = this;
|
|
|
|
var msgDate = new Date(msgData.time);
|
|
|
|
var msg = $(chat.find("#chan-" + data.chan + " .messages #msg-" + msgData.id));
|
|
|
|
|
|
|
|
// Top-most message in a channel
|
|
|
|
if (!lastDate) {
|
|
|
|
lastDate = msgDate;
|
2016-12-18 15:53:28 +00:00
|
|
|
msg.before(templates.date_marker({msgDate: msgDate}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastDate.toDateString() !== msgDate.toDateString()) {
|
2016-12-18 15:53:28 +00:00
|
|
|
msg.before(templates.date_marker({msgDate: msgDate}));
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lastDate = msgDate;
|
|
|
|
});
|
2017-04-17 09:35:27 +00:00
|
|
|
|
2017-04-22 16:04:46 +00:00
|
|
|
scrollable.find(".show-more-button").prop("disabled", false);
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("network", function(data) {
|
2016-07-02 08:26:23 +00:00
|
|
|
renderNetworks(data);
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
sidebar.find(".chan")
|
|
|
|
.last()
|
|
|
|
.trigger("click");
|
2016-07-02 08:26:23 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
$("#connect")
|
|
|
|
.find(".btn")
|
|
|
|
.prop("disabled", false)
|
2014-09-14 21:26:22 +00:00
|
|
|
.end();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2016-03-08 18:50:48 +00:00
|
|
|
socket.on("network_changed", function(data) {
|
|
|
|
sidebar.find("#network-" + data.network).data("options", data.serverOptions);
|
|
|
|
});
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
socket.on("nick", function(data) {
|
2014-09-25 23:51:53 +00:00
|
|
|
var id = data.network;
|
|
|
|
var nick = data.nick;
|
|
|
|
var network = sidebar.find("#network-" + id).data("nick", nick);
|
|
|
|
if (network.find(".active").length) {
|
|
|
|
setNick(nick);
|
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("part", function(data) {
|
2016-07-11 05:20:23 +00:00
|
|
|
var chanMenuItem = sidebar.find(".chan[data-id='" + data.chan + "']");
|
2014-09-19 23:56:38 +00:00
|
|
|
|
2016-07-11 05:20:23 +00:00
|
|
|
// When parting from the active channel/query, jump to the network's lobby
|
|
|
|
if (chanMenuItem.hasClass("active")) {
|
|
|
|
chanMenuItem.parent(".network").find(".lobby").click();
|
2014-09-19 23:56:38 +00:00
|
|
|
}
|
2016-07-11 05:20:23 +00:00
|
|
|
|
|
|
|
chanMenuItem.remove();
|
|
|
|
$("#chan-" + data.chan).remove();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("quit", function(data) {
|
|
|
|
var id = data.network;
|
|
|
|
sidebar.find("#network-" + id)
|
|
|
|
.remove()
|
|
|
|
.end();
|
|
|
|
var chan = sidebar.find(".chan")
|
|
|
|
.eq(0)
|
|
|
|
.trigger("click");
|
|
|
|
if (chan.length === 0) {
|
|
|
|
sidebar.find(".empty").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
socket.on("toggle", function(data) {
|
2014-09-27 23:22:34 +00:00
|
|
|
var toggle = $("#toggle-" + data.id);
|
2016-12-18 15:53:28 +00:00
|
|
|
toggle.parent().after(templates.toggle({toggle: data}));
|
2014-09-27 23:22:34 +00:00
|
|
|
switch (data.type) {
|
|
|
|
case "link":
|
|
|
|
if (options.links) {
|
|
|
|
toggle.click();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "image":
|
|
|
|
if (options.thumbnails) {
|
|
|
|
toggle.click();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-09-27 19:17:05 +00:00
|
|
|
});
|
2014-12-11 22:42:22 +00:00
|
|
|
|
2015-04-26 19:42:31 +00:00
|
|
|
socket.on("topic", function(data) {
|
2015-04-26 22:20:54 +00:00
|
|
|
var topic = $("#chan-" + data.chan).find(".header .topic");
|
2016-12-18 15:53:28 +00:00
|
|
|
topic.html(helpers_parse(data.topic));
|
2015-04-26 22:20:54 +00:00
|
|
|
// .attr() is safe escape-wise but consider the capabilities of the attribute
|
|
|
|
topic.attr("title", data.topic);
|
2015-04-26 19:42:31 +00:00
|
|
|
});
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
socket.on("users", function(data) {
|
2016-02-17 02:29:44 +00:00
|
|
|
var chan = chat.find("#chan-" + data.chan);
|
|
|
|
|
|
|
|
if (chan.hasClass("active")) {
|
|
|
|
socket.emit("names", {
|
|
|
|
target: data.chan
|
|
|
|
});
|
2016-05-01 09:41:17 +00:00
|
|
|
} else {
|
2016-02-17 02:29:44 +00:00
|
|
|
chan.data("needsNamesRefresh", true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-03-23 10:15:44 +00:00
|
|
|
socket.on("names", renderChannelUsers);
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2017-04-22 13:03:00 +00:00
|
|
|
var options = require("./options");
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-10-20 04:41:30 +00:00
|
|
|
var windows = $("#windows");
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
var viewport = $("#viewport");
|
2016-12-18 15:53:28 +00:00
|
|
|
var sidebarSlide = slideoutMenu(viewport[0], sidebar[0]);
|
2016-02-12 11:34:10 +00:00
|
|
|
var contextMenuContainer = $("#context-menu-container");
|
|
|
|
var contextMenu = $("#context-menu");
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-06-12 02:16:17 +00:00
|
|
|
$("#main").on("click", function(e) {
|
|
|
|
if ($(e.target).is(".lt")) {
|
|
|
|
sidebarSlide.toggle(!sidebarSlide.isOpen());
|
|
|
|
} else if (sidebarSlide.isOpen()) {
|
|
|
|
sidebarSlide.toggle(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
viewport.on("click", ".rt", function(e) {
|
2014-08-16 16:15:17 +00:00
|
|
|
var self = $(this);
|
|
|
|
viewport.toggleClass(self.attr("class"));
|
2016-06-12 02:16:17 +00:00
|
|
|
e.stopPropagation();
|
2017-04-14 18:29:04 +00:00
|
|
|
chat.find(".chan.active .chat").trigger("msg.sticky");
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2016-03-19 18:20:11 +00:00
|
|
|
function positionContextMenu(that, e) {
|
|
|
|
var offset;
|
2016-02-12 11:34:10 +00:00
|
|
|
var menuWidth = contextMenu.outerWidth();
|
|
|
|
var menuHeight = contextMenu.outerHeight();
|
|
|
|
|
2016-03-19 18:20:11 +00:00
|
|
|
if (that.hasClass("menu")) {
|
|
|
|
offset = that.offset();
|
|
|
|
offset.left -= menuWidth - that.outerWidth();
|
|
|
|
offset.top += that.outerHeight();
|
|
|
|
return offset;
|
2016-02-12 11:34:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 18:20:11 +00:00
|
|
|
offset = {left: e.pageX, top: e.pageY};
|
|
|
|
|
|
|
|
if ((window.innerWidth - offset.left) < menuWidth) {
|
|
|
|
offset.left = window.innerWidth - menuWidth;
|
2016-02-12 11:34:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 18:20:11 +00:00
|
|
|
if ((window.innerHeight - offset.top) < menuHeight) {
|
|
|
|
offset.top = window.innerHeight - menuHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
2016-02-12 11:34:10 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 18:20:11 +00:00
|
|
|
function showContextMenu(that, e) {
|
2016-02-12 11:34:10 +00:00
|
|
|
var target = $(e.currentTarget);
|
|
|
|
var output = "";
|
|
|
|
|
|
|
|
if (target.hasClass("user")) {
|
2016-12-18 15:53:28 +00:00
|
|
|
output = templates.contextmenu_item({
|
2016-02-12 11:34:10 +00:00
|
|
|
class: "user",
|
|
|
|
text: target.text(),
|
|
|
|
data: target.data("name")
|
|
|
|
});
|
2016-05-01 09:41:17 +00:00
|
|
|
} else if (target.hasClass("chan")) {
|
2016-12-18 15:53:28 +00:00
|
|
|
output = templates.contextmenu_item({
|
2016-02-12 11:34:10 +00:00
|
|
|
class: "chan",
|
|
|
|
text: target.data("title"),
|
|
|
|
data: target.data("target")
|
|
|
|
});
|
2016-12-18 15:53:28 +00:00
|
|
|
output += templates.contextmenu_divider();
|
|
|
|
output += templates.contextmenu_item({
|
2016-02-12 11:34:10 +00:00
|
|
|
class: "close",
|
2016-03-09 20:04:07 +00:00
|
|
|
text: target.hasClass("lobby") ? "Disconnect" : target.hasClass("channel") ? "Leave" : "Close",
|
2016-02-12 11:34:10 +00:00
|
|
|
data: target.data("target")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
contextMenuContainer.show();
|
|
|
|
contextMenu
|
|
|
|
.html(output)
|
2016-03-19 18:20:11 +00:00
|
|
|
.css(positionContextMenu($(that), e));
|
2016-02-12 11:34:10 +00:00
|
|
|
|
|
|
|
return false;
|
2016-03-19 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
viewport.on("contextmenu", ".user, .network .chan", function(e) {
|
|
|
|
return showContextMenu(this, e);
|
|
|
|
});
|
|
|
|
|
|
|
|
viewport.on("click", "#chat .menu", function(e) {
|
|
|
|
e.currentTarget = $(e.currentTarget).closest(".chan")[0];
|
|
|
|
return showContextMenu(this, e);
|
2016-02-12 11:34:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
contextMenuContainer.on("click contextmenu", function() {
|
|
|
|
contextMenuContainer.hide();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-08-11 05:13:41 +00:00
|
|
|
function resetInputHeight(input) {
|
|
|
|
input.style.height = input.style.minHeight;
|
|
|
|
}
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
var input = $("#input")
|
|
|
|
.history()
|
2017-04-03 01:03:01 +00:00
|
|
|
.on("input", function() {
|
2016-06-05 02:48:41 +00:00
|
|
|
var style = window.getComputedStyle(this);
|
2016-07-15 06:42:47 +00:00
|
|
|
|
|
|
|
// Start by resetting height before computing as scrollHeight does not
|
|
|
|
// decrease when deleting characters
|
2016-08-11 05:13:41 +00:00
|
|
|
resetInputHeight(this);
|
2016-07-15 06:42:47 +00:00
|
|
|
|
2016-06-05 02:48:41 +00:00
|
|
|
this.style.height = Math.min(
|
|
|
|
Math.round(window.innerHeight - 100), // prevent overflow
|
|
|
|
this.scrollHeight
|
|
|
|
+ Math.round(parseFloat(style.borderTopWidth) || 0)
|
|
|
|
+ Math.round(parseFloat(style.borderBottomWidth) || 0)
|
|
|
|
) + "px";
|
|
|
|
|
2017-04-14 18:29:04 +00:00
|
|
|
chat.find(".chan.active .chat").trigger("msg.sticky"); // fix growing
|
2016-06-05 02:48:41 +00:00
|
|
|
})
|
2016-12-11 00:13:26 +00:00
|
|
|
.tab(completeNicks, {hint: false})
|
2017-04-30 10:18:21 +00:00
|
|
|
.textcomplete([
|
|
|
|
emojiStrategy, nicksStrategy, chanStrategy, commandStrategy,
|
|
|
|
foregroundColorStrategy, backgroundColorStrategy
|
|
|
|
], {
|
2016-12-11 00:13:26 +00:00
|
|
|
dropdownClassName: "textcomplete-menu",
|
|
|
|
placement: "top"
|
|
|
|
}).on({
|
|
|
|
"textComplete:show": function() {
|
|
|
|
$(this).data("autocompleting", true);
|
|
|
|
},
|
|
|
|
"textComplete:hide": function() {
|
|
|
|
$(this).data("autocompleting", false);
|
|
|
|
}
|
|
|
|
});
|
2014-09-10 19:23:56 +00:00
|
|
|
|
2016-12-16 19:55:02 +00:00
|
|
|
var focus = $.noop;
|
|
|
|
if (!("ontouchstart" in window || navigator.maxTouchPoints > 0)) {
|
|
|
|
focus = function() {
|
|
|
|
if (chat.find(".active").hasClass("chan")) {
|
|
|
|
input.focus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$(window).on("focus", focus);
|
|
|
|
|
|
|
|
chat.on("click", ".chat", function() {
|
|
|
|
setTimeout(function() {
|
|
|
|
var text = "";
|
|
|
|
if (window.getSelection) {
|
|
|
|
text = window.getSelection().toString();
|
|
|
|
} else if (document.selection && document.selection.type !== "Control") {
|
|
|
|
text = document.selection.createRange().text;
|
|
|
|
}
|
|
|
|
if (!text) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
}, 2);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Triggering click event opens the virtual keyboard on mobile
|
|
|
|
// This can only be called from another interactive event (e.g. button click)
|
|
|
|
var forceFocus = function() {
|
|
|
|
input.trigger("click").focus();
|
|
|
|
};
|
|
|
|
|
2016-10-09 08:54:44 +00:00
|
|
|
$("#form").on("submit", function(e) {
|
2014-08-16 16:15:17 +00:00
|
|
|
e.preventDefault();
|
2016-12-16 19:55:02 +00:00
|
|
|
forceFocus();
|
2014-08-16 16:15:17 +00:00
|
|
|
var text = input.val();
|
2016-05-25 07:23:03 +00:00
|
|
|
|
|
|
|
if (text.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
input.val("");
|
2016-08-11 05:13:41 +00:00
|
|
|
resetInputHeight(input.get(0));
|
2016-05-25 07:23:03 +00:00
|
|
|
|
2014-09-19 20:24:11 +00:00
|
|
|
if (text.indexOf("/clear") === 0) {
|
2014-09-21 15:21:26 +00:00
|
|
|
clear();
|
|
|
|
return;
|
2014-09-19 20:24:11 +00:00
|
|
|
}
|
2016-05-25 07:23:03 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
socket.emit("input", {
|
|
|
|
target: chat.data("id"),
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-30 19:38:53 +00:00
|
|
|
function findCurrentNetworkChan(name) {
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
|
|
|
return $(".network .chan.active")
|
2016-02-29 16:48:57 +00:00
|
|
|
.parent(".network")
|
2016-05-30 19:38:53 +00:00
|
|
|
.find(".chan")
|
|
|
|
.filter(function() {
|
|
|
|
return $(this).data("title").toLowerCase() === name;
|
|
|
|
})
|
|
|
|
.first();
|
|
|
|
}
|
|
|
|
|
2016-07-29 06:10:29 +00:00
|
|
|
$("button#set-nick").on("click", function() {
|
|
|
|
toggleNickEditor(true);
|
|
|
|
|
|
|
|
// Selects existing nick in the editable text field
|
|
|
|
var element = document.querySelector("#nick-value");
|
|
|
|
element.focus();
|
|
|
|
var range = document.createRange();
|
|
|
|
range.selectNodeContents(element);
|
|
|
|
var selection = window.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
selection.addRange(range);
|
|
|
|
});
|
|
|
|
|
|
|
|
$("button#cancel-nick").on("click", cancelNick);
|
|
|
|
$("button#submit-nick").on("click", submitNick);
|
|
|
|
|
|
|
|
function toggleNickEditor(toggle) {
|
|
|
|
$("#nick").toggleClass("editable", toggle);
|
|
|
|
$("#nick-value").attr("contenteditable", toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function submitNick() {
|
2016-10-01 17:04:03 +00:00
|
|
|
var newNick = $("#nick-value").text().trim();
|
|
|
|
|
|
|
|
if (newNick.length === 0) {
|
|
|
|
cancelNick();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleNickEditor(false);
|
2016-07-29 06:10:29 +00:00
|
|
|
|
|
|
|
socket.emit("input", {
|
|
|
|
target: chat.data("id"),
|
|
|
|
text: "/nick " + newNick
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function cancelNick() {
|
|
|
|
setNick(sidebar.find(".chan.active").closest(".network").data("nick"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#nick-value").keypress(function(e) {
|
|
|
|
switch (e.keyCode ? e.keyCode : e.which) {
|
|
|
|
case 13: // Enter
|
|
|
|
// Ensures a new line is not added when pressing Enter
|
|
|
|
e.preventDefault();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}).keyup(function(e) {
|
|
|
|
switch (e.keyCode ? e.keyCode : e.which) {
|
|
|
|
case 13: // Enter
|
|
|
|
submitNick();
|
|
|
|
break;
|
|
|
|
case 27: // Escape
|
|
|
|
cancelNick();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-30 19:38:53 +00:00
|
|
|
chat.on("click", ".inline-channel", function() {
|
|
|
|
var name = $(this).data("chan");
|
|
|
|
var chan = findCurrentNetworkChan(name);
|
|
|
|
|
|
|
|
if (chan.length) {
|
2016-02-29 16:48:57 +00:00
|
|
|
chan.click();
|
|
|
|
} else {
|
|
|
|
socket.emit("input", {
|
|
|
|
target: chat.data("id"),
|
2016-05-30 19:38:53 +00:00
|
|
|
text: "/join " + name
|
2016-02-29 16:48:57 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-30 19:38:53 +00:00
|
|
|
chat.on("click", ".user", function() {
|
|
|
|
var name = $(this).data("name");
|
|
|
|
var chan = findCurrentNetworkChan(name);
|
|
|
|
|
|
|
|
if (chan.length) {
|
|
|
|
chan.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.emit("input", {
|
|
|
|
target: chat.data("id"),
|
|
|
|
text: "/whois " + name
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-28 17:48:34 +00:00
|
|
|
sidebar.on("click", ".chan, button", function(e, data) {
|
|
|
|
// Pushes states to history web API when clicking elements with a data-target attribute.
|
|
|
|
// States are very trivial and only contain a single `clickTarget` property which
|
|
|
|
// contains a CSS selector that targets elements which takes the user to a different view
|
|
|
|
// when clicked. The `popstate` event listener will trigger synthetic click events using that
|
|
|
|
// selector and thus take the user to a different view/state.
|
|
|
|
if (data && data.pushState === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const self = $(this);
|
|
|
|
const target = self.data("target");
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const state = {};
|
|
|
|
|
|
|
|
if (self.hasClass("chan")) {
|
|
|
|
state.clickTarget = `.chan[data-id="${self.data("id")}"]`;
|
|
|
|
} else {
|
|
|
|
state.clickTarget = `#footer button[data-target="${target}"]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (history && history.pushState) {
|
2017-04-24 11:20:48 +00:00
|
|
|
if (data && data.replaceHistory && history.replaceState) {
|
|
|
|
history.replaceState(state, null, null);
|
|
|
|
} else {
|
|
|
|
history.pushState(state, null, null);
|
|
|
|
}
|
2017-01-28 17:48:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-22 18:54:38 +00:00
|
|
|
sidebar.on("click", ".chan, button", function() {
|
2014-08-16 16:15:17 +00:00
|
|
|
var self = $(this);
|
|
|
|
var target = self.data("target");
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.data(
|
|
|
|
"id",
|
|
|
|
self.data("id")
|
|
|
|
);
|
2014-09-21 16:46:43 +00:00
|
|
|
socket.emit(
|
|
|
|
"open",
|
|
|
|
self.data("id")
|
|
|
|
);
|
2014-08-16 16:15:17 +00:00
|
|
|
|
|
|
|
sidebar.find(".active").removeClass("active");
|
|
|
|
self.addClass("active")
|
|
|
|
.find(".badge")
|
|
|
|
.removeClass("highlight")
|
|
|
|
.empty();
|
|
|
|
|
2014-09-13 17:36:59 +00:00
|
|
|
if (sidebar.find(".highlight").length === 0) {
|
2016-07-10 20:38:08 +00:00
|
|
|
toggleNotificationMarkers(false);
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 02:16:17 +00:00
|
|
|
sidebarSlide.toggle(false);
|
|
|
|
|
2016-07-06 07:08:27 +00:00
|
|
|
var lastActive = $("#windows > .active");
|
2016-05-13 10:23:05 +00:00
|
|
|
|
|
|
|
lastActive
|
2016-04-17 10:50:03 +00:00
|
|
|
.removeClass("active")
|
|
|
|
.find(".chat")
|
|
|
|
.unsticky();
|
2014-09-10 19:24:03 +00:00
|
|
|
|
2016-07-24 05:50:15 +00:00
|
|
|
var lastActiveChan = lastActive
|
2016-07-06 07:08:27 +00:00
|
|
|
.find(".chan.active")
|
|
|
|
.removeClass("active");
|
2016-07-05 23:23:46 +00:00
|
|
|
|
2016-07-24 05:50:15 +00:00
|
|
|
lastActiveChan
|
|
|
|
.find(".unread-marker")
|
|
|
|
.appendTo(lastActiveChan.find(".messages"));
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
var chan = $(target)
|
|
|
|
.addClass("active")
|
2016-07-05 23:23:46 +00:00
|
|
|
.trigger("show");
|
2016-04-17 10:50:03 +00:00
|
|
|
|
2017-06-05 11:40:25 +00:00
|
|
|
let title = $(document.body).data("app-name");
|
2014-11-07 19:52:38 +00:00
|
|
|
if (chan.data("title")) {
|
|
|
|
title = chan.data("title") + " — " + title;
|
|
|
|
}
|
|
|
|
document.title = title;
|
2014-12-11 22:42:22 +00:00
|
|
|
|
2016-12-21 01:13:05 +00:00
|
|
|
var placeholder = "";
|
|
|
|
if (chan.data("type") === "channel" || chan.data("type") === "query") {
|
2017-01-25 04:55:57 +00:00
|
|
|
placeholder = `Write to ${chan.data("title")}`;
|
2016-12-21 01:13:05 +00:00
|
|
|
}
|
|
|
|
input.attr("placeholder", placeholder);
|
|
|
|
|
2014-09-25 23:51:53 +00:00
|
|
|
if (self.hasClass("chan")) {
|
2016-07-06 07:08:27 +00:00
|
|
|
$("#chat-container").addClass("active");
|
2016-04-10 09:08:31 +00:00
|
|
|
setNick(self.closest(".network").data("nick"));
|
2014-09-25 23:51:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 11:01:31 +00:00
|
|
|
var chanChat = chan.find(".chat");
|
2017-04-06 16:22:56 +00:00
|
|
|
if (chanChat.length > 0 && chan.data("type") !== "special") {
|
2016-07-10 11:01:31 +00:00
|
|
|
chanChat.sticky();
|
|
|
|
}
|
|
|
|
|
2016-02-17 02:29:44 +00:00
|
|
|
if (chan.data("needsNamesRefresh") === true) {
|
|
|
|
chan.data("needsNamesRefresh", false);
|
|
|
|
socket.emit("names", {target: self.data("id")});
|
|
|
|
}
|
|
|
|
|
2016-12-16 19:55:02 +00:00
|
|
|
focus();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
sidebar.on("click", "#sign-out", function() {
|
2017-04-22 13:03:00 +00:00
|
|
|
storage.remove("token");
|
2014-08-16 16:15:17 +00:00
|
|
|
location.reload();
|
|
|
|
});
|
|
|
|
|
|
|
|
sidebar.on("click", ".close", function() {
|
|
|
|
var cmd = "/close";
|
|
|
|
var chan = $(this).closest(".chan");
|
|
|
|
if (chan.hasClass("lobby")) {
|
|
|
|
cmd = "/quit";
|
2014-09-22 18:54:38 +00:00
|
|
|
var server = chan.find(".name").html();
|
2014-08-16 16:15:17 +00:00
|
|
|
if (!confirm("Disconnect from " + server + "?")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.emit("input", {
|
|
|
|
target: chan.data("id"),
|
|
|
|
text: cmd
|
|
|
|
});
|
|
|
|
chan.css({
|
|
|
|
transition: "none",
|
2014-09-13 17:37:10 +00:00
|
|
|
opacity: 0.4
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-02-12 11:34:10 +00:00
|
|
|
contextMenu.on("click", ".context-menu-item", function() {
|
|
|
|
switch ($(this).data("action")) {
|
|
|
|
case "close":
|
2016-12-29 21:43:10 +00:00
|
|
|
$(".networks .chan[data-target='" + $(this).data("data") + "'] .close").click();
|
2016-02-12 11:34:10 +00:00
|
|
|
break;
|
|
|
|
case "chan":
|
2016-12-29 21:43:10 +00:00
|
|
|
$(".networks .chan[data-target='" + $(this).data("data") + "']").click();
|
2016-02-12 11:34:10 +00:00
|
|
|
break;
|
|
|
|
case "user":
|
2016-12-29 21:43:10 +00:00
|
|
|
$(".channel.active .users .user[data-name='" + $(this).data("data") + "']").click();
|
2016-02-12 11:34:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
chat.on("input", ".search", function() {
|
2017-01-02 02:20:48 +00:00
|
|
|
const value = $(this).val();
|
2017-01-28 17:37:26 +00:00
|
|
|
const parent = $(this).closest(".users");
|
|
|
|
const names = parent.find(".names-original");
|
|
|
|
const container = parent.find(".names-filtered");
|
2016-12-30 05:32:27 +00:00
|
|
|
|
2017-01-28 17:37:26 +00:00
|
|
|
if (!value.length) {
|
|
|
|
container.hide();
|
|
|
|
names.show();
|
|
|
|
return;
|
|
|
|
}
|
2016-12-30 05:32:27 +00:00
|
|
|
|
|
|
|
const fuzzyOptions = {
|
|
|
|
pre: "<b>",
|
|
|
|
post: "</b>",
|
2017-04-08 12:34:31 +00:00
|
|
|
extract: (el) => $(el).text()
|
2016-12-30 05:32:27 +00:00
|
|
|
};
|
|
|
|
|
2017-01-28 17:37:26 +00:00
|
|
|
const result = fuzzy.filter(
|
2016-12-30 05:32:27 +00:00
|
|
|
value,
|
|
|
|
names.find(".user").toArray(),
|
|
|
|
fuzzyOptions
|
2017-01-28 17:37:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
names.hide();
|
|
|
|
container.html(templates.user_filtered({matches: result})).show();
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
|
|
|
|
2016-10-14 15:49:08 +00:00
|
|
|
chat.on("msg", ".messages", function(e, target, msg) {
|
2016-09-25 06:41:10 +00:00
|
|
|
var unread = msg.unread;
|
|
|
|
msg = msg.msg;
|
|
|
|
|
2016-10-14 15:49:08 +00:00
|
|
|
if (msg.self) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:06:23 +00:00
|
|
|
var button = sidebar.find(".chan[data-target='" + target + "']");
|
2016-04-22 16:38:59 +00:00
|
|
|
if (msg.highlight || (options.notifyAllMessages && msg.type === "message")) {
|
2014-09-13 22:37:23 +00:00
|
|
|
if (!document.hasFocus() || !$(target).hasClass("active")) {
|
2016-02-18 19:19:18 +00:00
|
|
|
if (options.notification) {
|
2016-11-08 06:02:56 +00:00
|
|
|
try {
|
|
|
|
pop.play();
|
|
|
|
} catch (exception) {
|
|
|
|
// On mobile, sounds can not be played without user interaction.
|
|
|
|
}
|
2014-09-15 21:13:03 +00:00
|
|
|
}
|
2016-07-10 20:38:08 +00:00
|
|
|
toggleNotificationMarkers(true);
|
2016-02-29 07:25:40 +00:00
|
|
|
|
2016-03-03 16:43:30 +00:00
|
|
|
if (options.desktopNotifications && Notification.permission === "granted") {
|
2016-02-29 07:25:40 +00:00
|
|
|
var title;
|
|
|
|
var body;
|
|
|
|
|
|
|
|
if (msg.type === "invite") {
|
|
|
|
title = "New channel invite:";
|
2016-04-11 04:56:59 +00:00
|
|
|
body = msg.from + " invited you to " + msg.channel;
|
2016-02-29 07:25:40 +00:00
|
|
|
} else {
|
|
|
|
title = msg.from;
|
2016-04-22 16:38:59 +00:00
|
|
|
if (!button.hasClass("query")) {
|
2016-03-05 20:39:59 +00:00
|
|
|
title += " (" + button.data("title").trim() + ")";
|
2016-02-29 07:25:40 +00:00
|
|
|
}
|
2016-12-17 11:05:12 +00:00
|
|
|
if (msg.type === "message") {
|
|
|
|
title += " says:";
|
|
|
|
}
|
2016-12-18 16:06:47 +00:00
|
|
|
body = msg.text.replace(/\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?|[\x00-\x1F]|\x7F/g, "").trim();
|
2016-02-28 06:36:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 05:16:10 +00:00
|
|
|
try {
|
|
|
|
var notify = new Notification(title, {
|
|
|
|
body: body,
|
|
|
|
icon: "img/logo-64.png",
|
|
|
|
tag: target
|
|
|
|
});
|
2016-11-16 16:42:06 +00:00
|
|
|
notify.addEventListener("click", function() {
|
2016-11-07 05:16:10 +00:00
|
|
|
window.focus();
|
|
|
|
button.click();
|
|
|
|
this.close();
|
2016-11-16 16:42:06 +00:00
|
|
|
});
|
2016-11-07 05:16:10 +00:00
|
|
|
} catch (exception) {
|
|
|
|
// `new Notification(...)` is not supported and should be silenced.
|
|
|
|
}
|
2014-09-13 22:37:23 +00:00
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 19:46:22 +00:00
|
|
|
if (button.hasClass("active")) {
|
2014-08-16 16:15:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-10 19:23:56 +00:00
|
|
|
|
2016-09-25 06:41:10 +00:00
|
|
|
if (!unread) {
|
2014-08-16 16:15:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-18 15:53:28 +00:00
|
|
|
var badge = button.find(".badge").html(helpers_roundBadgeNumber(unread));
|
2016-09-25 06:41:10 +00:00
|
|
|
|
|
|
|
if (msg.highlight) {
|
|
|
|
badge.addClass("highlight");
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
2016-10-14 15:49:08 +00:00
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2014-09-28 21:51:24 +00:00
|
|
|
chat.on("click", ".show-more-button", function() {
|
2014-08-16 16:15:17 +00:00
|
|
|
var self = $(this);
|
2017-04-05 19:15:28 +00:00
|
|
|
var count = self.parent().next(".messages").children(".msg").length;
|
2017-04-17 09:35:27 +00:00
|
|
|
self.prop("disabled", true);
|
2014-09-10 19:23:56 +00:00
|
|
|
socket.emit("more", {
|
2014-09-28 21:51:24 +00:00
|
|
|
target: self.data("id"),
|
2014-08-16 16:15:17 +00:00
|
|
|
count: count
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
chat.on("click", ".toggle-button", function() {
|
|
|
|
var self = $(this);
|
2016-10-09 08:54:44 +00:00
|
|
|
var localChat = self.closest(".chat");
|
|
|
|
var bottom = localChat.isScrollBottom();
|
2014-09-30 21:44:29 +00:00
|
|
|
var content = self.parent().next(".toggle-content");
|
|
|
|
if (bottom && !content.hasClass("show")) {
|
|
|
|
var img = content.find("img");
|
2015-09-30 22:39:57 +00:00
|
|
|
if (img.length !== 0 && !img.width()) {
|
2014-09-30 21:44:29 +00:00
|
|
|
img.on("load", function() {
|
2016-10-09 08:54:44 +00:00
|
|
|
localChat.scrollBottom();
|
2014-09-30 21:44:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content.toggleClass("show");
|
|
|
|
if (bottom) {
|
2016-10-09 08:54:44 +00:00
|
|
|
localChat.scrollBottom();
|
2014-09-30 21:44:29 +00:00
|
|
|
}
|
2014-09-27 19:17:05 +00:00
|
|
|
});
|
|
|
|
|
2016-02-17 00:14:43 +00:00
|
|
|
var forms = $("#sign-in, #connect, #change-password");
|
2014-08-16 16:15:17 +00:00
|
|
|
|
|
|
|
windows.on("show", "#sign-in", function() {
|
2016-10-09 08:54:44 +00:00
|
|
|
$(this).find("input").each(function() {
|
2014-08-16 16:15:17 +00:00
|
|
|
var self = $(this);
|
|
|
|
if (self.val() === "") {
|
|
|
|
self.focus();
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-13 17:23:36 +00:00
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
2016-10-05 21:30:17 +00:00
|
|
|
if ($("body").hasClass("public")) {
|
|
|
|
$("#connect").one("show", function() {
|
2016-12-18 15:53:28 +00:00
|
|
|
var params = URI(document.location.search);
|
2016-10-05 21:30:17 +00:00
|
|
|
params = params.search(true);
|
|
|
|
// Possible parameters: name, host, port, password, tls, nick, username, realname, join
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Iterating_over_own_properties_only
|
|
|
|
for (var key in params) {
|
|
|
|
if (params.hasOwnProperty(key)) {
|
|
|
|
var value = params[key];
|
|
|
|
// \W searches for non-word characters
|
|
|
|
key = key.replace(/\W/g, "");
|
|
|
|
|
|
|
|
var element = $("#connect input[name='" + key + "']");
|
|
|
|
// if the element exists, it isn't disabled, and it isn't hidden
|
|
|
|
if (element.length > 0 && !element.is(":disabled") && !element.is(":hidden")) {
|
|
|
|
if (element.is(":checkbox")) {
|
|
|
|
element.prop("checked", (value === "1" || value === "true") ? true : false);
|
|
|
|
} else {
|
|
|
|
element.val(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-03-03 16:43:30 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
forms.on("submit", "form", function(e) {
|
2014-09-13 17:23:36 +00:00
|
|
|
e.preventDefault();
|
2014-08-16 16:15:17 +00:00
|
|
|
var event = "auth";
|
|
|
|
var form = $(this);
|
2014-10-09 14:35:29 +00:00
|
|
|
form.find(".btn")
|
|
|
|
.attr("disabled", true)
|
|
|
|
.end();
|
2015-09-30 22:39:57 +00:00
|
|
|
if (form.closest(".window").attr("id") === "connect") {
|
2014-08-16 16:15:17 +00:00
|
|
|
event = "conn";
|
2016-02-17 00:14:43 +00:00
|
|
|
} else if (form.closest("div").attr("id") === "change-password") {
|
|
|
|
event = "change-password";
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
var values = {};
|
|
|
|
$.each(form.serializeArray(), function(i, obj) {
|
|
|
|
if (obj.value !== "") {
|
|
|
|
values[obj.name] = obj.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (values.user) {
|
2017-04-22 13:03:00 +00:00
|
|
|
storage.set("user", values.user);
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
|
|
|
socket.emit(
|
|
|
|
event, values
|
|
|
|
);
|
|
|
|
});
|
2014-12-11 22:42:22 +00:00
|
|
|
|
2017-01-09 16:24:04 +00:00
|
|
|
forms.on("focusin", ".nick", function() {
|
|
|
|
// Need to set the first "lastvalue", so it can be used in the below function
|
|
|
|
var nick = $(this);
|
|
|
|
nick.data("lastvalue", nick.val());
|
|
|
|
});
|
|
|
|
|
2014-11-06 12:00:14 +00:00
|
|
|
forms.on("input", ".nick", function() {
|
2014-11-05 22:19:29 +00:00
|
|
|
var nick = $(this).val();
|
2017-01-09 16:24:04 +00:00
|
|
|
var usernameInput = forms.find(".username");
|
|
|
|
|
|
|
|
// Because this gets called /after/ it has already changed, we need use the previous value
|
|
|
|
var lastValue = $(this).data("lastvalue");
|
|
|
|
|
|
|
|
// They were the same before the change, so update the username field
|
|
|
|
if (usernameInput.val() === lastValue) {
|
|
|
|
usernameInput.val(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the "previous" value, for next time
|
|
|
|
$(this).data("lastvalue", nick);
|
2014-11-05 22:19:29 +00:00
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-12-17 20:03:12 +00:00
|
|
|
(function HotkeysScope() {
|
2017-03-11 10:41:46 +00:00
|
|
|
Mousetrap.bind([
|
|
|
|
"pageup",
|
|
|
|
"pagedown"
|
|
|
|
], function(e, key) {
|
|
|
|
let container = windows.find(".window.active");
|
2017-04-24 11:01:24 +00:00
|
|
|
if (container.is(":animated")) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-11 10:41:46 +00:00
|
|
|
|
|
|
|
// Chat windows scroll message container
|
|
|
|
if (container.attr("id") === "chat-container") {
|
|
|
|
container = container.find(".chan.active .chat");
|
|
|
|
}
|
|
|
|
|
2017-04-03 01:03:01 +00:00
|
|
|
const offset = container.get(0).clientHeight * 0.9;
|
2017-03-11 10:41:46 +00:00
|
|
|
let scrollTop = container.scrollTop();
|
|
|
|
|
|
|
|
if (key === "pageup") {
|
2017-04-03 01:03:01 +00:00
|
|
|
scrollTop = Math.floor(scrollTop - offset);
|
2017-03-11 10:41:46 +00:00
|
|
|
} else {
|
2017-04-03 01:03:01 +00:00
|
|
|
scrollTop = Math.ceil(scrollTop + offset);
|
2017-03-11 10:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
container.stop().animate({
|
|
|
|
scrollTop: scrollTop
|
|
|
|
}, 200);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2016-12-17 20:03:12 +00:00
|
|
|
Mousetrap.bind([
|
|
|
|
"command+up",
|
|
|
|
"command+down",
|
|
|
|
"ctrl+up",
|
|
|
|
"ctrl+down"
|
|
|
|
], function(e, keys) {
|
|
|
|
var channels = sidebar.find(".chan");
|
|
|
|
var index = channels.index(channels.filter(".active"));
|
|
|
|
var direction = keys.split("+").pop();
|
|
|
|
switch (direction) {
|
|
|
|
case "up":
|
|
|
|
// Loop
|
|
|
|
var upTarget = (channels.length + (index - 1 + channels.length)) % channels.length;
|
|
|
|
channels.eq(upTarget).click();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "down":
|
|
|
|
// Loop
|
|
|
|
var downTarget = (channels.length + (index + 1 + channels.length)) % channels.length;
|
|
|
|
channels.eq(downTarget).click();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2014-09-10 19:23:56 +00:00
|
|
|
|
2016-12-17 20:03:12 +00:00
|
|
|
Mousetrap.bind([
|
|
|
|
"command+shift+l",
|
|
|
|
"ctrl+shift+l"
|
|
|
|
], function(e) {
|
|
|
|
if (e.target === input[0]) {
|
|
|
|
clear();
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
|
2016-12-17 20:03:12 +00:00
|
|
|
Mousetrap.bind([
|
|
|
|
"escape"
|
|
|
|
], function() {
|
|
|
|
contextMenuContainer.hide();
|
|
|
|
});
|
2014-09-19 11:18:02 +00:00
|
|
|
|
2016-12-17 20:03:12 +00:00
|
|
|
var colorsHotkeys = {
|
|
|
|
k: "\x03",
|
|
|
|
b: "\x02",
|
|
|
|
u: "\x1F",
|
|
|
|
i: "\x1D",
|
|
|
|
o: "\x0F",
|
|
|
|
};
|
|
|
|
|
|
|
|
for (var hotkey in colorsHotkeys) {
|
|
|
|
Mousetrap.bind([
|
|
|
|
"command+" + hotkey,
|
|
|
|
"ctrl+" + hotkey
|
|
|
|
], function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const cursorPosStart = input.prop("selectionStart");
|
|
|
|
const cursorPosEnd = input.prop("selectionEnd");
|
|
|
|
const value = input.val();
|
|
|
|
let newValue = value.substring(0, cursorPosStart) + colorsHotkeys[e.key];
|
|
|
|
|
|
|
|
if (cursorPosStart === cursorPosEnd) {
|
|
|
|
// If no text is selected, insert at cursor
|
|
|
|
newValue += value.substring(cursorPosEnd, value.length);
|
|
|
|
} else {
|
|
|
|
// If text is selected, insert formatting character at start and the end
|
|
|
|
newValue += value.substring(cursorPosStart, cursorPosEnd) + colorsHotkeys[e.key] + value.substring(cursorPosEnd, value.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
input
|
|
|
|
.val(newValue)
|
|
|
|
.get(0).setSelectionRange(cursorPosStart + 1, cursorPosEnd + 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}());
|
2016-02-12 11:34:10 +00:00
|
|
|
|
2014-12-11 23:01:19 +00:00
|
|
|
setInterval(function() {
|
|
|
|
chat.find(".chan:not(.active)").each(function() {
|
|
|
|
var chan = $(this);
|
2016-12-19 10:48:54 +00:00
|
|
|
if (chan.find(".messages .msg").slice(0, -100).remove().length) {
|
2014-12-11 23:01:19 +00:00
|
|
|
chan.find(".show-more").addClass("show");
|
2016-10-03 18:03:19 +00:00
|
|
|
|
|
|
|
// Remove date-seperators that would otherwise be "stuck" at the top
|
|
|
|
// of the channel
|
2017-05-03 07:54:07 +00:00
|
|
|
chan.find(".date-marker-container").each(function() {
|
|
|
|
if ($(this).next().hasClass("date-marker-container")) {
|
2016-12-19 10:48:54 +00:00
|
|
|
$(this).remove();
|
2016-10-03 18:03:19 +00:00
|
|
|
}
|
|
|
|
});
|
2014-12-11 23:01:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}, 1000 * 10);
|
|
|
|
|
2014-09-19 20:24:11 +00:00
|
|
|
function clear() {
|
2016-12-19 10:48:54 +00:00
|
|
|
chat.find(".active")
|
|
|
|
.find(".show-more").addClass("show").end()
|
2017-05-03 07:54:07 +00:00
|
|
|
.find(".messages .msg, .date-marker-container").remove();
|
2014-09-19 20:24:11 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 00:13:26 +00:00
|
|
|
function completeNicks(word) {
|
2017-04-27 18:32:58 +00:00
|
|
|
const users = chat.find(".active .users");
|
|
|
|
|
|
|
|
// Lobbies and private chats do not have an user list
|
|
|
|
if (!users.length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-04-24 23:01:24 +00:00
|
|
|
const words = users.data("nicks");
|
2014-10-08 21:21:41 +00:00
|
|
|
|
2016-12-11 00:13:26 +00:00
|
|
|
return $.grep(
|
|
|
|
words,
|
2017-04-08 12:34:31 +00:00
|
|
|
(w) => !w.toLowerCase().indexOf(word.toLowerCase())
|
2016-12-11 00:13:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeCommands(word) {
|
2017-04-24 23:01:24 +00:00
|
|
|
const words = constants.commands.slice();
|
2016-12-11 00:13:26 +00:00
|
|
|
|
|
|
|
return $.grep(
|
|
|
|
words,
|
2017-04-08 12:34:31 +00:00
|
|
|
(w) => !w.toLowerCase().indexOf(word.toLowerCase())
|
2016-12-11 00:13:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeChans(word) {
|
2017-04-24 23:01:24 +00:00
|
|
|
const words = [];
|
2014-10-08 21:21:41 +00:00
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
sidebar.find(".chan")
|
2014-08-16 16:15:17 +00:00
|
|
|
.each(function() {
|
2017-04-24 23:01:24 +00:00
|
|
|
const self = $(this);
|
2014-10-08 21:21:41 +00:00
|
|
|
if (!self.hasClass("lobby")) {
|
|
|
|
words.push(self.data("title"));
|
|
|
|
}
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|
2014-10-08 21:21:41 +00:00
|
|
|
|
2014-08-16 16:15:17 +00:00
|
|
|
return $.grep(
|
|
|
|
words,
|
2017-04-08 12:34:31 +00:00
|
|
|
(w) => !w.toLowerCase().indexOf(word.toLowerCase())
|
2014-08-16 16:15:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-09-15 18:32:25 +00:00
|
|
|
function confirmExit() {
|
2014-12-11 22:42:22 +00:00
|
|
|
if ($("body").hasClass("public")) {
|
|
|
|
window.onbeforeunload = function() {
|
|
|
|
return "Are you sure you want to navigate away from this page?";
|
|
|
|
};
|
|
|
|
}
|
2014-09-15 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 23:05:07 +00:00
|
|
|
function sortable() {
|
2016-10-18 21:53:39 +00:00
|
|
|
sidebar.find(".networks").sortable({
|
2014-09-23 23:05:07 +00:00
|
|
|
axis: "y",
|
|
|
|
containment: "parent",
|
2016-10-18 21:53:39 +00:00
|
|
|
cursor: "move",
|
2014-10-10 20:42:04 +00:00
|
|
|
distance: 12,
|
2014-09-23 23:05:07 +00:00
|
|
|
items: ".network",
|
|
|
|
handle: ".lobby",
|
|
|
|
placeholder: "network-placeholder",
|
|
|
|
forcePlaceholderSize: true,
|
2016-10-18 21:53:39 +00:00
|
|
|
tolerance: "pointer", // Use the pointer to figure out where the network is in the list
|
|
|
|
|
2014-09-23 23:05:07 +00:00
|
|
|
update: function() {
|
2014-09-24 19:42:36 +00:00
|
|
|
var order = [];
|
|
|
|
sidebar.find(".network").each(function() {
|
|
|
|
var id = $(this).data("id");
|
|
|
|
order.push(id);
|
|
|
|
});
|
|
|
|
socket.emit(
|
|
|
|
"sort", {
|
|
|
|
type: "networks",
|
|
|
|
order: order
|
|
|
|
}
|
|
|
|
);
|
2016-11-22 12:14:17 +00:00
|
|
|
|
|
|
|
ignoreSortSync = true;
|
2014-09-23 23:05:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
sidebar.find(".network").sortable({
|
|
|
|
axis: "y",
|
|
|
|
containment: "parent",
|
2016-10-18 21:53:39 +00:00
|
|
|
cursor: "move",
|
2014-10-10 20:42:04 +00:00
|
|
|
distance: 12,
|
2014-09-23 23:05:07 +00:00
|
|
|
items: ".chan:not(.lobby)",
|
|
|
|
placeholder: "chan-placeholder",
|
|
|
|
forcePlaceholderSize: true,
|
2016-10-18 21:53:39 +00:00
|
|
|
tolerance: "pointer", // Use the pointer to figure out where the channel is in the list
|
|
|
|
|
2014-09-24 19:42:36 +00:00
|
|
|
update: function(e, ui) {
|
|
|
|
var order = [];
|
|
|
|
var network = ui.item.parent();
|
|
|
|
network.find(".chan").each(function() {
|
|
|
|
var id = $(this).data("id");
|
|
|
|
order.push(id);
|
|
|
|
});
|
|
|
|
socket.emit(
|
|
|
|
"sort", {
|
|
|
|
type: "channels",
|
|
|
|
target: network.data("id"),
|
|
|
|
order: order
|
|
|
|
}
|
|
|
|
);
|
2016-11-22 12:14:17 +00:00
|
|
|
|
|
|
|
ignoreSortSync = true;
|
2014-09-23 23:05:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-22 12:14:17 +00:00
|
|
|
socket.on("sync_sort", function(data) {
|
|
|
|
// Syncs the order of channels or networks when they are reordered
|
|
|
|
if (ignoreSortSync) {
|
|
|
|
ignoreSortSync = false;
|
|
|
|
return; // Ignore syncing because we 'caused' it
|
|
|
|
}
|
|
|
|
|
|
|
|
var type = data.type;
|
|
|
|
var order = data.order;
|
|
|
|
|
|
|
|
if (type === "networks") {
|
|
|
|
var container = $(".networks");
|
|
|
|
|
|
|
|
$.each(order, function(index, value) {
|
|
|
|
var position = $(container.children()[index]);
|
|
|
|
|
|
|
|
if (position.data("id") === value) { // Network in correct place
|
|
|
|
return true; // No point in continuing
|
|
|
|
}
|
|
|
|
|
|
|
|
var network = container.find("#network-" + value);
|
|
|
|
|
|
|
|
$(network).insertBefore(position);
|
|
|
|
});
|
|
|
|
} else if (type === "channels") {
|
|
|
|
var network = $("#network-" + data.target);
|
|
|
|
|
|
|
|
$.each(order, function(index, value) {
|
|
|
|
if (index === 0) { // Shouldn't attempt to move lobby
|
|
|
|
return true; // same as `continue` -> skip to next item
|
|
|
|
}
|
|
|
|
|
|
|
|
var position = $(network.children()[index]); // Target channel at position
|
|
|
|
|
|
|
|
if (position.data("id") === value) { // Channel in correct place
|
|
|
|
return true; // No point in continuing
|
|
|
|
}
|
|
|
|
|
|
|
|
var channel = network.find(".chan[data-id=" + value + "]"); // Channel at position
|
|
|
|
|
|
|
|
$(channel).insertBefore(position);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-25 23:51:53 +00:00
|
|
|
function setNick(nick) {
|
2016-07-29 06:10:29 +00:00
|
|
|
// Closes the nick editor when canceling, changing channel, or when a nick
|
|
|
|
// is set in a different tab / browser / device.
|
|
|
|
toggleNickEditor(false);
|
2016-10-01 17:04:03 +00:00
|
|
|
|
|
|
|
$("#nick-value").text(nick);
|
2014-09-25 23:51:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 20:38:08 +00:00
|
|
|
function toggleNotificationMarkers(newState) {
|
|
|
|
// Toggles the favicon to red when there are unread notifications
|
2016-02-24 17:19:05 +00:00
|
|
|
if (favicon.data("toggled") !== newState) {
|
|
|
|
var old = favicon.attr("href");
|
|
|
|
favicon.attr("href", favicon.data("other"));
|
|
|
|
favicon.data("other", old);
|
|
|
|
favicon.data("toggled", newState);
|
|
|
|
}
|
2016-07-10 20:38:08 +00:00
|
|
|
|
|
|
|
// Toggles a dot on the menu icon when there are unread notifications
|
|
|
|
$("#viewport .lt").toggleClass("notified", newState);
|
2016-02-24 17:19:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 11:10:55 +00:00
|
|
|
$(document).on("visibilitychange focus click", () => {
|
2017-04-15 15:40:19 +00:00
|
|
|
if (sidebar.find(".highlight").length === 0) {
|
|
|
|
toggleNotificationMarkers(false);
|
2014-08-16 16:15:17 +00:00
|
|
|
}
|
2017-04-15 15:40:19 +00:00
|
|
|
});
|
2016-12-10 09:33:36 +00:00
|
|
|
|
2017-04-22 04:06:07 +00:00
|
|
|
// Compute how many milliseconds are remaining until the next day starts
|
|
|
|
function msUntilNextDay() {
|
|
|
|
return moment().add(1, "day").startOf("day") - moment();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all Today/Yesterday date markers in the DOM and recompute their
|
|
|
|
// labels. When done, restart the timer for the next day.
|
|
|
|
function updateDateMarkers() {
|
|
|
|
$(".date-marker-text[data-label='Today'], .date-marker-text[data-label='Yesterday']")
|
|
|
|
.closest(".date-marker-container")
|
|
|
|
.each(function() {
|
|
|
|
$(this).replaceWith(templates.date_marker({msgDate: $(this).data("timestamp")}));
|
|
|
|
});
|
|
|
|
|
|
|
|
// This should always be 24h later but re-computing exact value just in case
|
|
|
|
setTimeout(updateDateMarkers, msUntilNextDay());
|
|
|
|
}
|
|
|
|
setTimeout(updateDateMarkers, msUntilNextDay());
|
|
|
|
|
2016-12-10 09:33:36 +00:00
|
|
|
// Only start opening socket.io connection after all events have been registered
|
|
|
|
socket.open();
|
2017-01-28 17:48:34 +00:00
|
|
|
|
2017-04-08 12:34:31 +00:00
|
|
|
window.addEventListener("popstate", (e) => {
|
|
|
|
const {state} = e;
|
|
|
|
if (!state) {
|
|
|
|
return;
|
2017-01-28 17:48:34 +00:00
|
|
|
}
|
2017-04-08 12:34:31 +00:00
|
|
|
|
|
|
|
const {clickTarget} = state;
|
|
|
|
if (clickTarget) {
|
|
|
|
$(clickTarget).trigger("click", {
|
|
|
|
pushState: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-16 16:15:17 +00:00
|
|
|
});
|