SideBar.js 6.8 KB

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