SideBar.js 5.0 KB

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