Gui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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, Node } 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(this.CoreSystem);
  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: 375, height: 667});
  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 targetNode = this.CoreSystem.ray({event});
  104. // targetNode.content
  105. targetNode.props.selected = true;
  106. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  107. let containerNode = new Node(Math.random(), {}, node);
  108. let View = this.CoreSystem.getCurrentView();
  109. View.addViewNode(containerNode, targetNode);
  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. <View style={{borderRightWidth:"1px",
  165. borderColor:"#B4b4b4",background:'white'}}>
  166. <Button
  167. color="white"
  168. title={<Icon containerStyle={{paddingLeft:14,paddingRight:14,paddingTop:2,paddingBottom:2}} name='filter'
  169. color="#36BBAD"
  170. />
  171. }
  172. />
  173. </View>
  174. {pages}
  175. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  176. <View style= {PageButtons.addNewContainer}>
  177. <Text style={PageButtons.addNewText}>Add new </Text>
  178. <Icon color="white" name='add'/>
  179. </View>
  180. </View>
  181. </View>);
  182. }
  183. }
  184. const PageButtons = StyleSheet.create({
  185. container:{
  186. flex:1,
  187. flexDirection:'row',
  188. width:40,
  189. },
  190. activePage:{
  191. backgroundColor:"#36BBAD",
  192. color:"white"
  193. },
  194. text:{
  195. fontSize:16,
  196. letterSpacing: 0,
  197. color: '#D9D9D9',
  198. opacity: 1,
  199. },
  200. page:{
  201. borderRightWidth:"1px",
  202. borderColor:"#B4b4b4",
  203. backgroundColor:"#FFFFFF",
  204. cursor:'pointer',
  205. textAlign:"center",
  206. overflow:'hidden',
  207. width:160,
  208. height:48,
  209. },
  210. addNew:{
  211. textcolor:"white",
  212. backgroundColor:"#D6D6D6",
  213. cursor:'pointer',
  214. width:160,
  215. height:48,
  216. },
  217. addNewContainer:{
  218. flex:1,
  219. flexDirection:'row',
  220. justifyContent:"center",
  221. alignItems:'center'
  222. },
  223. addNewText:{
  224. fontSize:16,
  225. letterSpacing: 0,
  226. color: 'white',
  227. opacity: 1,
  228. }
  229. })
  230. //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"}}}}}';
  231. //jsLoad Test
  232. 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}}}}}'
  233. const MudleBarStyle = StyleSheet.create({
  234. container:{
  235. backgroundColor:'red'
  236. }
  237. })
  238. const styles = StyleSheet.create({
  239. container: {
  240. flex: 1,
  241. backgroundColor: '#fff',
  242. alignItems: 'center',
  243. justifyContent: 'space-between',
  244. flexDirection: 'row'
  245. },
  246. toolbarView: {
  247. justifyContent: 'space-around',
  248. alignItems: 'center',
  249. padding: 16
  250. },
  251. mainView: {
  252. backgroundColor: '#065b69',
  253. alignSelf:'stretch',
  254. flex: 1,
  255. justifyContent: 'center',
  256. alignItems: 'center'
  257. },
  258. mobileView: {
  259. backgroundColor: 'white',
  260. borderWidth: 4,
  261. borderColor: 'orange',
  262. flex:1,
  263. alignSelf: 'stretch',
  264. },
  265. attributesView: {
  266. width:256,
  267. alignSelf: 'stretch',
  268. flexDirection: 'column',
  269. }
  270. });