ModuleUtilities.js 10 KB

12345
  1. import React from'react';import Types from'./Types.js';import Logger from'../systems/Logger';import{FontAwesomeIcon}from'@fortawesome/react-fontawesome';import{Card,CardTitle,Icon,Input,Button}from'react-materialize';import ReactJson from'react-json-view';import Dependencies from'../systems/Dependencies';export class RemoveMarker extends React.Component{constructor(a){super(a)}drop(){this.props.onRemove&&this.props.onRemove()}render(){let a=localStorage.getItem('AdminMode');return'admin'===a?React.createElement('div',{className:'btn btn-flat '+this.props.className,onClick:()=>this.drop()},React.createElement('div',{className:'row'},React.createElement('div',{className:'col s2'},React.createElement('div',{className:'RemoveMarker'})),React.createElement('div',{className:'col s5'},React.createElement('span',null,'Remove')))):''}}export class InputMarker extends React.Component{constructor(a){super(a)}drop(){return window._selectedModule?void(this.props.onDrop&&this.props.onDrop(window._selectedModule)):console.log('No selected module!')}render(){return React.createElement('div',{className:'btn btn-flat '+(this.props.className||''),onClick:()=>this.drop(),onDragOver:a=>a.preventDefault(),onDrop:()=>this.drop()},React.createElement('div',{className:'InputMarker'}),React.createElement('span',null,this.props.name||'No NAME'))}}// Blueball Corporation Output marker handling for green light
  2. let OutputMarkers=[];export class OutputMarker extends React.Component{constructor(a){super(a)}componentDidMount(){OutputMarkers.push(this)}componentWillUnmount(){let a=OutputMarkers.indexOf(this);-1<a&&OutputMarkers.splice(a,1)}drop(a){a.stopPropagation(),this.props.module?(window._selectedModule=this.props.module,OutputMarkers.map(a=>a.forceUpdate())):console.log('No module bind on this marker')}_dragStart(a){this.drop(a)}render(){let a=this.props.module===window._selectedModule?'selected':'';return React.createElement('div',{className:'row left-align',style:{margin:0}},React.createElement('div',{className:'col s2'},React.createElement('div',{className:'OutputMarker '+a,onClick:a=>this.drop(a),draggable:'true',onDragStart:a=>this._dragStart(a)})),React.createElement('div',{className:'col s10'},this.props.text))}}export class OutputLink extends Dependencies{constructor(a){super(a)}drag(){this.LinkManager.selectOutput(this.props.output),this.props.onUpdate&&this.props.onUpdate(),this.forceUpdate()}render(){// console.log(this.props.output)
  3. return this.props.output?React.createElement('div',{className:'row Output'},React.createElement('div',{className:'col s2'},React.createElement('div',{className:'OutputMarker',onClick:()=>this.drag(),draggable:'true',onDragStart:()=>this.drag()})),React.createElement('div',{className:'col s5'},this.props.output.name),React.createElement('div',{className:'col s5'},React.createElement('button',{tabIndex:'-1',type:'button',className:'btn btn-secondary clearLinks',onClick:()=>{this.LinkManager.breakOutputLinks(this.props.output),this.forceUpdate()}},React.createElement('span',null,this.LinkManager.getOutputConnections(this.props.output).length),React.createElement(Icon,{tiny:!0},'leak_remove')))):''}}export class InputLink extends Dependencies{constructor(a){super(a)}drop(){// console.log("Input drop", this.props.input);
  4. this.LinkManager.connectSelected(this.props.input),this.props.onUpdate&&this.props.onUpdate(),this.forceUpdate()}render(){return this.props.input?React.createElement('div',{className:'row Input'},React.createElement('div',{className:'col s2'},React.createElement('div',{className:'InputMarker',onClick:()=>this.drop(),onDrop:()=>this.drop()})),React.createElement('div',{className:'col s5'},this.props.input.name),React.createElement('div',{className:'col s5'},React.createElement('button',{tabIndex:-1,type:'button',className:'btn btn-secondary clearLinks',onClick:()=>{this.LinkManager.breakInputLinks(this.props.input),this.forceUpdate()}},React.createElement('span',null,this.LinkManager.getInputConnections(this.props.input).length),React.createElement(Icon,{tiny:!0},'leak_remove')))):''}}export class MakeView extends React.Component{constructor(a){super(a),this.state={view:a.module.view.value||!1}}setViewUrl(a){//console.log(url);
  5. this.props.module._change('view',a)}render(){return this.state.view?React.createElement('div',{className:'form-group'},React.createElement('label',null,'View'),React.createElement('input',{type:'text',placeholder:'View',onChange:a=>{''===a.target.value&&this.setViewUrl(''),this.setState({view:a.target.value})},className:'form-control form-control-sm',value:this.state.view,onKeyPress:a=>{'Enter'===a.key&&this.setViewUrl(this.state.view)}})):React.createElement('button',{className:'form-control form-control-sm',onClick:()=>this.setState({view:!0})},'MakeView')}}export class ArrayInput extends React.Component{constructor(a){super(a),this.state={newdata:null,text:''},this.updateType(this.props.input.arrType)}updateType(a){switch(this.Type=a,this.Type){case Types.Boolean:this.ctor=BooleanInput;break;case Types.Number:case Types.Real:case Types.Text:this.ctor=TextInput;break;case Types.Object:this.ctor=ObjectInput;break;default:Logger.warning('Array input with no arrType');}}remove(a){let b=[...this.props.input.value],c=b.indexOf(a);b.splice(c,1),this.props.change(b)}add(a){let b=[...this.props.input.value,a];this.props.change(b)}change(a,b){let c=[...this.props.input.value];c[a]=b,this.props.change(c)}render(){let a=this.ctor;return React.createElement('div',{className:'form-group ArrayInput'},React.createElement('div',{className:'row'},this.props.withoutLink?'':React.createElement('div',{className:'col s12'},React.createElement(InputLink,{input:this.props.input})),React.createElement('div',{className:'col s12'},this.ctor?this.props.input.value.map((b,c)=>React.createElement('div',{className:'row valign-wrapper',key:c+c},React.createElement('div',{className:'col'},c),React.createElement('div',{className:'col s11'},React.createElement(a,{key:c,input:{value:b},change:a=>this.change(c,a),withoutLink:!0})))):'Cannot render this type: '+this.Type),React.createElement('div',{className:'col s12'},React.createElement('div',{className:'row valign-wrapper'},React.createElement('div',{className:'col s5'},React.createElement(Button,{onClick:()=>{this.add(this.state.newdata),this.setState({newdata:null})}},'Add')),React.createElement('div',{className:'col s7'},React.createElement(a,{input:{},change:a=>this.setState({newdata:a}),withoutLink:!0}))))))}}export class TextInput extends React.Component{constructor(a){super(a)}render(){let a=9876568*Math.random();return React.createElement('div',{className:'row TextInput'},this.props.withoutLink?'':React.createElement('div',{className:'col s12'},React.createElement(InputLink,{input:this.props.input})),React.createElement('div',{className:'col '+(this.props.withoutLink?'s12':'s6')},React.createElement('input',{type:'text',placeholder:this.props.input.name,id:a,className:'form-control form-control-sm',tabIndex:'0',defaultValue:this.props.input.value,onBlur:a=>{this.props.change(a.target.value)},onKeyPress:a=>('Enter'===a.key&&this.props.change(a.target.value),a)})))}}export class ObjectInput extends React.Component{constructor(a){super(a)}render(){let a=this.props.input.value;return React.createElement('div',{className:'row ObjectInput'},this.props.withoutLink?'':React.createElement('div',{className:'col s12'},React.createElement('div',{className:'row'},React.createElement('div',{className:'col s10'},React.createElement(InputLink,{input:this.props.input})),React.createElement('div',{className:'col s2'},React.createElement('input',{type:'text',className:'pasteField',onPaste:a=>{let b=a.clipboardData.getData('Text');try{let a=JSON.parse(b);this.props.change(a)}catch(a){}}})))),React.createElement('div',{className:'col s12'},React.createElement(ReactJson,{src:a,onAdd:a=>{this.props.change(a.new_value)},theme:'apathy',onEdit:a=>this.props.change(a.updated_src),displayDataTypes:!1,enableClipboard:!0,collapsed:!0,collapseStringsAfterLength:7,onDelete:a=>this.props.change(a.updated_src)})))}}export class BooleanInput extends React.Component{constructor(a){super(a)}render(){let a=this.props.input.value;return React.createElement('div',{className:'row BooleanInput'},React.createElement('div',{className:'col s12'},React.createElement(InputLink,{input:this.props.input})),React.createElement('div',{className:'switch col s12'},React.createElement('label',null,'Off',React.createElement('input',{type:'checkbox',checked:a}),React.createElement('span',{className:'lever',onClick:()=>{this.props.change(!a)}}),'On')))}}export function RendererInput(a,b){switch(a.type){case Types.View:break;return React.createElement(MakeView,{module:this});case Types.Boolean:return React.createElement(BooleanInput,{input:a,change:c=>b(a.name,c,a)});case Types.Whatever:case Types.Text:case Types.Number:case Types.Real:return React.createElement(TextInput,{input:a,change:c=>b(a.name,c,a)});case Types.Array:return React.createElement(ArrayInput,{input:a,change:c=>b(a.name,c,a)});case Types.Object:return React.createElement(ObjectInput,{input:a,change:c=>b(a.name,c,a)});case Types.URL:break;case Types.Link:return React.createElement('div',{className:'row'},React.createElement('div',{className:'s6'},React.createElement(InputLink,{input:a})));default:throw new Error('Input not valid',a);}}export function RendererOutput(a){return React.createElement('div',{className:'row'},React.createElement('div',{className:'col s6'},React.createElement(OutputLink,{output:a})))}export function Resolver(a,b){const c='Resolver cannot parse Input Field';switch(a.type){case Types.Object:a.value=b||a.value||{};break;case Types.Boolean:b!==void 0&&null!==b&&(a.value=!!b);break;case Types.Number:try{a.value=parseInt(b===void 0||null===b?a.value:b)}catch(b){Logger.warning(c,{inputField:a})}break;case Types.Real:try{a.value=parseFloat(b===void 0||null===b?a.value:b)}catch(b){Logger.warning(c,{inputField:a})}break;case Types.Text:b!==void 0&&null!==b&&(a.value=b);break;case Types.Array:if(!Array.isArray(b))try{'string'==typeof b&&(a.value=JSON.parse(b))}catch(b){Logger.warning(c,{inputField:a})}else a.value=b;break;default:a.value=b;}return a.value}