Refactor viewer (template literals, classes, minor refactoring)

This commit is contained in:
chylex 2018-08-05 12:43:30 +02:00
parent 5ce4feaf89
commit 1d4479e5d2
5 changed files with 67 additions and 75 deletions

View File

@ -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.
*/

View File

@ -32,10 +32,9 @@ var GUI = (function(){
// -------------
var showSettingsModal = function(){
showModal(560, [
"<label><input id='dht-cfg-imgpreviews' type='checkbox'> Image Previews</label><br>",
"<label><input id='dht-cfg-formatting' type='checkbox'> Message Formatting</label><br>"
].join(""));
showModal(560, `
<label><input id='dht-cfg-imgpreviews' type='checkbox'> Image Previews</label><br>
<label><input id='dht-cfg-formatting' type='checkbox'> Message Formatting</label><br>`);
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, [
"<p>Discord History Tracker is developed by <a href='https://chylex.com'>chylex</a> as an <a href='"+linkGH+"/blob/master/LICENSE.md'>open source</a> project.</p>",
"<sub>BETA v.7, released 16 Feb 2018</sub>",
"<p>Please, report any issues and suggestions to the <a href='"+linkGH+"/issues'>tracker</a>. If you want to support the development, please spread the word and consider <a href='https://www.patreon.com/chylex'>becoming a patron</a>. Any support is appreciated!</p>",
"<p>",
"<a href='"+linkGH+"/issues'>Issue Tracker</a> &nbsp;&mdash;&nbsp; ",
"<a href='"+linkGH+"'>GitHub Repository</a> &nbsp;&mdash;&nbsp; ",
"<a href='https://twitter.com/chylexmc'>Developer's Twitter</a>",
"</p>"
].join(""));
showModal(560, `
<p>Discord History Tracker is developed by <a href='https://chylex.com'>chylex</a> as an <a href='${linkGH}/blob/master/LICENSE.md'>open source</a> project.</p>
<sub>BETA v.7, released 16 Feb 2018</sub>
<p>Please, report any issues and suggestions to the <a href='${linkGH}/issues'>tracker</a>. If you want to support the development, please spread the word and consider <ref='https://www.patreon.com/chylex'>becoming a patron</a>. Any support is appreciated!</p>
<p><a href='${linkGH}/issues'>Issue Tracker</a> &nbsp;&mdash;&nbsp; <a href='${linkGH}'>GitHub Repository</a> &nbsp;&mdash;&nbsp; <a href='https://twitter.com/chylexmc'>Developer's Twitter</a></p>`);
};
return {
@ -269,4 +263,4 @@ var GUI = (function(){
DOM.id("messages").scrollTop = 0;
}
};
})();
})();

View File

@ -1,5 +1,4 @@
var PROCESSOR = function(messageObject){
};
var PROCESSOR = {};
// ------------------------
// Global filter generators

View File

@ -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": "&lt;unknown&gt;", "type": "ERROR" };
};
getServer(index){
return this.meta.servers[index] || { "name": "&lt;unknown&gt;", "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": "&lt;unknown&gt;" };
};
getUser(index){
return this.meta.users[this.meta.userindex[index]] || { "name": "&lt;unknown&gt;" };
}
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] || {};
}
}

View File

@ -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);
});
}
}