dbs.js 4.0 KB

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