dbs.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. load: function(callback) {
  17. // load all dbs, when all loaded call main app callback function
  18. for (var i = 0; i < this.langs.length; i++) {
  19. this.loadJSON(this.langs[i].lc, 'assets/jsondb/'+this.langs[i].db, callback)
  20. }
  21. },
  22. loadJSON: function(lc, file, callback){
  23. var xobj = new XMLHttpRequest();
  24. xobj.overrideMimeType("application/json");
  25. // TODO: load and unzip gziped json
  26. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  27. xobj.onreadystatechange = function () {
  28. // console.log('onreadystatechange', xobj.readyState);
  29. switch(xobj.readyState){
  30. case 3:
  31. console.log('loading');
  32. break;
  33. case 4:
  34. if (xobj.status === 200) {
  35. this.onJSONLoaded(lc, xobj.responseText, callback);
  36. } else {
  37. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  38. }
  39. break;
  40. }
  41. }.bind(this);
  42. xobj.open('GET', file, true);
  43. xobj.send(null);
  44. },
  45. onJSONLoaded: function(lc, json, callback){
  46. console.log('onDBLoaded');
  47. this.data[lc] = JSON.parse(json);
  48. this.loaded_dbs ++;
  49. //
  50. if (this.loaded_dbs == this.langs.length) {
  51. this.parseByID(callback);
  52. }
  53. },
  54. parseByID: function(callback){
  55. for(l in this.data){
  56. // console.log('l', l);
  57. this.data_byid[l] = {};
  58. for (p in this.data[l]) {
  59. // console.log(this.data[l][p]);
  60. for (e in this.data[l][p].enonces) {
  61. // console.log('e',e);
  62. this.data_byid[l][this.data[l][p].enonces[e].id] = this.data[l][p].enonces[e];
  63. for (c in this.data[l][p].enonces[e].childs){
  64. // console.log(_db[p][e][c]);
  65. this.data_byid[l][this.data[l][p].enonces[e].childs[c].id] = this.data[l][p].enonces[e].childs[c];
  66. }
  67. }
  68. }
  69. }
  70. // console.log('this.data_byid', this.data_byid);
  71. callback();
  72. }
  73. }