main.js 4.5 KB

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