thelounge/client/js/socket-events/init.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-05-18 20:08:54 +00:00
"use strict";
const $ = require("jquery");
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;
// TODO: this is hacky
if (lastMessageId > -1) {
sidebar.find(".networks").empty();
$("#chat").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-05-18 20:08:54 +00:00
const id = data.active;
const target = sidebar.find("[data-id='" + id + "']").trigger("click", {
replaceHistory: true
});
const dataTarget = document.querySelector("[data-target='" + window.location.hash + "']");
if (window.location.hash && dataTarget) {
dataTarget.click();
} else if (target.length === 0) {
2017-05-18 20:08:54 +00:00
const first = sidebar.find(".chan")
.eq(0)
.trigger("click");
if (first.length === 0) {
$("#footer").find(".connect").trigger("click", {
pushState: false,
});
}
}
});