From 7da1af61212fc462ebd9042ff0fcbc8cf355629a Mon Sep 17 00:00:00 2001 From: kor Date: Wed, 18 Jul 2018 01:08:20 +1200 Subject: [PATCH] Refactor database usage to datawrap.js. --- content/Memex.ndtl | 7 ++ index.html | 3 +- logic/datawrap.js | 191 +++++++++++++++++++++++++++++++ logic/main.js | 272 +++++++-------------------------------------- 4 files changed, 239 insertions(+), 234 deletions(-) create mode 100644 logic/datawrap.js diff --git a/content/Memex.ndtl b/content/Memex.ndtl index c228d28..3a04487 100644 --- a/content/Memex.ndtl +++ b/content/Memex.ndtl @@ -1548,4 +1548,11 @@ COVOX DUBSLIDE DATE : 12018-07-17 SEEN : true NOTE : Made with LSDJ + +THE POT COVER + LINK : https://www.youtube.com/watch?v=8zfYO9sZjrE + TYPE : music + DATE : 12018-07-17 + SEEN : true + NOTE : Four trumpeteers, a drummer, guitarist, saxman and epic Sophia Urista cover 'The Pot' ` \ No newline at end of file diff --git a/index.html b/index.html index 06ef9e2..7beb461 100644 --- a/index.html +++ b/index.html @@ -11,6 +11,7 @@ + memex @@ -26,7 +27,5 @@ main.install(); main.start(); - - \ No newline at end of file diff --git a/logic/datawrap.js b/logic/datawrap.js new file mode 100644 index 0000000..a85da2b --- /dev/null +++ b/logic/datawrap.js @@ -0,0 +1,191 @@ +function DataWrap() +{ + // REFERENCE + this.database = null; + this.keys = null; + + // SETTINGS + this.statsNumTags = 10; + + this.install = function() + { + this.database = new Indental(DATABASE).parse(); + this.keys = Object.keys(this.database); + this.processDatabase(); + } + + this.processDatabase = function() + { + for (i = 0; i < this.keys.length; i++) + { + let value = this.database[this.keys[i]]; + + // TAGS + if (typeof value.TAGS !== 'undefined') + { + var tags = value.TAGS.split(","); + + for (var t = 0; t < tags.length; t++) + { + tags[t] = tags[t].trim().toLowerCase(); + } + + this.database[this.keys[i]].TAGS = tags; + } + + // TERMS + if (typeof value.TERM !== 'undefined') + { + let termRunic = new Runic(value.TERM).raw; + let formattedTerms = []; + + for (var t = 0; t < termRunic.length; t++) + { + term = termRunic[t].substr(2).split(':'); + for (var e = 0; e < term.length; e++) + { + term[e] = term[e].trim(); + } + formattedTerms.push(term); + } + + this.database[this.keys[i]].TERM = formattedTerms; + } + + this.database[this.keys[i]].DIID = i; + } + console.log(this.database); + } + + this.filter = function(target) + { + var tempDatabase = {}; + + if (target == 'home') + { + console.log('Display \'home\''); + tempDatabase = this.database; + } + else if (target == 'term') + { + console.log('Display \'terms\''); + + for (i = 0; i < this.keys.length; i++) + { + let value = this.database[this.keys[i]]; + if (typeof value.TERM !== 'undefined') + { + tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + } + } + } + else + { + var splitTarget = target.split("-"); + if (splitTarget[0] == 'tag') + { + // TAG + console.log('Display tag \'' + splitTarget[1] + '\''); + for (i = 0; i < this.keys.length; i++) + { + let value = this.database[this.keys[i]]; + if (typeof value.TAGS !== 'undefined') + { + for (var t = 0; t < value.TAGS.length; t++) + { + if (value.TAGS[t] == splitTarget[1]) + { + tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + } + } + } + } + } + else if (splitTarget[0] == 'type') + { + // TYPE + console.log('Display type \'' + splitTarget[1] + '\''); + var tempDatabase = {} + for (i = 0; i < this.keys.length; i++) + { + let value = this.database[this.keys[i]]; + if (typeof value.TYPE !== 'undefined') + { + if (value.TYPE == splitTarget[1]) + { + tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + } + } + } + } + } + return tempDatabase; + } + + this.getStats = function(db = this.database) + { + // CALCULATE + let dbKeys = Object.keys(db); + var stats = + { + total: this.keys.length, + types: {}, + tags: {}, + terms: 0, + }; + + for (var i = 0; i < dbKeys.length; i++) + { + // TYPE + if (typeof db[dbKeys[i]].TYPE !== 'undefined') + { + if (typeof stats.types[db[dbKeys[i]].TYPE] !== 'undefined') + { + stats.types[db[dbKeys[i]].TYPE] ++; + } + else + { + stats.types[db[dbKeys[i]].TYPE] = 1; + } + } + + // TAGS + if (typeof db[dbKeys[i]].TAGS !== 'undefined') + { + for (var t = 0; t < db[dbKeys[i]].TAGS.length; t++) + { + if (typeof stats.tags[db[dbKeys[i]].TAGS[t]] !== 'undefined') + { + stats.tags[db[dbKeys[i]].TAGS[t]] ++; + } + else + { + stats.tags[db[dbKeys[i]].TAGS[t]] = 1; + } + } + } + + // TERM + if (typeof db[dbKeys[i]].TERM !== 'undefined') + { + stats.terms += db[dbKeys[i]].TERM.length; + } + } + + // SORT TAGS, TAKE TOP X + // Create items array + var items = Object.keys(stats.tags).map(function(key) + { + return [key, stats.tags[key]]; + }); + // Sort the array based on the second element + items.sort(function(first, second) + { + return second[1] - first[1]; + }); + + stats.tags = items.slice(0, this.statsNumTags); + + return stats; + } +} \ No newline at end of file diff --git a/logic/main.js b/logic/main.js index 8683416..454dfe9 100644 --- a/logic/main.js +++ b/logic/main.js @@ -1,8 +1,7 @@ function Main() { // REFERENCE - this.database = null; - this.keys = null; + this.db = null; this.msnry = null; this.grid = null; this.menu = null; @@ -10,14 +9,12 @@ function Main() // SETTINGS this.useMasonry = true; this.divNamePre = 'item'; - this.statsNumTags = 10; // MAIN this.install = function() { - this.database = new Indental(DATABASE).parse(); - this.keys = Object.keys(this.database); - this.processDatabase(); + this.db = new DataWrap(DATABASE); + this.db.install(); this.grid = document.getElementById("grid"); this.menu = document.getElementById("menu"); @@ -33,7 +30,7 @@ function Main() }); } - this.displayStats(this.database); + this.displayStats(this.db.getStats()); } this.start = function() @@ -43,12 +40,10 @@ function Main() this.load = function(target = "home") { - console.log('load'); - target = target.substr(0,1) == "#" ? target.substr(1,target.length-1) : target target = target.trim() == "" ? "home" : target - if(target === '') + if (target === '') { window.history.replaceState(undefined, undefined, "#" + target) } @@ -58,346 +53,159 @@ function Main() window.location.hash = target; } - var tempDatabase = {} - - if (target == 'home') - { - console.log('Display \'home\''); - tempDatabase = this.database; - } - else if (target == 'term') - { - console.log('Display \'terms\''); - - for (i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (typeof value.TERM !== 'undefined') - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } - } - else - { - var splitTarget = target.split("-"); - console.log('split: ' + splitTarget[0]); - - if (splitTarget[0] == 'tag') - { - // TAG - console.log('Display tag \'' + splitTarget[1] + '\''); - - for (i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (typeof value.TAGS !== 'undefined') - { - for (var t = 0; t < value.TAGS.length; t++) - { - if (value.TAGS[t] == splitTarget[1]) - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } - } - } - } - else if (splitTarget[0] == 'type') - { - // TYPE - console.log('Display type \'' + splitTarget[1] + '\''); - - var tempDatabase = {} - for (i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (typeof value.TYPE !== 'undefined') - { - if (value.TYPE == splitTarget[1]) - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } - } - } - } + var entries = this.db.filter(target); // DISPLAY this.grid.innerHTML = ''; - this.displayEntries(tempDatabase); + this.displayEntries(entries); + // LAYOUT if (this.useMasonry) { this.msnry.reloadItems(); this.msnry.layout(); } - // this.displayStats(tempDatabase); + // this.displayStats(this.db.getStats(entries)); } - this.processDatabase = function() + this.displayStats = function(stats) { - for (i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - - // TAGS - if (typeof value.TAGS !== 'undefined') - { - var tags = value.TAGS.split(","); - - for (var t = 0; t < tags.length; t++) - { - tags[t] = tags[t].trim().toLowerCase(); - } - - this.database[this.keys[i]].TAGS = tags; - } - - // TERMS - if (typeof value.TERM !== 'undefined') - { - let termRunic = new Runic(value.TERM).raw; - let formattedTerms = []; - - for (var t = 0; t < termRunic.length; t++) - { - term = termRunic[t].substr(2).split(':'); - for (var e = 0; e < term.length; e++) - { - term[e] = term[e].trim(); - } - formattedTerms.push(term); - } - - this.database[this.keys[i]].TERM = formattedTerms; - } - - this.database[this.keys[i]].DIID = i; - } - console.log(this.database); - } - - this.missing = function(target) - { - console.warn(`Could not find ${target}.`); - //this.el.innerHTML = `

Could not find page ${target}

`; - } - - this.touch = function(target) - { - console.log('touch'); - - var link = target.getAttribute("href") ? target.getAttribute("href") : target.parentNode.getAttribute("href") - - if(!link){ return; } - if(link.substr(0,1) != "#"){ return; } - - this.load(link.substr(1,link.length-1)); - } - - //document.addEventListener('mouseup', (e)=>{ this.touch(e.target); e.preventDefault(); }); - - this.displayStats = function(db) - { - // CALCULATE - let dbKeys = Object.keys(db); - let types = {}; - let tags = {}; - let terms = 0; - let i = 0; - while (i < dbKeys.length) - { - // TYPE - if (typeof db[dbKeys[i]].TYPE !== 'undefined') - { - if (typeof types[db[dbKeys[i]].TYPE] !== 'undefined') - { - types[db[dbKeys[i]].TYPE] ++; - } - else - { - types[db[dbKeys[i]].TYPE] = 1; - } - } - - // TAGS - if (typeof db[dbKeys[i]].TAGS !== 'undefined') - { - for (var t = 0; t < db[dbKeys[i]].TAGS.length; t++) - { - if (typeof tags[db[dbKeys[i]].TAGS[t]] !== 'undefined') - { - tags[db[dbKeys[i]].TAGS[t]] ++; - } - else - { - tags[db[dbKeys[i]].TAGS[t]] = 1; - } - } - } - - // TERM - if (typeof db[dbKeys[i]].TERM !== 'undefined') - { - terms += db[dbKeys[i]].TERM.length; - } - - i++; - } - - // SORT TAGS, TAKE TOP 5 - // Create items array - var items = Object.keys(tags).map(function(key) { - return [key, tags[key]]; - }); - - // Sort the array based on the second element - items.sort(function(first, second) { - return second[1] - first[1]; - }); - - tags = items.slice(0, this.statsNumTags); - console.log(tags); - - - // DISPLAY let menuContent = ``; // TYPE menuContent += ``; menuContent += ``; menuContent += ``; - if (typeof types['article'] !== 'undefined') + if (typeof stats.types['article'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['podcast'] !== 'undefined') + if (typeof stats.types['podcast'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['video'] !== 'undefined') + if (typeof stats.types['video'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['list'] !== 'undefined') + if (typeof stats.types['list'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['book'] !== 'undefined') + if (typeof stats.types['book'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['game'] !== 'undefined') + if (typeof stats.types['game'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['service'] !== 'undefined') + if (typeof stats.types['service'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['lecture'] !== 'undefined') + if (typeof stats.types['lecture'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['quote'] !== 'undefined') + if (typeof stats.types['quote'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['tool'] !== 'undefined') + if (typeof stats.types['tool'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } - if (typeof types['music'] !== 'undefined') + if (typeof stats.types['music'] !== 'undefined') { menuContent += ``; menuContent += ``; menuContent += ``; } // TERM - if (terms > 0) + if (stats.terms > 0) { // menuContent += ``; menuContent += ``; menuContent += ``; menuContent += ``; } // TAGS - if (tags.length > 0) + if (stats.tags.length > 0) { menuContent += `