memex/docs/logic/view.js

527 lines
16 KiB
JavaScript
Raw Normal View History

function View()
{
this.msnry = null;
2018-07-23 13:20:03 +00:00
this.overlay = null;
this.container = null;
this.grid = null;
this.menu = null;
var parent = this;
2018-07-21 17:46:18 +00:00
this.install = function()
{
2018-07-23 13:20:03 +00:00
this.overlay = document.getElementById("overlay");
this.container = document.getElementById("container");
this.grid = document.getElementById("grid");
this.menu = document.getElementById("menu");
2018-07-22 19:45:04 +00:00
if (SETTINGS.USEMASONRY)
{
2018-07-22 17:44:46 +00:00
this.msnry = new Masonry('.grid',
{
2018-09-15 13:37:29 +00:00
itemSelector: '.griditem',
columnWidth: 350,
gutter: 20,
fitWidth: true,
transitionDuration: 0,
});
}
}
2018-07-22 19:45:04 +00:00
this.display = function(db)
{
2018-10-30 03:01:22 +00:00
if (window.showAdd !== undefined && window.showAdd)
{
main.add.setOverlay(false);
}
2018-07-23 13:20:03 +00:00
// 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
2018-07-22 19:45:04 +00:00
if (SETTINGS.USEMASONRY)
{
this.msnry.reloadItems();
this.msnry.layout();
2018-07-22 17:44:46 +00:00
if (SETTINGS.MASONRYCOMPLETE || SETTINGS.MASONRYPROGRESS)
{
let imgLoad = imagesLoaded( container );
if (SETTINGS.MASONRYCOMPLETE)
{
// When all images finish: redo mansonry layout
imgLoad.on( 'always', function() { parent.msnry.layout(); } );
}
if (SETTINGS.MASONRYPROGRESS)
{
// 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];
2018-09-15 13:37:29 +00:00
let itemClass = "griditem";
2018-07-22 19:45:04 +00:00
if (SETTINGS.WIDEGRIDITEM)
2018-07-17 14:36:13 +00:00
{
2018-10-29 14:04:20 +00:00
if (this.isDefined(value.WIDE) && value.WIDE)
2018-07-18 13:06:29 +00:00
{
2018-09-15 13:37:29 +00:00
itemClass += " griditem-wide";
2018-07-18 13:06:29 +00:00
}
else if (this.isDefined(value.QOTE))
2018-07-17 14:36:13 +00:00
{
if (Array.isArray(value.QOTE) && value.QOTE.length > 4)
{
2018-09-15 13:37:29 +00:00
itemClass += " griditem-wide";
2018-07-17 14:36:13 +00:00
}
}
}
2018-10-30 14:05:23 +00:00
let onclickImage = ``;
if (SETTINGS.SHOWIMAG && this.isType(value.TYPE, 'image'))
2018-07-18 11:17:51 +00:00
{
2018-10-30 03:01:22 +00:00
itemClass += " griditem-image";
onclickImage = `onclick="main.view.handleImageClick(event, this, '${value.FILE}');"
style="cursor: pointer;"`;
2018-07-18 11:17:51 +00:00
}
let entry = ``;
2018-10-30 14:05:23 +00:00
entry += `<div class="${itemClass}" id="${SETTINGS.GRIDITEMIDBASE + value.DIID}">`;
2018-07-18 13:06:29 +00:00
// ITEM DIV
2018-10-29 14:04:20 +00:00
if (this.isDefined(value.LINK))
{
var idUrl = "url";
if (this.isDefined(value.SEEN) && value.SEEN === "true")
{
2018-10-30 03:01:22 +00:00
idUrl = "urlseen";
}
// LINK START
if (SETTINGS.SHOWLINK && !this.isObject(value.LINK))
2018-07-21 17:46:18 +00:00
{
// 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}">`;
2018-07-21 17:46:18 +00:00
}
}
2018-07-18 11:17:51 +00:00
// UPPER CONTENT START
2018-07-22 19:45:04 +00:00
if (SETTINGS.SHOWUPPER)
{
2018-10-30 14:05:23 +00:00
entry += `<div class="griditem-containerupper" ${onclickImage}>`;
2018-07-21 17:46:18 +00:00
// TITLE
2018-07-22 19:45:04 +00:00
if (SETTINGS.SHOWTITLE)
{
2018-09-15 13:37:29 +00:00
entry += `<div class="griditem-title">${key.to_properCase()}</div>`;
}
2018-07-21 17:46:18 +00:00
// LINK END
if (SETTINGS.SHOWLINK && this.isDefined(value.LINK))
2018-07-18 15:28:10 +00:00
{
if (this.isObject(value.LINK))
{
for (let l = 0; l < value.LINK.length; l++)
{
entry += `<a class="griditem-link" href="${String(value.LINK[l])}" id="${idUrl}">`;
entry += `<div class="griditem-linkcontainer"><i class="griditem-linkicon fas fa-link"></i><div class="griditem-linktitle">${this.extractRootDomain(value.LINK[l])}</div></div></a>`;
}
}
else
{
entry += `<div class="griditem-linkcontainer"><i class="griditem-linkicon fas fa-link"></i><div class="griditem-linktitle">${this.extractRootDomain(value.LINK)}</div></div></a>`;
}
2018-07-18 15:28:10 +00:00
}
2018-07-21 17:46:18 +00:00
// TYPE
if (SETTINGS.SHOWTYPE && this.isDefined(value.TYPE))
2018-10-30 03:01:22 +00:00
{
entry += `<div class="griditem-typecontainer">`;
for (let tc = 0; tc < value.TYPE.length; tc++)
{
const icon = getTypeIconName(value.TYPE[tc]);
entry += `<a class="griditem-type" href='#type-${value.TYPE[tc]}'>`;
entry += `<i class="griditem-typeicon ${icon}"></i>`;
entry += `</a>`;
}
entry += `</div>`; // griditem-typecontainer
2018-07-19 04:46:43 +00:00
}
2018-07-21 17:46:18 +00:00
// UPPER CONTENT END
entry += `</div>`;
}
2018-07-18 11:17:51 +00:00
// LOWER CONTENT START
2018-07-22 19:45:04 +00:00
if (SETTINGS.SHOWLOWER)
{
2018-10-30 14:05:23 +00:00
entry += `<div class="griditem-containerlower" ${onclickImage}>`;
2018-07-21 17:46:18 +00:00
2018-08-13 15:29:24 +00:00
// AUTHOR
if (SETTINGS.SHOWAUTH && this.isDefined(value.AUTH))
2018-08-13 15:29:24 +00:00
{
2018-10-30 03:01:22 +00:00
entry += `<div class="griditem-auth"><i class="fas fa-user textIcon"></i>${value.AUTH}</div>`;
2018-08-13 15:29:24 +00:00
}
2018-07-21 17:46:18 +00:00
// TAGS
if (SETTINGS.SHOWTAGS && this.isDefined(value.TAGS))
{
2018-10-30 03:01:22 +00:00
entry += `<div class="griditem-tags"><i class="fas fa-tag textIcon"></i>`;
for (var i = 0; i < value.TAGS.length; i++)
{
2018-10-30 03:01:22 +00:00
entry += `<a class="griditem-taglink" href="#tag-${value.TAGS[i]}">${value.TAGS[i]}</a>`;
if (i+1 !== value.TAGS.length)
2018-07-21 17:46:18 +00:00
{
2018-10-30 03:01:22 +00:00
entry += `, `;
}
};
entry += `</div>`;
2018-07-21 17:46:18 +00:00
}
// PROJECT
if (SETTINGS.SHOWPROJ && this.isDefined(value.PROJ))
2018-10-29 14:04:20 +00:00
{
entry += `<div class="griditem-proj"><i class="fas fa-leaf textIcon"></i>`;
for (var i = 0; i < value.PROJ.length; i++)
{
entry += `<a class="griditem-taglink" href="#proj-${value.PROJ[i]}">${value.PROJ[i].to_properCase()}</a>`;
if (i + 1 != value.PROJ.length)
{
entry += `, `;
}
}
entry += `</div>`;
2018-07-21 17:46:18 +00:00
}
2018-10-29 13:27:30 +00:00
// TERM
if (SETTINGS.SHOWTERM && this.isDefined(value.TERM))
2018-10-29 13:27:30 +00:00
{
entry += this.doMultilineFormatting(value.TERM, "griditem-term", "fas fa-ribbon textIcon");
2018-10-29 13:27:30 +00:00
}
2018-07-21 17:46:18 +00:00
// NOTE
if (SETTINGS.SHOWNOTE && this.isDefined(value.NOTE))
{
entry += this.doMultilineFormatting(value.NOTE, "griditem-note", "fas fa-sticky-note textIcon");
}
2018-07-21 17:46:18 +00:00
// QUOTE
if (SETTINGS.SHOWQOTE && this.isDefined(value.QOTE))
2018-07-21 17:46:18 +00:00
{
entry += this.doMultilineFormatting(value.QOTE, "griditem-quote", "fas fa-comment textIcon");
2018-07-21 17:46:18 +00:00
}
2018-07-21 17:46:18 +00:00
// PROGRESS
if (SETTINGS.SHOWPROG && this.isDefined(value.PROG))
2018-07-21 17:46:18 +00:00
{
2018-10-30 03:01:22 +00:00
entry += `<div class="griditem-prog"><i class="fas fa-clock textIcon"></i>${value.PROG}</div>`;
2018-07-21 17:46:18 +00:00
}
2018-07-18 11:17:51 +00:00
// IMAGE - for non-image-type-entry
if (SETTINGS.SHOWIMAG
&& !this.isType(value.TYPE, 'image')
&& this.isDefined(value.FILE)
&& this.isImage(value.FILE))
{
entry += `<div class="image">`;
2018-10-30 14:05:23 +00:00
entry += `<img class="griditem-img" src="content/media/${value.FILE}" onclick="main.lightbox.load('content/media/${value.FILE}')">`;
entry += `</div>`;
}
2018-10-29 14:41:42 +00:00
// FILE
if (SETTINGS.SHOWFILE && this.isDefined(value.FILE))
2018-10-29 14:41:42 +00:00
{
if (this.isObject(value.FILE))
2018-10-29 14:41:42 +00:00
{
for (var i = 0; i < value.FILE.length; i++)
2018-10-29 14:41:42 +00:00
{
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>`;
2018-10-29 14:41:42 +00:00
}
}
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>`;
}
2018-10-29 14:41:42 +00:00
}
2018-07-21 17:46:18 +00:00
// LOWER CONTENT END
entry += `</div>`;
}
2018-07-18 11:17:51 +00:00
// IMAGE - for image-type-entry
if (SETTINGS.SHOWIMAG
&& this.isType(value.TYPE, 'image')
&& this.isDefined(value.FILE)
&& this.isImage(value.FILE))
{
2018-10-30 03:01:22 +00:00
entry += `<div class="image">`;
if (SETTINGS.SHOWOVERLAY)
{
2018-10-30 03:01:22 +00:00
entry += `<div class="image-overlay"></div>`;
}
2018-10-30 03:01:22 +00:00
entry += `<img class="griditem-image-img" src="content/media/${value.FILE}">`;
entry += `</div>`;
}
entry += `</div>`;
return entry;
}
2018-07-22 19:45:04 +00:00
this.stats = function(value)
{
let menuContent = ``;
2018-10-30 03:01:22 +00:00
if (window.showAdd !== undefined && window.showAdd)
{
// ADD
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-itemgroup">`;
menuContent += `<a href='#add' class="menu-item">`;
menuContent += `<b>a</b>dd`;
menuContent += `</a>`;
2018-09-14 18:55:32 +00:00
menuContent += `</div>`;
}
2018-07-23 13:20:03 +00:00
2018-08-13 16:30:09 +00:00
// TOTAL
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-itemgroup">`;
menuContent += `<a href='#' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.total}</div>`;
menuContent += `<i class="menu-itemicon fas fa-asterisk"></i>`;
menuContent += `</a>`;
2018-09-14 18:55:32 +00:00
menuContent += `</div>`;
2018-08-13 16:30:09 +00:00
// DONE
2018-10-30 22:19:49 +00:00
if (SETTINGS.SHOWDONE)
{
menuContent += `<div class="menu-itemgroup">`;
menuContent += `<a href='#done-true' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.done}</div>`;
menuContent += `<i class="menu-itemicon fas fa-check"></i>`;
menuContent += `</a>`;
menuContent += `<a href='#done-false' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.total - value.done}</div>`;
menuContent += `<i class="menu-itemicon fas fa-times"></i>`;
menuContent += `</a>`;
menuContent += `</div>`;
}
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-itemgroup">`;
for (let ty = 0; ty < Math.min(value.types.length, SETTINGS.STATSNUMTYPE); ty++)
{
2018-10-30 03:01:22 +00:00
const type = value.types[ty][0];
const count = value.types[ty][1];
const icon = getTypeIconName(type);
2018-10-30 03:01:22 +00:00
menuContent += `<a href='#type-${type}' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${count}</div>`;
menuContent += `<i class="menu-itemicon ${icon}"></i>`;
menuContent += `</a>`;
}
2018-09-14 18:55:32 +00:00
menuContent += `</div>`;
2018-09-14 22:53:56 +00:00
// TERM
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-itemgroup">`;
2018-07-22 19:45:04 +00:00
if (value.terms > 0)
{
// menuContent += `<div class="menu-item-space"></div>`;
2018-09-14 18:55:32 +00:00
menuContent += `<a href='#term' class="menu-item">`;
menuContent += `<div class="menu-itemcount">${value.terms}</div>`;
menuContent += `<i class="menu-itemicon fas fa-ribbon"></i>`;
menuContent += `</a>`;
}
2018-09-14 18:55:32 +00:00
menuContent += `</div>`;
2018-07-17 18:17:43 +00:00
// TAGS
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-itemgroup">`;
2018-07-22 19:45:04 +00:00
if (value.tags.length > 0)
{
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-tagcontainer">`;
menuContent += `<i class="menu-tagicon fas fa-tag"></i>`;
for (var t = 0; t < Math.min(value.tags.length, SETTINGS.STATSNUMTAGS); t++)
{
2018-09-14 18:55:32 +00:00
menuContent += `<a class="menu-tag" href='#tag-${value.tags[t][0]}'>`;
// menuContent += `<i class="fas fa-tag textIcon"></i>`;
2018-09-14 18:55:32 +00:00
menuContent += `<div class="menu-tagcount">${value.tags[t][1]}</div>`;
menuContent += `<div class="menu-taglabel">${value.tags[t][0]}</div>`;
menuContent += `</a>`;
}
menuContent += `</div>`;
}
2018-09-14 18:55:32 +00:00
menuContent += `</div>`;
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)
2018-08-11 16:31:55 +00:00
{
let result = '';
if (Array.isArray(data))
{
for (var i in data)
{
2018-10-29 14:17:10 +00:00
if (data[i].substring(0, 2) == "> ")
2018-08-11 16:31:55 +00:00
{
// New item
2018-08-12 14:48:17 +00:00
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();
}
2018-08-12 15:05:56 +00:00
result += `<div class="${className}"><i class="${iconName}"></i><b>${titleSplit[0]}</b>: ${titleSplit[1]}</div>`;
2018-08-12 14:48:17 +00:00
}
else
{
result += `<div class="${className}"><i class="${iconName}"></i>${data[i].substring(2)}</div>`;
}
2018-08-11 16:31:55 +00:00
}
2018-10-30 03:01:22 +00:00
else if (data[i].substring(0, 2) === "& ")
2018-08-11 16:31:55 +00:00
{
// New line in current item
result += `<div class="${className}">${data[i].substring(2)}</div>`;
}
2018-10-29 14:17:10 +00:00
else if (data[i].substring(0, 2) == "- ")
{
// Bullet point
result += `<div class="${className}"><i class="fas fa-caret-right textIcon"></i>${data[i].substring(2)}</div>`;
}
2018-08-11 16:31:55 +00:00
else
{
// Handle unformatted
result += `<div class="${className}"><i class="${iconName}"></i>${data[i]}</div>`;
}
}
}
else
{
// Handle not array
result += `<div class="${className}"><i class="${iconName}"></i>${data}</div>`;
}
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;
2018-07-22 17:44:46 +00:00
// extracting the root domain here
// if there is a subdomain
if (arrLen > 2)
{
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
2018-07-22 17:44:46 +00:00
// check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
2018-10-30 03:01:22 +00:00
if (splitArr[arrLen - 2].length === 2 && splitArr[arrLen - 1].length === 2)
{
2018-07-22 17:44:46 +00:00
// 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;
2018-07-22 17:44:46 +00:00
// find & remove protocol (http, ftp, etc.) and get hostname
2018-08-12 14:48:17 +00:00
if (url.indexOf("://") > -1)
{
hostname = url.split('/')[2];
}
2018-08-12 14:48:17 +00:00
else
{
hostname = url.split('/')[0];
}
2018-07-22 17:44:46 +00:00
// find & remove port number
hostname = hostname.split(':')[0];
2018-07-22 17:44:46 +00:00
// find & remove "?"
hostname = hostname.split('?')[0];
return hostname;
}
}