123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // _
- // (_)________ ____
- // / / ___/ __ \/ __ \
- // / (__ ) /_/ / / / /
- // __/ /____/\____/_/ /_/
- // /___/
- module.exports = {
- lang:'fr',
- langs:[
- {
- 'lc':'lat',
- 'label':'Latin (Carl Gebhardt)',
- 'db':'0-LAT-ethicadb.json'
- },
- {
- 'lc':'fr',
- 'label':'Français (Traduction par Charles Appuhn)',
- 'db':'2-Appuhn-FR-ethicadb.json'
- },
- {
- 'lc':'bra',
- 'label':'Brazilian (Tradução Roberto Brandão)',
- 'db':'ethica-bresilen.json'
- },
- {
- 'lc':'en',
- 'label':'English (Translated by R. H. M. Elwes)',
- 'db':'3-Elwes-EN-ethicadb.json'
- }
- ],
- data:[],
- loaded_dbs:0,
- data_byid:[],
- data_strct:{},
- load: function(callback) {
- // load all dbs, when all loaded call main app callback function
- for (var i = 0; i < this.langs.length; i++) {
- this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
- }
- },
- loadJSON: function(lc, file, callback){
- var xobj = new XMLHttpRequest();
- xobj.overrideMimeType("application/json");
- // TODO: load and unzip gziped json
- // xobj.setRequestHeader('Accept-Encoding', 'gzip');
- xobj.onreadystatechange = function () {
- // console.log('onreadystatechange', xobj.readyState);
- switch(xobj.readyState){
- case 3:
- console.log('loading');
- break;
- case 4:
- if (xobj.status === 200) {
- this.onJSONLoaded(lc, xobj.responseText, callback);
- } else {
- console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
- }
- break;
- }
- }.bind(this);
- xobj.open('GET', file, true);
- xobj.send(null);
- },
- onJSONLoaded: function(lc, json, callback){
- console.log('onDBLoaded '+lc);
- this.data[lc] = JSON.parse(json);
- this.loaded_dbs ++;
- //
- if (this.loaded_dbs == this.langs.length) {
- console.log('All db loaded : data', this.data);
- this.parseByID(callback);
- }
- },
- parseByID: function(callback){
- for(l in this.data){
- // console.log('l', l);
- this.data_byid[l] = {};
- for (p in this.data[l]) {
- if(this.data[l][p].type !== "intro"){
- // console.log(this.data[l][p]);
- for (e in this.data[l][p].enonces) {
- // console.log('e',e);
- this.data_byid[l][this.data[l][p].enonces[e].id] = this.data[l][p].enonces[e];
- for (c in this.data[l][p].enonces[e].childs){
- // console.log(_db[p][e][c]);
- this.data_byid[l][this.data[l][p].enonces[e].childs[c].id] = this.data[l][p].enonces[e].childs[c];
- }
- }
- }
- }
- }
- // console.log('this.data_byid', this.data_byid);
- this.parseStrct(callback);
- },
- parseStrct: function(callback){
- var id, item, obj, links_match, link, tid;
- for (id in this.data_byid[this.langs[0].lc]) {
- item = this.data_byid[this.langs[0].lc][id];
- // console.log(item);
- // skeep titles as they don't have structure data
- if(item.type == "title") continue;
- obj = {
- 'to':[],
- 'from':[],
- };
- // get links
- links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
- // console.log(links_match);
- // if links exist on text
- if(links_match){
- for(link of links_match){
- // console.log(link);
- // get the target id
- tid = link.match(/\((.+)\)/)[1];
- // avoid duplicates
- if (obj.to.indexOf(tid) == -1)
- obj.to.push(tid);
- // add id to "from" links in target
- // if target exists
- if(typeof this.data_strct[tid] !== 'undefined'){
- // avoid duplicates
- if (this.data_strct[tid].from.indexOf(tid) == -1)
- this.data_strct[tid].from.push(id);
- }else{
- // if targets does not exists, the db has an issue, warn about that
- console.log('!! warning : '+tid+' target id does not exists');
- }
- }
- }
- // add the item links to the main links listings
- this.data_strct[id] = obj;
- }
- // console.log('data_strct',this.data_strct);
- callback();
- }
- }
|