Refactor - clean up isDefined, isObject, if statements, helpers etc.

This commit is contained in:
kor 2018-10-30 23:29:02 +13:00
parent 8240650f53
commit c52491f3c1

View File

@ -80,11 +80,6 @@ function View()
imgLoad.on( 'always', function() { parent.msnry.layout(); } ); imgLoad.on( 'always', function() { parent.msnry.layout(); } );
} }
this.isDefined = function(value)
{
return (typeof value !== 'undefined');
}
this.buildEntry = function(db, key) this.buildEntry = function(db, key)
{ {
let value = db[key]; let value = db[key];
@ -95,7 +90,7 @@ function View()
{ {
itemClass += " griditem-wide"; itemClass += " griditem-wide";
} }
else if (typeof value.QOTE !== 'undefined') else if (this.isDefined(value.QOTE))
{ {
if (Array.isArray(value.QOTE) && value.QOTE.length > 4) if (Array.isArray(value.QOTE) && value.QOTE.length > 4)
{ {
@ -104,10 +99,7 @@ function View()
} }
} }
if ( if (SETTINGS.SHOWIMAG && this.isDefined(value.TYP) && value.TYPE === 'image')
SETTINGS.SHOWIMAG
&& typeof value.TYPE !== 'undefined' && value.TYPE === 'image'
)
{ {
itemClass += " griditem-image"; itemClass += " griditem-image";
} }
@ -119,19 +111,16 @@ function View()
if (this.isDefined(value.LINK)) if (this.isDefined(value.LINK))
{ {
var idUrl = "url"; var idUrl = "url";
if (typeof value.SEEN !== 'undefined' && value.SEEN === "true") if (this.isDefined(value.SEEN) && value.SEEN === "true")
{ {
idUrl = "urlseen"; idUrl = "urlseen";
} }
// LINK START // LINK START
if (SETTINGS.SHOWLINK) if (SETTINGS.SHOWLINK && !this.isObject(value.LINK))
{ {
if (typeof value.LINK != 'object') // If this item has only one link then make the whole title the link
{ entry += `<a class="griditem-link" href="${String(value.LINK)}" id="${idUrl}">`;
// If this item has only one link then make the whole title the link
entry += `<a class="griditem-link" href="${String(value.LINK)}" id="${idUrl}">`;
}
} }
} }
@ -147,9 +136,9 @@ function View()
} }
// LINK END // LINK END
if (SETTINGS.SHOWLINK && typeof value.LINK !== 'undefined') if (SETTINGS.SHOWLINK && this.isDefined(value.LINK))
{ {
if (typeof value.LINK == 'object') if (this.isObject(value.LINK))
{ {
for (let l = 0; l < value.LINK.length; l++) for (let l = 0; l < value.LINK.length; l++)
{ {
@ -164,7 +153,7 @@ function View()
} }
// TYPE // TYPE
if (SETTINGS.SHOWTYPE && typeof value.TYPE !== 'undefined') if (SETTINGS.SHOWTYPE && this.isDefined(value.TYPE))
{ {
entry += `<div class="griditem-typecontainer">`; entry += `<div class="griditem-typecontainer">`;
@ -188,13 +177,13 @@ function View()
entry += `<div class="griditem-containerlower">`; entry += `<div class="griditem-containerlower">`;
// AUTHOR // AUTHOR
if (SETTINGS.SHOWAUTH && typeof value.AUTH !== 'undefined') if (SETTINGS.SHOWAUTH && this.isDefined(value.AUTH))
{ {
entry += `<div class="griditem-auth"><i class="fas fa-user textIcon"></i>${value.AUTH}</div>`; entry += `<div class="griditem-auth"><i class="fas fa-user textIcon"></i>${value.AUTH}</div>`;
} }
// TAGS // TAGS
if (SETTINGS.SHOWTAGS && typeof value.TAGS !== 'undefined') if (SETTINGS.SHOWTAGS && this.isDefined(value.TAGS))
{ {
entry += `<div class="griditem-tags"><i class="fas fa-tag textIcon"></i>`; entry += `<div class="griditem-tags"><i class="fas fa-tag textIcon"></i>`;
for (var i = 0; i < value.TAGS.length; i++) for (var i = 0; i < value.TAGS.length; i++)
@ -209,74 +198,65 @@ function View()
} }
// PROJECT // PROJECT
if (SETTINGS.SHOWPROJ) if (SETTINGS.SHOWPROJ && this.isDefined(value.PROJ))
{ {
if (this.isDefined(value.PROJ)) entry += `<div class="griditem-proj"><i class="fas fa-leaf textIcon"></i>`;
for (var i = 0; i < value.PROJ.length; i++)
{ {
entry += `<div class="griditem-proj"><i class="fas fa-leaf textIcon"></i>`; entry += `<a class="griditem-taglink" href="#proj-${value.PROJ[i]}">${value.PROJ[i].to_properCase()}</a>`;
for (var i = 0; i < value.PROJ.length; i++) if (i + 1 != value.PROJ.length)
{ {
entry += `<a class="griditem-taglink" href="#proj-${value.PROJ[i]}">${value.PROJ[i]}</a>`; entry += `, `;
if (i + 1 != value.PROJ.length) }
{
entry += `, `;
}
};
entry += `</div>`;
} }
entry += `</div>`;
} }
// TERM // TERM
if (SETTINGS.SHOWTERM) if (SETTINGS.SHOWTERM && this.isDefined(value.TERM))
{ {
if (this.isDefined(value.TERM)) entry += this.doMultilineFormatting(value.TERM, "griditem-term", "fas fa-ribbon textIcon");
{
entry += this.buildArrayElement(value.TERM, "griditem-term", "fas fa-ribbon textIcon");
}
} }
// NOTE // NOTE
if (SETTINGS.SHOWNOTE && typeof value.NOTE !== 'undefined') if (SETTINGS.SHOWNOTE && this.isDefined(value.NOTE))
{ {
entry += this.buildArrayElement(value.NOTE, "griditem-note", "fas fa-sticky-note textIcon"); entry += this.doMultilineFormatting(value.NOTE, "griditem-note", "fas fa-sticky-note textIcon");
} }
// QUOTE // QUOTE
if (SETTINGS.SHOWQOTE && typeof value.QOTE !== 'undefined') if (SETTINGS.SHOWQOTE && this.isDefined(value.QOTE))
{ {
entry += this.buildArrayElement(value.QOTE, "griditem-quote", "fas fa-comment textIcon"); entry += this.doMultilineFormatting(value.QOTE, "griditem-quote", "fas fa-comment textIcon");
} }
// TERM // TERM
if (SETTINGS.SHOWTERM && typeof value.TERM !== 'undefined') if (SETTINGS.SHOWTERM && this.isDefined(value.TERM))
{ {
entry += this.buildArrayElement(value.TERM, "griditem-term", "fas fa-ribbon textIcon"); entry += this.doMultilineFormatting(value.TERM, "griditem-term", "fas fa-ribbon textIcon");
} }
// PROGRESS // PROGRESS
if (SETTINGS.SHOWPROG && typeof value.PROG !== 'undefined') if (SETTINGS.SHOWPROG && this.isDefined(value.PROG))
{ {
entry += `<div class="griditem-prog"><i class="fas fa-clock textIcon"></i>${value.PROG}</div>`; entry += `<div class="griditem-prog"><i class="fas fa-clock textIcon"></i>${value.PROG}</div>`;
} }
// FILE // FILE
if (SETTINGS.SHOWFILE) if (SETTINGS.SHOWFILE && this.isDefined(value.FILE))
{ {
if (this.isDefined(value.FILE)) if (this.isObject(value.FILE))
{ {
if (typeof value.FILE == 'object') for (var i = 0; i < value.FILE.length; i++)
{ {
for (var i = 0; i < value.FILE.length; i++) entry += `<div class="griditem-file"><i class="fas fa-folder-open textIcon"></i><a class="griditem-file-link" href="content/media/${value.FILE[i]}">${value.FILE[i]}</a></div>`;
{
entry += `<div class="griditem-file"><i class="fas fa-folder-open textIcon"></i><a class="griditem-file-link" href="content/media/${value.FILE[i]}">${value.FILE[i]}</a></div>`;
}
}
else
{
// single
entry += `<div class="griditem-file"><i class="fas fa-folder-open textIcon"></i><a class="griditem-file-link" href="content/media/${value.FILE}">${value.FILE}</a></div>`;
} }
} }
else
{
// single
entry += `<div class="griditem-file"><i class="fas fa-folder-open textIcon"></i><a class="griditem-file-link" href="content/media/${value.FILE}">${value.FILE}</a></div>`;
}
} }
// LOWER CONTENT END // LOWER CONTENT END
@ -284,11 +264,8 @@ function View()
} }
// IMAGE // IMAGE
if ( if (SETTINGS.SHOWIMAG && this.isDefined(value.TYPE)
SETTINGS.SHOWIMAG && value.TYPE === 'image' && this.isDefined(value.FILE))
&& typeof value.TYPE !== 'undefined' && value.TYPE === 'image'
&& typeof value.FILE !== 'undefined'
)
{ {
entry += `<div class="image">`; entry += `<div class="image">`;
if (SETTINGS.SHOWOVERLAY) if (SETTINGS.SHOWOVERLAY)
@ -306,7 +283,6 @@ function View()
this.stats = function(value) this.stats = function(value)
{ {
let menuContent = ``; let menuContent = ``;
if (window.showAdd !== undefined && window.showAdd) if (window.showAdd !== undefined && window.showAdd)
{ {
// ADD // ADD
@ -327,20 +303,17 @@ function View()
// DONE // DONE
menuContent += `<div class="menu-itemgroup">`; menuContent += `<div class="menu-itemgroup">`;
menuContent += `<a href='#done-true' class="menu-item">`; menuContent += `<a href='#done-true' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.done}</div>`; menuContent += `<div class="menu-itemcount">${value.done}</div>`;
menuContent += `<i class="menu-itemicon fas fa-check"></i>`; menuContent += `<i class="menu-itemicon fas fa-check"></i>`;
menuContent += `</a>`; menuContent += `</a>`;
menuContent += `<a href='#done-false' class="menu-item">`; menuContent += `<a href='#done-false' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.total - value.done}</div>`; menuContent += `<div class="menu-itemcount">${value.total - value.done}</div>`;
menuContent += `<i class="menu-itemicon fas fa-times"></i>`; menuContent += `<i class="menu-itemicon fas fa-times"></i>`;
menuContent += `</a>`; menuContent += `</a>`;
menuContent += `</div>`; menuContent += `</div>`;
menuContent += `<div class="menu-itemgroup">`;
menuContent += `<div class="menu-itemgroup">`;
for (let ty = 0; ty < Math.min(value.types.length, SETTINGS.STATSNUMTYPE); ty++) for (let ty = 0; ty < Math.min(value.types.length, SETTINGS.STATSNUMTYPE); ty++)
{ {
const type = value.types[ty][0]; const type = value.types[ty][0];
@ -363,7 +336,6 @@ function View()
menuContent += `<i class="menu-itemicon fas fa-ribbon"></i>`; menuContent += `<i class="menu-itemicon fas fa-ribbon"></i>`;
menuContent += `</a>`; menuContent += `</a>`;
} }
menuContent += `</div>`; menuContent += `</div>`;
// TAGS // TAGS
@ -383,12 +355,10 @@ function View()
menuContent += `</div>`; menuContent += `</div>`;
} }
menuContent += `</div>`; menuContent += `</div>`;
this.menu.innerHTML = menuContent;
this.menu.innerHTML = ``;
this.menu.innerHTML += menuContent;
} }
this.buildArrayElement = function(data, className, iconName) this.doMultilineFormatting = function(data, className, iconName)
{ {
let result = ''; let result = '';
if (Array.isArray(data)) if (Array.isArray(data))
@ -437,7 +407,6 @@ function View()
return result; return result;
} }
// HELPER
getTypeIconName = function(type) getTypeIconName = function(type)
{ {
let icon = ''; let icon = '';
@ -461,6 +430,17 @@ function View()
return icon; return icon;
} }
// GENERAL HELPER
this.isDefined = function(value)
{
return (typeof value !== 'undefined');
}
this.isObject = function(value)
{
return (typeof value == 'object');
}
String.prototype.to_properCase = function() String.prototype.to_properCase = function()
{ {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
@ -500,8 +480,6 @@ function View()
} }
else else
{ {
console.log('calling extract on: ' + url);
hostname = url.split('/')[0]; hostname = url.split('/')[0];
} }