diff --git a/src/renderer/scr.exec.js b/src/renderer/scr.exec.js index b10cb15..ca5576e 100644 --- a/src/renderer/scr.exec.js +++ b/src/renderer/scr.exec.js @@ -1,8 +1,6 @@ document.addEventListener("DOMContentLoaded", () => { DISCORD.setup(); - var CURRENTFILE; - var btnUploadFile = DOM.id("upload-file"); var inputUploadedFile = DOM.id("uploaded-file"); @@ -26,7 +24,7 @@ document.addEventListener("DOMContentLoaded", () => { } if (SAVEFILE.isValid(obj)){ - CURRENTFILE = new SAVEFILE(obj); + STATE.uploadFile(new SAVEFILE(obj)); reset(); } else{ @@ -44,13 +42,9 @@ document.addEventListener("DOMContentLoaded", () => { var eleChannels = DOM.id("channels"); var eleMessages = DOM.id("messages"); - eleChannels.innerHTML = ""; + eleChannels.innerHTML = STATE.getChannelList().map(channel => DISCORD.getChannelHTML(channel)).join(""); eleMessages.innerHTML = ""; - CURRENTFILE.getChannelList().forEach(channel => { - eleChannels.innerHTML += DISCORD.getChannelHTML(channel); - }); - Array.prototype.forEach.call(eleChannels.children, ele => { ele.addEventListener("click", e => { var currentChannel = DOM.cls("active", eleChannels)[0]; @@ -62,14 +56,8 @@ document.addEventListener("DOMContentLoaded", () => { ele.classList.add("active"); - var html = []; - var messages = CURRENTFILE.getChannelMessageObject(ele.getAttribute("data-channel")); - - for(var key of Object.keys(messages).sort()){ - html.push(DISCORD.getMessageHTML(CURRENTFILE.getMessage(messages[key]))); - } - - eleMessages.innerHTML = html.join(""); + STATE.selectChannel(ele.getAttribute("data-channel")); + eleMessages.innerHTML = STATE.getMessageList(0).map(message => DISCORD.getMessageHTML(message)).join(""); }); }); }; diff --git a/src/renderer/scr.savefile.js b/src/renderer/scr.savefile.js index ea322d2..e532cd8 100644 --- a/src/renderer/scr.savefile.js +++ b/src/renderer/scr.savefile.js @@ -19,27 +19,18 @@ SAVEFILE.prototype.getServer = function(index){ }; }; -SAVEFILE.prototype.getChannelList = function(){ - return Object.keys(this.meta.channels).map(key => ({ - id: key, - name: this.meta.channels[key].name, - server: this.getServer(this.meta.channels[key].server), - msgcount: Object.keys(this.data[key]).length || 0 - })); +SAVEFILE.prototype.getChannels = function(){ + return this.meta.channels || {}; }; SAVEFILE.prototype.getUser = function(index){ return this.meta.users[this.meta.userindex[index]] || { name: "<unknown>" }; }; -SAVEFILE.prototype.getChannelMessageObject = function(channel){ - return this.data[channel] || []; +SAVEFILE.prototype.getMessageCount = function(channel){ + return Object.keys(this.data[channel]).length || 0; }; -SAVEFILE.prototype.getMessage = function(message){ - return { - user: this.getUser(message.u), - timestamp: message.t, - contents: message.m - }; +SAVEFILE.prototype.getMessages = function(channel){ + return this.data[channel] || []; }; diff --git a/src/renderer/src.state.js b/src/renderer/src.state.js new file mode 100644 index 0000000..05c6cec --- /dev/null +++ b/src/renderer/src.state.js @@ -0,0 +1,47 @@ +var STATE = (function(){ + var FILE; + var MSGS; + + var selectedChannel; + + return { + uploadFile: function(file){ + FILE = file; + MSGS = null; + }, + + getChannelList: function(){ + var channels = FILE.getChannels(); + + return Object.keys(channels).map(key => ({ + id: key, + name: channels[key].name, + server: FILE.getServer(channels[key].server), + msgcount: FILE.getMessageCount(key) + })); + }, + + selectChannel: function(channel){ + selectedChannel = channel; + MSGS = Object.keys(FILE.getMessages(channel)).sort(); + }, + + getMessageList: function(startIndex, count){ + if (!MSGS){ + return []; + } + + var messages = FILE.getMessages(selectedChannel); + + return MSGS.slice(startIndex, !count ? undefined : startIndex+count).map(key => { + var message = messages[key]; + + return { + user: FILE.getUser(message.u), + timestamp: message.t, + contents: message.m + }; + }); + } + }; +})();