dbs.js 14 KB

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