dots.js 8.9 KB

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