modulatum.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <style type="text/css">
  6. ul, li { margin: 0px; padding: 0px; list-style: none; }
  7. .typemenu li { float: left; }
  8. #editor {clear: both; width: 100%; height: 30px;}
  9. .module { background-color: #afafaf; }
  10. </style>
  11. </head>
  12. <body onload="startit();">
  13. <div id="editor"></div>
  14. <div id="main"></div>
  15. <script>
  16. function loada(){
  17. var header = document.getElementsByTagName("head")[0];
  18. var current_script = document.getElementById("current_script");
  19. var current_style = document.getElementById("current_style");
  20. header.removeChild(current_script);
  21. header.removeChild(current_style);
  22. var newscript = document.createElement("script");
  23. newscript.type = "text/javascript";
  24. newscript.src = "./current.js?"+Math.random();
  25. newscript.id = "current_script";
  26. header.appendChild(newscript);
  27. var newstyle = document.createElement("link");
  28. newstyle.type = "text/css";
  29. newstyle.rel = "stylesheet";
  30. newstyle.href = "./current.css?"+Math.random();
  31. newstyle.id = "current_style";
  32. header.appendChild(newstyle);
  33. }
  34. function MT(){
  35. this.modules = [];
  36. this.events = {};
  37. }
  38. MT.prototype.register = function(name,func) {
  39. console.log("register",name,func);
  40. this[name] = func;
  41. }
  42. MT.prototype.on = function(eventName,func){
  43. this.events[eventName] = this.events[eventName] || [];
  44. this.events[eventName].push(func);
  45. }
  46. MT.prototype.emit = function(eventName,content){
  47. (this.events[eventName]||[]).map(function(func){
  48. func.apply(func,[content]);
  49. })
  50. }
  51. var site = new MT();
  52. var data = new MT();
  53. var register = function(name,func){ site.register(name,func) } ;
  54. var sss = [{
  55. "type": "layout"
  56. }, {
  57. "type": "blog"
  58. }, {
  59. "type": "menu"
  60. }, {
  61. "type": "form"
  62. }, {
  63. "type": "statement"
  64. }, {
  65. "type": "note"
  66. }, {
  67. "type": "comment"
  68. }, {
  69. "type": "picture"
  70. }, {
  71. "type": "text",
  72. "fields": ["title", "text"],
  73. "init": function(obj, tnode){
  74. var holder = dd("div",obj.id+"-holder","holder");
  75. var title = dd("h1",obj.id+"-title","title");
  76. title.contentEditable = true;
  77. title.addEventListener("input", function() {
  78. console.log("input event fired");
  79. }, false);
  80. title.addEventListener("blur", function() {
  81. console.log("change event fired");
  82. }, false);
  83. holder.appendChild(title)
  84. var text = dd("p",obj.id+"-text","text");
  85. text.contentEditable = true;
  86. text.addEventListener("input", function() {
  87. console.log("text input event fired");
  88. }, false);
  89. text.addEventListener("blur", function() {
  90. console.log("text change event fired");
  91. }, false);
  92. holder.appendChild( text );
  93. tnode.appendChild(holder);
  94. var aa = this;
  95. requestAnimationFrame(function(){aa.update(obj,tnode)})
  96. },
  97. "update": function(obj,tnode){
  98. console.log("UPDATE", this.type);
  99. var data=getData(obj.id)
  100. this.fields.map(function(f){
  101. console.log("UPDATE", this.type, obj.id+"-"+ f);
  102. document.getElementById(obj.id+"-"+f).innerHTML = data[f];
  103. })
  104. },
  105. "saveable": function(){
  106. }
  107. }, {
  108. "type": "map"
  109. }]
  110. function startit(){
  111. console.log("SITESTART" , sss)
  112. var s=dd("ul","editormenu","typemenu")
  113. sss.map(function(t){
  114. var sa = document.createElement("li")
  115. var saa = document.createElement("button")
  116. saa.onclick = function(){
  117. site.emit("addElement",da(t));
  118. }
  119. t._but = saa;
  120. saa.textContent= t.type;
  121. sa.appendChild(saa);
  122. s.appendChild(sa);
  123. })
  124. document.getElementById("editor").appendChild(s);
  125. site.emit("start","helo");
  126. };
  127. site.on("addElement",function(type){
  128. console.log("ADDING",type);
  129. site.modules.push( type )
  130. render();
  131. });
  132. function guid() {
  133. function s4() {
  134. return Math.floor((1 + Math.random()) * 0x10000)
  135. .toString(16)
  136. .substring(1);
  137. }
  138. return s4() + '-' + s4() + s4();
  139. }
  140. var shadow = {};
  141. function render() {
  142. site.modules.map(function(m) {
  143. if(shadow[m.id]){
  144. if(m.model.update){
  145. m.model.update( m, shadow[m.id] )
  146. }
  147. }else{
  148. shadow[m.id] = dd("div",m.id,"module "+m.model.type)
  149. if(m.model.init){
  150. m.model.init( m, shadow[m.id] )
  151. }
  152. document.getElementById("main").appendChild( shadow[m.id] )
  153. console.log( "create",m.id,shadow[m.id].textContent );
  154. }
  155. });
  156. }
  157. function getData(id,cb){
  158. return {"id":id,"content":"dfasdlfs"}
  159. }
  160. function da(t){
  161. return {"id":"e"+(guid()),"model":t }
  162. }
  163. function dd(ns, id, cla) {
  164. var rr = document.createElement(ns);
  165. rr.id = id;
  166. rr.className = cla;
  167. return rr
  168. }
  169. </script>
  170. <script>
  171. console.log("OKOKOKO");
  172. /*
  173. raws: rendable
  174. im: a blog post type
  175. { def }
  176. im: a menu thing type
  177. im: a video window type
  178. im: a contact formular
  179. im: a privacy statment
  180. im: a note
  181. function Note(){}
  182. im: a comment
  183. im: a picture
  184. im: a window with collection
  185. im: a graphics
  186. im: a map
  187. im: a button
  188. im: a layout
  189. */
  190. var ss = [{
  191. "type": "layout",
  192. "children": [{
  193. "type": "contentarea"
  194. }, {
  195. "type": "contentarea"
  196. }, {
  197. "type": "contentarea"
  198. }
  199. ]
  200. }
  201. ]
  202. </script>
  203. </body>
  204. </html>