Gui.js 11 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. }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. removeFrame(routeName) {
  140. this.CoreSystem.removePage(routeName);
  141. this.forceUpdate();
  142. }
  143. addFrame(routeName) {
  144. if(!routeName)
  145. routeName = "Page " + (Object.keys(this.CoreSystem.Routing.topology.nodes).length + 1);
  146. let VS = new Library.View();
  147. let container = VS.getDefaultContainer();
  148. container.props.selected = true;
  149. let mainContent = new ViewNode(Math.random(), 'BaseContainer', {width: 375, height: 667});
  150. VS.setContent(mainContent, container);
  151. this.CoreSystem.addPage(routeName,VS);
  152. this.CoreSystem.goto(routeName);
  153. this.forceUpdate()
  154. }
  155. renderPageBar(){
  156. let currentView = this.CoreSystem.Routing.getCurrentView();
  157. let pages = Object.keys(this.CoreSystem.Routing.topology.nodes).map((route,index) => {
  158. let view = this.CoreSystem.Routing.getView(route)
  159. let background = view === currentView ? PageButtons.activePage :(null)
  160. let textColor = view === currentView ? PageButtons.selectedText :(null)
  161. return (
  162. <View style={[PageButtons.page , background ] } onClick = {(e) => {
  163. console.log(e);
  164. this.CoreSystem.goto(route);
  165. this.forceUpdate()
  166. }}>
  167. <View style = {{ flex:1,
  168. justifyContent:'space-around',
  169. flexDirection: 'row',
  170. alignItems:'center'}}>
  171. <Text style={[PageButtons.text, textColor]}>{route}</Text>
  172. {view !== currentView && <Text style={[PageButtons.text, textColor, {color: '#77000066'}]} onClick={(e) => {
  173. this.removeFrame(route);
  174. e.stopPropagation();
  175. return false;
  176. }}>X</Text>}
  177. </View>
  178. </View>
  179. )
  180. })
  181. return (
  182. <View style={PageButtons.container}>
  183. <View style={{borderRightWidth:"1px",
  184. borderColor:"#B4b4b4",
  185. background:'white', paddingTop: 4}}>
  186. <Button
  187. color="white"
  188. title={<Icon name='filter' color="#36BBAD" />
  189. }
  190. />
  191. </View>
  192. {pages}
  193. <View style={PageButtons.addNew} onClick = {() => this.addFrame()}>
  194. <View style= {PageButtons.addNewContainer}>
  195. <Text style={PageButtons.addNewText}>Add new </Text>
  196. <Icon color="white" name='add' iconStyle={{fontSize: 12, paddingTop: 8}}/>
  197. </View>
  198. </View>
  199. </View>);
  200. }
  201. }
  202. const PageButtons = StyleSheet.create({
  203. container:{
  204. flex:1,
  205. flexDirection:'row',
  206. width:40
  207. },
  208. activePage:{
  209. backgroundColor:"#36BBAD",
  210. color:"white"
  211. },
  212. text:{
  213. fontSize:14,
  214. letterSpacing: 0,
  215. color: '#D9D9D9',
  216. opacity: 1,
  217. fontFamily: 'light'
  218. },
  219. selectedText: {
  220. color: "#FFFFFF"
  221. },
  222. page:{
  223. borderRightWidth:"1px",
  224. borderColor:"#B4b4b4",
  225. backgroundColor:"#F8F8F8",
  226. cursor:'pointer',
  227. textAlign:"center",
  228. overflow:'hidden',
  229. width:160,
  230. height:48,
  231. },
  232. addNew:{
  233. backgroundColor:"#D6D6D6",
  234. cursor:'pointer',
  235. width:160,
  236. height:48,
  237. },
  238. addNewContainer:{
  239. flex:1,
  240. flexDirection:'row',
  241. justifyContent:"center",
  242. alignItems:'center'
  243. },
  244. addNewText:{
  245. fontSize:14,
  246. letterSpacing: 0,
  247. color: 'white',
  248. opacity: 1,
  249. fontFamily: 'thin'
  250. }
  251. })
  252. //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"}}}}}';
  253. //jsLoad Test
  254. 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}}}}}'
  255. const MudleBarStyle = StyleSheet.create({
  256. container:{
  257. backgroundColor:'red'
  258. }
  259. })
  260. const styles = StyleSheet.create({
  261. container: {
  262. flex: 1,
  263. backgroundColor: '#fff',
  264. alignItems: 'center',
  265. justifyContent: 'space-between',
  266. flexDirection: 'row'
  267. },
  268. toolbarView: {
  269. justifyContent: 'space-around',
  270. alignItems: 'center',
  271. padding: 16
  272. },
  273. mainView: {
  274. backgroundColor: '#065b69',
  275. alignSelf:'stretch',
  276. flex: 1,
  277. justifyContent: 'center',
  278. alignItems: 'center'
  279. },
  280. mobileView: {
  281. backgroundColor: 'white',
  282. borderWidth: 4,
  283. borderColor: 'orange',
  284. flex:1,
  285. alignSelf: 'stretch',
  286. },
  287. attributesView: {
  288. width:256,
  289. alignSelf: 'stretch',
  290. flexDirection: 'column',
  291. }
  292. });