Gui.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 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. background:'white', paddingTop: 4}}>
  188. <Button
  189. color="white"
  190. title={<Icon name='filter' color="#36BBAD" />
  191. }
  192. />
  193. </View>
  194. {pages}
  195. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  196. <View style= {PageButtons.addNewContainer}>
  197. <Text style={PageButtons.addNewText}>Add new </Text>
  198. <Icon color="white" name='add' iconStyle={{fontSize: 12, paddingTop: 8}}/>
  199. </View>
  200. </View>
  201. </View>);
  202. }
  203. }
  204. const PageButtons = StyleSheet.create({
  205. container:{
  206. flex:1,
  207. flexDirection:'row',
  208. width:40
  209. },
  210. activePage:{
  211. backgroundColor:"#36BBAD",
  212. color:"white"
  213. },
  214. text:{
  215. fontSize:14,
  216. letterSpacing: 0,
  217. color: '#D9D9D9',
  218. opacity: 1,
  219. fontFamily: 'light'
  220. },
  221. selectedText: {
  222. color: "#FFFFFF"
  223. },
  224. page:{
  225. borderRightWidth:"1px",
  226. borderColor:"#B4b4b4",
  227. backgroundColor:"#F8F8F8",
  228. cursor:'pointer',
  229. textAlign:"center",
  230. overflow:'hidden',
  231. width:160,
  232. height:48,
  233. },
  234. addNew:{
  235. backgroundColor:"#D6D6D6",
  236. cursor:'pointer',
  237. width:160,
  238. height:48,
  239. },
  240. addNewContainer:{
  241. flex:1,
  242. flexDirection:'row',
  243. justifyContent:"center",
  244. alignItems:'center'
  245. },
  246. addNewText:{
  247. fontSize:14,
  248. letterSpacing: 0,
  249. color: 'white',
  250. opacity: 1,
  251. fontFamily: 'thin'
  252. }
  253. })
  254. //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"}}}}}';
  255. //jsLoad Test
  256. const jsLoad = ``;
  257. const MudleBarStyle = StyleSheet.create({
  258. container:{
  259. backgroundColor:'red'
  260. }
  261. })
  262. const styles = StyleSheet.create({
  263. container: {
  264. flex: 1,
  265. backgroundColor: '#fff',
  266. alignItems: 'center',
  267. justifyContent: 'space-between',
  268. flexDirection: 'row'
  269. },
  270. toolbarView: {
  271. justifyContent: 'space-around',
  272. alignItems: 'center',
  273. padding: 16
  274. },
  275. mainView: {
  276. backgroundColor: '#065b69',
  277. alignSelf:'stretch',
  278. flex: 1,
  279. justifyContent: 'center',
  280. alignItems: 'center'
  281. },
  282. mobileView: {
  283. backgroundColor: 'white',
  284. borderWidth: 4,
  285. borderColor: 'orange',
  286. flex:1,
  287. alignSelf: 'stretch',
  288. },
  289. attributesView: {
  290. width:256,
  291. alignSelf: 'stretch',
  292. flexDirection: 'column',
  293. }
  294. });