Gui.js 7.2 KB

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