Gui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 container = VS.getDefaultContainer();
  139. container.props.selected = true;
  140. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 375, height: 667});
  141. VS.setContent(mainContent, container);
  142. this.CoreSystem.addPage(routeName,VS);
  143. this.forceUpdate()
  144. }
  145. renderPageBar(){
  146. let currentView = this.CoreSystem.Routing.getCurrentView();
  147. let pages = Object.keys(this.CoreSystem.Routing.topology.nodes).map((route,index) => {
  148. let view = this.CoreSystem.Routing.getView(route)
  149. let background = view === currentView ? PageButtons.activePage :(null)
  150. let textColor = view === currentView ? PageButtons.selectedText :(null)
  151. return (
  152. <View style={[PageButtons.page , background ] } onClick = {(e) => {
  153. this.CoreSystem.goto(route)
  154. this.forceUpdate()
  155. }}>
  156. <View style = {{ flex:1,
  157. justifyContent:'center',
  158. alignItems:'center'}}>
  159. <Text style={[PageButtons.text, textColor]}>{route}</Text>
  160. </View>
  161. </View>
  162. )
  163. })
  164. return (
  165. <View style={PageButtons.container}>
  166. <View style={{borderRightWidth:"1px",
  167. borderColor:"#B4b4b4",
  168. background:'white', paddingTop: 4}}>
  169. <Button
  170. color="white"
  171. title={<Icon name='filter' color="#36BBAD" />
  172. }
  173. />
  174. </View>
  175. {pages}
  176. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  177. <View style= {PageButtons.addNewContainer}>
  178. <Text style={PageButtons.addNewText}>Add new </Text>
  179. <Icon color="white" name='add' iconStyle={{fontSize: 12, paddingTop: 8}}/>
  180. </View>
  181. </View>
  182. </View>);
  183. }
  184. }
  185. const PageButtons = StyleSheet.create({
  186. container:{
  187. flex:1,
  188. flexDirection:'row',
  189. width:40
  190. },
  191. activePage:{
  192. backgroundColor:"#36BBAD",
  193. color:"white"
  194. },
  195. text:{
  196. fontSize:14,
  197. letterSpacing: 0,
  198. color: '#D9D9D9',
  199. opacity: 1,
  200. },
  201. selectedText: {
  202. color: "#FFFFFF"
  203. },
  204. page:{
  205. borderRightWidth:"1px",
  206. borderColor:"#B4b4b4",
  207. backgroundColor:"#F8F8F8",
  208. cursor:'pointer',
  209. textAlign:"center",
  210. overflow:'hidden',
  211. width:160,
  212. height:48,
  213. },
  214. addNew:{
  215. backgroundColor:"#D6D6D6",
  216. cursor:'pointer',
  217. width:160,
  218. height:48,
  219. },
  220. addNewContainer:{
  221. flex:1,
  222. flexDirection:'row',
  223. justifyContent:"center",
  224. alignItems:'center'
  225. },
  226. addNewText:{
  227. fontSize:14,
  228. letterSpacing: 0,
  229. color: 'white',
  230. opacity: 1,
  231. }
  232. })
  233. //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"}}}}}';
  234. //jsLoad Test
  235. 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}}}}}'
  236. const MudleBarStyle = StyleSheet.create({
  237. container:{
  238. backgroundColor:'red'
  239. }
  240. })
  241. const styles = StyleSheet.create({
  242. container: {
  243. flex: 1,
  244. backgroundColor: '#fff',
  245. alignItems: 'center',
  246. justifyContent: 'space-between',
  247. flexDirection: 'row'
  248. },
  249. toolbarView: {
  250. justifyContent: 'space-around',
  251. alignItems: 'center',
  252. padding: 16
  253. },
  254. mainView: {
  255. backgroundColor: '#065b69',
  256. alignSelf:'stretch',
  257. flex: 1,
  258. justifyContent: 'center',
  259. alignItems: 'center'
  260. },
  261. mobileView: {
  262. backgroundColor: 'white',
  263. borderWidth: 4,
  264. borderColor: 'orange',
  265. flex:1,
  266. alignSelf: 'stretch',
  267. },
  268. attributesView: {
  269. width:256,
  270. alignSelf: 'stretch',
  271. flexDirection: 'column',
  272. }
  273. });