From 0806fcc658323b3d2d68e56621aa9645254ae690 Mon Sep 17 00:00:00 2001 From: kor Date: Tue, 6 Nov 2018 04:00:16 +1300 Subject: [PATCH] Refactor to messy testing with promise setup. --- docs/logic/main.js | 35 ++++++++++++++++++++++---- docs/logic/view/grid.js | 16 +++++++++--- docs/logic/wrap.js | 56 +++++++++++++++++++++++++---------------- 3 files changed, 77 insertions(+), 30 deletions(-) diff --git a/docs/logic/main.js b/docs/logic/main.js index f295cb3..b54f5b3 100644 --- a/docs/logic/main.js +++ b/docs/logic/main.js @@ -1,7 +1,7 @@ function Main() { this.util = null; - this.db = null; + this.database = null; this.grid = null; this.nav = null; this.add = null; @@ -16,8 +16,8 @@ function Main() this.install = function() { this.util = new Util(); - this.db = new Wrap(); - this.db.install(DATABASE); + this.database = new Wrap(); + // this.database.install(DATABASE); this.grid = new Grid(); this.grid.install( document.querySelector('main'), @@ -41,8 +41,33 @@ function Main() this.start = function() { - this.load(window.document.location.hash); - this.nav.display(this.db.stats()); + console.log(Date.now() + ' - start'); + let dbPromise = this.database.start(new Indental(DATABASE).parse()); + dbPromise.then((db) => { + console.log(Date.now() + ' - db ready'); + // console.log(db); + + let dbKeys = Object.keys(db); + let i = 0; + let contentHtml = ''; + while (i < dbKeys.length) + { + contentHtml += this.grid.buildArticle(db[dbKeys[i]], dbKeys[i]); + i++; + } + + return contentHtml; + }) + .then((html) => { + console.log(Date.now() + ' - html ready'); + document.querySelector('main').innerHTML = html; + console.log(Date.now() + ' - rendered!'); + }) + .catch((error) => { + console.log('ERROR:', error); + }) + // this.load(window.document.location.hash); + // this.nav.display(this.db.stats()); } this.load = function(target) diff --git a/docs/logic/view/grid.js b/docs/logic/view/grid.js index b5a074d..932624c 100644 --- a/docs/logic/view/grid.js +++ b/docs/logic/view/grid.js @@ -24,6 +24,16 @@ function Grid() } } + this.formatArticle = function(entry) + { + return '
test
'; + } + + this.addHtmlToPage = function() + { + this.container.innerHTML += contentHtml; + } + this.display = function(db) { if (window.showAdd !== undefined && window.showAdd) @@ -37,7 +47,7 @@ function Grid() let contentHtml = ''; while (i < dbKeys.length) { - contentHtml += this.buildArticle(db, dbKeys[i]); + contentHtml += this.buildArticle(db[dbKeys[i]], dbKeys[i]); i++; } this.container.innerHTML = contentHtml; @@ -65,9 +75,9 @@ function Grid() } } - this.buildArticle = function(db, key) + this.buildArticle = function(value, key) { - let value = db[key]; + // let value = db[key]; let itemClass = "article"; if (SETTINGS.WIDEARTICLE) { diff --git a/docs/logic/wrap.js b/docs/logic/wrap.js index ac9a883..594c565 100644 --- a/docs/logic/wrap.js +++ b/docs/logic/wrap.js @@ -7,46 +7,58 @@ function Wrap() { this.database = new Indental(data).parse(); this.keys = Object.keys(this.database); - this.process(); } - this.process = function() + this.start = function(data) { - for (let i = 0; i < this.keys.length; i++) + return new Promise(function(resolve, reject) { - let entry = this.database[this.keys[i]]; - - entry.AUTH = this.commaSplit(entry.AUTH); - entry.TAGS = this.commaSplit(entry.TAGS); - entry.TYPE = this.commaSplit(entry.TYPE); - entry.PROJ = this.commaSplit(entry.PROJ); - - // LINK - if (typeof entry.LINK == 'object') + let commaSplit = function(data) { - for (let l = 0; l < entry.LINK.length; l++) + if (data !== undefined) { - if (entry.LINK[l].substr(0,2) == '> ') + var result = data.split(","); + for (var c = 0; c < result.length; c++) { - entry.LINK[l] = entry.LINK[l].substr(2,entry.LINK[l].length-1); + result[c] = result[c].trim().toLowerCase(); } + return result; } + return data; } - // FILE - if (typeof entry.FILE == 'object') + let objectSplit = function(data) { - for (let f = 0; f < entry.FILE.length; f++) + if (typeof data == 'object') { - if (entry.FILE[f].substr(0,2) == '> ') + for (let o = 0; o < data.length; o++) { - entry.FILE[f] = entry.FILE[f].substr(2,entry.FILE[f].length-1); + if (data[o].substr(0,2) == '> ') + { + data[o] = data[o].substr(2,data[o].length-1); + } } } + return data; } - this.database[this.keys[i]].DIID = i; - } + let keys = Object.keys(data); + for (let i = 0; i < keys.length; i++) + { + let entry = data[keys[i]]; + + entry.AUTH = commaSplit(entry.AUTH); + entry.TAGS = commaSplit(entry.TAGS); + entry.TYPE = commaSplit(entry.TYPE); + entry.PROJ = commaSplit(entry.PROJ); + + entry.LINK = objectSplit(entry.LINK); + entry.FILE = objectSplit(entry.FILE); + + data[keys[i]].DIID = i; + } + resolve(data); + }); } this.filter = function(target)