Gui.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //import CoreSystem from 'core-system';
  2. import ToolBox from './ToolBox';
  3. import Keyboard from './keyboard';
  4. import Mouse from './mouse';
  5. import React from 'react';
  6. import {StyleSheet, View, Text} from 'react-native';
  7. import Library from 'trapilib/dist/lib';
  8. import SideBar from './SideBar';
  9. import MainBar from './MainBar';
  10. import ModuleBar from './ModuleBar';
  11. import { ViewNode } from 'trapilib/dist/lib/systems/ViewSystem';
  12. let {
  13. CoreSystem,
  14. ViewSystem
  15. } = Library;
  16. export default class Gui {
  17. constructor() {
  18. this.CoreSystem = new CoreSystem();
  19. this.ModuleBar = new ModuleBar(this.CoreSystem); // Add CoreSystem deps
  20. this.Keyboard = new Keyboard();
  21. this.phoneRef = React.createRef();
  22. this.phone = this.phoneRef;
  23. this.toolboxRef = React.createRef();
  24. this.toolbox = this.toolboxRef;
  25. this.ToolBox = new ToolBox();
  26. this.MainBar = new MainBar();
  27. this.load();
  28. this.__onUpdate = null;
  29. this.SideBar = new SideBar(this.CoreSystem);
  30. this.ToolBox.onUpdate( () =>{
  31. this.SideBar.tool = this.ToolBox.activeTool;
  32. this.forceUpdate();
  33. });
  34. this.MainBar.onSave( () => {
  35. this.saveEnv();
  36. this.forceUpdate()
  37. })
  38. }
  39. onMount() {
  40. if(this.phone){
  41. // thats fine
  42. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  43. this.phoneMouse = new Mouse(this.phone.current).droppable();
  44. }
  45. if(this.ToolBoxMouse !== undefined){
  46. this.ToolBoxMouse.listen()
  47. .on('LeftDown',(e) => this.ToolManagement(e))
  48. this.phoneMouse.listen()
  49. .on('LeftDown',(e) => this.ToolManagement(e))
  50. .on('Drop', (e) => this.dropContent(e))
  51. }
  52. }
  53. saveEnv(){
  54. let env = this.CoreSystem.export();
  55. localStorage.setItem('environment', JSON.stringify(env));
  56. this.forceUpdate();
  57. try{
  58. this.CoreSystem.import(JSON.parse(js));
  59. this.forceUpdate();
  60. } catch(e) {
  61. console.log(e);
  62. }
  63. }
  64. ToolManagement(e){
  65. console.log("whattttt")
  66. if(this.ToolBox.activeTool === null){
  67. return null;
  68. }else{
  69. this.processElement(e)
  70. }
  71. }
  72. onUpdate(fn){
  73. this.__onUpdate = fn
  74. }
  75. forceUpdate(){
  76. this.__onUpdate && this.__onUpdate();
  77. }
  78. load() {
  79. this.ToolBox = new ToolBox(this.CoreSystem);
  80. try{
  81. let env = localStorage.getItem('environment');
  82. if( env === null){
  83. this.CoreSystem.import(JSON.parse(jsLoad));
  84. }else{
  85. this.CoreSystem.import(JSON.parse(env));
  86. }
  87. this.forceUpdate();
  88. } catch(e) {
  89. console.log(e);
  90. }
  91. }
  92. processElement(event){
  93. let Node = this.CoreSystem.ray({event});
  94. let NewNode = this.ToolBox.editNode(Node);
  95. this.forceUpdate();
  96. }
  97. dropContent({event, data}) {
  98. data = JSON.parse(data);
  99. let Node = this.CoreSystem.ray({event});
  100. // Node.content
  101. Node.props.selected = true;
  102. console.log(Node, data);
  103. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  104. Node.content = node;
  105. console.log(node);
  106. this.forceUpdate();
  107. }
  108. mobileRender(){
  109. return (
  110. <div ref={this.phoneRef}>
  111. <View style={styles.mobileView}>
  112. {this.CoreSystem.render()}
  113. </View>
  114. </div>
  115. )
  116. }
  117. renderSideBar(){
  118. return this.SideBar.render();
  119. }
  120. renderMainBar(){
  121. return this.MainBar.render();
  122. }
  123. renderToolBox(){
  124. return (
  125. <div ref = {this.toolboxRef}>
  126. <View style={styles.toolbarView}>
  127. {this.ToolBox.render()}
  128. </View>
  129. </div>)
  130. }
  131. renderModuleBar(){
  132. return (
  133. <View>
  134. {this.ModuleBar.render()}
  135. </View>
  136. )
  137. }
  138. }
  139. //const jsLoad = '{"Routing":{"topology":{"nodes":{"test":{"id":"test","routeName":"test","uri":"test","view":"VS1","defaultLink":null},"sample":{"id":"sample","routeName":"sample","uri":"sample","view":"VS2","defaultLink":null}},"links":{"test":{"out":[],"in":[]},"sample":{"out":[],"in":[]}}},"home":"test"},"Views":{"views":{"VS1":{"tree":{"nodes":{"A":{"id":"A","depth":0,"value":"RootComp","namespace":"default","props":{}},"B":{"id":"B","depth":1,"value":"ViewComp","namespace":"default","props":{"text":"ASD LOOK AT ME LOOK OO O.O (O.O)"}},"C":{"id":"C","depth":1,"value":"ViewComp","namespace":"default","props":{}}},"links":{"A":{"out":[{"to":"B","from":"A"},{"to":"C","from":"A"}],"in":[]},"B":{"out":[],"in":[{"to":"B","from":"A"}]},"C":{"out":[],"in":[{"to":"C","from":"A"}]}},"levels":[["A"],["B","C"]],"rootId":"A"}},"VS2":{"tree":{"nodes":{"P":{"id":"P","depth":0,"value":"RootComp","namespace":"default","props":{}}},"links":{"P":{"out":[],"in":[]}},"levels":[["P"]],"rootId":"P"}}}}}';
  140. //jsLoad Test
  141. const jsLoad = '{"Routing":{"topology":{"nodes":{"test":{"id":"test","routeName":"test","uri":"test","view":"VS1","defaultLink":null}},"links":{"test":{"out":[],"in":[]}}},"home":"test"},"Views":{"views":{"VS1":{"tree":{"nodes":{"0.5907657036474692":{"id":0.5907657036474692,"depth":0,"isRow":true,"isCol":false},"0.51066596548815":{"id":0.51066596548815,"depth":1,"isRow":false,"isCol":true},"0.8855478722619232":{"id":0.8855478722619232,"depth":1,"isRow":false,"isCol":true,"content":{"id":"A","depth":null,"value":"RootComp","namespace":"default","props":{"text":"This is the Starting point!!!Try add on me ","style":{"container":22,"text":23}}}},"0.4630979404426283":{"id":0.4630979404426283,"depth":1,"isRow":false,"isCol":true},"0.7144981784945621":{"id":0.7144981784945621,"depth":2,"isRow":true,"isCol":false},"0.0666081688424911":{"id":0.0666081688424911,"depth":2,"isRow":true,"isCol":false}},"links":{"0.5907657036474692":{"out":[{"to":0.51066596548815,"from":0.5907657036474692},{"to":0.8855478722619232,"from":0.5907657036474692},{"to":0.4630979404426283,"from":0.5907657036474692}],"in":[]},"0.51066596548815":{"out":[],"in":[{"to":0.51066596548815,"from":0.5907657036474692}]},"0.8855478722619232":{"out":[{"to":0.7144981784945621,"from":0.8855478722619232},{"to":0.0666081688424911,"from":0.8855478722619232}],"in":[{"to":0.8855478722619232,"from":0.5907657036474692}]},"0.4630979404426283":{"out":[],"in":[{"to":0.4630979404426283,"from":0.5907657036474692}]},"0.7144981784945621":{"out":[],"in":[{"to":0.7144981784945621,"from":0.8855478722619232}]},"0.0666081688424911":{"out":[],"in":[{"to":0.0666081688424911,"from":0.8855478722619232}]}},"levels":[[0.5907657036474692],[0.51066596548815,0.8855478722619232,0.4630979404426283],[0.7144981784945621,0.0666081688424911]],"rootId":0.5907657036474692}}}}}'
  142. const styles = StyleSheet.create({
  143. container: {
  144. flex: 1,
  145. backgroundColor: '#fff',
  146. alignItems: 'center',
  147. justifyContent: 'space-between',
  148. flexDirection: 'row'
  149. },
  150. toolbarView: {
  151. justifyContent: 'space-around',
  152. alignItems: 'center',
  153. padding: 16
  154. },
  155. mainView: {
  156. backgroundColor: '#065b69',
  157. alignSelf:'stretch',
  158. flex: 1,
  159. justifyContent: 'center',
  160. alignItems: 'center'
  161. },
  162. mobileView: {
  163. backgroundColor: 'white',
  164. borderWidth: 4,
  165. borderColor: 'orange',
  166. // iphone X
  167. width:1125/3,
  168. height: 2436/3
  169. },
  170. attributesView: {
  171. width:256,
  172. alignSelf: 'stretch',
  173. flexDirection: 'column',
  174. }
  175. });