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