Gui.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. <View style = {{ flex:1,
  155. justifyContent:'center',
  156. alignItems:'center'}}>
  157. <Text style={PageButtons.text}>{route}</Text>
  158. </View>
  159. </View>
  160. )
  161. })
  162. return (
  163. <View style={PageButtons.container}>
  164. <Button
  165. color="#F1F1F1"
  166. title={<Icon name='filter'
  167. color="#36BBAD"
  168. />
  169. }
  170. />
  171. {pages}
  172. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  173. <View style= {PageButtons.addNewContainer}>
  174. <Text style={PageButtons.addNewText}>Add new </Text>
  175. <Icon color="white" name='add'/>
  176. </View>
  177. </View>
  178. </View>);
  179. }
  180. }
  181. const PageButtons = StyleSheet.create({
  182. container:{
  183. flex:1,
  184. paddingLeft:15,
  185. flexDirection:'row',
  186. },
  187. activePage:{
  188. backgroundColor:"#36BBAD",
  189. color:"white"
  190. },
  191. text:{
  192. fontSize:16,
  193. letterSpacing: 0,
  194. color: '#D9D9D9',
  195. opacity: 1,
  196. },
  197. page:{
  198. borderRightWidth:"1px",
  199. borderColor:"#B4b4b4",
  200. backgroundColor:"#FFFFFF",
  201. cursor:'pointer',
  202. textAlign:"center",
  203. overflow:'hidden',
  204. width:160,
  205. height:49,
  206. },
  207. addNew:{
  208. textcolor:"white",
  209. backgroundColor:"#D6D6D6",
  210. cursor:'pointer',
  211. width:160,
  212. height:49,
  213. },
  214. addNewContainer:{
  215. flex:1,
  216. flexDirection:'row',
  217. justifyContent:"center",
  218. alignItems:'center'
  219. },
  220. addNewText:{
  221. fontSize:16,
  222. letterSpacing: 0,
  223. color: 'white',
  224. opacity: 1,
  225. }
  226. })
  227. //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"}}}}}';
  228. //jsLoad Test
  229. 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}}}}}'
  230. const MudleBarStyle = StyleSheet.create({
  231. container:{
  232. backgroundColor:'red'
  233. }
  234. })
  235. const styles = StyleSheet.create({
  236. container: {
  237. flex: 1,
  238. backgroundColor: '#fff',
  239. alignItems: 'center',
  240. justifyContent: 'space-between',
  241. flexDirection: 'row'
  242. },
  243. toolbarView: {
  244. justifyContent: 'space-around',
  245. alignItems: 'center',
  246. padding: 16
  247. },
  248. mainView: {
  249. backgroundColor: '#065b69',
  250. alignSelf:'stretch',
  251. flex: 1,
  252. justifyContent: 'center',
  253. alignItems: 'center'
  254. },
  255. mobileView: {
  256. backgroundColor: 'white',
  257. borderWidth: 4,
  258. borderColor: 'orange',
  259. flex:1,
  260. alignSelf: 'stretch',
  261. // iphone X
  262. width:1125/3,
  263. height: 2436/3
  264. },
  265. attributesView: {
  266. width:256,
  267. alignSelf: 'stretch',
  268. flexDirection: 'column',
  269. }
  270. });