dbs.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // _
  2. // (_)________ ____
  3. // / / ___/ __ \/ __ \
  4. // / (__ ) /_/ / / / /
  5. // __/ /____/\____/_/ /_/
  6. // /___/
  7. module.exports = {
  8. lang:'fr',
  9. langs:[
  10. {'lc':'lat', 'label':'Latin', 'db':'0-LAT-ethicadb.json'},
  11. {'lc':'fr', 'label':'Français', 'db':'2-Appuhn-FR-ethicadb.json'},
  12. {'lc':'bra', 'label':'Brazilian', 'db':'ethica-bresilen.json'},
  13. {'lc':'en', 'label':'English', 'db':'3-Elwes-EN-ethicadb.json'}
  14. ],
  15. data:[],
  16. loaded_dbs:0,
  17. data_byid:[],
  18. data_strct:{},
  19. load: function(callback) {
  20. // load all dbs, when all loaded call main app callback function
  21. for (var i = 0; i < this.langs.length; i++) {
  22. this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
  23. }
  24. },
  25. loadJSON: function(lc, file, callback){
  26. var xobj = new XMLHttpRequest();
  27. xobj.overrideMimeType("application/json");
  28. // TODO: load and unzip gziped json
  29. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  30. xobj.onreadystatechange = function () {
  31. // console.log('onreadystatechange', xobj.readyState);
  32. switch(xobj.readyState){
  33. case 3:
  34. console.log('loading');
  35. break;
  36. case 4:
  37. if (xobj.status === 200) {
  38. this.onJSONLoaded(lc, xobj.responseText, callback);
  39. } else {
  40. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  41. }
  42. break;
  43. }
  44. }.bind(this);
  45. xobj.open('GET', file, true);
  46. xobj.send(null);
  47. },
  48. onJSONLoaded: function(lc, json, callback){
  49. console.log('onDBLoaded '+lc);
  50. this.data[lc] = JSON.parse(json);
  51. this.loaded_dbs ++;
  52. //
  53. if (this.loaded_dbs == this.langs.length) {
  54. this.parseByID(callback);
  55. }
  56. },
  57. parseByID: function(callback){
  58. for(l in this.data){
  59. // console.log('l', l);
  60. this.data_byid[l] = {};
  61. for (p in this.data[l]) {
  62. // console.log(this.data[l][p]);
  63. for (e in this.data[l][p].enonces) {
  64. // console.log('e',e);
  65. this.data_byid[l][this.data[l][p].enonces[e].id] = this.data[l][p].enonces[e];
  66. for (c in this.data[l][p].enonces[e].childs){
  67. // console.log(_db[p][e][c]);
  68. this.data_byid[l][this.data[l][p].enonces[e].childs[c].id] = this.data[l][p].enonces[e].childs[c];
  69. }
  70. }
  71. }
  72. }
  73. // console.log('this.data_byid', this.data_byid);
  74. this.parseStrct(callback);
  75. },
  76. parseStrct: function(callback){
  77. var id, item, obj, links_match, link, tid;
  78. for (id in this.data_byid[this.langs[0].lc]) {
  79. item = this.data_byid[this.langs[0].lc][id];
  80. // console.log(item);
  81. // skeep titles as they don't have structure data
  82. if(item.type == "title") continue;
  83. obj = {
  84. 'to':[],
  85. 'from':[],
  86. };
  87. // get links
  88. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  89. // console.log(links_match);
  90. // if links exist on text
  91. if(links_match){
  92. for(link of links_match){
  93. // console.log(link);
  94. // get the target id
  95. tid = link.match(/\((.+)\)/)[1];
  96. // avoid duplicates
  97. if (obj.to.indexOf(tid) == -1)
  98. obj.to.push(tid);
  99. // add id to "from" links in target
  100. // if target exists
  101. if(typeof this.data_strct[tid] !== 'undefined'){
  102. // avoid duplicates
  103. if (this.data_strct[tid].from.indexOf(tid) == -1)
  104. this.data_strct[tid].from.push(id);
  105. }else{
  106. // if targets does not exists, the db has an issue, warn about that
  107. console.log('!! warning : '+tid+' target id does not exists');
  108. }
  109. }
  110. }
  111. // add the item links to the main links listings
  112. this.data_strct[id] = obj;
  113. }
  114. // console.log('data_strct',this.data_strct);
  115. callback();
  116. }
  117. }