import React from 'react'; import {StyleSheet, View, Text} from 'react-native'; import Library from 'trapilib/dist/lib'; import { TextInput, Image, Button } from 'react-native'; import TextEditor from '../Components/TextEditor'; import Alignment from '../Components/Alignment'; import ContainerEditor from '../Components/ContainerEditor'; // import Numbers from '../Components/Numbers'; // import ColorEditor from '../Components/ColorEditor'; import TypeHandler from '../Components/TypeHandler.js'; import BaseSystem from './System'; let { CoreSystem, ViewSystem, ViewNode, DataTypes } = Library; function download(filename, text) { var element = document.createElement('a'); element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text)); element.setAttribute('download', filename); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); } export default class SideBar extends BaseSystem { constructor(CoreSystem, ToolBox){ super(); this.CoreSystem = CoreSystem; this.ToolBox = ToolBox; this.showAllData = false; } toggleAll() { this.showAllData = !this.showAllData; this.forceUpdate(); } import() { let CurrentView = this.CoreSystem.getCurrentView(); let text = window.prompt("Feed me plz"); if(text) { CurrentView.import(JSON.parse(text), this.ContainerNode.id); } } export() { let CurrentView = this.CoreSystem.getCurrentView(); let data = CurrentView.export(); download("Export_" + new Date().toISOString()+".txt", JSON.stringify(data)); } removeNode(node) { let CurrentView = this.CoreSystem.getCurrentView(); console.log(node) if (CurrentView.has(node)) { CurrentView.remove(node); } this.forceUpdate(); } editNode(node, text,key){ node.props[key] = text; this.forceUpdate(); } editNodeStyle(node, style) { node.props = { ...node.props, ...style } this.forceUpdate(); } createHandler(Structure, viewNodeProps, key, onChange) { let Editor = TypeHandler(Structure); let editorProps = { title: key, onChange: (value) => { onChange && onChange(value, key) } }; switch(Structure.type){ case DataTypes.Types.Array: editorProps.value = viewNodeProps[key]; break; case DataTypes.Types.Integer: case DataTypes.Types.Real: editorProps.number = viewNodeProps[key]; break; case DataTypes.Types.Boolean: editorProps.checked = viewNodeProps[key] === 'true' || viewNodeProps[key] == true; break; case DataTypes.Types.Text: editorProps.text = viewNodeProps[key]; break; default: //Must create a generic Vuiew Component return () } return } render(){ let ModuleSystem = this.CoreSystem.ModuleSystem; let CurrentView = this.CoreSystem.getCurrentView(); let selectedNode = this.ToolBox.selectedNode; let Structure = {}, styles = {}, nodeStructure = {}; let viewNodeProps = {}, nodeProps = {}; if (selectedNode && CurrentView.has(selectedNode)) { this.ContainerNode = selectedNode; if(selectedNode.content && selectedNode.content.value) { let ctor = ModuleSystem.fromViewNode(selectedNode.content); styles = ctor.Styles; viewNodeProps = ModuleSystem.validateProps(ctor, selectedNode.content.props); Structure = ctor.Inputs; } nodeProps = ModuleSystem.validateProps(selectedNode.ctor, selectedNode.props); nodeStructure = selectedNode.ctor.Inputs; } else { this.ContainerNode = null; } let StyleData = Object.keys(styles || {}).map((key,index) => ) let contentData = Object.keys(Structure || {}).map((key,index) => {this.createHandler(Structure[key], viewNodeProps, key, (v, f) => this.editNode(selectedNode.content, v,f))} ) let containerData =Object.keys(nodeStructure || {}).map((key, index) => {this.createHandler(nodeStructure[key], nodeProps, key, (v,f) => this.editNode(selectedNode, v, f))} ); return ( {this.ContainerNode ? [ this.editNodeStyle(selectedNode, style)} />, this.editNode(selectedNode, v, k)}/>, {contentData} , this.toggleAll()}>Show All ] : Click on elements} {this.showAllData ? {StyleData.length > 0 ? ( ):(null)} {this.ContainerNode && Container