main.js 5.5 KB

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