Gui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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
  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. }else{
  92. this.CoreSystem.import(JSON.parse(env));
  93. }
  94. this.forceUpdate();
  95. } catch(e) {
  96. console.log(e);
  97. }
  98. }
  99. processElement(event){
  100. let Node = this.CoreSystem.ray({event});
  101. let NewNode = this.ToolBox.editNode(Node);
  102. this.forceUpdate();
  103. }
  104. dropContent({event, data}) {
  105. data = JSON.parse(data);
  106. let targetNode = this.CoreSystem.ray({event});
  107. // targetNode.content
  108. targetNode.props.selected = true;
  109. let node = new ViewNode(Math.random(), data.ctor, {}, data.namespace);
  110. let containerNode = new Node(Math.random(), {}, node);
  111. let View = this.CoreSystem.getCurrentView();
  112. View.addViewNode(containerNode, targetNode);
  113. this.forceUpdate();
  114. }
  115. mobileRender(){
  116. return this.Environment.render();
  117. }
  118. renderSideBar(){
  119. return this.SideBar.render();
  120. }
  121. renderMainBar(){
  122. return this.MainBar.render();
  123. }
  124. renderToolBox(){
  125. return (
  126. <div ref = {this.toolboxRef}>
  127. <View style={styles.toolbarView}>
  128. {this.ToolBox.render()}
  129. </View>
  130. </div>)
  131. }
  132. renderModuleBar(){
  133. return (
  134. <View >
  135. {this.ModuleBar.render()}
  136. </View>
  137. )
  138. }
  139. addFrame(routeName = Math.random().toString(36).substring(7)) {
  140. let VS = new Library.View();
  141. let container = VS.getDefaultContainer();
  142. container.props.selected = true;
  143. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 375, height: 667});
  144. VS.setContent(mainContent, container);
  145. this.CoreSystem.addPage(routeName,VS);
  146. this.forceUpdate()
  147. }
  148. renderPageBar(){
  149. let currentView = this.CoreSystem.Routing.getCurrentView();
  150. let pages = Object.keys(this.CoreSystem.Routing.topology.nodes).map((route,index) => {
  151. let view = this.CoreSystem.Routing.getView(route)
  152. let background = view === currentView ? PageButtons.activePage :(null)
  153. let textColor = view === currentView ? PageButtons.selectedText :(null)
  154. return (
  155. <View style={[PageButtons.page , background ] } onClick = {(e) => {
  156. this.CoreSystem.goto(route)
  157. this.forceUpdate()
  158. }}>
  159. <View style = {{ flex:1,
  160. justifyContent:'center',
  161. alignItems:'center'}}>
  162. <Text style={[PageButtons.text, textColor]}>{route}</Text>
  163. </View>
  164. </View>
  165. )
  166. })
  167. return (
  168. <View style={PageButtons.container}>
  169. <View style={{borderRightWidth:"1px",
  170. borderColor:"#B4b4b4",
  171. background:'white', paddingTop: 4}}>
  172. <Button
  173. color="white"
  174. title={<Icon name='filter' color="#36BBAD" />
  175. }
  176. />
  177. </View>
  178. {pages}
  179. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  180. <View style= {PageButtons.addNewContainer}>
  181. <Text style={PageButtons.addNewText}>Add new </Text>
  182. <Icon color="white" name='add' iconStyle={{fontSize: 12, paddingTop: 8}}/>
  183. </View>
  184. </View>
  185. </View>);
  186. }
  187. }
  188. const PageButtons = StyleSheet.create({
  189. container:{
  190. flex:1,
  191. flexDirection:'row',
  192. width:40
  193. },
  194. activePage:{
  195. backgroundColor:"#36BBAD",
  196. color:"white"
  197. },
  198. text:{
  199. fontSize:14,
  200. letterSpacing: 0,
  201. color: '#D9D9D9',
  202. opacity: 1,
  203. fontFamily: 'light'
  204. },
  205. selectedText: {
  206. color: "#FFFFFF"
  207. },
  208. page:{
  209. borderRightWidth:"1px",
  210. borderColor:"#B4b4b4",
  211. backgroundColor:"#F8F8F8",
  212. cursor:'pointer',
  213. textAlign:"center",
  214. overflow:'hidden',
  215. width:160,
  216. height:48,
  217. },
  218. addNew:{
  219. backgroundColor:"#D6D6D6",
  220. cursor:'pointer',
  221. width:160,
  222. height:48,
  223. },
  224. addNewContainer:{
  225. flex:1,
  226. flexDirection:'row',
  227. justifyContent:"center",
  228. alignItems:'center'
  229. },
  230. addNewText:{
  231. fontSize:14,
  232. letterSpacing: 0,
  233. color: 'white',
  234. opacity: 1,
  235. fontFamily: 'thin'
  236. }
  237. })
  238. //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"}}}}}';
  239. //jsLoad Test
  240. const jsLoad = '{"Routing":{"topology":{"nodes":{"test":{"id":"test","routeName":"test","uri":"test","view":"VS1","defaultLink":null}},"links":{"test":{"out":[],"in":[]}}},"home":"test"},"Views":{"views":{"VS1":{"tree":{"nodes":{"0.5907657036474692":{"id":0.5907657036474692,"depth":0,"isRow":true,"isCol":false},"0.51066596548815":{"id":0.51066596548815,"depth":1,"isRow":false,"isCol":true},"0.8855478722619232":{"id":0.8855478722619232,"depth":1,"isRow":false,"isCol":true,"content":{"id":"A","depth":null,"value":"RootComp","namespace":"default","props":{"text":"This is the Starting point!!!Try add on me ","style":{"container":22,"text":23}}}},"0.4630979404426283":{"id":0.4630979404426283,"depth":1,"isRow":false,"isCol":true},"0.7144981784945621":{"id":0.7144981784945621,"depth":2,"isRow":true,"isCol":false},"0.0666081688424911":{"id":0.0666081688424911,"depth":2,"isRow":true,"isCol":false}},"links":{"0.5907657036474692":{"out":[{"to":0.51066596548815,"from":0.5907657036474692},{"to":0.8855478722619232,"from":0.5907657036474692},{"to":0.4630979404426283,"from":0.5907657036474692}],"in":[]},"0.51066596548815":{"out":[],"in":[{"to":0.51066596548815,"from":0.5907657036474692}]},"0.8855478722619232":{"out":[{"to":0.7144981784945621,"from":0.8855478722619232},{"to":0.0666081688424911,"from":0.8855478722619232}],"in":[{"to":0.8855478722619232,"from":0.5907657036474692}]},"0.4630979404426283":{"out":[],"in":[{"to":0.4630979404426283,"from":0.5907657036474692}]},"0.7144981784945621":{"out":[],"in":[{"to":0.7144981784945621,"from":0.8855478722619232}]},"0.0666081688424911":{"out":[],"in":[{"to":0.0666081688424911,"from":0.8855478722619232}]}},"levels":[[0.5907657036474692],[0.51066596548815,0.8855478722619232,0.4630979404426283],[0.7144981784945621,0.0666081688424911]],"rootId":0.5907657036474692}}}}}'
  241. const MudleBarStyle = StyleSheet.create({
  242. container:{
  243. backgroundColor:'red'
  244. }
  245. })
  246. const styles = StyleSheet.create({
  247. container: {
  248. flex: 1,
  249. backgroundColor: '#fff',
  250. alignItems: 'center',
  251. justifyContent: 'space-between',
  252. flexDirection: 'row'
  253. },
  254. toolbarView: {
  255. justifyContent: 'space-around',
  256. alignItems: 'center',
  257. padding: 16
  258. },
  259. mainView: {
  260. backgroundColor: '#065b69',
  261. alignSelf:'stretch',
  262. flex: 1,
  263. justifyContent: 'center',
  264. alignItems: 'center'
  265. },
  266. mobileView: {
  267. backgroundColor: 'white',
  268. borderWidth: 4,
  269. borderColor: 'orange',
  270. flex:1,
  271. alignSelf: 'stretch',
  272. },
  273. attributesView: {
  274. width:256,
  275. alignSelf: 'stretch',
  276. flexDirection: 'column',
  277. }
  278. });