dbs.js 3.6 KB

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