Gui.js 9.5 KB

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