ModeText.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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
  198. ? m("h3", {
  199. // 'ref':vn.attrs.href,
  200. // onclick(e){ WHAT IS THIS STATE ACTIVE ???
  201. // vn.state.active = vn.state.active ? 0 : 1;
  202. // }
  203. }, m.trust(markdown.renderInline(this.type)))
  204. : null,
  205. // create text node
  206. m(_Text, {'text':this.text, 'id':this.id}),
  207. // add children (only if not nested)
  208. // typeof vn.attrs.childs !== 'undefined' && !this.nested
  209. // ? vn.attrs.childs.map(c => { return m(_Item, c); })
  210. // : null
  211. ]
  212. )
  213. }
  214. };
  215. // ______
  216. // / ____/___ ____ ____ ________
  217. // / __/ / __ \/ __ \/ __ \/ ___/ _ \
  218. // / /___/ / / / /_/ / / / / /__/ __/
  219. // /_____/_/ /_/\____/_/ /_/\___/\___/
  220. var _Enonce = {
  221. partid:null,
  222. id:null,
  223. title:null,
  224. text:null,
  225. nested:false,
  226. childs:[],
  227. oninit(vn){
  228. // // console.log('Enonce on init', vn);
  229. this.partid = vn.attrs.partid;
  230. this.id = vn.attrs.id;
  231. this.title = vn.attrs.title || "";
  232. this.text = vn.attrs.text;
  233. this.childs = vn.attrs.childs || [];
  234. this.nested = vn.attrs.nested || false;
  235. this.dottype = vn.attrs.dottype || "no-dottype";
  236. },
  237. onbeforeupdate(vn, old) {
  238. // console.log(vn.attrs.childs);
  239. this.title = vn.attrs.title || "";
  240. this.text = vn.attrs.text;
  241. this.childs = vn.attrs.childs || [];
  242. this.nested = vn.attrs.nested || false;
  243. // if(vn.attrs.id == '1d1') console.log('_Enonce UPDATE, text :', vn.attrs.text);
  244. },
  245. view(vn){
  246. // if(vn.attrs.id == '1d1') console.log('_Enonce VIEW, text :', vn.attrs.text);
  247. return m("section", {
  248. 'id' :this.id,
  249. 'class' :`enonce${this.nested ? ' nested':''} ${this.dottype}`
  250. },
  251. [
  252. // create title node (only if not nested)
  253. !this.nested ? m("h2", {}, m.trust(markdown.renderInline(this.title))) : null,
  254. // create text node
  255. m(_Text, {'text':this.text, 'id':this.id}),
  256. // add children (only if not nested)
  257. typeof this.childs !== 'undefined' && !this.nested
  258. ? this.childs.map(c => { return m(_Item, c); })
  259. : null
  260. ])
  261. }
  262. }
  263. // ____ __
  264. // / __ \____ ______/ /_
  265. // / /_/ / __ `/ ___/ __/
  266. // / ____/ /_/ / / / /_
  267. // /_/ \__,_/_/ \__/
  268. var _Part = {
  269. oninit(vn){
  270. // this.id = vn.attrs.id;
  271. // this.title = vn.attrs.title || '';
  272. // this.enonces = vn.attrs.enonces;
  273. },
  274. onbeforeupdate(vn, old){
  275. // console.log('_Part, onbeforeupdate old',old);
  276. // this.title = vn.attrs.title || '';
  277. // this.enonces = vn.attrs.enonces;
  278. },
  279. view(vn){
  280. // console.log("part enonces", vn.attrs.enonces);
  281. return m("section", {
  282. 'id' :vn.attrs.id,
  283. 'class' :'part'
  284. },
  285. [
  286. // create title node
  287. m("h1", {'class':'part-title', 'part':vn.attrs.id}, m.trust(markdown.renderInline(vn.attrs.title))),
  288. // create text node
  289. vn.attrs.enonces.map(e => {
  290. // console.log(e.title +" - "+ e.type);
  291. // var title = e.title || '';
  292. switch (e.type) {
  293. case "title":
  294. // handle titles
  295. // console.log('title');
  296. return m("h2", {'class':'title'}, m.trust(markdown.renderInline(e.title)));
  297. break;
  298. case "filet":
  299. // handle filets
  300. // console.log('filet');
  301. return m("h4", {'class':'filet'}, m.trust(markdown.renderInline(e.title)));
  302. break;
  303. default:
  304. // or build structure
  305. return m(_Enonce, Object.assign({"partid":vn.attrs.id},e));
  306. }
  307. })
  308. ]
  309. )
  310. }
  311. }
  312. // ____ __
  313. // / _/___ / /__________
  314. // / // __ \/ __/ ___/ __ \
  315. // _/ // / / / /_/ / / /_/ /
  316. // /___/_/ /_/\__/_/ \____/
  317. var _Intro = {
  318. oninit(vn){
  319. // console.log('_Intro : oninit : vn', vn);
  320. this.id = vn.attrs.id;
  321. this.text = vn.attrs.text || '';
  322. },
  323. onbeforeupdate(vn, old){
  324. this.id = vn.attrs.id;
  325. this.text = vn.attrs.text || '';
  326. },
  327. view(vn){
  328. return m("section", {'class':'intro'}, m("p",m.trust(markdown.renderInline(this.text))));
  329. }
  330. }
  331. // ______ __
  332. // /_ __/__ _ __/ /_
  333. // / / / _ \| |/_/ __/
  334. // / / / __/> </ /_
  335. // /_/ \___/_/|_|\__/
  336. module.exports = {
  337. // oninit(vn){
  338. // // this.lang = vn.attrs.lang;
  339. // console.log('ModeText oninit : lang', vn.attrs.lang);
  340. // // i18next.changeLanguage(vn.attrs.lang);
  341. // },
  342. oncreate(vn){
  343. document.body.classList.add('mode-text');
  344. _Ui.init();
  345. },
  346. // onbeforeupdate(vn, old){
  347. // console.log('ModeText onbeforeupdate : lang', vn.attrs.lang);
  348. // // i18next.changeLanguage(vn.attrs.lang);
  349. // },
  350. view(vn){
  351. // console.log('_ModeText view : _dbs.data', _dbs.data);
  352. return m('main', {id:"content", 'class':'mode-text'}, _dbs.data[vn.attrs.lang].map((p) => {
  353. // console.log("MAP _dbs", p);
  354. if(p.id == 'intro'){
  355. return m(_Intro,p);
  356. }else{
  357. return m(_Part,p);
  358. }
  359. })
  360. );
  361. }
  362. }