From 2c94713ba04cdf4e3a6d5b13555f0cbd636ce73c Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 5 Feb 2017 16:11:52 +1300 Subject: [PATCH] persistence_set/get functions, save the join/part status --- client/dcwebui.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/client/dcwebui.js b/client/dcwebui.js index 2eb2276..d852fb9 100644 --- a/client/dcwebui.js +++ b/client/dcwebui.js @@ -258,9 +258,7 @@ var submit = function() { if (hub_state === 0) { hub_state = 1; - if (window.localStorage) { - localStorage["login"] = str; - } + persistence_set("login", str); var name_parts = str.split(":", 2); hub_last_nick = name_parts[0]; @@ -594,6 +592,7 @@ var joinparts_getstr = function() { var toggle_joinparts = function(ev) { var $el = ev.target || ev.srcElement; show_joins = ! show_joins; + persistence_set("show_joins", show_joins); $el.innerHTML = joinparts_getstr(); }; @@ -609,7 +608,7 @@ var updateTitle = function() { var sock = {}; var hub_state = 0; // [disconnected, sent-nick, connected] var hub_last_nick = ''; -var hub_hubname = 'DCWebUI'; +var hub_hubname = DCWEBUI_CONF.title; var pm_tabs = {}; // nick => tabid var next_tabid = 1; @@ -619,15 +618,27 @@ var last_tab = "tab-main"; var show_joins = false; +/* */ + +var persistence_set = function(key, value) { + if (window.localStorage) { + window.localStorage[key] = JSON.stringify(value); + } +}; + +var persistence_get = function(key, fallback) { + try { + return JSON.parse( window.localStorage[key] ); + } catch (ex) { + return fallback; + } +}; + window.onload = function() { write("tab-main").system("Communicating with server..."); - if (window.localStorage) { - var saved_login = localStorage["login"]; - if (saved_login !== undefined) { - $("#chatbox").value = saved_login; - } - } + $("#chatbox").value = persistence_get("login", ""); + show_joins = persistence_get("show_joins", false); document.title = DCWEBUI_CONF.title;