ModeText.js 13 KB

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