From 1d4479e5d2ec56723caf96440e5f7d32b1b32451 Mon Sep 17 00:00:00 2001 From: chylex Date: Sun, 5 Aug 2018 12:43:30 +0200 Subject: [PATCH] Refactor viewer (template literals, classes, minor refactoring) --- src/renderer/scr.dom.js | 5 --- src/renderer/scr.gui.js | 24 ++++------- src/renderer/scr.processor.js | 3 +- src/renderer/scr.savefile.js | 78 ++++++++++++++++++----------------- src/renderer/scr.template.js | 32 +++++++------- 5 files changed, 67 insertions(+), 75 deletions(-) diff --git a/src/renderer/scr.dom.js b/src/renderer/scr.dom.js index 3e68f0d..59ebeb9 100644 --- a/src/renderer/scr.dom.js +++ b/src/renderer/scr.dom.js @@ -30,11 +30,6 @@ var DOM = (function(){ */ fcls: (cls, parent) => (parent || document).getElementsByClassName(cls)[0], - /* - * Returns the first child element that has the specified tag. Parent defaults to the entire document. - */ - ftag: (tag, parent) => (parent || document).getElementsByTagName(tag)[0], - /* * Creates an element, adds it to the DOM, and returns it. */ diff --git a/src/renderer/scr.gui.js b/src/renderer/scr.gui.js index 81a1b69..4530103 100644 --- a/src/renderer/scr.gui.js +++ b/src/renderer/scr.gui.js @@ -32,10 +32,9 @@ var GUI = (function(){ // ------------- var showSettingsModal = function(){ - showModal(560, [ - "
", - "
" - ].join("")); + showModal(560, ` +
+
`); var setupCheckBox = function(id, settingName){ var ele = DOM.id(id); @@ -50,16 +49,11 @@ var GUI = (function(){ var showInfoModal = function(){ var linkGH = "https://github.com/chylex/Discord-History-Tracker"; - showModal(560, [ - "

Discord History Tracker is developed by chylex as an open source project.

", - "BETA v.7, released 16 Feb 2018", - "

Please, report any issues and suggestions to the tracker. If you want to support the development, please spread the word and consider becoming a patron. Any support is appreciated!

", - "

", - "Issue Tracker  —  ", - "GitHub Repository  —  ", - "Developer's Twitter", - "

" - ].join("")); + showModal(560, ` +

Discord History Tracker is developed by chylex as an open source project.

+BETA v.7, released 16 Feb 2018 +

Please, report any issues and suggestions to the tracker. If you want to support the development, please spread the word and consider becoming a patron. Any support is appreciated!

+

Issue Tracker  —  GitHub Repository  —  Developer's Twitter

`); }; return { @@ -269,4 +263,4 @@ var GUI = (function(){ DOM.id("messages").scrollTop = 0; } }; -})(); \ No newline at end of file +})(); diff --git a/src/renderer/scr.processor.js b/src/renderer/scr.processor.js index c6dae28..e7484ff 100644 --- a/src/renderer/scr.processor.js +++ b/src/renderer/scr.processor.js @@ -1,5 +1,4 @@ -var PROCESSOR = function(messageObject){ -}; +var PROCESSOR = {}; // ------------------------ // Global filter generators diff --git a/src/renderer/scr.savefile.js b/src/renderer/scr.savefile.js index f8ce660..7aa0c04 100644 --- a/src/renderer/scr.savefile.js +++ b/src/renderer/scr.savefile.js @@ -1,47 +1,49 @@ -var SAVEFILE = function(parsedObj){ - var me = this; - - me.meta = parsedObj.meta; - me.meta.users = me.meta.users || {}; - me.meta.userindex = me.meta.userindex || []; - me.meta.servers = me.meta.servers || []; - me.meta.channels = me.meta.channels || {}; - - me.data = parsedObj.data; -}; +class SAVEFILE{ + constructor(parsedObj){ + var me = this; + + me.meta = parsedObj.meta; + me.data = parsedObj.data; + + me.meta.users = me.meta.users || {}; + me.meta.userindex = me.meta.userindex || []; + me.meta.servers = me.meta.servers || []; + me.meta.channels = me.meta.channels || {}; + }; -SAVEFILE.isValid = function(parsedObj){ - return parsedObj && typeof parsedObj.meta === "object" && typeof parsedObj.data === "object"; -}; + static isValid(parsedObj){ + return parsedObj && typeof parsedObj.meta === "object" && typeof parsedObj.data === "object"; + }; -SAVEFILE.prototype.getServer = function(index){ - return this.meta.servers[index] || { "name": "<unknown>", "type": "ERROR" }; -}; + getServer(index){ + return this.meta.servers[index] || { "name": "<unknown>", "type": "ERROR" }; + } -SAVEFILE.prototype.getChannels = function(){ - return this.meta.channels; -}; + getChannels(){ + return this.meta.channels; + } -SAVEFILE.prototype.getChannelById = function(channel){ - return this.meta.channels[channel] || { "id": channel, "name": channel }; -}; + getChannelById(channel){ + return this.meta.channels[channel] || { "id": channel, "name": channel }; + } -SAVEFILE.prototype.getUsers = function(){ - return this.meta.users; -}; + getUsers(){ + return this.meta.users; + } -SAVEFILE.prototype.getUser = function(index){ - return this.meta.users[this.meta.userindex[index]] || { "name": "<unknown>" }; -}; + getUser(index){ + return this.meta.users[this.meta.userindex[index]] || { "name": "<unknown>" }; + } -SAVEFILE.prototype.getUserById = function(user){ - return this.meta.users[user] || { "name": user }; -}; + getUserById(user){ + return this.meta.users[user] || { "name": user }; + } -SAVEFILE.prototype.getUserIndex = function(user){ - return this.meta.userindex.indexOf(user); -}; + getUserIndex(user){ + return this.meta.userindex.indexOf(user); + } -SAVEFILE.prototype.getMessages = function(channel){ - return this.data[channel] || {}; -}; + getMessages(channel){ + return this.data[channel] || {}; + } +} diff --git a/src/renderer/scr.template.js b/src/renderer/scr.template.js index b703dba..c44b70e 100644 --- a/src/renderer/scr.template.js +++ b/src/renderer/scr.template.js @@ -1,18 +1,20 @@ var TEMPLATE_REGEX = /{([^{}]+?)}/g; -var TEMPLATE = function(contents){ - this.contents = contents; -}; +class TEMPLATE{ + constructor(contents){ + this.contents = contents; + }; -TEMPLATE.prototype.apply = function(obj, processor){ - return this.contents.replace(TEMPLATE_REGEX, (full, match) => { - var value = match.split(".").reduce((o, property) => o[property], obj); - - if (processor){ - var updated = processor(match, value); - return typeof updated === "undefined" ? DOM.escapeHTML(value) : updated; - } - - return DOM.escapeHTML(value); - }); -}; + apply(obj, processor){ + return this.contents.replace(TEMPLATE_REGEX, (full, match) => { + var value = match.split(".").reduce((o, property) => o[property], obj); + + if (processor){ + var updated = processor(match, value); + return typeof updated === "undefined" ? DOM.escapeHTML(value) : updated; + } + + return DOM.escapeHTML(value); + }); + } +}