memex/logic/main.js

211 lines
4.9 KiB
JavaScript
Raw Normal View History

2018-07-13 15:56:09 +00:00
this.DB = new Indental(DATABASE.memex).parse();
let keys = Object.keys(DB);
let page = 0;
let lastEntry = -1;
let postPerPage = 1000;
function Main()
{
console.log(DB);
let view = ``;
let html = document.body;
view += `<div class="grid">`;
view += `${displayEntries(DB)}`;
view += `</div>`;
2018-07-13 15:56:09 +00:00
html.innerHTML = view;
var msnry = new Masonry( '.grid', {
itemSelector: '.grid-item',
columnWidth: 350,
gutter: 20,
fitWidth: true,
transitionDuration: 0,
});
2018-07-13 15:56:09 +00:00
}
function displayEntries(db)
{
let entries = ``;
page += postPerPage;
var i = lastEntry+1;
var value;
while (i < Math.min(keys.length, page))
{
value = db[keys[i]];
// ENTRY
2018-07-14 12:02:18 +00:00
entries += `<div class="grid-item">`;
2018-07-14 15:02:57 +00:00
entries += `<div class="title">${keys[i].toProperCase()}</div>`;
2018-07-13 15:56:09 +00:00
// LINK
if (typeof value.LINK !== 'undefined')
{
var idUrl = "url";
if (typeof value.SEEN !== 'undefined')
{
if (value.SEEN == "true")
{
idUrl = "urlseen";
}
}
2018-07-14 15:02:57 +00:00
entries += `<div class="link"><i class="fas fa-link textIcon"></i><a href="${String(value.LINK)}" id="${idUrl}">${extractRootDomain(value.LINK)}</a></div>`;
2018-07-13 15:56:09 +00:00
}
2018-07-14 12:02:18 +00:00
// TYPE
if (typeof value.TYPE !== 'undefined')
{
entries += `<div id="type">`;
2018-07-14 15:02:57 +00:00
entries += `<a href='#type:${String(value.TYPE)}'>`;
2018-07-14 12:02:18 +00:00
if (value.TYPE == 'article')
{
entries += `<i class="far fa-newspaper"></i>`;
}
else if (value.TYPE == 'podcast')
{
entries += `<i class="fas fa-podcast"></i>`;
}
else if (value.TYPE == 'video')
{
entries += `<i class="fas fa-tv"></i>`;
}
else if (value.TYPE == 'list')
{
entries += `<i class="fas fa-file-alt"></i>`;
}
else if (value.TYPE == 'book')
{
entries += `<i class="fas fa-book-open"></i>`;
}
else if (value.TYPE == 'game')
{
entries += `<i class="fas fa-gamepad"></i>`;
}
else if (value.TYPE == 'service')
{
entries += `<i class="fas fa-server"></i>`;
}
else if (value.TYPE == 'lecture')
{
entries += `<i class="fas fa-chalkboard-teacher"></i>`;
}
else if (value.TYPE == 'quote')
{
entries += `<i class="fas fa-comment"></i>`;
}
else if (value.TYPE == 'tool')
{
entries += `<i class="fas fa-wrench"></i>`;
}
else if (value.TYPE == 'music')
{
entries += `<i class="fas fa-music"></i>`;
}
2018-07-14 15:02:57 +00:00
entries += `</a>`;
2018-07-14 12:02:18 +00:00
entries += `</div>`;
}
2018-07-13 15:56:09 +00:00
// TAGS
if (typeof value.TAGS !== 'undefined')
{
2018-07-14 15:02:57 +00:00
entries += `<div class="tags"><i class="fas fa-tag textIcon"></i><a href=#tag:${value.TAGS.toLowerCase()}>${value.TAGS.toLowerCase()}</a></div>`;
2018-07-13 15:56:09 +00:00
}
// NOTE
if (typeof value.NOTE !== 'undefined')
{
2018-07-14 15:02:57 +00:00
entries += `<div class="note"><i class="fas fa-sticky-note textIcon"></i>${value.NOTE}</div>`;
2018-07-13 15:56:09 +00:00
}
// QUOTE
if (typeof value.QOTE !== 'undefined')
{
2018-07-14 15:02:57 +00:00
entries += `<div class="quote"><i class="fas fa-comment textIcon"></i>${value.QOTE}</div>`;
2018-07-13 15:56:09 +00:00
}
// TERM
if (typeof value.TERM !== 'undefined')
{
2018-07-14 15:02:57 +00:00
entries += `<div class="term"><i class="fas fa-ribbon textIcon"></i>${value.TERM}</div>`;
2018-07-14 12:17:00 +00:00
}
// PROGRESS
if (typeof value.PROG !== 'undefined')
{
2018-07-14 15:02:57 +00:00
entries += `<div class="prog"><i class="fas fa-clock textIcon"></i>${value.PROG}</div>`;
2018-07-14 12:17:00 +00:00
}
2018-07-13 15:56:09 +00:00
entries += `</div>`;
lastEntry = i;
i += 1;
}
entries += doPagination();
return entries;
}
function doPagination()
{
return `
<div id="pagination">
<a id="loadmore" onClick="loadMore();">${lastEntry < keys.length -1 ? `Load more ▼` : ``}</a>
</div>
`
}
function loadMore()
{
pagination.remove();
document.getElementById("content").innerHTML += doJournal(DB);
}
String.prototype.toProperCase = 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
function extractHostname(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;
}
// Source: https://stackoverflow.com/questions/8498592/extract-hostname-name-from-string
function extractRootDomain(url)
{
var domain = 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;
}