main.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @Author: Bachir Soussi Chiadmi <bach>
  3. * @Date: 16-04-2017
  4. * @Email: bachir@figureslibres.io
  5. * @Last modified by: bach
  6. * @Last modified time: 18-04-2017
  7. * @License: GPL-V3
  8. */
  9. require('normalize.css/normalize.css');
  10. require('./fonts/amiri/amiri.css');
  11. require('./fonts/dejavu/dejavu.css');
  12. require('./fonts/opensans/opensans.css');
  13. var m = require('mithril');
  14. // var marked = require('marked');
  15. var markdown = require('markdown-it')()
  16. .use(require('markdown-it-footnote'));
  17. // console.log("Hello Ethica");
  18. var _db = require('./jsondb/2-Appuhn-FR-ethicadb.json')
  19. // console.log("_db", _db);
  20. var _db_by_id = {};
  21. for (p in _db) {
  22. for (e in _db[p]) {
  23. // console.log(_db[p][e]);
  24. _db_by_id[_db[p][e].id] = _db[p][e];
  25. for (c in _db[p][e]){
  26. // console.log(_db[p][e][c]);
  27. _db_by_id[_db[p][e][c].id] = _db[p][e][c];
  28. }
  29. }
  30. }
  31. console.log('_db_by_id', _db_by_id);
  32. // __ _ __
  33. // / / (_)___ / /__
  34. // / / / / __ \/ //_/
  35. // / /___/ / / / / ,<
  36. // /_____/_/_/ /_/_/|_|
  37. var _Link = {
  38. targetid:"",
  39. opened:false,
  40. oninit: function(vn){
  41. // console.log("INIT LINK : vn", vn);
  42. // define target id
  43. vn.state.tid = vn.attrs.href;
  44. },
  45. view: function(vn){
  46. if(vn.state.opened){
  47. // console.log('vn.state.tid', vn.state);
  48. return m('div', {'class':'opened-link'},
  49. [
  50. m('span', {'class':"link text"}, vn.children),
  51. typeof _db_by_id[vn.state.tid].childs != "undefined"
  52. ? m(_Enonce, _db_by_id[vn.state.tid])
  53. : m(_Item, _db_by_id[vn.state.tid])
  54. ]
  55. );
  56. }else{
  57. // console.log(vn);
  58. return m('a', {
  59. 'class':'link',
  60. 'href':'#'+vn.state.tid,
  61. 'rel':vn.state.tid,
  62. onclick:function(e){
  63. e.preventDefault();
  64. console.log('click', vn);
  65. vn.state.opened = true;
  66. return false;
  67. }
  68. }, vn.children);
  69. }
  70. }
  71. }
  72. // ______ __
  73. // /_ __/__ _ __/ /_
  74. // / / / _ \| |/_/ __/
  75. // / / / __/> </ /_
  76. // /_/ \___/_/|_|\__/
  77. // recusive function to record information of all subnodes
  78. function parseTextDom(nodes){
  79. var list = [];
  80. // loop through childNodes
  81. for (var i = 0; i < nodes.length; i++) {
  82. var n = {};
  83. if(typeof nodes[i].localName != "undefined"){
  84. n.tag=nodes[i].localName;
  85. if (n.tag == 'p') {
  86. // replace p by div as we will insert other div in them
  87. n.tag = 'div';
  88. n.attrs = {'class':'paragraph'};
  89. }
  90. if (n.tag == 'a') {
  91. // record the href attribute for cross reference
  92. n.attrs = {'href':nodes[i].attributes.href.value};
  93. }
  94. if(nodes[i].childNodes.length){
  95. // again parse node's childs
  96. n.childs = parseTextDom(nodes[i].childNodes);
  97. }
  98. }else if (nodes[i].textContent.length > 1){
  99. // if node has no localName it is probably a #text node
  100. // we record it if it's not empty
  101. n.tag='#text';
  102. n.text=nodes[i].textContent;
  103. }
  104. // add the node to the list if it has a tag
  105. if(typeof n.tag != "undefined")
  106. list.push(n);
  107. }
  108. return list;
  109. }
  110. // recusive fucntion to generate mithril object from information recorded with parseTextDom()
  111. function populateTextDom(n,i){
  112. // console.log('populateTextDom : '+i,n);
  113. return n.tag == "#text"
  114. ? m.trust(n.text)
  115. : m(
  116. n.tag != 'a' ? n.tag : _Link,
  117. typeof n.attrs != "undefined" ? n.attrs : {},
  118. typeof n.childs != "undefined"
  119. ? n.childs.map(populateTextDom)
  120. : typeof n.text != "undefined"
  121. ? m.trust(n.text)
  122. : null
  123. );
  124. }
  125. var _Text = {
  126. textchilds:[],
  127. oninit: function(vn){
  128. debug = vn.attrs.id == '1a5';
  129. // convert markdown to html
  130. var texthtml = markdown.render(vn.attrs.text)
  131. // parse html string to virtual dom
  132. var textdom = new DOMParser().parseFromString(texthtml, "text/html");
  133. // get the text nodes (avoid html document extra)
  134. if(debug) console.log('textdom',textdom);
  135. vn.state.textchilds = parseTextDom(textdom.getElementsByTagName('body')[0].childNodes);
  136. },
  137. view: function(vn){
  138. // console.log('_Text :: view : '+vn.attrs.slug, vn);
  139. // console.log('vn.state.textchilds', vn.state.textchilds);
  140. return m('div',
  141. {'class':'text'},
  142. // loop through childNodes list generated by parseTextDom() in init
  143. vn.state.textchilds.map(populateTextDom)
  144. ); // /m.div.text
  145. } // view:
  146. }
  147. // ______
  148. // / _/ /____ ____ ___
  149. // / // __/ _ \/ __ `__ \
  150. // _/ // /_/ __/ / / / / /
  151. // /___/\__/\___/_/ /_/ /_/
  152. var _Item = {
  153. id:null,
  154. part:null,
  155. type:null,
  156. nested:false,
  157. oninit: function(vn){
  158. // console.log('vn.attrs', vn.attrs);
  159. vn.state.id = vn.attrs.id;
  160. // vn.state.part = vn.state.slug.match(/^(\d)(.+)/)[1];
  161. vn.state.nested = vn.attrs.nested | false;
  162. },
  163. view: function(vn){
  164. return m("section", {
  165. 'id':vn.state.id,
  166. 'class':'item'+(vn.state.nested ? ' nested':'')
  167. },
  168. [
  169. // create title node
  170. m("h3", {
  171. // 'ref':vn.attrs.href,
  172. onclick: function(e){
  173. vn.state.active = vn.state.active ? 0 : 1;
  174. }
  175. }, vn.attrs.type),
  176. // create text node
  177. m(_Text, {'text':vn.attrs.text, 'id':vn.attrs.id})
  178. ]
  179. )
  180. }
  181. };
  182. // ______
  183. // / ____/___ ____ ____ ________
  184. // / __/ / __ \/ __ \/ __ \/ ___/ _ \
  185. // / /___/ / / / /_/ / / / / /__/ __/
  186. // /_____/_/ /_/\____/_/ /_/\___/\___/
  187. var _Enonce = {
  188. partid:null,
  189. id:null,
  190. title:null,
  191. nested:false,
  192. childs:[],
  193. oninit:function(vn){
  194. // console.log('Enonce on init', vn);
  195. vn.state.partid = vn.attrs.partid;
  196. vn.state.id = vn.attrs.id;
  197. vn.state.title = vn.attrs.title;
  198. vn.state.childs = vn.attrs.childs;
  199. vn.state.nested = vn.attrs.nested | false;
  200. },
  201. view: function(vn){
  202. return m("section", {
  203. 'id' :vn.state.id,
  204. 'class' :'enonce'+(vn.state.nested ? ' nested':'')
  205. },
  206. [
  207. // create title node
  208. m("h2", {}, vn.attrs.title),
  209. // create text node
  210. m(_Text, {'text':vn.attrs.text, 'id':vn.state.id}),
  211. // addd children
  212. vn.state.childs.map(function(c){
  213. return m(_Item, c)
  214. })
  215. ])
  216. }
  217. }
  218. // ____ __
  219. // / __ \____ ______/ /_
  220. // / /_/ / __ `/ ___/ __/
  221. // / ____/ /_/ / / / /_
  222. // /_/ \__,_/_/ \__/
  223. var _Part = {
  224. id:null, // get the part number
  225. title:null,
  226. enonces:[],
  227. oninit:function(vn){
  228. // console.log('Part on init', vn);
  229. vn.state.id = vn.attrs.id;
  230. vn.state.title = vn.attrs.title;
  231. vn.state.enonces = vn.attrs.enonces;
  232. },
  233. view: function(vn){
  234. return m("section", {
  235. 'id' :vn.state.id,
  236. 'class' :'part'
  237. },
  238. [
  239. // create title node
  240. m("h1", {'class':'part'}, vn.state.title),
  241. // create text node
  242. vn.state.enonces.map(function(e){
  243. return m(_Enonce, Object.assign({"partid":vn.state.id},e))
  244. })
  245. ])
  246. }
  247. }
  248. // ______
  249. // /_ __/_______ ___
  250. // / / / ___/ _ \/ _ \
  251. // / / / / / __/ __/
  252. // /_/ /_/ \___/\___/
  253. var _Tree = {
  254. view: function(){
  255. // return m('main', {id:"content"}, DataItems.map(c => m(_Item,c)));
  256. return m('main', {id:"content"}, _db.map(function(p){
  257. // console.log("MAP _db", _db[k]);
  258. return m(_Part,p);
  259. })
  260. );
  261. }
  262. }
  263. m.mount(document.getElementById('app'), _Tree);