inline.js 8.6 KB

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