SideBar.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React from 'react';
  2. import {StyleSheet, View, Text} from 'react-native';
  3. import Library from 'trapilib/dist/lib';
  4. import { TextInput, Image, Button } from 'react-native';
  5. import TextEditor from '../Components/TextEditor';
  6. import Alignment from '../Components/Alignment';
  7. import ContainerEditor from '../Components/ContainerEditor';
  8. // import Numbers from '../Components/Numbers';
  9. // import ColorEditor from '../Components/ColorEditor';
  10. import TypeHandler from '../Components/TypeHandler.js';
  11. import BaseSystem from './System';
  12. let {
  13. CoreSystem,
  14. ViewSystem,
  15. ViewNode,
  16. DataTypes
  17. } = Library;
  18. function download(filename, text) {
  19. var element = document.createElement('a');
  20. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  21. element.setAttribute('download', filename);
  22. element.style.display = 'none';
  23. document.body.appendChild(element);
  24. element.click();
  25. document.body.removeChild(element);
  26. }
  27. export default class SideBar extends BaseSystem {
  28. constructor(CoreSystem, ToolBox){
  29. super();
  30. this.CoreSystem = CoreSystem;
  31. this.ToolBox = ToolBox;
  32. this.showAllData = false;
  33. }
  34. toggleAll() {
  35. this.showAllData = !this.showAllData;
  36. this.forceUpdate();
  37. }
  38. import() {
  39. let CurrentView = this.CoreSystem.getCurrentView();
  40. let text = window.prompt("Feed me plz");
  41. if(text) {
  42. CurrentView.import(JSON.parse(text), this.ContainerNode.id);
  43. }
  44. }
  45. export() {
  46. let CurrentView = this.CoreSystem.getCurrentView();
  47. let data = CurrentView.export();
  48. download("Export_" + new Date().toISOString()+".txt", JSON.stringify(data));
  49. }
  50. removeNode(node) {
  51. let CurrentView = this.CoreSystem.getCurrentView();
  52. console.log(node)
  53. if (CurrentView.has(node)) {
  54. CurrentView.remove(node);
  55. }
  56. this.forceUpdate();
  57. }
  58. editNode(node, text, key){
  59. node.props[key] = text;
  60. this.forceUpdate();
  61. }
  62. editNodeStyle(node, style) {
  63. node.props = {
  64. ...node.props,
  65. ...style
  66. }
  67. this.forceUpdate();
  68. }
  69. createHandler(Structure, viewNodeProps, key, onChange) {
  70. let Editor = TypeHandler(Structure);
  71. let editorProps = {
  72. title: key,
  73. onChange: (value) => {
  74. onChange && onChange(value, key)
  75. }
  76. };
  77. let val = viewNodeProps[key];
  78. switch(Structure.type){
  79. case DataTypes.Types.Integer:
  80. case DataTypes.Types.Real:
  81. case DataTypes.Types.Text:
  82. case DataTypes.Types.Action:
  83. case DataTypes.Types.Module:
  84. editorProps.value = val;
  85. break;
  86. case DataTypes.Types.Boolean:
  87. editorProps.value = val === 'true' || val == true;
  88. break;
  89. case DataTypes.Types.Array:
  90. editorProps.value = val;
  91. editorProps.template = Structure.Template;
  92. break;
  93. default:
  94. //Must create a generic Vuiew Component
  95. return (<View></View>)
  96. }
  97. return <Editor {...editorProps} key={key}/>
  98. }
  99. render(){
  100. let ModuleSystem = this.CoreSystem.ModuleSystem;
  101. let CurrentView = this.CoreSystem.getCurrentView();
  102. let selectedNode = this.ToolBox.selectedNode;
  103. let Structure = {},
  104. styles = {},
  105. nodeStructure = {};
  106. let viewNodeProps = {},
  107. nodeProps = {};
  108. if (selectedNode && CurrentView.has(selectedNode)) {
  109. this.ContainerNode = selectedNode;
  110. if(selectedNode.content && selectedNode.content.value) {
  111. let ctor = ModuleSystem.fromViewNode(selectedNode.content);
  112. styles = ctor.Styles;
  113. viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
  114. Structure = ctor.Inputs;
  115. }
  116. nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
  117. nodeStructure = selectedNode.ctor.Inputs;
  118. } else {
  119. this.ContainerNode = null;
  120. }
  121. let StyleData = Object.keys(styles || {}).map((key,index) =>
  122. <option key={selectedNode.id + key} value = {key} selected={this._selectedStyle === key}>
  123. {key}
  124. </option>
  125. )
  126. let contentData = Object.keys(Structure || {}).map((key,index) =>
  127. <View key={selectedNode.id + key} style={{padding:8}}>
  128. {this.createHandler(Structure[key], viewNodeProps, key, (v, f) =>
  129. this.editNode(selectedNode.content, v,f))}
  130. </View>
  131. )
  132. let containerData =Object.keys(nodeStructure || {}).map((key, index) =>
  133. <View key={selectedNode.id + key} >
  134. {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) =>
  135. this.editNode(selectedNode, v, f))}
  136. </View>
  137. );
  138. return (
  139. <View>
  140. {this.ContainerNode ?
  141. [
  142. <Alignment key={'Alignment'} onSelect={(style) => this.editNodeStyle(selectedNode, style)} />,
  143. <ContainerEditor key={'ContainerEditor'} structure={Structure}
  144. nodeProps={nodeProps}
  145. key={this.ContainerNode.id}
  146. onChange={(k,v) => this.editNode(selectedNode, v, k)}/>,
  147. <View style={PropertiesContainer.container} key={'ContentData'}>
  148. {contentData}
  149. </View>,
  150. <View key={'ShowAll'}>
  151. <Text style={SideBarStyle.title} onClick={() => this.toggleAll()}>Show All</Text>
  152. </View>
  153. ]
  154. : <Text style={{padding: 24}}>Click on elements</Text>}
  155. {this.showAllData ?
  156. <View>
  157. {StyleData.length > 0 ? (
  158. <View key={Math.random()}>
  159. <select onChange={(event) => {
  160. this._selectedStyle = event.target.value;
  161. this.ContainerNode.content.props = {...this.ContainerNode.content.props, ...styles[event.target.value]};
  162. this.forceUpdate();
  163. }}>
  164. {StyleData}
  165. </select>
  166. </View>):(null)}
  167. <View>
  168. {this.ContainerNode && <View key={this.ContainerNode.id}>
  169. <Text>Container</Text>
  170. <Button onPress={() => this.import()} title={'Import'}/>
  171. <Button onPress={() => this.export()} title={'Export'}/>
  172. <Image name='near me'
  173. style={{width: 20, height: 20}}
  174. source= {require('../assets/icon.png')}
  175. containerStyle={{}}
  176. onClick={() => this.removeNode(this.ContainerNode)}
  177. />
  178. </View>}
  179. {containerData}
  180. </View>
  181. <View style={PropertiesContainer.container}>
  182. {contentData}
  183. </View>
  184. </View>
  185. : null}
  186. </View>
  187. )
  188. }
  189. }
  190. const SelectedStyle = StyleSheet.create({
  191. container:{
  192. padding:5,
  193. borderWidth:2,
  194. borderColor:'green'
  195. }
  196. })
  197. const PropertiesContainer = StyleSheet.create({
  198. container:{
  199. marginTop:20,
  200. borderColor:'#D0D0D0'
  201. },
  202. child:{
  203. padding:10
  204. }
  205. })
  206. const SideBarStyle = StyleSheet.create({
  207. container:{
  208. padding:1,
  209. flex:1,
  210. flexDirection:'row',
  211. },
  212. title: {
  213. padding: 24,
  214. fontSize: 12,
  215. letterSpacing: 0.5,
  216. color: "#BFBFBF",
  217. opacity: 1,
  218. cursor: 'pointer'
  219. },
  220. body:{
  221. paddingLeft:5,
  222. paddingRight:5
  223. }
  224. })