function View() { this.msnry = null; this.overlay = null; this.container = null; this.grid = null; this.menu = null; var parent = this; const SETTINGS = { STATSNUMTAGS: 5, STATSNUMTYPE: 10, WIDEGRIDITEM: true, USEMASONRY: true, GRIDITEMIDBASE: 'item', SHOWUPPER: true, SHOWTITLE: true, SHOWAUTH: true, SHOWTYPE: true, SHOWLINK: true, SHOWLOWER: true, SHOWTAGS: true, SHOWPROJ: true, SHOWNOTE: true, SHOWQOTE: true, SHOWTERM: true, SHOWPROG: true, SHOWIMAG: true, SHOWOVERLAY: true } this.install = function() { this.overlay = document.getElementById("overlay"); this.container = document.getElementById("container"); this.grid = document.getElementById("grid"); this.menu = document.getElementById("menu"); if (SETTINGS.USEMASONRY) { this.msnry = new Masonry('.grid', { itemSelector: '.griditem', columnWidth: 350, gutter: 20, fitWidth: true, transitionDuration: 0, }); } } this.display = function(db) { if (window.showAdd != undefined && window.showAdd) { main.add.setOverlay(false); } // BUILD this.grid.innerHTML = ''; var dbKeys = Object.keys(db); var i = 0; while (i < dbKeys.length) { this.buildEntry(db, dbKeys[i]); i++; } // LAYOUT if (SETTINGS.USEMASONRY) { this.msnry.reloadItems(); this.msnry.layout(); } var imgLoad = imagesLoaded( container ); // When all images finish: redo mansonry layout imgLoad.on( 'always', function() { parent.msnry.layout(); } ); } this.isDefined = function(value) { return (typeof value !== 'undefined'); } this.buildEntry = function(db, key) { let value = db[key]; let itemClass = "griditem"; if (SETTINGS.WIDEGRIDITEM) { if (this.isDefined(value.WIDE) && value.WIDE) { itemClass += " griditem-wide"; } else if (typeof value.QOTE !== 'undefined') { if (Array.isArray(value.QOTE) && value.QOTE.length > 4) { itemClass += " griditem-wide"; } } } if (SETTINGS.SHOWIMAG) { if (this.isDefined(value.TYPE) && value.TYPE == 'image') { itemClass += " griditem-image"; } } let entry = ``; // ITEM DIV entry += `
`; if (this.isDefined(value.LINK)) { var idUrl = "url"; if (this.isDefined(value.SEEN)) { if (value.SEEN == "true") { idUrl = "urlseen"; } } // LINK START if (SETTINGS.SHOWLINK) { if (typeof value.LINK != 'object') { // If this item has only one link then make the whole title the link entry += ``; } } } // UPPER CONTENT START if (SETTINGS.SHOWUPPER) { entry += `
`; // TITLE if (SETTINGS.SHOWTITLE) { entry += `
${key.to_properCase()}
`; } // LINK END if (SETTINGS.SHOWLINK) { if (this.isDefined(value.LINK)) { if (typeof value.LINK == 'object') { for (let l = 0; l < value.LINK.length; l++) { entry += `
`; entry += ``; } } else { entry += ``; } } } // TYPE if (SETTINGS.SHOWTYPE) { entry += `
`; if (this.isDefined(value.TYPE)) { if (typeof value.TYPE == 'object') { // This entry has an array of types for (var i = 0; i < value.TYPE.length; i++) { entry += this.doTypeIcon(value.TYPE[i]); } } else { // This entry has a single type entry += this.doTypeIcon(value.TYPE[i]); } } entry += `
`; //griditem-typecontainer } // UPPER CONTENT END entry += `
`; } // LOWER CONTENT START if (SETTINGS.SHOWLOWER) { entry += `
`; // AUTHOR if (SETTINGS.SHOWAUTH) { if (this.isDefined(value.AUTH)) { entry += `
${value.AUTH}
`; } } // TAGS if (SETTINGS.SHOWTAGS) { if (this.isDefined(value.TAGS)) { entry += `
`; for (var i = 0; i < value.TAGS.length; i++) { entry += `${value.TAGS[i]}`; if (i + 1 != value.TAGS.length) { entry += `, `; } }; entry += `
`; } } // PROJECT if (SETTINGS.SHOWPROJ) { if (this.isDefined(value.PROJ)) { entry += `
`; for (var i = 0; i < value.PROJ.length; i++) { entry += `${value.PROJ[i]}`; if (i + 1 != value.PROJ.length) { entry += `, `; } }; entry += `
`; } } // TERM if (SETTINGS.SHOWTERM) { if (this.isDefined(value.TERM)) { entry += this.buildArrayElement(value.TERM, "griditem-term", "fas fa-ribbon textIcon"); } } // NOTE if (SETTINGS.SHOWNOTE) { if (this.isDefined(value.NOTE)) { entry += this.buildArrayElement(value.NOTE, "griditem-note", "fas fa-sticky-note textIcon"); } } // QUOTE if (SETTINGS.SHOWQOTE) { if (this.isDefined(value.QOTE)) { entry += this.buildArrayElement(value.QOTE, "griditem-quote", "fas fa-comment textIcon"); } } // PROGRESS if (SETTINGS.SHOWPROG) { if (this.isDefined(value.PROG)) { entry += `
${value.PROG}
`; } } // LOWER CONTENT END entry += `
`; } // IMAGE if (SETTINGS.SHOWIMAG) { if (this.isDefined(value.TYPE) && value.TYPE == 'image') { if (this.isDefined(value.FILE)) { entry += `
`; if (SETTINGS.SHOWOVERLAY) { entry += `
`; } entry += ``; entry += `
`; } } } entry += `
`; this.grid.innerHTML += entry; } this.doTypeIcon = function(type, count) { let result = ``; switch (type) { case 'article': result += ``; break; case 'podcast': result += ``; break; case 'video': result += ``; break; case 'list': result += ``; break; case 'book': result += ``; break; case 'game': result += ``; break; case 'service': result += ``; break; case 'lecture': result += ``; break; case 'quote': result += ``; break; case 'tool': result += ``; break; case 'music': result += ``; break; case 'image': result += ``; break; case 'encyclopedia': result += ``; break; } result += ``; return result; } this.stats = function(value) { let menuContent = ``; if (window.showAdd != undefined && window.showAdd) { // ADD menuContent += ``; } // TOTAL menuContent += ``; // DONE menuContent += ``; menuContent += ``; // TERM menuContent += ``; // TAGS menuContent += ``; this.menu.innerHTML = ``; this.menu.innerHTML += menuContent; } this.buildArrayElement = function(data, className, iconName) { let result = ''; if (Array.isArray(data)) { for (var i in data) { if (data[i].substring(0, 2) == "> ") { // New item if (data[i].includes(": ")) { let titleSplit = data[i].substring(2).split(': '); // .substring(2) removes the "> " for (var e = 0; e < titleSplit.length; e++) { titleSplit[e] = titleSplit[e].trim(); } result += `
${titleSplit[0]}: ${titleSplit[1]}
`; } else { result += `
${data[i].substring(2)}
`; } } else if (data[i].substring(0, 2) == "& ") { // New line in current item result += `
${data[i].substring(2)}
`; } else if (data[i].substring(0, 2) == "- ") { // Bullet point result += `
${data[i].substring(2)}
`; } else { // Handle unformatted result += `
${data[i]}
`; } } } else { // Handle not array result += `
${data}
`; } return result; } // HELPER String.prototype.to_properCase = function() { return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); } // Source: https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string this.extractRootDomain = function(url) { var domain = this.extractHostname(url), splitArr = domain.split('.'), arrLen = splitArr.length; // extracting the root domain here // if there is a subdomain if (arrLen > 2) { domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1]; // check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk") if (splitArr[arrLen - 2].length == 2 && splitArr[arrLen - 1].length == 2) { // this is using a ccTLD domain = splitArr[arrLen - 3] + '.' + domain; } } return domain; } // Source: https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string this.extractHostname = function(url) { var hostname; // find & remove protocol (http, ftp, etc.) and get hostname if (url.indexOf("://") > -1) { hostname = url.split('/')[2]; } else { hostname = url.split('/')[0]; } // find & remove port number hostname = hostname.split(':')[0]; // find & remove "?" hostname = hostname.split('?')[0]; return hostname; } }