SideBar.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 Numbers from '../Components/Numbers';
  7. // import ColorEditor from '../Components/ColorEditor';
  8. import TypeHandler from '../Components/TypeHandler.js';
  9. import BaseSystem from './System';
  10. let {
  11. CoreSystem,
  12. ViewSystem,
  13. ViewNode,
  14. DataTypes
  15. } = Library;
  16. function download(filename, text) {
  17. var element = document.createElement('a');
  18. element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  19. element.setAttribute('download', filename);
  20. element.style.display = 'none';
  21. document.body.appendChild(element);
  22. element.click();
  23. document.body.removeChild(element);
  24. }
  25. export default class SideBar extends BaseSystem {
  26. constructor(CoreSystem, ToolBox){
  27. super();
  28. this.CoreSystem = CoreSystem;
  29. this.ToolBox = ToolBox;
  30. }
  31. import() {
  32. let CurrentView = this.CoreSystem.getCurrentView();
  33. let text = window.prompt("Feed me plz");
  34. if(text) {
  35. CurrentView.import(JSON.parse(text), this.ContainerNode.id);
  36. }
  37. }
  38. export() {
  39. let CurrentView = this.CoreSystem.getCurrentView();
  40. let data = CurrentView.export();
  41. download("Export_" + new Date().toISOString()+".txt", JSON.stringify(data));
  42. }
  43. removeNode(node) {
  44. let CurrentView = this.CoreSystem.getCurrentView();
  45. console.log(node)
  46. if (CurrentView.has(node)) {
  47. CurrentView.remove(node);
  48. }
  49. this.forceUpdate();
  50. }
  51. editNode(node, text,key){
  52. node.props[key] = text;
  53. this.forceUpdate();
  54. }
  55. createHandler(Structure, viewNodeProps, key, onChange) {
  56. let Editor = TypeHandler(Structure);
  57. let editorProps = {
  58. title: key,
  59. onChange: (value) => {
  60. onChange && onChange(value, key)
  61. }
  62. };
  63. switch(Structure.type){
  64. case DataTypes.Types.Array:
  65. editorProps.value = viewNodeProps[key];
  66. break;
  67. case DataTypes.Types.Integer:
  68. case DataTypes.Types.Bool:
  69. editorProps.number = viewNodeProps[key];
  70. break;
  71. case DataTypes.Types.Text:
  72. editorProps.text = viewNodeProps[key];
  73. break;
  74. default:
  75. //Must create a generic Vuiew Component
  76. return (<View></View>)
  77. }
  78. return <Editor {...editorProps} key={key}/>
  79. }
  80. render(){
  81. let ModuleSystem = this.CoreSystem.ModuleSystem;
  82. let CurrentView = this.CoreSystem.getCurrentView();
  83. let selectedNode = this.ToolBox.selectedNode;
  84. let Structure = {},
  85. styles = {},
  86. nodeStructure = {};
  87. let viewNodeProps = {},
  88. nodeProps = {};
  89. if (selectedNode && CurrentView.has(selectedNode)) {
  90. this.ContainerNode = selectedNode;
  91. if(selectedNode.content && selectedNode.content.value) {
  92. let ctor = ModuleSystem.fromViewNode(selectedNode.content);
  93. styles = ctor.Styles;
  94. viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
  95. Structure = ctor.Inputs;
  96. }
  97. nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
  98. nodeStructure = selectedNode.ctor.Inputs;
  99. } else {
  100. this.ContainerNode = null;
  101. }
  102. let StyleData = Object.keys(styles || {}).map((key,index) =>
  103. <option key={selectedNode.id + key} value = {key} selected={this._selectedStyle === key}>
  104. {key}
  105. </option>
  106. )
  107. let contentData = Object.keys(Structure || {}).map((key,index) =>
  108. <View key={selectedNode.id + key}>
  109. {this.createHandler(Structure[key], viewNodeProps, key, (v, f) =>
  110. this.editNode(selectedNode.content, v,f))}
  111. </View>
  112. )
  113. let containerData =Object.keys(nodeStructure || {}).map((key, index) =>
  114. <View key={selectedNode.id + key}>
  115. {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) =>
  116. this.editNode(selectedNode, v, f))}
  117. </View>
  118. );
  119. return (
  120. <View>
  121. <Text style={SideBarStyle.title}>Side Bar</Text>
  122. {StyleData.length > 0 ? (
  123. <View key={Math.random()}>
  124. <select onChange={(event) => {
  125. this._selectedStyle = event.target.value;
  126. this.ContainerNode.content.props = {...this.ContainerNode.content.props, ...styles[event.target.value]};
  127. this.forceUpdate();
  128. }}>
  129. {StyleData}
  130. </select>
  131. </View>):(null)}
  132. <View>
  133. {this.ContainerNode && <View key={this.ContainerNode.id}>
  134. <Text>Container</Text>
  135. <Button onPress={() => this.import()} title={'Import'}/>
  136. <Button onPress={() => this.export()} title={'Export'}/>
  137. <TextEditor title="Inner Columns"
  138. text={this.ContainerNode && CurrentView.getChildren(this.ContainerNode).length +''}
  139. onChange={(cols) => {
  140. alert("Not working");
  141. }}/>
  142. <Image name='near me'
  143. style={{width: 20, height: 20 , color:'#606060'}}
  144. source= {require('../assets/icon.png')}
  145. containerStyle={{}}
  146. onClick={() => this.removeNode(this.ContainerNode)}
  147. />
  148. </View>}
  149. {containerData}
  150. </View>
  151. <View style={PropertiesContainer.container}>
  152. {contentData}
  153. </View>
  154. </View>
  155. )
  156. }
  157. }
  158. const SelectedStyle = StyleSheet.create({
  159. container:{
  160. padding:5,
  161. borderWidth:2,
  162. borderColor:'green'
  163. }
  164. })
  165. const PropertiesContainer = StyleSheet.create({
  166. container:{
  167. marginTop:20,
  168. borderTopWidth:0.8,
  169. borderColor:'#D0D0D0'
  170. },
  171. child:{
  172. padding:10
  173. }
  174. })
  175. const SideBarStyle = StyleSheet.create({
  176. container:{
  177. padding:1,
  178. flex:1,
  179. flexDirection:'row',
  180. },
  181. title:{
  182. fontSize:20,
  183. textAlign:'center',
  184. fontFamily:'halvetica',
  185. color:'black',
  186. borderBottomWidth:1,
  187. borderBottomColor:'black'
  188. },
  189. body:{
  190. paddingLeft:5,
  191. paddingRight:5
  192. }
  193. })