Gui.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, TouchableOpacity
  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.disabledTools = true;
  28. this.CoreSystem = new CoreSystem(true);
  29. this.CoreSystem.onUpdate(() => {
  30. this.forceUpdate();
  31. })
  32. this.Keyboard = new Keyboard().listen();
  33. this.Keyboard.on('s', () => {
  34. this.ToolBox.selectTool('Select');
  35. this.forceUpdate();
  36. });
  37. this.Keyboard.on(46, () => {
  38. this.ToolBox.selectedNode && this.SideBar.removeNode(this.ToolBox.selectedNode);
  39. this.forceUpdate();
  40. });
  41. this.ToolBox = new ToolBox(this.CoreSystem);
  42. this.MainBar = new MainBar(this.CoreSystem);
  43. this.ModuleBar = new ModuleBar(this.CoreSystem); // Add CoreSystem deps
  44. this.SideBar = new SideBar(this.CoreSystem, this.ToolBox);
  45. this.Environment = new Environment(this.CoreSystem, this.ToolBox, this.phoneRef);
  46. this.Environment.onUpdate(() => this.forceUpdate())
  47. this.SideBar.onUpdate(() => this.forceUpdate())
  48. this.ToolBox.onUpdate( () => {
  49. this.disabledTools = !this.ToolBox.activeTool;
  50. this.CoreSystem.ActionSystem.toggle(!this.disabledTools);
  51. this.SideBar.tool = this.ToolBox.activeTool;
  52. this.forceUpdate();
  53. });
  54. this.ModuleBar.onUpdate(() => this.forceUpdate())
  55. this.MainBar.onUpdate(() => this.forceUpdate())
  56. this.MainBar.onSave( () => {
  57. this.saveEnv();
  58. this.forceUpdate()
  59. })
  60. this.load();
  61. // Setup mainContainer
  62. let cview = this.CoreSystem.getCurrentView();
  63. let container = cview.getDefaultContainer();
  64. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 375, height: 667});
  65. cview.setContent(mainContent, container);
  66. }
  67. onMount() {
  68. this.forceUpdate();
  69. if(this.phone){
  70. // thats fine
  71. this.ToolBoxMouse = new Mouse(this.toolbox.current);
  72. this.phoneMouse = new Mouse(this.phone.current).droppable();
  73. }
  74. if(this.ToolBoxMouse !== undefined){
  75. this.ToolBoxMouse.listen()
  76. .on('LeftDown',(e) => this.ToolManagement(e))
  77. this.phoneMouse.listen()
  78. .on('LeftDown',(e) => this.ToolManagement(e))
  79. .on('Drop', (e) => this.dropContent(e))
  80. }
  81. }
  82. saveEnv(){
  83. let env = this.CoreSystem.export();
  84. localStorage.setItem('environment', JSON.stringify(env));
  85. this.forceUpdate();
  86. try{
  87. this.CoreSystem.import(JSON.parse(js));
  88. this.forceUpdate();
  89. } catch(e) {
  90. console.log(e);
  91. }
  92. }
  93. ToolManagement(e){
  94. if(this.ToolBox.activeTool !== null && !this.disabledTools){
  95. this.processElement(e)
  96. }
  97. }
  98. load() {
  99. try{
  100. let env = localStorage.getItem('environment');
  101. if( env === null){
  102. // this.CoreSystem.import(JSON.parse(jsLoad));
  103. this.CoreSystem.fresh();
  104. }else{
  105. this.CoreSystem.import(JSON.parse(env));
  106. }
  107. this.forceUpdate();
  108. } catch(e) {
  109. console.log(e);
  110. }
  111. }
  112. processElement(event){
  113. let Node = this.CoreSystem.ray({event});
  114. let NewNode = this.ToolBox.editNode(Node);
  115. this.forceUpdate();
  116. }
  117. dropContent({event, data}) {
  118. data = JSON.parse(data);
  119. let targetNode = this.CoreSystem.ray({event});
  120. // targetNode.content
  121. targetNode.props.selected = true;
  122. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  123. let containerNode = new Node(Math.random(), {}, node);
  124. let View = this.CoreSystem.getCurrentView();
  125. View.addViewNode(containerNode, targetNode);
  126. this.forceUpdate();
  127. }
  128. mobileRender(){
  129. return this.Environment.render();
  130. }
  131. renderSideBar(){
  132. return this.SideBar.render();
  133. }
  134. renderMainBar(){
  135. return this.MainBar.render();
  136. }
  137. renderToolBox(){
  138. return (
  139. <div ref = {this.toolboxRef}>
  140. <View style={styles.toolbarView}>
  141. {this.ToolBox.render()}
  142. </View>
  143. </div>)
  144. }
  145. renderModuleBar(){
  146. return (
  147. <View >
  148. {this.ModuleBar.render()}
  149. </View>
  150. )
  151. }
  152. removeFrame(routeName) {
  153. this.CoreSystem.removePage(routeName);
  154. this.forceUpdate();
  155. }
  156. addFrame(routeName) {
  157. if(!routeName)
  158. routeName = "Page " + (Object.keys(this.CoreSystem.Routing.topology.nodes).length + 1);
  159. let VS = new Library.View();
  160. let container = VS.getDefaultContainer();
  161. container.props.selected = true;
  162. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 375, height: 667});
  163. VS.setContent(mainContent, container);
  164. this.CoreSystem.addPage(routeName,VS);
  165. this.CoreSystem.goto(routeName);
  166. this.forceUpdate()
  167. }
  168. renderPageBar(){
  169. let currentView = this.CoreSystem.Routing.getCurrentView();
  170. let pages = Object.keys(this.CoreSystem.Routing.topology.nodes).map((route,index) => {
  171. let view = this.CoreSystem.Routing.getView(route)
  172. let background = view === currentView ? PageButtons.activePage :(null)
  173. let textColor = view === currentView ? PageButtons.selectedText :(null)
  174. return (
  175. <View key={route} style={[PageButtons.page , background ] } onClick = {(e) => {
  176. console.log(e);
  177. this.CoreSystem.goto(route);
  178. this.forceUpdate()
  179. }}>
  180. <View style = {{ flex:1,
  181. justifyContent:'space-around',
  182. flexDirection: 'row',
  183. alignItems:'center'}}>
  184. <Text style={[PageButtons.text, textColor]}>{route}</Text>
  185. {view !== currentView && <Text style={[PageButtons.text, textColor, {color: '#77000066'}]} onClick={(e) => {
  186. this.removeFrame(route);
  187. e.stopPropagation();
  188. return false;
  189. }}>X</Text>}
  190. </View>
  191. </View>
  192. )
  193. })
  194. return (
  195. <View style={PageButtons.container}>
  196. <View style={{borderRightWidth:"1px",
  197. borderColor:"#B4b4b4",
  198. backgroundColor:'white', paddingTop: 4}}>
  199. <Icon name='filter' color="#36BBAD" />
  200. </View>
  201. {pages}
  202. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  203. <View style= {PageButtons.addNewContainer}>
  204. <Text style={PageButtons.addNewText}>Add new </Text>
  205. <Icon color="white" name='add' iconStyle={{fontSize: 12, paddingTop: 8}}/>
  206. </View>
  207. </View>
  208. </View>);
  209. }
  210. }
  211. const PageButtons = StyleSheet.create({
  212. container:{
  213. flex:1,
  214. flexDirection:'row',
  215. width:40
  216. },
  217. activePage:{
  218. backgroundColor:"#36BBAD"
  219. },
  220. text:{
  221. fontSize:14,
  222. letterSpacing: 0,
  223. color: '#D9D9D9',
  224. opacity: 1,
  225. fontFamily: 'light'
  226. },
  227. selectedText: {
  228. color: "#FFFFFF"
  229. },
  230. page:{
  231. borderRightWidth:"1px",
  232. borderColor:"#B4b4b4",
  233. backgroundColor:"#F8F8F8",
  234. cursor:'pointer',
  235. overflow:'hidden',
  236. width:160,
  237. height:48,
  238. },
  239. addNew:{
  240. backgroundColor:"#D6D6D6",
  241. cursor:'pointer',
  242. width:160,
  243. height:48,
  244. },
  245. addNewContainer:{
  246. flex:1,
  247. flexDirection:'row',
  248. justifyContent:"center",
  249. alignItems:'center'
  250. },
  251. addNewText:{
  252. fontSize:14,
  253. letterSpacing: 0,
  254. color: 'white',
  255. opacity: 1,
  256. fontFamily: 'thin'
  257. }
  258. })
  259. //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"}}}}}';
  260. //jsLoad Test
  261. const jsLoad = ``;
  262. const MudleBarStyle = StyleSheet.create({
  263. container:{
  264. backgroundColor:'red'
  265. }
  266. })
  267. const styles = StyleSheet.create({
  268. container: {
  269. flex: 1,
  270. backgroundColor: '#fff',
  271. alignItems: 'center',
  272. justifyContent: 'space-between',
  273. flexDirection: 'row'
  274. },
  275. toolbarView: {
  276. justifyContent: 'space-around',
  277. alignItems: 'center',
  278. padding: 16
  279. },
  280. mainView: {
  281. backgroundColor: '#065b69',
  282. alignSelf:'stretch',
  283. flex: 1,
  284. justifyContent: 'center',
  285. alignItems: 'center'
  286. },
  287. mobileView: {
  288. backgroundColor: 'white',
  289. borderWidth: 4,
  290. borderColor: 'orange',
  291. flex:1,
  292. alignSelf: 'stretch',
  293. },
  294. attributesView: {
  295. width:256,
  296. alignSelf: 'stretch',
  297. flexDirection: 'column',
  298. }
  299. });