From 6195e5d8f64c9c230c361290d1deb275fafdacdd Mon Sep 17 00:00:00 2001 From: Al McKinlay Date: Thu, 15 Mar 2018 09:18:06 +0000 Subject: [PATCH] Save collapsed state to localstorage --- client/js/render.js | 20 +++++++++++++++++--- client/views/network.tpl | 1 + 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/client/js/render.js b/client/js/render.js index cbf12023..6b612929 100644 --- a/client/js/render.js +++ b/client/js/render.js @@ -11,6 +11,7 @@ const condensed = require("./condensed"); const JoinChannel = require("./join-channel"); const helpers_parse = require("./libs/handlebars/parse"); const Userlist = require("./userlist"); +const storage = require("./localStorage"); const chat = $("#chat"); const sidebar = $("#sidebar"); @@ -195,6 +196,8 @@ function renderChannelUsers(data) { } function renderNetworks(data, singleNetwork) { + const collapsed = new Set(JSON.parse(storage.get("thelounge.networks.collapsed"))); + sidebar.find(".empty").hide(); sidebar.find(".networks").append( templates.network({ @@ -202,6 +205,10 @@ function renderNetworks(data, singleNetwork) { }) ); + collapsed.forEach((key) => { + collapseNetwork($(`.network[data-uuid="${key}"] button.collapse-network`)); + }); + // Add keyboard handlers to the "Join a channel…" form inputs/button JoinChannel.handleKeybinds(); @@ -305,8 +312,12 @@ function loadMoreHistory(entries) { }); } -sidebar.on("click", ".collapse-network", (e) => { - const collapseButton = $(e.target); +sidebar.on("click", "button.collapse-network", (e) => collapseNetwork($(e.target))); + +function collapseNetwork(target) { + const collapseButton = target.closest(".collapse-network"); + const networks = new Set(JSON.parse(storage.get("thelounge.networks.collapsed"))); + const networkuuid = collapseButton.closest(".network").data("uuid"); if (collapseButton.closest(".network").find(".active").length > 0) { collapseButton.closest(".lobby").click(); @@ -317,10 +328,13 @@ sidebar.on("click", ".collapse-network", (e) => { if (collapseButton.attr("aria-expanded") === "true") { collapseButton.attr("aria-expanded", false); collapseButton.attr("aria-label", "Expand"); + networks.add(networkuuid); } else { collapseButton.attr("aria-expanded", true); collapseButton.attr("aria-label", "Collapse"); + networks.delete(networkuuid); } + storage.set("thelounge.networks.collapsed", JSON.stringify([...networks])); return false; -}); +} diff --git a/client/views/network.tpl b/client/views/network.tpl index 1f14835f..607ee358 100644 --- a/client/views/network.tpl +++ b/client/views/network.tpl @@ -3,6 +3,7 @@ id="network-{{id}}" class="network name-{{slugify name}} {{#if serverOptions.NETWORK}}network-{{slugify serverOptions.NETWORK}}{{/if}} {{#unless status.connected}}not-connected{{/unless}} {{#unless status.secure}}not-secure{{/unless}}" data-id="{{id}}" + data-uuid="{{uuid}}" data-nick="{{nick}}" data-options="{{tojson serverOptions}}" role="region"