tree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 _Dot = {
  15. id:null,
  16. type:'',
  17. text:'',
  18. summary:'',
  19. active:true,
  20. opened:false,
  21. links:null,
  22. parents:[],
  23. lang:_dbs.lang,
  24. setuptext:function(vn){
  25. // console.log('setuptext', vn);
  26. // construct text
  27. this.text = vn.attrs.text || '';
  28. this.rendered_text = markdown.render(this.text);
  29. // construct summary
  30. // TODO: summary needs more work (strip tags, markdown render)
  31. this.summary = this.text.match('([^ ]*[ ]{0,1}){1,6}')[0];
  32. this.summary = this.summary.trim().replace(/_([^_]+)$/g, "_$1_");
  33. this.summary = this.summary.replace(/\[([^\]]+)$/g, "$1");
  34. this.summary = markdown.renderInline(this.summary) + " …";
  35. },
  36. oninit: function(vn){
  37. this.id = vn.attrs.id;
  38. this.type = vn.attrs.type;
  39. this.setuptext(vn);
  40. if(typeof vn.attrs.active !== 'undefined')
  41. this.active = vn.attrs.active;
  42. // links
  43. this.links = _dbs.data_strct[this.id];
  44. // console.log(this.links);
  45. // parents memorize where do we come from to avoid duplicates and looping nav
  46. if(vn.attrs.parents){
  47. this.parents = this.parents.concat(vn.attrs.parents);
  48. // console.log('_Dot init '+this.id+' parents :',this.parents);
  49. }
  50. },
  51. oncreate: function(vn){
  52. if(this.active){
  53. vn.dom.classList.remove('disabled');
  54. }else{
  55. vn.dom.classList.add('disabled');
  56. }
  57. },
  58. onbeforeupdate: function(vn){
  59. // console.log('onbeforeupdate');
  60. if(this.lang != _dbs.lang){
  61. this.lang = _dbs.lang;
  62. this.setuptext(vn);
  63. }
  64. },
  65. view: function(vn){
  66. if (this.active && this.opened) {
  67. // full view of dot with linked dots
  68. // console.log('_Dot view '+this.id+' parents :',this.parents);
  69. var dot_content = [
  70. // links to
  71. this.links.to.length
  72. ? m('nav', {'class':'links to'}, this.links.to.map(function(id){
  73. // console.log(id);
  74. if(typeof _dbs.data_byid[_dbs.lang][id] !== 'undefined'){
  75. return m(_Dot, {
  76. "id":id,
  77. 'text':_dbs.data_byid[_dbs.lang][id].text,
  78. 'type':'',
  79. // passe the memory of crossed dots plus the current one
  80. 'parents':vn.state.parents.concat([vn.state.id]),
  81. // activate link only if not in parents (already went through it)
  82. 'active':vn.state.parents.indexOf(id) == -1 ? true:false
  83. });
  84. }
  85. })
  86. )
  87. : null,
  88. // id
  89. m('span', {'class':'id'}, this.id), // this.type+' '+
  90. // bullet
  91. m('span', {'class':'bullet'}, m.trust('⚫')),
  92. // full text
  93. m('section', {
  94. 'class':'text',
  95. onmouseover: function(e){
  96. e.preventDefault();
  97. if(e.target.nodeName == "A" ){
  98. // console.log("over e.target", e.target);
  99. // console.log('over vn', vn);
  100. var id = e.target.getAttribute("href");
  101. // add highlight class
  102. vn.dom.querySelector('nav.links>div[uid="'+id+'"]').classList.add('highlight');
  103. }else{
  104. // remove all hilight class
  105. for (link of vn.dom.querySelectorAll('nav.links>div.dot')) {
  106. link.classList.remove('highlight');
  107. }
  108. }
  109. },
  110. onclick:function(e){
  111. e.preventDefault();
  112. if(e.target.nodeName == "A" ){
  113. // console.log("over e.target", e.target);
  114. // console.log('over vn', vn);
  115. var id = e.target.getAttribute("href");
  116. // add highlight class
  117. vn.dom.querySelector('nav.links>div[uid="'+id+'"]>.summary').click();
  118. }
  119. }
  120. }, m.trust(this.rendered_text)),
  121. // links from
  122. this.links.from.length
  123. ? m('nav', {'class':'links from'}, this.links.from.map(function(id){
  124. // retrun a dot
  125. return m(_Dot, {
  126. "id":id,
  127. 'text':_dbs.data_byid[_dbs.lang][id].text,
  128. 'type':'',
  129. // passe the memory of crossed dots plus the current one
  130. 'parents':vn.state.parents.concat([vn.state.id]),
  131. // activate link only if not in parents (already went through it)
  132. 'active':vn.state.parents.indexOf(id) == -1 ? true:false
  133. });
  134. })
  135. )
  136. : null
  137. ];
  138. }else{
  139. // preview dot
  140. var dot_content = [
  141. m('span', {'class':'id'}, this.id), // this.type+' '+
  142. m('span', {'class':'bullet'}, m.trust('•')),
  143. m('p', {
  144. 'class':'summary',
  145. onclick:function(e){
  146. // TODO: animate openening (text and links separatly)
  147. vn.state.opened = true;
  148. }
  149. }, m.trust(this.summary))
  150. ];
  151. }
  152. return m('div',{
  153. 'uid':this.id,
  154. 'class':'dot'
  155. },
  156. dot_content
  157. );
  158. },
  159. onupdate: function(vn){
  160. // console.log('_Dot : onupdate', vn);
  161. if(this.active){
  162. if (this.opened){
  163. vn.dom.classList.add('opened');
  164. if(this.links.to.length)
  165. vn.dom.classList.add('to-links');
  166. if(this.links.from.length)
  167. vn.dom.classList.add('from-links');
  168. }else{
  169. vn.dom.classList.remove('opened');
  170. }
  171. }
  172. }
  173. }
  174. /*
  175. down vote
  176. Here's full list of black dotlikes from unicode
  177. ● - ● - Black Circle
  178. ⏺ - ⏺ - Black Circle for Record
  179. ⚫ - ⚫ - Medium Black Circle
  180. ⬤ - ⬤ - Black Large Circle
  181. ⧭ - ⧭ - Black Circle with Down Arrow
  182. 🞄 - 🞄 - Black Slightly Small Circle
  183. • - • - Bullet
  184. ∙ - ∙ - Bullet Operator
  185. ⋅ - ⋅ - Dot Operator
  186. 🌑 - 🌑 - New Moon Symbol
  187. */
  188. // _______ _ __ __
  189. // / ___/ / (_) /__/ /
  190. // / /__/ _ \/ / / _ /
  191. // \___/_//_/_/_/\_,_/
  192. var _Child = {
  193. id:null,
  194. part:null,
  195. type:null,
  196. // nested:false,
  197. text:'',
  198. oninit: function(vn){
  199. // console.log('vn.attrs', vn.attrs);
  200. this.id = vn.attrs.id;
  201. this.type = vn.attrs.type;
  202. // vn.state.part = vn.state.slug.match(/^(\d)(.+)/)[1];
  203. this.text = vn.attrs.text;
  204. // this.nested = vn.attrs.nested || false;
  205. },
  206. onbeforeupdate: function(vn, old){
  207. // this.nested = vn.attrs.nested || false;
  208. this.type = vn.attrs.type;
  209. this.text = vn.attrs.text;
  210. },
  211. view: function(vn){
  212. return m(_Dot, {"id":this.id, 'text':this.text, 'type':this.type});
  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:function(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. },
  236. onbeforeupdate:function(vn, old) {
  237. // console.log(vn.attrs.childs);
  238. this.title = vn.attrs.title || "";
  239. this.text = vn.attrs.text;
  240. this.childs = vn.attrs.childs || [];
  241. // this.nested = vn.attrs.nested || false;
  242. // if(vn.attrs.id == '1d1') console.log('_Enonce UPDATE, text :', vn.attrs.text);
  243. },
  244. view: function(vn){
  245. // if(vn.attrs.id == '1d1') console.log('_Enonce VIEW, text :', vn.attrs.text);
  246. return [
  247. // create dot
  248. m(_Dot, {"id":this.id, 'text':this.text,'type':this.title}),
  249. // addd children
  250. this.childs.map(function(c){
  251. return m(_Child, c);
  252. })
  253. ]
  254. }
  255. }
  256. // ____ __
  257. // / __ \____ ______/ /_
  258. // / /_/ / __ `/ ___/ __/
  259. // / ____/ /_/ / / / /_
  260. // /_/ \__,_/_/ \__/
  261. var _Part = {
  262. oninit: function(vn){
  263. this.id = vn.attrs.id;
  264. this.title = vn.attrs.title || "";
  265. this.enonces = vn.attrs.enonces;
  266. },
  267. onbeforeupdate: function(vn, old){
  268. // console.log('_Part, onbeforeupdate old',old);
  269. this.title = vn.attrs.title || "";
  270. this.enonces = vn.attrs.enonces;
  271. },
  272. view: function(vn){
  273. // console.log(vn.attrs.enonces);
  274. return m("section", {
  275. 'id' :this.id,
  276. 'class' :'part'
  277. },
  278. [
  279. // create title node
  280. m("h1", {'class':'part-title', 'part':this.id}, m.trust(markdown.renderInline(this.title))),
  281. // create text node
  282. this.enonces.map(function(e){
  283. // console.log(e.text);
  284. // return m(_Enonce, Object.assign({"partid":this.id},e))
  285. switch (e.type) {
  286. case "title":
  287. // handle titles
  288. return m("h2", {'class':'title'}, m.trust(markdown.renderInline(e.title)));
  289. break;
  290. case "filet":
  291. // handle filets
  292. return m("h4", {'class':'filet'}, m.trust(markdown.renderInline(e.title)));
  293. break;
  294. default:
  295. // or build structure
  296. return m(_Enonce, Object.assign({"partid":this.id},e));
  297. }
  298. })
  299. ]
  300. )
  301. }
  302. }
  303. // ______
  304. // /_ __/_______ ___
  305. // / / / ___/ _ \/ _ \
  306. // / / / / / __/ __/
  307. // /_/ /_/ \___/\___/
  308. module.exports = {
  309. // oninit : function(vn){
  310. // this.lang = vn.attrs.lang;
  311. // },
  312. oncreate: function(vn){
  313. document.body.classList.add('tree');
  314. _Ui.init();
  315. },
  316. view: function(vn){
  317. console.log('_Tree view', vn.attrs.lang);
  318. return [
  319. m(_Header),
  320. m('main', {id:"content", 'class':'tree'}, _dbs.data[vn.attrs.lang].map(function(p){
  321. return m(_Part,p);
  322. })
  323. ),
  324. m(_Footer)
  325. ];
  326. }
  327. }
  328. // function(){
  329. // switch(_dbs.lang){
  330. // case 'fr':
  331. // return "Hello dots !";
  332. // break;
  333. // case 'bra':
  334. // return '"Hola dots !"'
  335. // break;
  336. // }
  337. // }