Gui.js 9.5 KB

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