function ViewMasonry()
{
// REFERENCE
this.msnry = null;
this.grid = null;
this.menu = null;
// 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");
console.log(1);
if (this.useMasonry)
{
console.log(2);
this.msnry = new Masonry('.grid', {
itemSelector: '.grid-item',
columnWidth: 350,
gutter: 20,
fitWidth: true,
transitionDuration: 0,
});
console.log(3);
var imgLoad = imagesLoaded('.grid');
function onAlways( instance ) {
console.log('all images are loaded');
//this.msnry.layout();
console.log(this.msnry);
}
imgLoad.on( 'always', onAlways );
// imgLoad.off( 'always', onAlways );
imgLoad.on( 'progress', function(instance, image)
{
var result = image.isLoaded ? 'loaded' : 'broken';
console.log( 'image is ' + result + ' for ' + image.img.src );
});
}
}
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";
}
}
}
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 += `
`;
}
// TITLE
entry += `${key.to_properCase()}
`;
// LINK END
if (typeof value.LINK !== 'undefined')
{
entry += `${this.extractRootDomain(value.LINK)}
`;
}
// TYPE
if (typeof value.TYPE !== 'undefined')
{
entry += `
`;
}
// TAGS
if (typeof value.TAGS !== 'undefined')
{
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}
`;
}
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;
}
}