ModeConnections.js 10 KB

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