SideBar.js 6.6 KB

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