dbs.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // _
  2. // (_)________ ____
  3. // / / ___/ __ \/ __ \
  4. // / (__ ) /_/ / / / /
  5. // __/ /____/\____/_/ /_/
  6. // /___/
  7. var _i18n = require('./i18n');
  8. module.exports = {
  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_bytype:[], // not sure we'll use it ...
  35. types:[],
  36. active_type_filter:null,
  37. data_strct:{},
  38. rx_id:/^(\d)(app|agd|\d\d|pr|ad|ap|c|p|d|a)(cd|sc|\d\d|d|c|a|l|p|\d)?(e|\d|sc)?(d|c|a|sc)?$/,
  39. id_strct:[
  40. {full:'partie',dim:'part.'},
  41. {
  42. 'prop':{full:'proposition', dim:'prop.'}, // \d\d
  43. 'app' :{full:'appendice', dim:'app.'},
  44. 'agd' :{full:'def-gen-affect'},
  45. 'pr' :{full:'preface', dim:'pref.'},
  46. 'ad' :{full:'def-affect'},
  47. 'ap' :{full:'appendice', dim:'app.'},
  48. 'c' :{full:'corollaire', dim:'cor.'},
  49. 'p' :{full:'postulat', dim:'post.'},
  50. 'd' :{full:'definition', dim:'def.'},
  51. 'a' :{full:'axiom', dim:'ax.'},
  52. },
  53. {
  54. // \d\d
  55. // \d
  56. 'cd' :{full:'corollaire-demo'},
  57. 'sc' :{full:'scolie', dim:'scol.'},
  58. 'd' :{full:'demonstration', dim:'demo.'},
  59. 'c' :{full:'corollaire', dim:'cor.'},
  60. 'a' :{full:'axiom', dim:'ax.'},
  61. 'l' :{full:'lemme', dim:'lem.'},
  62. 'p' :{full:'postulat', dim:'post.'},
  63. 'e' :{full:'explication', dim:'exp.'},
  64. },
  65. {
  66. // \d
  67. 'e' :{full:'explication', dim:'exp.'},
  68. 'sc' :{full:'scolie', dim:'scol.'},
  69. 'c' :{full:'corollaire', dim:'cor.'},
  70. },
  71. {
  72. 'd' :{full:'demonstration', dim:'demo.'},
  73. 'c' :{full:'corollaire', dim:'cor.'},
  74. 'a' :{full:'axiom', dim:'ax.'},
  75. 'sc' :{full:'scolie', dim:'scol.'},
  76. }
  77. ],
  78. // loading progress
  79. loaded_by_file:{},
  80. // totalloaded:0,
  81. loader: document.getElementById('db-loaded'),
  82. load(callback) {
  83. // load all dbs, when all loaded call main app callback function
  84. for (var i = 0; i < this.langs.length; i++) {
  85. this.loaded_by_file[this.langs[i].lc] = 0;
  86. this.loadJSON(this.langs[i].lc, '/assets/jsondb/'+this.langs[i].db, callback)
  87. }
  88. },
  89. loadJSON(lc, file, callback){
  90. var xobj = new XMLHttpRequest();
  91. xobj.overrideMimeType("application/json");
  92. // TODO: load and unzip gziped json
  93. // xobj.setRequestHeader('Accept-Encoding', 'gzip');
  94. // Display loading progress
  95. xobj.addEventListener("progress", function(oEvent){
  96. if (oEvent.lengthComputable) {
  97. var percentComplete = oEvent.loaded / oEvent.total * 100;
  98. // console.log(lc+' loaded :',percentComplete);
  99. this.loaded_by_file[lc] = percentComplete;
  100. var totalloaded = 0;
  101. for (var i = 0; i < this.langs.length; i++) {
  102. totalloaded += this.loaded_by_file[this.langs[i].lc];
  103. }
  104. this.loader.style.width = (totalloaded/this.langs.length)+"%";
  105. } else {
  106. // Unable to compute progress information since the total size is unknown
  107. console.log('no progress');
  108. }
  109. }.bind(this));
  110. xobj.onreadystatechange = function () {
  111. // console.log('onreadystatechange', xobj.readyState);
  112. switch(xobj.readyState){
  113. case 3:
  114. // console.log('loading');
  115. break;
  116. break;
  117. case 4:
  118. if (xobj.status === 200) {
  119. this.onJSONLoaded(lc, xobj.responseText, callback);
  120. } else {
  121. console.log("Status de la réponse: %d (%s)", xobj.status, xobj.statusText);
  122. }
  123. break;
  124. }
  125. }.bind(this);
  126. xobj.open('GET', file, true);
  127. xobj.send(null);
  128. },
  129. onJSONLoaded(lc, json, callback){
  130. // console.log('onDBLoaded '+lc);
  131. this.data[lc] = JSON.parse(json);
  132. this.loaded_dbs ++;
  133. //
  134. if (this.loaded_dbs == this.langs.length) {
  135. // console.log('All db loaded : data', this.data);
  136. this.parseByID(callback);
  137. }
  138. },
  139. parseByID(callback){
  140. // TODO: make this function recursive to avoid code repetition
  141. // create a id keyed array of contents
  142. var e_id, c_id, cc_id,
  143. dottype;
  144. // loop through languages
  145. for(let l in this.data){
  146. // console.log('l', l);
  147. this.data_byid[l] = {};
  148. this.data_bytype[l] = {};
  149. this.types[l] = [];
  150. _i18n.setLang(l);
  151. // loop through parts
  152. for (let p in this.data[l]) {
  153. if(this.data[l][p].type !== "intro"){
  154. // console.log(this.data[l][p]);
  155. // loop through enonces
  156. for (let e in this.data[l][p].enonces) {
  157. // console.log('e',e);
  158. if(this.data[l][p].enonces[e].id){
  159. e_id = this.data[l][p].enonces[e].id;
  160. // guess the type from the id
  161. // console.log(this.data[l][p].enonces[e].id);
  162. dottype = this.setupType(e_id);
  163. this.data[l][p].enonces[e].dottype = dottype;
  164. // breadcrumb (full tree title)
  165. this.data[l][p].enonces[e].breadcrumb = this.setupBreadcrumb(e_id);
  166. // records childs in array keyed by ids
  167. this.data_byid[l][e_id] = this.data[l][p].enonces[e];
  168. // data by dottype
  169. if (dottype) {
  170. if(typeof this.data_bytype[l][dottype] == "undefined") this.data_bytype[l][dottype] = {};
  171. if(this.types[l].indexOf(dottype) == -1) this.types[l].push(dottype);
  172. this.data_bytype[l][dottype][e_id] = this.data[l][p].enonces[e];
  173. }
  174. }
  175. // loop through childs
  176. for (let c in this.data[l][p].enonces[e].childs){
  177. // console.log(_db[p][e][c]);
  178. if(this.data[l][p].enonces[e].childs[c].id){
  179. c_id = this.data[l][p].enonces[e].childs[c].id;
  180. // guess the type from the id
  181. dottype = this.setupType(c_id);
  182. this.data[l][p].enonces[e].childs[c].dottype = dottype;
  183. // breadcrumb (full tree title)
  184. this.data[l][p].enonces[e].childs[c].breadcrumb = this.setupBreadcrumb(c_id);
  185. // records childs in array keyed by ids
  186. this.data_byid[l][c_id] = this.data[l][p].enonces[e].childs[c];
  187. // data by dottype
  188. if (dottype) {
  189. if(typeof this.data_bytype[l][dottype] == "undefined") this.data_bytype[l][dottype] = {};
  190. if(this.types[l].indexOf(dottype) == -1) this.types[l].push(dottype);
  191. this.data_bytype[l][dottype][c_id] = this.data[l][p].enonces[e].childs[c];
  192. }
  193. }
  194. // loop through childs of childs
  195. for (let cc in this.data[l][p].enonces[e].childs[c].childs){
  196. // console.log(_db[p][e][c]);
  197. if(this.data[l][p].enonces[e].childs[c].childs[cc].id){
  198. cc_id = this.data[l][p].enonces[e].childs[c].childs[cc].id;
  199. // console.log('cc_id', cc_id);
  200. // guess the type from the id
  201. dottype = this.setupType(cc_id);
  202. this.data[l][p].enonces[e].childs[c].childs[cc].dottype = dottype;
  203. // breadcrumb (full tree title)
  204. this.data[l][p].enonces[e].childs[c].childs[cc].breadcrumb = this.setupBreadcrumb(cc_id);
  205. // records childs in array keyed by ids
  206. this.data_byid[l][cc_id] = this.data[l][p].enonces[e].childs[c].childs[cc];
  207. // data by dottype
  208. if (dottype) {
  209. if(typeof this.data_bytype[l][dottype] == "undefined") this.data_bytype[l][dottype] = {};
  210. if(this.types[l].indexOf(dottype) == -1) this.types[l].push(dottype);
  211. this.data_bytype[l][dottype][cc_id] = this.data[l][p].enonces[e].childs[c].childs[cc];
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. // console.log('this.data_byid', this.data_byid);
  221. this.parseStrct(callback);
  222. },
  223. setupBreadcrumb(id){
  224. // /^(\d)(app|agd|\d\d|pr|ad|ap|c|p|d|a)(cd|sc|\d\d|d|c|a|l|p|\d)?(e|\d|sc)?(d|c|a|sc)?$/
  225. var breadcrumb_array = [];
  226. var m = id.match(this.rx_id);
  227. var mode = 'full';
  228. if(m){
  229. m.shift();
  230. // removing undefined
  231. let m_clean = [];
  232. for (let i = 0; i < m.length; i++) {
  233. if(typeof m[i] !== 'undefined'){
  234. m_clean.push(m[i]);
  235. }
  236. }
  237. // console.log('m_clean', m_clean);
  238. for (let i = 0; i < m_clean.length; i++) { // we loop through match result variables
  239. // for (let i = m_clean.length-1; i >= 0; i--) { // we loop through match result variables in reverse order
  240. // console.log('m_clean['+i+']', m_clean[i]);
  241. if(i == 0){ // first digit is part number
  242. breadcrumb_array.unshift(`${this.id_strct[i]['dim']} ${m_clean[i]}`);
  243. }else{
  244. // display mode
  245. // mode = i !== m_clean.length-1 ? 'dim' : 'full';
  246. mode = 'full';
  247. if(typeof m_clean[i] !== 'undefined' && isNaN(m_clean[i])){ // if not a number we get the label
  248. breadcrumb_array.unshift(_i18n.t(`${this.id_strct[i][m_clean[i]][mode]}`));
  249. // breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  250. }else{ // if its a number
  251. if(i == 1){ // we just add the number to the breadcrumb preceded by 'Proposition'
  252. breadcrumb_array.unshift(_i18n.t(`${this.id_strct[i]['prop'][mode]}`) + ` ${m_clean[i]}`);
  253. }else{ // we just add the number to the breadcrumb behind the last added item
  254. // breadcrumb_array.unshift(`${m_clean[i]}`);
  255. // debugger;
  256. breadcrumb_array.splice(1, 0, `${m_clean[i]}`);
  257. }
  258. }
  259. }
  260. }
  261. }
  262. // console.log('breadcrumb_array', breadcrumb_array);
  263. return breadcrumb_array.join(' ');
  264. },
  265. setupType(id){
  266. // console.log('setupType',id);
  267. // /^(\d)(app|agd|\d\d|pr|ad|ap|c|p|d|a)(cd|sc|\d\d|d|c|a|l|p|\d)?(e|\d|sc)?(d|c|a|sc)?$/
  268. var m = id.match(this.rx_id);
  269. if(m){
  270. switch(true){
  271. case /^\d{2}$/.test(m[2]): // proposition
  272. switch(true){
  273. case /^cd$/.test(m[3]) : return 'corollaire-demo';
  274. case /^sc$/.test(m[3]) : return 'scolie';
  275. case /^d$/.test(m[3]) : return 'demonstration';
  276. case /^c$/.test(m[3]) :
  277. switch(true){
  278. case /^sc$/.test(m[4]): return 'scolie';
  279. case /^d$/.test(m[4]) : return 'demonstration';
  280. case /^sc$/.test(m[5]): return 'scolie';
  281. case /^\d$/.test(m[4]): return 'corollaire';
  282. case !m[4] : return 'corollaire';
  283. }
  284. case /^a$/.test(m[3]) : return 'prop-axiom';
  285. case /^l$/.test(m[3]) :
  286. switch(true){
  287. case /^d$/.test(m[5]) : return 'lemme-demonstration';
  288. case /^sc$/.test(m[5]): return 'lemme-scolie';
  289. case /^c$/.test(m[5]) : return 'lemme-corrollaire';
  290. case !m[5] : return 'lemme';
  291. }
  292. case /^p$/.test(m[3]) : return 'postulat';
  293. case /^\d$/.test(m[3]) : return '??';
  294. case /^\d{2}$/.test(m[3]) : return '??';
  295. case !m[3] : return 'proposition';
  296. }
  297. case /^app|ap$/.test(m[2]) : return 'appendice';
  298. case /^agd$/.test(m[2]) : return 'def-gen-affect';
  299. case /^pr$/.test(m[2]) : return 'preface';
  300. case /^ad$/.test(m[2]) :
  301. switch(true){
  302. case /^e$/.test(m[4]) :return 'explication';
  303. case !m[4] :return 'def-affect';
  304. }
  305. case /^c$/.test(m[2]) : return 'chapitre';
  306. case /^p$/.test(m[2]) : return 'postulat';
  307. case /^d$/.test(m[2]) :
  308. switch(true){
  309. case /^e$/.test(m[4]) : return 'explication';
  310. case !m[4] : return 'definition';
  311. }
  312. case /^a$/.test(m[2]) : return 'axiom';
  313. }
  314. // }
  315. }
  316. // console.log(`${this.id} -> ${this.dottype}`,m);
  317. // TODO: fix false ids
  318. // we have app and ap for appendice (1app | 4ap)
  319. // 213def ??
  320. // 209cd demo ou corollaire-demo ??
  321. // 210csc scolie ou corollaire-scolie ??
  322. // 213l1d demo ??
  323. },
  324. parseStrct(callback){
  325. var id, item, obj, links_match, link, tid;
  326. for (id in this.data_byid[this.langs[0].lc]) {
  327. item = this.data_byid[this.langs[0].lc][id];
  328. // console.log(item);
  329. // skeep titles as they don't have structure data
  330. if(item.type == "title") continue;
  331. obj = {
  332. 'to':[],
  333. 'from':[],
  334. };
  335. // get links
  336. links_match = item.text.match(/\[[^\]]+\]\([^\)]+\)/g);
  337. // console.log(links_match);
  338. // if links exist on text
  339. if(links_match){
  340. for(link of links_match){
  341. // for(i in links_match){
  342. // link = links_match[i];
  343. // console.log(link);
  344. // get the target id
  345. tid = link.match(/\((.+)\)/)[1];
  346. // avoid duplicates
  347. if (obj.to.indexOf(tid) == -1)
  348. obj.to.push(tid);
  349. // add id to "from" links in target
  350. // if target exists
  351. if(typeof this.data_strct[tid] !== 'undefined'){
  352. // avoid duplicates
  353. if (this.data_strct[tid].from.indexOf(tid) == -1)
  354. this.data_strct[tid].from.push(id);
  355. }else{
  356. // if targets does not exists, the db has an issue, warn about that
  357. // console.log(`!! warning : ${tid} target id does not exists`);
  358. }
  359. }
  360. }
  361. // add the item links to the main links listings
  362. this.data_strct[id] = obj;
  363. }
  364. // console.log('data_strct',this.data_strct);
  365. callback();
  366. }
  367. }