From 36e2dca38c8db86a4c6bd2bc32d4991f3893b187 Mon Sep 17 00:00:00 2001 From: kor Date: Tue, 6 Nov 2018 13:05:29 +1300 Subject: [PATCH] Add db query filtering again with promise setup and benchmarking. --- docs/logic/main.js | 87 +++++++++++------- docs/logic/view/grid.js | 10 --- docs/logic/wrap.js | 189 +++++++++++++++++++++------------------- 3 files changed, 151 insertions(+), 135 deletions(-) diff --git a/docs/logic/main.js b/docs/logic/main.js index e40fca4..f309e54 100644 --- a/docs/logic/main.js +++ b/docs/logic/main.js @@ -56,7 +56,29 @@ function Main() this.database.start(new Indental(DATABASE).parse()) .then((db) => { this.timediff('process db'); - return this.grid.buildAllArticles(db); + let target = window.document.location.hash; + + lightbox.close(); + document.activeElement.blur(); + if (this.queryCur !== 'add') + { + this.queryPrev = this.queryCur; + } + + target = target.substr(0,1) === "#" ? target.substr(1,target.length-1) : target; + this.queryCur = target.trim(); + + if (window.location.hash != this.queryCur) + { + window.location.hash = this.queryCur; + } + + this.timediff('prep query \'' + this.queryCur + '\''); + return this.database.filter(db, this.queryCur); + }) + .then((filtered) => { + this.timediff('filter db'); + return this.grid.buildAllArticles(filtered); }) .then((html) => { this.timediff('build html'); @@ -71,40 +93,39 @@ function Main() // this.nav.display(this.db.stats()); } - this.load = function(target) - { - lightbox.close(); +// this.load = function(target) +// { +// lightbox.close(); +// document.activeElement.blur(); +// if (this.queryCur !== 'add') +// { +// this.queryPrev = this.queryCur; +// } - document.activeElement.blur(); - if (this.queryCur !== 'add') - { - this.queryPrev = this.queryCur; - } +// target = target.substr(0,1) === "#" ? target.substr(1,target.length-1) : target; +// this.queryCur = target.trim(); - target = target.substr(0,1) === "#" ? target.substr(1,target.length-1) : target; - this.queryCur = target.trim(); +// if (window.location.hash != this.queryCur) +// { +// window.location.hash = this.queryCur; +// } - if (window.location.hash != this.queryCur) - { - window.location.hash = this.queryCur; - } - - if (this.queryCur === 'add') - { - if (window.showAdd != undefined && window.showAdd) - { - this.add.show(); - } - else - { - window.location.hash = this.queryPrev; - } - } - else - { - this.grid.display(this.db.filter(this.queryCur)); - } - } +// if (this.queryCur === 'add') +// { +// if (window.showAdd != undefined && window.showAdd) +// { +// this.add.show(); +// } +// else +// { +// window.location.hash = this.queryPrev; +// } +// } +// else +// { +// this.grid.display(this.db.filter(this.queryCur)); +// } +// } } -window.addEventListener("hashchange", function() { main.load(window.document.location.hash); }); \ No newline at end of file +// window.addEventListener("hashchange", function() { main.load(window.document.location.hash); }); \ No newline at end of file diff --git a/docs/logic/view/grid.js b/docs/logic/view/grid.js index ee250af..60a4d59 100644 --- a/docs/logic/view/grid.js +++ b/docs/logic/view/grid.js @@ -24,16 +24,6 @@ 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) diff --git a/docs/logic/wrap.js b/docs/logic/wrap.js index 594c565..546f741 100644 --- a/docs/logic/wrap.js +++ b/docs/logic/wrap.js @@ -61,129 +61,134 @@ function Wrap() }); } - this.filter = function(target) + this.filter = function(db, target) { - var tempDatabase = {}; + let tempDatabase = {}; if (target == '') { - tempDatabase = this.database; - } - else if (target == 'term') - { - for (let 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]]; - } - } + tempDatabase = db; } else { - var splitTarget = target.split("-"); - if (splitTarget[0] == 'tag') + let keys = Object.keys(db); + if (target == 'term') { - // TAG - let tagRequest = decodeURI(splitTarget[1]); - for (let i = 0; i < this.keys.length; i++) + for (let i = 0; i < keys.length; i++) { - let value = this.database[this.keys[i]]; - if (typeof value.TAGS !== 'undefined') + let value = db[keys[i]]; + if (typeof value.TERM !== 'undefined') { - for (let t = 0; t < value.TAGS.length; t++) - { - if (value.TAGS[t] == tagRequest) - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } + tempDatabase[keys[i]] = db[keys[i]]; } } } - if (splitTarget[0] == 'proj') + else { - // PROJECT - let projectRequest = decodeURI(splitTarget[1]); - for (let i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (typeof value.PROJ !== 'undefined') + var splitTarget = target.split("-"); + if (splitTarget[0] == 'tag') + { + // TAG + let tagRequest = decodeURI(splitTarget[1]); + for (let i = 0; i < keys.length; i++) { - for (let p = 0; p < value.PROJ.length; p++) + let value = db[keys[i]]; + if (typeof value.TAGS !== 'undefined') { - if (value.PROJ[p] == projectRequest) + for (let t = 0; t < value.TAGS.length; t++) { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } - } - } - } - else if (splitTarget[0] == 'type') - { - // TYPE - let typeRequest = decodeURI(splitTarget[1]); - for (let i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (typeof value.TYPE !== 'undefined') - { - if (typeof value.TYPE == 'object') - { - // This entry has multiple types - for (let t = 0; t < value.TYPE.length; t++) - { - if (value.TYPE[t] == typeRequest) + if (value.TAGS[t] == tagRequest) { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + tempDatabase[keys[i]] = db[keys[i]]; + } + } + } + } + } + if (splitTarget[0] == 'proj') + { + // PROJECT + let projectRequest = decodeURI(splitTarget[1]); + for (let i = 0; i < keys.length; i++) + { + let value = db[keys[i]]; + if (typeof value.PROJ !== 'undefined') + { + for (let p = 0; p < value.PROJ.length; p++) + { + if (value.PROJ[p] == projectRequest) + { + tempDatabase[keys[i]] = db[keys[i]]; + } + } + } + } + } + else if (splitTarget[0] == 'type') + { + // TYPE + let typeRequest = decodeURI(splitTarget[1]); + for (let i = 0; i < keys.length; i++) + { + let value = db[keys[i]]; + if (typeof value.TYPE !== 'undefined') + { + if (typeof value.TYPE == 'object') + { + // This entry has multiple types + for (let t = 0; t < value.TYPE.length; t++) + { + if (value.TYPE[t] == typeRequest) + { + tempDatabase[keys[i]] = db[keys[i]]; + } + } + } + else + { + // This entry has a single type + if (value.TYPE == typeRequest) + { + tempDatabase[keys[i]] = db[keys[i]]; + } + } + } + } + } + else if (splitTarget[0] == 'done') + { + // DONE + let doneValue = decodeURI(splitTarget[1]); + for (let i = 0; i < keys.length; i++) + { + let value = db[keys[i]]; + if (doneValue == 'true') + { + // true + if (typeof value.DONE !== 'undefined') + { + if (value.DONE == 'true') + { + tempDatabase[keys[i]] = db[keys[i]]; } } } else { - // This entry has a single type - if (value.TYPE == typeRequest) + // false + if (typeof value.DONE === 'undefined') { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + tempDatabase[keys[i]] = db[keys[i]]; } - } - } - } - } - else if (splitTarget[0] == 'done') - { - // DONE - let doneValue = decodeURI(splitTarget[1]); - for (let i = 0; i < this.keys.length; i++) - { - let value = this.database[this.keys[i]]; - if (doneValue == 'true') - { - // true - if (typeof value.DONE !== 'undefined') - { - if (value.DONE == 'true') + else if (value.DONE == false) { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; + tempDatabase[keys[i]] = db[keys[i]]; } } } - else - { - // false - if (typeof value.DONE === 'undefined') - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - else if (value.DONE == false) - { - tempDatabase[this.keys[i]] = this.database[this.keys[i]]; - } - } } } } + return tempDatabase; }