dots.js 7.9 KB

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