function View() { this.msnry = null; this.overlay = null; this.container = null; this.grid = null; this.menu = null; var parent = this; 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 let dbKeys = Object.keys(db); let i = 0; let contentHtml = ''; while (i < dbKeys.length) { contentHtml += this.buildEntry(db, dbKeys[i]); i++; } this.grid.innerHTML = contentHtml; // LAYOUT if (SETTINGS.USEMASONRY) { this.msnry.reloadItems(); this.msnry.layout(); if (SETTINGS.MASONRYCOMPLETE || SETTINGS.MASONRYPROGRESS) { let imgLoad = imagesLoaded( container ); if (!SETTINGS.MASONRYPROGRESS) { // When all images finish: redo mansonry layout imgLoad.on( 'always', function() { parent.msnry.layout(); } ); } else { // As images load one by one: redo masonry layout imgLoad.on( 'progress', function() { parent.msnry.layout(); } ); } } } } 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 (this.isDefined(value.QOTE)) { if (Array.isArray(value.QOTE) && value.QOTE.length > SETTINGS.AUTOWIDETRIGGER) { itemClass += " griditem-wide"; } } } let onclickImage = ``; let entryIsImageType = (SETTINGS.SHOWIMAG && this.isType(value.TYPE, 'image')); if (entryIsImageType) { itemClass += " griditem-image"; onclickImage = `onclick="main.view.handleImageClick(event, this, '${value.FILE}');" style="cursor: pointer;"`; } let entry = ``; entry += `
`; // ITEM DIV if (this.isDefined(value.LINK)) { var idUrl = "url"; if (this.isDefined(value.SEEN) && value.SEEN === "true") { idUrl = "urlseen"; } // LINK START if (SETTINGS.SHOWLINK && !this.isObject(value.LINK)) { // If this item has only one link then make the whole title the link entry += ``; } } // UPPER CONTENT START if (SETTINGS.SHOWUPPER) { let upperClass = 'griditem-containerupper'; if (entryIsImageType) { upperClass = 'griditem-containerupper-image'; } entry += `
`; // TITLE if (SETTINGS.SHOWTITLE) { entry += `
${key.to_properCase()}
`; } // LINK END if (SETTINGS.SHOWLINK && this.isDefined(value.LINK)) { if (this.isObject(value.LINK)) { for (let l = 0; l < value.LINK.length; l++) { entry += `
`; entry += ``; } } else { entry += ``; } } // TYPE if (SETTINGS.SHOWTYPE && this.isDefined(value.TYPE)) { entry += `
`; for (let tc = 0; tc < value.TYPE.length; tc++) { const icon = getTypeIconName(value.TYPE[tc]); entry += ``; entry += ``; entry += ``; } entry += `
`; // griditem-typecontainer } // UPPER CONTENT END entry += `
`; } // LOWER CONTENT START if (SETTINGS.SHOWLOWER) { let lowerClass = 'griditem-containerlower'; if (entryIsImageType) { lowerClass = 'griditem-containerlower-image'; } entry += `
`; // AUTHOR if (SETTINGS.SHOWAUTH && this.isDefined(value.AUTH)) { for (var i = 0; i < value.AUTH.length; i++) { entry += `
${value.AUTH[i].to_properCase()}
`; } } // TAGS if (SETTINGS.SHOWTAGS && 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 && this.isDefined(value.PROJ)) { entry += `
`; for (var i = 0; i < value.PROJ.length; i++) { entry += `${value.PROJ[i].to_properCase()}`; if (i + 1 != value.PROJ.length) { entry += `, `; } } entry += `
`; } // TERM if (SETTINGS.SHOWTERM && this.isDefined(value.TERM)) { entry += this.doMultilineFormatting(value.TERM, "griditem-term", "fas fa-ribbon textIcon"); } // NOTE if (SETTINGS.SHOWNOTE && this.isDefined(value.NOTE)) { entry += this.doMultilineFormatting(value.NOTE, "griditem-note", "fas fa-sticky-note textIcon"); } // QUOTE if (SETTINGS.SHOWQOTE && this.isDefined(value.QOTE)) { entry += this.doMultilineFormatting(value.QOTE, "griditem-quote", "fas fa-comment textIcon"); } // PROGRESS if (SETTINGS.SHOWPROG && this.isDefined(value.PROG)) { entry += `
${value.PROG}
`; } // IMAGE - for non-image-type-entry if (SETTINGS.SHOWIMAG && !this.isType(value.TYPE, 'image') && this.isDefined(value.FILE) && this.isImage(value.FILE)) { entry += `
`; entry += ``; entry += `
`; } // FILE if (SETTINGS.SHOWFILE && this.isDefined(value.FILE)) { if (this.isObject(value.FILE)) { for (var i = 0; i < value.FILE.length; i++) { entry += ``; } } else { // single entry += ``; } } // LOWER CONTENT END entry += `
`; } // IMAGE - for image-type-entry if (entryIsImageType && this.isDefined(value.FILE) && this.isImage(value.FILE)) { entry += `
`; if (SETTINGS.SHOWOVERLAY) { entry += `
`; } entry += ``; entry += `
`; } entry += `
`; return entry; } this.stats = function(value) { let menuContent = ``; if (window.showAdd !== undefined && window.showAdd) { // ADD menuContent += ``; } // TOTAL menuContent += ``; // DONE if (SETTINGS.SHOWDONE) { menuContent += ``; } menuContent += ``; // TERM menuContent += ``; // TAGS menuContent += ``; this.menu.innerHTML = menuContent; } this.handleImageClick = function(e, element, file) { e = e || window.event; var target = e.target || e.srcElement; if (target == element) { // If user is clicking given element, or element's background... // as opposed to an element's child content, then do lightbox. // This stops lightbox from happening when clicking on tags, file etc main.lightbox.load(`content/media/${file}`); } } this.doMultilineFormatting = 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; } getTypeIconName = function(type) { let icon = ''; switch (type) { case 'article': icon = 'far fa-newspaper'; break; case 'podcast': icon = 'fas fa-podcast'; break; case 'video': icon = 'fas fa-tv'; break; case 'list': icon = 'fas fa-file-alt'; break; case 'book': icon = 'fas fa-book-open'; break; case 'game': icon = 'fas fa-gamepad'; break; case 'service': icon = 'fas fa-server'; break; case 'lecture': icon = 'fas fa-chalkboard-teacher'; break; case 'quote': icon = 'fas fa-comment'; break; case 'tool': icon = 'fas fa-wrench'; break; case 'music': icon = 'fas fa-music'; break; case 'image': icon = 'fas fa-image'; break; case 'encyclopedia': icon = 'fas fa-globe'; break; case 'term': icon = 'fas fa-ribbon'; break; } return icon; } // HELPER this.isDefined = function(value) { return (typeof value !== 'undefined'); } this.isObject = function(value) { return (typeof value == 'object'); } this.isImage = function(filename) { return (/\.(gif|jpg|jpeg|tiff|png)$/i).test(filename); } this.isType = function(typeArray, value) { if (this.isDefined(typeArray)) { for (var i = 0; i < typeArray.length; i++) { if (typeArray[i] == value) { return true; } } } return false; } 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; } }