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. switch(Structure.type){
  78. case DataTypes.Types.Array:
  79. editorProps.value = viewNodeProps[key];
  80. break;
  81. case DataTypes.Types.Integer:
  82. case DataTypes.Types.Real:
  83. case DataTypes.Types.Bool:
  84. editorProps.number = viewNodeProps[key];
  85. break;
  86. case DataTypes.Types.Text:
  87. editorProps.text = viewNodeProps[key];
  88. break;
  89. default:
  90. //Must create a generic Vuiew Component
  91. return (<View></View>)
  92. }
  93. return <Editor {...editorProps} key={key}/>
  94. }
  95. render(){
  96. let ModuleSystem = this.CoreSystem.ModuleSystem;
  97. let CurrentView = this.CoreSystem.getCurrentView();
  98. let selectedNode = this.ToolBox.selectedNode;
  99. let Structure = {},
  100. styles = {},
  101. nodeStructure = {};
  102. let viewNodeProps = {},
  103. nodeProps = {};
  104. if (selectedNode && CurrentView.has(selectedNode)) {
  105. this.ContainerNode = selectedNode;
  106. if(selectedNode.content && selectedNode.content.value) {
  107. let ctor = ModuleSystem.fromViewNode(selectedNode.content);
  108. styles = ctor.Styles;
  109. viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
  110. Structure = ctor.Inputs;
  111. }
  112. nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
  113. nodeStructure = selectedNode.ctor.Inputs;
  114. } else {
  115. this.ContainerNode = null;
  116. }
  117. let StyleData = Object.keys(styles || {}).map((key,index) =>
  118. <option key={selectedNode.id + key} value = {key} selected={this._selectedStyle === key}>
  119. {key}
  120. </option>
  121. )
  122. let contentData = Object.keys(Structure || {}).map((key,index) =>
  123. <View key={selectedNode.id + key} >
  124. {this.createHandler(Structure[key], viewNodeProps, key, (v, f) =>
  125. this.editNode(selectedNode.content, v,f))}
  126. </View>
  127. )
  128. let containerData =Object.keys(nodeStructure || {}).map((key, index) =>
  129. <View key={selectedNode.id + key} >
  130. {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) =>
  131. this.editNode(selectedNode, v, f))}
  132. </View>
  133. );
  134. return (
  135. <View>
  136. {this.ContainerNode ?
  137. [
  138. <Alignment onSelect={(style) => this.editNodeStyle(selectedNode, style)} />,
  139. <ContainerEditor structure={Structure}
  140. nodeProps={nodeProps}
  141. key={this.ContainerNode.id}
  142. onChange={(k,v) => this.editNode(selectedNode, v, k)}/>,
  143. <View style={PropertiesContainer.container}>
  144. {contentData}
  145. </View>,
  146. <View style={SideBarStyle.title}>
  147. <Text style={{cursor: 'pointer'}} onClick={() => this.toggleAll()}>Show All</Text>
  148. </View>
  149. ]
  150. : <Text style={{padding: 24}}>Click on elements</Text>}
  151. {this.showAllData ?
  152. <View>
  153. {StyleData.length > 0 ? (
  154. <View key={Math.random()}>
  155. <select onChange={(event) => {
  156. this._selectedStyle = event.target.value;
  157. this.ContainerNode.content.props = {...this.ContainerNode.content.props, ...styles[event.target.value]};
  158. this.forceUpdate();
  159. }}>
  160. {StyleData}
  161. </select>
  162. </View>):(null)}
  163. <View>
  164. {this.ContainerNode && <View key={this.ContainerNode.id}>
  165. <Text>Container</Text>
  166. <Button onPress={() => this.import()} title={'Import'}/>
  167. <Button onPress={() => this.export()} title={'Export'}/>
  168. <TextEditor title="Inner Columns"
  169. text={this.ContainerNode && CurrentView.getChildren(this.ContainerNode).length +''}
  170. onChange={(cols) => {
  171. alert("Not working");
  172. }}/>
  173. <Image name='near me'
  174. style={{width: 20, height: 20 , color:'#606060'}}
  175. source= {require('../assets/icon.png')}
  176. containerStyle={{}}
  177. onClick={() => this.removeNode(this.ContainerNode)}
  178. />
  179. </View>}
  180. {containerData}
  181. </View>
  182. <View style={PropertiesContainer.container}>
  183. {contentData}
  184. </View>
  185. </View>
  186. : null}
  187. </View>
  188. )
  189. }
  190. }
  191. const SelectedStyle = StyleSheet.create({
  192. container:{
  193. padding:5,
  194. borderWidth:2,
  195. borderColor:'green'
  196. }
  197. })
  198. const PropertiesContainer = StyleSheet.create({
  199. container:{
  200. marginTop:20,
  201. borderColor:'#D0D0D0'
  202. },
  203. child:{
  204. padding:10
  205. }
  206. })
  207. const SideBarStyle = StyleSheet.create({
  208. container:{
  209. padding:1,
  210. flex:1,
  211. flexDirection:'row',
  212. },
  213. title: {
  214. padding: 24,
  215. fontSize: 12,
  216. letterSpacing: 0.5,
  217. color: "#BFBFBF",
  218. opacity: 1
  219. },
  220. body:{
  221. paddingLeft:5,
  222. paddingRight:5
  223. }
  224. })