SideBar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. console.log("YOUR LOOKINGH")
  94. console.log(ctor)
  95. styles = ctor.Styles;
  96. viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
  97. Structure = ctor.Inputs;
  98. }
  99. nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
  100. nodeStructure = selectedNode.ctor.Inputs;
  101. } else {
  102. this.ContainerNode = null;
  103. }
  104. let StyleData = Object.keys(styles || {}).map((key,index) =>
  105. <option key={selectedNode.id + key} value = {key} selected={this._selectedStyle === key}>
  106. {key}
  107. </option>
  108. )
  109. console.log("@@@@@@@@@@@@@@@@@@")
  110. console.log(StyleData)
  111. console.log(styles)
  112. console.log("!!!!!!!!!!!!!!!!!!!!")
  113. let contentData = Object.keys(Structure || {}).map((key,index) =>
  114. <View key={selectedNode.id + key}>
  115. {this.createHandler(Structure[key], viewNodeProps, key, (v, f) =>
  116. this.editNode(selectedNode.content, v,f))}
  117. </View>
  118. )
  119. let containerData =Object.keys(nodeStructure || {}).map((key, index) =>
  120. <View key={selectedNode.id + key}>
  121. {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) =>
  122. this.editNode(selectedNode, v, f))}
  123. </View>
  124. );
  125. return (
  126. <View>
  127. <Text style={SideBarStyle.title}>Side Bar</Text>
  128. <View key={Math.random()}>
  129. <select onChange={(event) => {
  130. this._selectedStyle = event.target.value;
  131. this.ContainerNode.content.props = {...this.ContainerNode.content.props, ...styles[event.target.value]};
  132. this.forceUpdate();
  133. }}>
  134. {StyleData}
  135. </select>
  136. </View>
  137. <View>
  138. {this.ContainerNode && <View key={this.ContainerNode.id}>
  139. <Text>Container</Text>
  140. <Button onPress={() => this.import()} title={'Import'}/>
  141. <Button onPress={() => this.export()} title={'Export'}/>
  142. <TextEditor title="Inner Columns"
  143. text={this.ContainerNode && CurrentView.getChildren(this.ContainerNode).length +''}
  144. onChange={(cols) => {
  145. alert("Not working");
  146. }}/>
  147. <Image name='near me'
  148. style={{width: 20, height: 20 , color:'#606060'}}
  149. source= {require('../assets/icon.png')}
  150. containerStyle={{}}
  151. onClick={() => this.removeNode(this.ContainerNode)}
  152. />
  153. </View>}
  154. {containerData}
  155. </View>
  156. <View style={PropertiesContainer.container}>
  157. {contentData}
  158. </View>
  159. </View>
  160. )
  161. }
  162. }
  163. const SelectedStyle = StyleSheet.create({
  164. container:{
  165. padding:5,
  166. borderWidth:2,
  167. borderColor:'green'
  168. }
  169. })
  170. const PropertiesContainer = StyleSheet.create({
  171. container:{
  172. marginTop:20,
  173. borderTopWidth:0.8,
  174. borderColor:'#D0D0D0'
  175. },
  176. child:{
  177. padding:10
  178. }
  179. })
  180. const SideBarStyle = StyleSheet.create({
  181. container:{
  182. padding:1,
  183. flex:1,
  184. flexDirection:'row'
  185. },
  186. title:{
  187. fontSize:20,
  188. textAlign:'center',
  189. fontFamily:'halvetica',
  190. color:'black',
  191. borderBottomWidth:1,
  192. borderBottomColor:'black'
  193. },
  194. body:{
  195. paddingLeft:5,
  196. paddingRight:5
  197. }
  198. })