tree.js 9.8 KB

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