SideBar.js 6.7 KB

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