SideBar.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. console.log(typeof value);
  61. onChange && onChange(value, key)
  62. }
  63. };
  64. switch(Structure.type){
  65. case DataTypes.Types.Integer:
  66. case DataTypes.Types.Bool:
  67. editorProps.number = viewNodeProps[key];
  68. break;
  69. case DataTypes.Types.Text:
  70. editorProps.text = viewNodeProps[key];
  71. break;
  72. default:
  73. //Must create a generic Vuiew Component
  74. return (<View></View>)
  75. }
  76. return <Editor {...editorProps} key={key}/>
  77. }
  78. render(){
  79. let ModuleSystem = this.CoreSystem.ModuleSystem;
  80. let CurrentView = this.CoreSystem.getCurrentView();
  81. let selectedNode = this.ToolBox.selectedNode;
  82. console.log("TOOLBOX" , this.ToolBox);
  83. let Structure = {},
  84. nodeStructure = {};
  85. let viewNodeProps = {},
  86. nodeProps = {};
  87. if (selectedNode && CurrentView.has(selectedNode)) {
  88. this.ContainerNode = selectedNode;
  89. if(selectedNode.content && selectedNode.content.value) {
  90. let ctor = ModuleSystem.fromViewNode(selectedNode.content);
  91. viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props);
  92. Structure = ctor.Inputs;
  93. }
  94. nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props);
  95. nodeStructure = selectedNode.ctor.Inputs;
  96. } else {
  97. this.ContainerNode = null;
  98. }
  99. let contentData = Object.keys(Structure || {}).map((key,index) =>
  100. <View key={selectedNode.id + key}>
  101. {this.createHandler(Structure[key], viewNodeProps, key, (v, f) =>
  102. this.editNode(selectedNode.content, v,f))}
  103. </View>
  104. )
  105. let containerData =Object.keys(nodeStructure || {}).map((key, index) =>
  106. <View key={selectedNode.id + key}>
  107. {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) =>
  108. this.editNode(selectedNode, v, f))}
  109. </View>
  110. );
  111. return (
  112. <View>
  113. <Text style={SideBarStyle.title}>Side Bar</Text>
  114. <View>
  115. {this.ContainerNode && <View key={this.ContainerNode.id}>
  116. <Text>Container</Text>
  117. <Button onPress={() => this.import()} title={'Import'}/>
  118. <Button onPress={() => this.export()} title={'Export'}/>
  119. <TextEditor title="Inner Columns"
  120. text={this.ContainerNode && CurrentView.getChildren(this.ContainerNode).length +''}
  121. onChange={(cols) => {
  122. alert("Not working");
  123. }}/>
  124. <Image name='near me'
  125. style={{width: 20, height: 20 , color:'#606060'}}
  126. source= {require('../assets/icon.png')}
  127. containerStyle={{}}
  128. onClick={() => this.removeNode(this.ContainerNode)}
  129. />
  130. </View>}
  131. {containerData}
  132. </View>
  133. <View style={PropertiesContainer.container}>
  134. {contentData}
  135. </View>
  136. </View>
  137. )
  138. }
  139. }
  140. const SelectedStyle = StyleSheet.create({
  141. container:{
  142. padding:5,
  143. borderWidth:2,
  144. borderColor:'green'
  145. }
  146. })
  147. const PropertiesContainer = StyleSheet.create({
  148. container:{
  149. marginTop:20,
  150. borderTopWidth:0.8,
  151. borderColor:'#D0D0D0'
  152. },
  153. child:{
  154. padding:10
  155. }
  156. })
  157. const SideBarStyle = StyleSheet.create({
  158. container:{
  159. padding:1,
  160. flex:1,
  161. flexDirection:'row'
  162. },
  163. title:{
  164. fontSize:20,
  165. textAlign:'center',
  166. fontFamily:'halvetica',
  167. color:'black',
  168. borderBottomWidth:1,
  169. borderBottomColor:'black'
  170. },
  171. body:{
  172. paddingLeft:5,
  173. paddingRight:5
  174. }
  175. })