Gui.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.SideBar.onUpdate(() => {
  31. this.forceUpdate();
  32. })
  33. this.ToolBox.onUpdate( () =>{
  34. this.SideBar.tool = this.ToolBox.activeTool;
  35. this.forceUpdate();
  36. });
  37. this.MainBar.onSave( () => {
  38. this.saveEnv();
  39. this.forceUpdate()
  40. })
  41. }
  42. onMount() {
  43. if(this.phone){
  44. // thats fine
  45. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  46. this.phoneMouse = new Mouse(this.phone.current).droppable();
  47. }
  48. if(this.ToolBoxMouse !== undefined){
  49. this.ToolBoxMouse.listen()
  50. .on('LeftDown',(e) => this.ToolManagement(e))
  51. this.phoneMouse.listen()
  52. .on('LeftDown',(e) => this.ToolManagement(e))
  53. .on('Drop', (e) => this.dropContent(e))
  54. }
  55. }
  56. saveEnv(){
  57. let env = this.CoreSystem.export();
  58. localStorage.setItem('environment', JSON.stringify(env));
  59. this.forceUpdate();
  60. try{
  61. this.CoreSystem.import(JSON.parse(js));
  62. this.forceUpdate();
  63. } catch(e) {
  64. console.log(e);
  65. }
  66. }
  67. ToolManagement(e){
  68. console.log("whattttt")
  69. if(this.ToolBox.activeTool === null){
  70. return null;
  71. }else{
  72. this.processElement(e)
  73. }
  74. }
  75. onUpdate(fn){
  76. this.__onUpdate = fn
  77. }
  78. forceUpdate(){
  79. this.__onUpdate && this.__onUpdate();
  80. }
  81. load() {
  82. this.ToolBox = new ToolBox(this.CoreSystem);
  83. try{
  84. let env = localStorage.getItem('environment');
  85. if( env === null){
  86. this.CoreSystem.import(JSON.parse(jsLoad));
  87. }else{
  88. this.CoreSystem.import(JSON.parse(env));
  89. }
  90. this.forceUpdate();
  91. } catch(e) {
  92. console.log(e);
  93. }
  94. }
  95. processElement(event){
  96. let Node = this.CoreSystem.ray({event});
  97. let NewNode = this.ToolBox.editNode(Node);
  98. this.forceUpdate();
  99. }
  100. dropContent({event, data}) {
  101. data = JSON.parse(data);
  102. let Node = this.CoreSystem.ray({event});
  103. // Node.content
  104. Node.props.selected = true;
  105. console.log(Node, data);
  106. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  107. Node.content = node;
  108. console.log(node);
  109. this.forceUpdate();
  110. }
  111. mobileRender(){
  112. return (
  113. <div ref={this.phoneRef}>
  114. <View style={styles.mobileView}>
  115. {this.CoreSystem.render()}
  116. </View>
  117. </div>
  118. )
  119. }
  120. renderSideBar(){
  121. return this.SideBar.render();
  122. }
  123. renderMainBar(){
  124. return this.MainBar.render();
  125. }
  126. renderToolBox(){
  127. return (
  128. <div ref = {this.toolboxRef}>
  129. <View style={styles.toolbarView}>
  130. {this.ToolBox.render()}
  131. </View>
  132. </div>)
  133. }
  134. renderModuleBar(){
  135. return (
  136. <View >
  137. {this.ModuleBar.render()}
  138. </View>
  139. )
  140. }
  141. }
  142. //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"}}}}}';
  143. //jsLoad Test
  144. 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}}}}}'
  145. const MudleBarStyle = StyleSheet.create({
  146. container:{
  147. backgroundColor:'red'
  148. }
  149. })
  150. const styles = StyleSheet.create({
  151. container: {
  152. flex: 1,
  153. backgroundColor: '#fff',
  154. alignItems: 'center',
  155. justifyContent: 'space-between',
  156. flexDirection: 'row'
  157. },
  158. toolbarView: {
  159. justifyContent: 'space-around',
  160. alignItems: 'center',
  161. padding: 16
  162. },
  163. mainView: {
  164. backgroundColor: '#065b69',
  165. alignSelf:'stretch',
  166. flex: 1,
  167. justifyContent: 'center',
  168. alignItems: 'center'
  169. },
  170. mobileView: {
  171. backgroundColor: 'white',
  172. borderWidth: 4,
  173. borderColor: 'orange',
  174. // iphone X
  175. width:1125/3,
  176. height: 2436/3
  177. },
  178. attributesView: {
  179. width:256,
  180. alignSelf: 'stretch',
  181. flexDirection: 'column',
  182. }
  183. });