Refactor promise setup.

This commit is contained in:
kor 2018-11-07 01:34:55 +13:00
parent 55a9014be2
commit 0a3d0f794b
3 changed files with 97 additions and 91 deletions

View File

@ -20,7 +20,7 @@
<script src="logic/seer.js"></script> <script src="logic/seer.js"></script>
<script> <script>
var seer = new Seer(); var seer = new Seer();
seer.install(false, 1); seer.install(true, 1);
</script> </script>
<div class="error"></div> <div class="error"></div>

View File

@ -1,7 +1,8 @@
function Main() function Main()
{ {
this.util = null; this.util = null;
this.database = null; this.wrap = null;
this.articles = null;
this.grid = null; this.grid = null;
this.nav = null; this.nav = null;
this.add = null; this.add = null;
@ -17,8 +18,7 @@ function Main()
seer.note('load all js files'); seer.note('load all js files');
this.util = new Util(); this.util = new Util();
this.database = new Wrap(); this.wrap = new Wrap();
this.database.install(DATABASE);
this.grid = new Grid(); this.grid = new Grid();
this.grid.install( this.grid.install(
document.querySelector('main'), document.querySelector('main'),
@ -42,6 +42,49 @@ function Main()
seer.note('install main'); seer.note('install main');
} }
this.start = function()
{
this.wrap.start(DATABASE)
.then((db) =>
{
this.articles = db;
seer.note('process db');
let stats = this.wrap.stats(this.articles);
seer.note('calc stats');
this.nav.display(stats);
seer.note('render stats');
return this.load();
})
.catch((error) =>
{
console.log('ERROR: ' + error, error);
});
}
this.load = function()
{
this.resetPage();
this.updateQuery();
let filtered = this.wrap.filter(this.queryCur, this.articles);
seer.note('filter db');
this.grid.buildAllArticles(filtered)
.then((html) =>
{
seer.note('build html');
this.grid.newDisplay(html);
document.querySelector('.loading-wave').style.display = 'none';
return seer.report();
}).catch((error) =>
{
console.log('ERROR: ' + error, error);
});
}
this.resetPage = function() this.resetPage = function()
{ {
lightbox.close(); lightbox.close();
@ -62,39 +105,6 @@ function Main()
window.location.hash = this.queryCur; window.location.hash = this.queryCur;
} }
} }
this.start = function()
{
this.database.start();
seer.note('process db');
this.load();
}
this.load = function()
{
this.resetPage();
this.updateQuery();
seer.note('prep query');
let filtered = this.database.filter(this.queryCur);
seer.note('filter db');
this.grid.buildAllArticles(filtered)
.then((html) =>
{
seer.note('build html');
let stats = this.database.stats();
seer.note('calc stats');
this.nav.display(stats);
seer.note('render stats');
this.grid.newDisplay(html);
seer.report();
});
document.querySelector('.loading-wave').style.display = 'none';
}
} }
window.addEventListener("hashchange", function() { main.load(); }); window.addEventListener("hashchange", function() { main.load(); });

View File

@ -1,36 +1,61 @@
function Wrap() function Wrap()
{ {
this.database = null; this.start = function(data)
this.keys = null;
this.install = function(data)
{ {
this.database = new Indental(data).parse(); return new Promise(function(resolve, reject)
this.keys = Object.keys(this.database);
}
this.start = function()
{
let keys = Object.keys(this.database);
for (let i = 0; i < keys.length; i++)
{ {
let entry = this.database[keys[i]]; this.commaSplit = function(data)
{
if (data !== undefined)
{
var result = data.split(",");
for (var c = 0; c < result.length; c++)
{
result[c] = result[c].trim().toLowerCase();
}
return result;
}
return data;
}
entry.AUTH = commaSplit(entry.AUTH); this.objectSplit = function(data)
entry.TAGS = commaSplit(entry.TAGS); {
entry.TYPE = commaSplit(entry.TYPE); if (typeof data == 'object')
entry.PROJ = commaSplit(entry.PROJ); {
for (let o = 0; o < data.length; o++)
{
if (data[o].substr(0,2) == '> ')
{
data[o] = data[o].substr(2,data[o].length-1);
}
}
}
return data;
}
entry.LINK = objectSplit(entry.LINK); let database = new Indental(data).parse();
entry.FILE = objectSplit(entry.FILE); let keys = Object.keys(database);
for (let i = 0; i < keys.length; i++)
{
let entry = database[keys[i]];
this.database[keys[i]].DIID = 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);
entry.LINK = this.objectSplit(entry.LINK);
entry.FILE = this.objectSplit(entry.FILE);
database[keys[i]].DIID = i;
}
resolve(database);
});
} }
this.filter = function(target, data) this.filter = function(target, db)
{ {
let db = this.database;//data == undefined ? this.database : data;
let tempDatabase = {}; let tempDatabase = {};
if (target == '') if (target == '')
{ {
@ -159,42 +184,13 @@ function Wrap()
return tempDatabase; return tempDatabase;
} }
let commaSplit = function(data) this.stats = function(db)
{
if (data !== undefined)
{
var result = data.split(",");
for (var c = 0; c < result.length; c++)
{
result[c] = result[c].trim().toLowerCase();
}
return result;
}
return data;
}
let objectSplit = function(data)
{
if (typeof data == 'object')
{
for (let o = 0; o < data.length; o++)
{
if (data[o].substr(0,2) == '> ')
{
data[o] = data[o].substr(2,data[o].length-1);
}
}
}
return data;
}
this.stats = function(db = this.database)
{ {
// CALCULATE // CALCULATE
let dbKeys = Object.keys(db); let dbKeys = Object.keys(db);
var stats = var stats =
{ {
total: this.keys.length, total: dbKeys.length,
types: {}, types: {},
tags: {}, tags: {},
terms: 0, terms: 0,