inline.js 8.9 KB

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