function ViewMasonry() { // REFERENCE this.msnry = null; this.grid = null; this.menu = null; var parent = this; // SETTINGS this.statsNumTags = 5; this.statsNumTypes = 10; this.doDoubleWide = true; // SETTINGS this.useMasonry = true; this.divNamePre = 'item'; this.install = function() { this.grid = document.getElementById("grid"); this.menu = document.getElementById("menu"); if (this.useMasonry) { this.msnry = new Masonry('.grid', { itemSelector: '.grid-item', columnWidth: 350, gutter: 20, fitWidth: true, transitionDuration: 0, }); /////////////////////////// // Fix squashed image divs var imgLoad = imagesLoaded('.grid'); imgLoad.on( 'always', function (instance) { parent.msnry.layout(); } ); // Above imgLoad on always msnry.layout() should work but occassionally doesn't in Chrome and NEVER does in Firefox. window.onload = function () { parent.msnry.layout(); } // msnry.layout() called by window onload fixes things but not ideal. /////////////////////////// } } this.doEntries = function(db) { // BUILD this.grid.innerHTML = ''; var dbKeys = Object.keys(db); var i = 0; while (i < dbKeys.length) { this.buildEntry(db, dbKeys[i]); i++; } // LAYOUT if (this.useMasonry) { this.msnry.reloadItems(); this.msnry.layout(); } } this.buildEntry = function(db, key) { let value = db[key]; let itemClass = "grid-item"; if (this.doDoubleWide) { if (typeof value.QOTE !== 'undefined') { if (Array.isArray(value.QOTE) && value.QOTE.length > 4) { itemClass += " grid-item--width2"; } } } if (typeof value.TYPE !== 'undefined' && value.TYPE == 'image') { itemClass += " grid-item-image"; } let entry = ``; // DIV entry += `
`; if (typeof value.LINK !== 'undefined') { var idUrl = "url"; if (typeof value.SEEN !== 'undefined') { if (value.SEEN == "true") { idUrl = "urlseen"; } } // LINK START entry += ``; } // UPPER CONTENT START entry += `
`; // TITLE entry += `
${key.to_properCase()}
`; // LINK END if (typeof value.LINK !== 'undefined') { entry += `
`; } // TYPE if (typeof value.TYPE !== 'undefined') { entry += ``; entry += `
`; if (value.TYPE == 'article') { entry += ``; } else if (value.TYPE == 'podcast') { entry += ``; } else if (value.TYPE == 'video') { entry += ``; } else if (value.TYPE == 'list') { entry += ``; } else if (value.TYPE == 'book') { entry += ``; } else if (value.TYPE == 'game') { entry += ``; } else if (value.TYPE == 'service') { entry += ``; } else if (value.TYPE == 'lecture') { entry += ``; } else if (value.TYPE == 'quote') { entry += ``; } else if (value.TYPE == 'tool') { entry += ``; } else if (value.TYPE == 'music') { entry += ``; } entry += `
`; entry += `
`; } // UPPER CONTENT END entry += `
`; // LOWER CONTENT START entry += `
`; // TAGS if (typeof value.TAGS !== 'undefined') { entry += `
`; for (var i = 0; i < value.TAGS.length; i++) { entry += `${value.TAGS[i]}`; if (i+1 != value.TAGS.length) { entry += `, `; } }; entry += `
`; } // NOTE if (typeof value.NOTE !== 'undefined') { entry += `
${value.NOTE}
`; } // QUOTE if (typeof value.QOTE !== 'undefined') { entry += `
${value.QOTE}
`; } // TERM if (typeof value.TERM !== 'undefined') { for (var i = 0; i < value.TERM.length; i++) { entry += `
${value.TERM[i][0]}: ${value.TERM[i][1]}
`; } } // PROGRESS if (typeof value.PROG !== 'undefined') { entry += `
${value.PROG}
`; } // LOWER CONTENT END entry += `
`; // IMAGE if (typeof value.TYPE !== 'undefined' && value.TYPE == 'image') { if (typeof value.FILE !== 'undefined') { entry += ``; } } entry += `
`; this.grid.innerHTML += entry; } this.doStats = function(stats) { let menuContent = ``; // TYPE menuContent += ``; menuContent += ``; menuContent += ``; for (var ty = 0; ty < Math.min(stats.types.length, this.statsNumTypes); ty++) { if (stats.types[ty][0] == 'article') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'podcast') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'video') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'list') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'book') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'game') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'service') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'lecture') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'quote') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'tool') { menuContent += ``; menuContent += ``; menuContent += ``; } else if (stats.types[ty][0] == 'music') { menuContent += ``; menuContent += ``; menuContent += ``; } } menuContent += ``; // TERM if (stats.terms > 0) { // menuContent += ``; menuContent += ``; menuContent += ``; menuContent += ``; } menuContent += ``; // TAGS if (stats.tags.length > 0) { menuContent += ``; } this.menu.innerHTML = ``; this.menu.innerHTML += menuContent; } // 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; } }