Gui.js 9.2 KB

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