2017-05-18 20:08:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const $ = require("jquery");
|
2017-08-28 20:06:28 +00:00
|
|
|
const escape = require("css.escape");
|
2017-05-18 20:08:54 +00:00
|
|
|
const socket = require("../socket");
|
|
|
|
const render = require("../render");
|
2017-07-10 19:47:03 +00:00
|
|
|
const webpush = require("../webpush");
|
2017-05-18 20:08:54 +00:00
|
|
|
const sidebar = $("#sidebar");
|
|
|
|
const storage = require("../localStorage");
|
2017-08-28 15:03:27 +00:00
|
|
|
const utils = require("../utils");
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
socket.on("init", function(data) {
|
2017-08-28 15:03:27 +00:00
|
|
|
$("#loading-page-message, #connection-error").text("Rendering…");
|
|
|
|
|
|
|
|
const lastMessageId = utils.lastMessageId;
|
2017-08-28 20:06:28 +00:00
|
|
|
let previousActive = 0;
|
2017-08-28 15:03:27 +00:00
|
|
|
|
|
|
|
if (lastMessageId > -1) {
|
2017-08-28 20:06:28 +00:00
|
|
|
previousActive = sidebar.find(".active").data("id");
|
2017-08-28 15:03:27 +00:00
|
|
|
sidebar.find(".networks").empty();
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
|
|
|
if (data.networks.length === 0) {
|
2017-08-28 15:03:27 +00:00
|
|
|
sidebar.find(".empty").show();
|
|
|
|
|
2017-05-18 20:08:54 +00:00
|
|
|
$("#footer").find(".connect").trigger("click", {
|
|
|
|
pushState: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
render.renderNetworks(data);
|
|
|
|
}
|
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
if (lastMessageId > -1) {
|
|
|
|
$("#connection-error").removeClass("shown");
|
|
|
|
$(".show-more-button, #input").prop("disabled", false);
|
|
|
|
$("#submit").show();
|
|
|
|
} else {
|
|
|
|
if (data.token) {
|
|
|
|
storage.set("token", data.token);
|
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
webpush.configurePushNotifications(data.pushSubscription, data.applicationServerKey);
|
2017-07-10 19:47:03 +00:00
|
|
|
|
2017-08-28 15:03:27 +00:00
|
|
|
$("body").removeClass("signed-out");
|
|
|
|
$("#loading").remove();
|
|
|
|
$("#sign-in").remove();
|
2017-12-01 18:04:50 +00:00
|
|
|
|
|
|
|
if (window.g_LoungeErrorHandler) {
|
|
|
|
window.removeEventListener("error", window.g_LoungeErrorHandler);
|
|
|
|
window.g_LoungeErrorHandler = null;
|
|
|
|
}
|
2017-08-28 15:03:27 +00:00
|
|
|
}
|
2017-05-18 20:08:54 +00:00
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
openCorrectChannel(previousActive, data.active);
|
2017-05-18 20:08:54 +00:00
|
|
|
});
|
2017-08-28 20:06:28 +00:00
|
|
|
|
|
|
|
function openCorrectChannel(clientActive, serverActive) {
|
2017-09-20 07:44:36 +00:00
|
|
|
let target = $();
|
2017-08-28 20:06:28 +00:00
|
|
|
|
|
|
|
// Open last active channel
|
|
|
|
if (clientActive > 0) {
|
2017-12-23 08:00:16 +00:00
|
|
|
target = sidebar.find(`.chan[data-id="${clientActive}"]`);
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open window provided in location.hash
|
2017-09-20 07:44:36 +00:00
|
|
|
if (target.length === 0 && window.location.hash) {
|
2017-12-25 00:17:26 +00:00
|
|
|
target = $(`[data-target="${escape(window.location.hash)}"]`).first();
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open last active channel according to the server
|
2017-09-20 07:44:36 +00:00
|
|
|
if (serverActive > 0 && target.length === 0) {
|
2017-12-23 08:00:16 +00:00
|
|
|
target = sidebar.find(`.chan[data-id="${serverActive}"]`);
|
2017-08-28 20:06:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 07:44:36 +00:00
|
|
|
// Open first available channel
|
|
|
|
if (target.length === 0) {
|
|
|
|
target = sidebar.find(".chan").first();
|
|
|
|
}
|
|
|
|
|
2017-08-28 20:06:28 +00:00
|
|
|
// If target channel is found, open it
|
2017-09-20 07:44:36 +00:00
|
|
|
if (target.length > 0) {
|
2017-08-28 20:06:28 +00:00
|
|
|
target.trigger("click", {
|
2017-11-15 06:35:15 +00:00
|
|
|
replaceHistory: true,
|
2017-08-28 20:06:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the connect window
|
|
|
|
$("#footer .connect").trigger("click", {
|
2017-11-15 06:35:15 +00:00
|
|
|
pushState: false,
|
2017-08-28 20:06:28 +00:00
|
|
|
});
|
|
|
|
}
|