ModeText.js 11 KB

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