ModeConnections.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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', {
  199. 'class':'summary',
  200. onclick(e){
  201. if(!vn.state.opened) vn.state.opened = 1;
  202. }
  203. }, m.trust(this.summary))
  204. ]
  205. );
  206. },
  207. view(vn){
  208. return this.active && vn.state.opened
  209. ? this.viewOpenedContent(vn) // full view of dot with linked dots
  210. : this.viewPreviewContent(vn); // preview dot
  211. }
  212. }
  213. /*
  214. full list of black dotlikes from unicode
  215. ● - ● - Black Circle
  216. ⏺ - ⏺ - Black Circle for Record
  217. ⚫ - ⚫ - Medium Black Circle
  218. ⬤ - ⬤ - Black Large Circle
  219. ⧭ - ⧭ - Black Circle with Down Arrow
  220. 🞄 - 🞄 - Black Slightly Small Circle
  221. • - • - Bullet
  222. ∙ - ∙ - Bullet Operator
  223. ⋅ - ⋅ - Dot Operator
  224. 🌑 - 🌑 - New Moon Symbol
  225. */
  226. // _______ _ __ __
  227. // / ___/ / (_) /__/ /
  228. // / /__/ _ \/ / / _ /
  229. // \___/_//_/_/_/\_,_/
  230. var _Child = {
  231. // oninit(vn){},
  232. // onbeforeupdate(vn, old){},
  233. view(vn){
  234. return [
  235. !_dbs.active_type_filter || _dbs.active_type_filter == vn.attrs.dottype
  236. ? m(_Dot, vn.attrs)
  237. : null,
  238. // add children
  239. typeof vn.attrs.childs !== 'undefined'
  240. ? vn.attrs.childs.map(c => {
  241. c.lang = vn.attrs.lang;
  242. c.level = vn.attrs.level + 1;
  243. return m(_Child, c);
  244. })
  245. : null
  246. ];
  247. }
  248. };
  249. // ______
  250. // / ____/___ ____ ____ ________
  251. // / __/ / __ \/ __ \/ __ \/ ___/ _ \
  252. // / /___/ / / / /_/ / / / / /__/ __/
  253. // /_____/_/ /_/\____/_/ /_/\___/\___/
  254. var _Enonce = {
  255. // oninit(vn){},
  256. // onbeforeupdate(vn, old) {},
  257. view(vn){
  258. vn.attrs.level = 0;
  259. return [
  260. !_dbs.active_type_filter || _dbs.active_type_filter == vn.attrs.dottype
  261. // create dot
  262. ? m(_Dot, vn.attrs)
  263. : null,
  264. // add children
  265. vn.attrs.childs.map(c => {
  266. c.lang = vn.attrs.lang;
  267. c.level = 1;
  268. return m(_Child, c);
  269. })
  270. ]
  271. }
  272. }
  273. // ____ __
  274. // / __ \____ ______/ /_
  275. // / /_/ / __ `/ ___/ __/
  276. // / ____/ /_/ / / / /_
  277. // /_/ \__,_/_/ \__/
  278. var _Part = {
  279. oninit(vn){
  280. this.id = vn.attrs.id;
  281. this.title = vn.attrs.title || "";
  282. },
  283. onbeforeupdate(vn, old){
  284. this.title = vn.attrs.title || "";
  285. },
  286. view(vn){
  287. return m("section", {
  288. 'id' :this.id,
  289. 'class' :'part'
  290. },
  291. [
  292. // create title node
  293. m("h1", {'class':'part-title', 'part':this.id}, m.trust(markdown.renderInline(this.title))),
  294. // create text node
  295. vn.attrs.enonces.map(e => {
  296. // console.log(e.text);
  297. e.lang = vn.attrs.lang;
  298. switch (e.type) {
  299. case "title":
  300. // handle titles
  301. return !_dbs.active_type_filter
  302. ? m("h2", {'class':'title'}, m.trust(markdown.renderInline(e.title)))
  303. : null;
  304. break;
  305. case "filet":
  306. // handle filets
  307. return m("h4", {'class':'filet'}, m.trust(markdown.renderInline(e.title)));
  308. break;
  309. default:
  310. // or build structure
  311. return m(_Enonce, Object.assign({"partid":this.id},e));
  312. }
  313. })
  314. ]
  315. )
  316. }
  317. }
  318. // ____ __
  319. // / _/___ / /__________
  320. // / // __ \/ __/ ___/ __ \
  321. // _/ // / / / /_/ / / /_/ /
  322. // /___/_/ /_/\__/_/ \____/
  323. var _Intro = {
  324. oninit(vn){
  325. console.log('_Intro : oninit : vn', vn);
  326. this.id = vn.attrs.id;
  327. this.text = vn.attrs.text || '';
  328. },
  329. onbeforeupdate(vn, old){
  330. this.id = vn.attrs.id;
  331. this.text = vn.attrs.text || '';
  332. },
  333. view(vn){
  334. return m("section", {'class':'intro'}, m("p",m.trust(markdown.renderInline(this.text))));
  335. }
  336. }
  337. // ______ __ _
  338. // / ____/___ ____ ____ ___ _____/ /_(_)___ ____ _____
  339. // / / / __ \/ __ \/ __ \/ _ \/ ___/ __/ / __ \/ __ \/ ___/
  340. // / /___/ /_/ / / / / / / / __/ /__/ /_/ / /_/ / / / (__ )
  341. // \____/\____/_/ /_/_/ /_/\___/\___/\__/_/\____/_/ /_/____/
  342. module.exports = {
  343. oncreate(vn){
  344. document.body.classList.add('mode-connections');
  345. _Ui.init();
  346. },
  347. onbeforeupdate(vn, old){
  348. console.log('Connection, onbeforeupdate old',old, 'vn',vn);
  349. },
  350. view(vn){
  351. // console.log('_ModeConnections view', vn.attrs.lang);
  352. console.log('_dbs.active_type_filter : ', _dbs.active_type_filter);
  353. return m('main', {id:"content", 'class':'mode-connections'}, _dbs.data[vn.attrs.lang].map(p => {
  354. p.lang = vn.attrs.lang;
  355. if(p.id == 'intro'){
  356. return m(_Intro,p);
  357. }else{
  358. return m(_Part,p);
  359. }
  360. })
  361. );
  362. }
  363. }