tree.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. var m = require('mithril');
  2. var markdown = require('markdown-it')()
  3. .use(require('markdown-it-footnote'));
  4. var _dbs = require('./dbs');
  5. var _Header = require('./header');
  6. var _Footer = require('./footer');
  7. // __ _ __
  8. // / / (_)___ / /__
  9. // / / / / __ \/ //_/
  10. // / /___/ / / / / ,<
  11. // /_____/_/_/ /_/_/|_|
  12. var _Link = {
  13. tid:"",
  14. opened:false,
  15. oninit: function(vn){
  16. // console.log("INIT LINK : vn", vn);
  17. // define target id
  18. this.tid = vn.attrs.href;
  19. },
  20. onbeforeupdate: function(vn){
  21. this.tid = vn.attrs.href;
  22. },
  23. view: function(vn){
  24. this.tid_known = typeof _dbs.data_byid[_dbs.lang][this.tid] === 'undefined' ? false : true;
  25. if (!this.tid_known) {
  26. console.log('!! target id '+this.tid+' unkonwn !!');
  27. }
  28. if(this.opened && this.tid_known){
  29. // console.log('this.tid', vn.state);
  30. return m('div', {'class':'opened-link'},
  31. [
  32. m('span', {'class':"link text"}, vn.children),
  33. typeof _dbs.data_byid[_dbs.lang][this.tid].childs != "undefined"
  34. ? m(_Enonce, _dbs.data_byid[_dbs.lang][this.tid])
  35. : m(_Item, _dbs.data_byid[_dbs.lang][this.tid])
  36. ]
  37. );
  38. }else{
  39. // console.log(vn);
  40. return m('a', {
  41. 'class':'link',
  42. 'href':'#'+this.tid,
  43. 'rel':this.tid,
  44. onclick:function(e){
  45. e.preventDefault();
  46. console.log('click', this);
  47. vn.state.opened = true;
  48. return false;
  49. }
  50. }, vn.children); // c'est quoi ce vn.children ?
  51. }
  52. }
  53. }
  54. // ______ __
  55. // /_ __/__ _ __/ /_
  56. // / / / _ \| |/_/ __/
  57. // / / / __/> </ /_
  58. // /_/ \___/_/|_|\__/
  59. // recusive function to record information of all subnodes
  60. function parseTextDom(nodes){
  61. var list = [];
  62. // loop through childNodes
  63. for (var i = 0; i < nodes.length; i++) {
  64. var n = {};
  65. if(typeof nodes[i].localName != "undefined"){
  66. n.tag=nodes[i].localName;
  67. if (n.tag == 'p') {
  68. // replace p by div as we will insert other div in them
  69. n.tag = 'div';
  70. n.attrs = {'class':'paragraph'};
  71. }
  72. if (n.tag == 'a') {
  73. // record the href attribute for cross reference
  74. n.attrs = {'href':nodes[i].attributes.href.value};
  75. }
  76. if(nodes[i].childNodes.length){
  77. // again parse node's childs
  78. n.childs = parseTextDom(nodes[i].childNodes);
  79. }
  80. }else if (nodes[i].textContent.length > 1){
  81. // if node has no localName it is probably a #text node
  82. // we record it if it's not empty
  83. n.tag='#text';
  84. n.text=nodes[i].textContent;
  85. }
  86. // add the node to the list if it has a tag
  87. if(typeof n.tag != "undefined")
  88. list.push(n);
  89. }
  90. return list;
  91. }
  92. // recusive fucntion to generate mithril object from information recorded with parseTextDom()
  93. function populateTextDom(n,i){
  94. // console.log('populateTextDom : '+i,n);
  95. return n.tag == "#text"
  96. ? m.trust(n.text)
  97. : m(
  98. n.tag != 'a' ? n.tag : _Link,
  99. typeof n.attrs != "undefined" ? n.attrs : {},
  100. typeof n.childs != "undefined"
  101. ? n.childs.map(populateTextDom)
  102. : typeof n.text != "undefined"
  103. ? m.trust(n.text)
  104. : null
  105. );
  106. }
  107. var _Text = {
  108. id:null,
  109. text:"",
  110. texthtml:"",
  111. textdom:null,
  112. textchilds:[],
  113. parsetext: function(){
  114. // console.log('parsetext', this);
  115. // !! we need to convert markdown to html here because parseTextDom() is recursive through nodes tree
  116. // convert markdown to html
  117. this.texthtml = markdown.render(this.text)
  118. // TODO: fixe number link text disapear [3](1d3) ( in 104d )
  119. // TODO: fixe parenthèse disparait _(par les Défin. [test 3](1d3) et [test 5](1d5))_ ( in 104d )
  120. // parse html string to virtual dom
  121. this.textdom = new DOMParser().parseFromString(this.texthtml, "text/html");
  122. // get the text nodes (avoid html document extra)
  123. this.textchilds = parseTextDom(this.textdom.getElementsByTagName('body')[0].childNodes);
  124. },
  125. oninit: function(vn){
  126. this.id = vn.attrs.id;
  127. this.text = vn.attrs.text;
  128. this.parsetext();
  129. },
  130. onbeforeupdate: function(vn,old){
  131. this.text = vn.attrs.text;
  132. this.parsetext();
  133. },
  134. view: function(vn){
  135. // console.log('_Text :: view : '+vn.attrs.slug, vn);
  136. return m('div',
  137. {'class':'text'},
  138. // loop through childNodes list generated by parseTextDom() in init
  139. this.textchilds.map(populateTextDom)
  140. ); // /m.div.text
  141. } // view:
  142. }
  143. // ______
  144. // / _/ /____ ____ ___
  145. // / // __/ _ \/ __ `__ \
  146. // _/ // /_/ __/ / / / / /
  147. // /___/\__/\___/_/ /_/ /_/
  148. var _Item = {
  149. id:null,
  150. part:null,
  151. type:null,
  152. nested:false,
  153. oninit: function(vn){
  154. // console.log('vn.attrs', vn.attrs);
  155. this.id = vn.attrs.id;
  156. this.type = vn.attrs.type;
  157. // vn.state.part = vn.state.slug.match(/^(\d)(.+)/)[1];
  158. this.text = vn.attrs.text;
  159. this.nested = vn.attrs.nested || false;
  160. },
  161. onbeforeupdate: function(vn, old){
  162. this.nested = vn.attrs.nested || false;
  163. this.type = vn.attrs.type;
  164. this.text = vn.attrs.text;
  165. },
  166. view: function(vn){
  167. return m("section", {
  168. 'id':this.id,
  169. 'class':'item'+(this.nested ? ' nested':'')
  170. },
  171. [
  172. // create title node
  173. m("h3", {
  174. // 'ref':vn.attrs.href,
  175. onclick: function(e){
  176. vn.state.active = vn.state.active ? 0 : 1;
  177. }
  178. }, this.type),
  179. // create text node
  180. m(_Text, {'text':this.text, 'id':this.id})
  181. ]
  182. )
  183. }
  184. };
  185. // ______
  186. // / ____/___ ____ ____ ________
  187. // / __/ / __ \/ __ \/ __ \/ ___/ _ \
  188. // / /___/ / / / /_/ / / / / /__/ __/
  189. // /_____/_/ /_/\____/_/ /_/\___/\___/
  190. var _Enonce = {
  191. partid:null,
  192. id:null,
  193. title:null,
  194. text:null,
  195. nested:false,
  196. childs:[],
  197. oninit:function(vn){
  198. // // console.log('Enonce on init', vn);
  199. this.partid = vn.attrs.partid;
  200. this.id = vn.attrs.id;
  201. this.title = vn.attrs.title;
  202. this.text = vn.attrs.text;
  203. this.childs = vn.attrs.childs;
  204. this.nested = vn.attrs.nested || false;
  205. },
  206. onbeforeupdate:function(vn, old) {
  207. // console.log(vn.attrs.childs);
  208. this.title = vn.attrs.title;
  209. this.text = vn.attrs.text;
  210. this.childs = vn.attrs.childs;
  211. this.nested = vn.attrs.nested || false;
  212. // if(vn.attrs.id == '1d1') console.log('_Enonce UPDATE, text :', vn.attrs.text);
  213. },
  214. view: function(vn){
  215. // if(vn.attrs.id == '1d1') console.log('_Enonce VIEW, text :', vn.attrs.text);
  216. return m("section", {
  217. 'id' :this.id,
  218. 'class' :'enonce'+(this.nested ? ' nested':'')
  219. },
  220. [
  221. // create title node
  222. m("h2", {}, this.title),
  223. // create text node
  224. m(_Text, {'text':this.text, 'id':this.id}),
  225. // addd children
  226. this.childs.map(function(c){
  227. return m(_Item, c)
  228. })
  229. ])
  230. }
  231. }
  232. // ____ __
  233. // / __ \____ ______/ /_
  234. // / /_/ / __ `/ ___/ __/
  235. // / ____/ /_/ / / / /_
  236. // /_/ \__,_/_/ \__/
  237. var _Part = {
  238. oninit: function(vn){
  239. this.id = vn.attrs.id;
  240. this.title = vn.attrs.title;
  241. this.enonces = vn.attrs.enonces;
  242. },
  243. onbeforeupdate: function(vn, old){
  244. // console.log('_Part, onbeforeupdate old',old);
  245. this.title = vn.attrs.title;
  246. this.enonces = vn.attrs.enonces;
  247. },
  248. view: function(vn){
  249. // console.log(vn.attrs.enonces);
  250. return m("section", {
  251. 'id' :this.id,
  252. 'class' :'part'
  253. },
  254. [
  255. // create title node
  256. m("h1", {'class':'part'}, this.title),
  257. // create text node
  258. this.enonces.map(function(e){
  259. // console.log(e.text);
  260. return m(_Enonce, Object.assign({"partid":this.id},e))
  261. })
  262. ]
  263. )
  264. }
  265. }
  266. // ______
  267. // /_ __/_______ ___
  268. // / / / ___/ _ \/ _ \
  269. // / / / / / __/ __/
  270. // /_/ /_/ \___/\___/
  271. module.exports = {
  272. view: function(){
  273. console.log('_Tree view', _dbs.lang);
  274. return [
  275. m(_Header),
  276. m('main', {id:"content"}, _dbs.data[_dbs.lang].map(function(p){
  277. // console.log("MAP _dbs", p);
  278. return m(_Part,p);
  279. })
  280. ),
  281. m(_Footer)
  282. ]
  283. }
  284. }