Gui.js 7.2 KB

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