Gui.js 9.2 KB

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