Gui.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 {Dimensions, StyleSheet, View, Text , Button
  7. } from 'react-native';
  8. import { Icon } from 'react-native-elements'
  9. import Library from 'trapilib/dist/lib';
  10. import SideBar from './SideBar';
  11. import MainBar from './MainBar';
  12. import ModuleBar from './ModuleBar';
  13. import { ViewNode } from 'trapilib/dist/lib/systems/ViewSystem';
  14. import Environment from './Environment';
  15. import BaseSystem from './System';
  16. let {
  17. CoreSystem,
  18. ViewSystem,
  19. } = Library;
  20. export default class Gui extends BaseSystem {
  21. constructor() {
  22. super();
  23. this.phoneRef = React.createRef();
  24. this.phone = this.phoneRef;
  25. this.toolboxRef = React.createRef();
  26. this.toolbox = this.toolboxRef;
  27. this.CoreSystem = new CoreSystem();
  28. this.Keyboard = new Keyboard();
  29. this.ToolBox = new ToolBox(this.CoreSystem);
  30. this.MainBar = new MainBar();
  31. this.ModuleBar = new ModuleBar(this.CoreSystem); // Add CoreSystem deps
  32. this.SideBar = new SideBar(this.CoreSystem, this.ToolBox);
  33. this.Environment = new Environment(this.CoreSystem, this.ToolBox, this.phoneRef);
  34. this.Environment.onUpdate(() => this.forceUpdate())
  35. this.SideBar.onUpdate(() => this.forceUpdate())
  36. this.ToolBox.onUpdate( () => {
  37. this.SideBar.tool = this.ToolBox.activeTool;
  38. this.forceUpdate();
  39. });
  40. this.MainBar.onUpdate(() => this.forceUpdate())
  41. this.MainBar.onSave( () => {
  42. this.saveEnv();
  43. this.forceUpdate()
  44. })
  45. this.load();
  46. // Setup mainContainer
  47. let cview = this.CoreSystem.getCurrentView();
  48. let container = cview.getDefaultContainer();
  49. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 1125/3, height: 2436/3 - 10});
  50. cview.setContent(mainContent, container);
  51. }
  52. onMount() {
  53. this.forceUpdate();
  54. if(this.phone){
  55. // thats fine
  56. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  57. this.phoneMouse = new Mouse(this.phone.current).droppable();
  58. }
  59. if(this.ToolBoxMouse !== undefined){
  60. this.ToolBoxMouse.listen()
  61. .on('LeftDown',(e) => this.ToolManagement(e))
  62. this.phoneMouse.listen()
  63. .on('LeftDown',(e) => this.ToolManagement(e))
  64. .on('Drop', (e) => this.dropContent(e))
  65. }
  66. }
  67. saveEnv(){
  68. let env = this.CoreSystem.export();
  69. localStorage.setItem('environment', JSON.stringify(env));
  70. this.forceUpdate();
  71. try{
  72. this.CoreSystem.import(JSON.parse(js));
  73. this.forceUpdate();
  74. } catch(e) {
  75. console.log(e);
  76. }
  77. }
  78. ToolManagement(e){
  79. if(this.ToolBox.activeTool !== null){
  80. this.processElement(e)
  81. }
  82. }
  83. load() {
  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. addFrame(routeName = Math.random().toString(36).substring(7)) {
  137. let VS = new Library.View();
  138. let root = VS.getRoot();
  139. root.props.selected = true;
  140. VS.setColumns(root, 3);
  141. this.CoreSystem.addPage(routeName,VS);
  142. this.forceUpdate()
  143. }
  144. renderPageBar(){
  145. let currentView = this.CoreSystem.Routing.getCurrentView();
  146. let pages = Object.keys(this.CoreSystem.Routing.topology.nodes).map((route,index) => {
  147. let view = this.CoreSystem.Routing.getView(route)
  148. let background = view === currentView ? PageButtons.activePage :(null)
  149. return (
  150. <View style={[PageButtons.page , background ] } onClick = {(e) => {
  151. this.CoreSystem.goto(route)
  152. this.forceUpdate()
  153. }}>
  154. <Text style={PageButtons.text}>{route}</Text>
  155. </View>
  156. )
  157. })
  158. return (
  159. <View style={PageButtons.container}>
  160. <Button
  161. color="#F1F1F1"
  162. title={<Icon name='filter'
  163. color="#36BBAD"
  164. />
  165. }
  166. />
  167. {pages}
  168. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  169. <Text >Add new </Text>
  170. <Icon color="#606060" name='add'/>
  171. </View>
  172. </View>);
  173. }
  174. }
  175. const PageButtons = StyleSheet.create({
  176. container:{
  177. flex:1,
  178. paddingLeft:15,
  179. flexDirection:'row',
  180. },
  181. activePage:{
  182. backgroundColor:"#36BBAD",
  183. color:"white"
  184. },
  185. text:{
  186. flex:1,
  187. justifyContent:"center",
  188. alignItems:"center",
  189. fontSize:16,
  190. letterSpacing: 0,
  191. color: '#D9D9D9',
  192. opacity: 1,
  193. },
  194. page:{
  195. padding:' 0 30px 0 25px',
  196. borderColor:"white",
  197. borderColor:'black',
  198. backgroundColor:"#FFFFFF",
  199. cursor:'pointer',
  200. textAlign:"center",
  201. position : "relative",
  202. overflow:'hidden',
  203. display:'inline-block',
  204. width:160,
  205. height:49,
  206. textcolor:"white"
  207. },
  208. addNew:{
  209. textcolor:"white",
  210. backgroundColor:"#D6D6D6",
  211. cursor:'pointer',
  212. width:160,
  213. height:49,
  214. }
  215. })
  216. //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"}}}}}';
  217. //jsLoad Test
  218. 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}}}}}'
  219. const MudleBarStyle = StyleSheet.create({
  220. container:{
  221. backgroundColor:'red'
  222. }
  223. })
  224. const styles = StyleSheet.create({
  225. container: {
  226. flex: 1,
  227. backgroundColor: '#fff',
  228. alignItems: 'center',
  229. justifyContent: 'space-between',
  230. flexDirection: 'row'
  231. },
  232. toolbarView: {
  233. justifyContent: 'space-around',
  234. alignItems: 'center',
  235. padding: 16
  236. },
  237. mainView: {
  238. backgroundColor: '#065b69',
  239. alignSelf:'stretch',
  240. flex: 1,
  241. justifyContent: 'center',
  242. alignItems: 'center'
  243. },
  244. mobileView: {
  245. backgroundColor: 'white',
  246. borderWidth: 4,
  247. borderColor: 'orange',
  248. flex:1,
  249. alignSelf: 'stretch',
  250. // iphone X
  251. width:1125/3,
  252. height: 2436/3
  253. },
  254. attributesView: {
  255. width:256,
  256. alignSelf: 'stretch',
  257. flexDirection: 'column',
  258. }
  259. });