Add db query filtering again with promise setup and benchmarking.

This commit is contained in:
kor 2018-11-06 13:05:29 +13:00
parent 2f8809cafb
commit 36e2dca38c
3 changed files with 151 additions and 135 deletions

View File

@ -56,7 +56,29 @@ function Main()
this.database.start(new Indental(DATABASE).parse()) this.database.start(new Indental(DATABASE).parse())
.then((db) => { .then((db) => {
this.timediff('process 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) => { .then((html) => {
this.timediff('build html'); this.timediff('build html');
@ -71,40 +93,39 @@ function Main()
// this.nav.display(this.db.stats()); // this.nav.display(this.db.stats());
} }
this.load = function(target) // this.load = function(target)
{ // {
lightbox.close(); // lightbox.close();
// document.activeElement.blur();
// if (this.queryCur !== 'add')
// {
// this.queryPrev = this.queryCur;
// }
document.activeElement.blur(); // target = target.substr(0,1) === "#" ? target.substr(1,target.length-1) : target;
if (this.queryCur !== 'add') // this.queryCur = target.trim();
{
this.queryPrev = this.queryCur;
}
target = target.substr(0,1) === "#" ? target.substr(1,target.length-1) : target; // if (window.location.hash != this.queryCur)
this.queryCur = target.trim(); // {
// window.location.hash = this.queryCur;
// }
if (window.location.hash != this.queryCur) // if (this.queryCur === 'add')
{ // {
window.location.hash = this.queryCur; // if (window.showAdd != undefined && window.showAdd)
} // {
// this.add.show();
if (this.queryCur === 'add') // }
{ // else
if (window.showAdd != undefined && window.showAdd) // {
{ // window.location.hash = this.queryPrev;
this.add.show(); // }
} // }
else // else
{ // {
window.location.hash = this.queryPrev; // this.grid.display(this.db.filter(this.queryCur));
} // }
} // }
else
{
this.grid.display(this.db.filter(this.queryCur));
}
}
} }
window.addEventListener("hashchange", function() { main.load(window.document.location.hash); }); // window.addEventListener("hashchange", function() { main.load(window.document.location.hash); });

View File

@ -24,16 +24,6 @@ function Grid()
} }
} }
this.formatArticle = function(entry)
{
return '<div>test</div>';
}
this.addHtmlToPage = function()
{
this.container.innerHTML += contentHtml;
}
this.display = function(db) this.display = function(db)
{ {
if (window.showAdd !== undefined && window.showAdd) if (window.showAdd !== undefined && window.showAdd)

View File

@ -61,129 +61,134 @@ function Wrap()
}); });
} }
this.filter = function(target) this.filter = function(db, target)
{ {
var tempDatabase = {}; let tempDatabase = {};
if (target == '') if (target == '')
{ {
tempDatabase = this.database; tempDatabase = db;
}
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]];
}
}
} }
else else
{ {
var splitTarget = target.split("-"); let keys = Object.keys(db);
if (splitTarget[0] == 'tag') if (target == 'term')
{ {
// TAG for (let i = 0; i < keys.length; i++)
let tagRequest = decodeURI(splitTarget[1]);
for (let i = 0; i < this.keys.length; i++)
{ {
let value = this.database[this.keys[i]]; let value = db[keys[i]];
if (typeof value.TAGS !== 'undefined') if (typeof value.TERM !== 'undefined')
{ {
for (let t = 0; t < value.TAGS.length; t++) tempDatabase[keys[i]] = db[keys[i]];
{
if (value.TAGS[t] == tagRequest)
{
tempDatabase[this.keys[i]] = this.database[this.keys[i]];
}
}
} }
} }
} }
if (splitTarget[0] == 'proj') else
{ {
// PROJECT var splitTarget = target.split("-");
let projectRequest = decodeURI(splitTarget[1]); if (splitTarget[0] == 'tag')
for (let i = 0; i < this.keys.length; i++) {
{ // TAG
let value = this.database[this.keys[i]]; let tagRequest = decodeURI(splitTarget[1]);
if (typeof value.PROJ !== 'undefined') 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]]; if (value.TAGS[t] == tagRequest)
}
}
}
}
}
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)
{ {
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 else
{ {
// This entry has a single type // false
if (value.TYPE == typeRequest) if (typeof value.DONE === 'undefined')
{ {
tempDatabase[this.keys[i]] = this.database[this.keys[i]]; tempDatabase[keys[i]] = db[keys[i]];
} }
} else if (value.DONE == false)
}
}
}
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')
{ {
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; return tempDatabase;
} }