main.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. console.log("Hello Ethica");
  15. const db_fr = require('./jsondb/2-Appuhn-FR-ethicadb.json')
  16. console.log(db_fr);
  17. // console.log("DataItems", DataItems);
  18. // var DataItemsBySlug = {};
  19. // for (item in DataItems) {
  20. // // console.log(item);
  21. // DataItemsBySlug[DataItems[item].slug] = DataItems[item];
  22. // }
  23. // console.log("DataItemsBySlug", DataItemsBySlug);
  24. // __ _ __
  25. // / / (_)___ / /__
  26. // / / / / __ \/ //_/
  27. // / /___/ / / / / ,<
  28. // /_____/_/_/ /_/_/|_|
  29. var _Link = {
  30. targetslug:"",
  31. opened:false,
  32. oninit: function(vn){
  33. // console.log("vn.attrs", vn.attrs);
  34. vn.state.targetslug = vn.attrs.href.replace(/^.*\/(.+)$/, "$1").replace(/^(.+)\/$/, "$1");
  35. },
  36. view: function(vn){
  37. if(vn.state.opened){
  38. // return m('div', "Hello "+vn.state.targetslug);
  39. // console.log('vn.state.targetslug', vn.state);
  40. return m('div', {'class':'opened-link'},
  41. [
  42. m('span', {'class':"link text"}, vn.children),
  43. m(_Item, DataItemsBySlug[vn.state.targetslug])
  44. ]
  45. );
  46. }else{
  47. return m('a', {
  48. 'class':'link',
  49. 'href':'#'+vn.state.targetslug,
  50. 'rel':vn.state.targetslug,
  51. onclick:function(e){
  52. e.preventDefault();
  53. vn.state.opened = true;
  54. return false;
  55. }
  56. }, vn.children);
  57. }
  58. }
  59. }
  60. // ______ __
  61. // /_ __/__ _ __/ /_
  62. // / / / _ \| |/_/ __/
  63. // / / / __/> </ /_
  64. // /_/ \___/_/|_|\__/
  65. var _Text = {
  66. textchilds:[],
  67. oninit: function(vn){
  68. // initiaize text childNodes array
  69. vn.state.textchilds = [];
  70. // parse html string to virtual dom
  71. // console.log('vn.attrs.text',vn.attrs.text);
  72. var textdom = new DOMParser().parseFromString(vn.attrs.text, "text/html");
  73. // get the text nodes (avoid html document extra)
  74. var childs = textdom.getElementsByTagName('body')[0].childNodes;
  75. // loop through childNodes
  76. for (var i = 0; i < childs.length; i++) {
  77. // if not textnode, get only first level paragraphs
  78. if(typeof childs[i].localName != "undefined" && childs[i].localName == "p"){
  79. var p = {
  80. tag:"p",
  81. childs:[]
  82. }
  83. // loop though paragraph child nodes (normaly only textnodes and <a>)
  84. for (var j = 0; j < childs[i].childNodes.length; j++) {
  85. // if not textnode
  86. if(typeof childs[i].childNodes[j].localName != "undefined"){
  87. switch (childs[i].childNodes[j].localName) {
  88. case 'a':
  89. // console.log('childs[i].childNodes[j]', childs[i].childNodes[j]);
  90. p.childs.push({
  91. tag:'a',
  92. href:childs[i].childNodes[j].attributes.href.value,
  93. text:childs[i].childNodes[j].innerHTML
  94. });
  95. break;
  96. }
  97. }else{
  98. // if textnode
  99. // console.log(childs[i].childNodes[j]);
  100. // console.log("["+childs[i].childNodes[j].textContent+"]");
  101. p.childs.push({
  102. tag:'#text',
  103. text:childs[i].childNodes[j].textContent //+ (childs[i].childNodes[j].nextSibling ? " " : "")
  104. });
  105. }
  106. }
  107. vn.state.textchilds.push(p);
  108. }
  109. }
  110. },
  111. view: function(vn){
  112. // console.log('_Text :: view : '+vn.attrs.slug, vn);
  113. return m('div',
  114. {'class':'text'},
  115. // loop through first level paragraph
  116. vn.state.textchilds.map(function(p,i){
  117. // create paragraph and loop through text and link nodes
  118. return m('div', {'class':'paragraph'}, p.childs.map(function(c,j) {
  119. console.log('c',c);
  120. // return p.childs.map(function(c,j) {
  121. // create text and link node
  122. switch (c.tag) {
  123. case 'a':
  124. return m(_Link,
  125. {
  126. href:c.href
  127. }, c.text);
  128. break;
  129. case '#text':
  130. // console.log("["+c.text+"]");
  131. return m.trust(c.text);
  132. break;
  133. }
  134. }) // /map
  135. ); // /m.div.paragraph
  136. }) // /map
  137. ); // /m.div.text
  138. } // view:
  139. }
  140. // ______
  141. // / _/ /____ ____ ___
  142. // / // __/ _ \/ __ `__ \
  143. // _/ // /_/ __/ / / / / /
  144. // /___/\__/\___/_/ /_/ /_/
  145. var _Item = {
  146. slug:null,
  147. part:null,
  148. type:null,
  149. active:0,
  150. oninit: function(vn){
  151. // console.log('vn.attrs', vn.attrs);
  152. vn.state.slug = vn.attrs.slug;
  153. vn.state.part = vn.state.slug.match(/^(\d)(.+)/)[1];
  154. },
  155. view: function(vn){
  156. return m("section", {
  157. 'id':vn.attrs.slug,
  158. 'class':'item'+(vn.state.active ? ' active' : '')
  159. },
  160. [
  161. // create title node
  162. m("h1", {
  163. 'ref':vn.attrs.href,
  164. onclick: function(e){
  165. vn.state.active = vn.state.active ? 0 : 1;
  166. }
  167. }, "Part "+vn.state.part+" : "+vn.attrs.title),
  168. // create text node
  169. m(_Text, {'text':vn.attrs.text, 'slug':vn.attrs.slug})
  170. ]
  171. )
  172. }
  173. };
  174. // ______
  175. // /_ __/_______ ___
  176. // / / / ___/ _ \/ _ \
  177. // / / / / / __/ __/
  178. // /_/ /_/ \___/\___/
  179. var _Tree = {
  180. view: function(){
  181. // return m('main', {id:"content"}, DataItems.map(c => m(_Item,c)));
  182. return m('main', {id:"content"}, DataItems.map(function(c){
  183. return m(_Item,c);
  184. }));
  185. }
  186. }
  187. // m.mount(document.getElementById('app'), _Tree);