2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
2017-11-01 09:16:19 +00:00
|
|
|
const escape = require("css.escape");
|
2017-05-18 20:08:54 +00:00
|
|
|
const input = $("#input");
|
|
|
|
|
2017-08-28 09:18:31 +00:00
|
|
|
var serverHash = -1;
|
|
|
|
var lastMessageId = -1;
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
module.exports = {
|
2017-10-03 20:59:19 +00:00
|
|
|
inputCommands: {collapse, expand, join},
|
2017-09-02 16:28:36 +00:00
|
|
|
findCurrentNetworkChan,
|
2017-08-28 09:18:31 +00:00
|
|
|
serverHash,
|
|
|
|
lastMessageId,
|
2017-05-18 20:08:54 +00:00
|
|
|
confirmExit,
|
|
|
|
forceFocus,
|
|
|
|
move,
|
|
|
|
resetHeight,
|
|
|
|
setNick,
|
|
|
|
toggleNickEditor,
|
2017-09-06 19:03:56 +00:00
|
|
|
toggleNotificationMarkers,
|
|
|
|
requestIdleCallback,
|
2017-11-01 09:16:19 +00:00
|
|
|
isOpInChannel,
|
2017-05-18 20:08:54 +00:00
|
|
|
};
|
|
|
|
|
2017-09-02 16:28:36 +00:00
|
|
|
function findCurrentNetworkChan(name) {
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
|
|
|
return $(".network .chan.active")
|
|
|
|
.parent(".network")
|
|
|
|
.find(".chan")
|
|
|
|
.filter(function() {
|
|
|
|
return $(this).data("title").toLowerCase() === name;
|
|
|
|
})
|
|
|
|
.first();
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
function resetHeight(element) {
|
|
|
|
element.style.height = element.style.minHeight;
|
|
|
|
}
|
|
|
|
|
2017-11-01 09:16:19 +00:00
|
|
|
// Given a channel element will determine if the lounge user is Op in that channel
|
|
|
|
function isOpInChannel(channel) {
|
|
|
|
const channelID = channel.data("id");
|
|
|
|
const network = $("#sidebar .network").has(`.chan[data-id="${channelID}"]`);
|
|
|
|
const ownNick = network.data("nick");
|
|
|
|
const isOP = channel.find(`.users .user-mode.op .user[data-name="${escape(ownNick)}"]`).length;
|
|
|
|
return isOP;
|
|
|
|
}
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
// Triggering click event opens the virtual keyboard on mobile
|
|
|
|
// This can only be called from another interactive event (e.g. button click)
|
|
|
|
function forceFocus() {
|
|
|
|
input.trigger("click").focus();
|
|
|
|
}
|
|
|
|
|
2017-09-22 20:25:14 +00:00
|
|
|
function collapse() {
|
2018-01-26 08:52:51 +00:00
|
|
|
$(".chan.active .toggle-button.toggle-preview.opened").click();
|
2017-09-22 20:25:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expand() {
|
2018-01-26 08:52:51 +00:00
|
|
|
$(".chan.active .toggle-button.toggle-preview:not(.opened)").click();
|
2017-09-22 20:25:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:59:19 +00:00
|
|
|
function join(args) {
|
|
|
|
const channel = args[0];
|
|
|
|
if (channel) {
|
|
|
|
const chan = findCurrentNetworkChan(channel);
|
|
|
|
if (chan.length) {
|
|
|
|
chan.click();
|
|
|
|
return true;
|
|
|
|
}
|
2017-09-22 20:25:14 +00:00
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleNickEditor(toggle) {
|
|
|
|
$("#nick").toggleClass("editable", toggle);
|
|
|
|
$("#nick-value").attr("contenteditable", toggle);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNick(nick) {
|
|
|
|
// Closes the nick editor when canceling, changing channel, or when a nick
|
|
|
|
// is set in a different tab / browser / device.
|
|
|
|
toggleNickEditor(false);
|
|
|
|
|
|
|
|
$("#nick-value").text(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
const favicon = $("#favicon");
|
|
|
|
|
|
|
|
function toggleNotificationMarkers(newState) {
|
|
|
|
// Toggles the favicon to red when there are unread notifications
|
|
|
|
if (favicon.data("toggled") !== newState) {
|
|
|
|
var old = favicon.attr("href");
|
|
|
|
favicon.attr("href", favicon.data("other"));
|
|
|
|
favicon.data("other", old);
|
|
|
|
favicon.data("toggled", newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggles a dot on the menu icon when there are unread notifications
|
|
|
|
$("#viewport .lt").toggleClass("notified", newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
function confirmExit() {
|
|
|
|
if ($("body").hasClass("public")) {
|
|
|
|
window.onbeforeunload = function() {
|
|
|
|
return "Are you sure you want to navigate away from this page?";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function move(array, old_index, new_index) {
|
|
|
|
if (new_index >= array.length) {
|
|
|
|
let k = new_index - array.length;
|
|
|
|
while ((k--) + 1) {
|
|
|
|
this.push(undefined);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
array.splice(new_index, 0, array.splice(old_index, 1)[0]);
|
|
|
|
return array;
|
|
|
|
}
|
2017-09-06 19:03:56 +00:00
|
|
|
|
|
|
|
function requestIdleCallback(callback, timeout) {
|
|
|
|
if (window.requestIdleCallback) {
|
|
|
|
// During an idle period the user agent will run idle callbacks in FIFO order
|
|
|
|
// until either the idle period ends or there are no more idle callbacks eligible to be run.
|
|
|
|
window.requestIdleCallback(callback, {timeout: timeout});
|
|
|
|
} else {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|