dbs.js 4.2 KB

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