import React from "react" import {TbResize} from 'react-icons/tb' import {BiCube, BiRefresh} from 'react-icons/bi' import {BsTrash} from 'react-icons/bs' import {CgLayoutGridSmall} from 'react-icons/cg' import '../../css/counter.css' export default class CustomNodeIframe extends React.Component { constructor({id , data}){ super() this.myRef = React.createRef() this.state = { id : id, reachable : this.isFetchable(data.host), selected : true, data : data, width : 540, height : 600, size : false, iframe : 0, initial_pos : 0, initial_size : 0, } } handelSelected = () => { this.setState({id : this.state.id, reachable : this.state.reachable, selected : !this.state.selected, data : this.state.data, width : this.state.width, height : this.state.height, size : this.state.size, iframe : this.state.iframe, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) } handelSizeState = () => { this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : this.state.width, height : this.state.height, size : !this.state.size, iframe : this.state.iframe, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) } isFetchable = async (host) => { fetch(host, {mode: 'no-cors'}).then((re) => { return true }).catch((err)=>{ return false }) return false } onNodeClick = (id) => { this.state.data.delete(id) } onRefresh(){ if(!this.isFetchable(this.state.data.host)){ this.onNodeClick(this.state.id) } else{ this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : this.state.width, height : this.state.height, size : this.state.size, iframe : this.state.iframe + 1, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) } } handelOnChange(evt, type){ this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : type === "width" ? parseInt(evt.target.value) : this.state.width, height : type === "height" ? parseInt(evt.target.value) : this.state.height, size : this.state.size, iframe : this.state.iframe, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) type === "width" ? this.myRef.current.style.width = `${parseInt(evt.target.value)}px` : this.myRef.current.style.height = `${parseInt(evt.target.value)}px` } handelSize(evt, increment, change){ if (evt === "increment") { this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : change === "width" ? this.state.width + increment : this.state.width, height : change === "height" ? this.state.height + increment : this.state.height, size : this.state.size, iframe : this.state.iframe, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) change === "width" ? this.myRef.current.style.width = `${this.state.width + increment}px` : this.myRef.current.style.height = `${this.state.height + increment}px` } } //(Experimental) resize nodes by dragging initial = (e) => { this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : this.state.width, height : this.state.height , size : this.state.size, iframe : this.state.iframe, initial_pos : e.clientY, initial_size : this.myRef.current.offsetHeight }) } resize = (e) => { var new_height = parseInt(this.state.initial_size) + parseInt(e.clientY - this.state.initial_pos) this.setState({id : this.state.id, reachable : this.state.reachable, selected : this.state.selected, data : this.state.data, width : this.state.width, height : new_height, size : this.state.size, iframe : this.state.iframe, initial_pos : this.state.initial_pos, initial_size : this.state.initial_size}) this.myRef.current.style.height = `${new_height}px` } Counter(focus, size){ return (
this.handelOnChange(e, focus)} onKeyDown={(e) => {this.handelSize(e.key, size, focus)}}>
) } render(){ if (!this.state.reachable) {this.onNodeClick(this.state.id) } return (<> <>
this.onNodeClick(this.state.id)}>
this.onRefresh()}>
{ this.state.size &&
{this.Counter("width", this.state.width)} {this.Counter("height", this.state.height)}
}
{/* (Experimental) Do not uncomment */}
{ this.initial(e) }} onDrag={(e) => { this.resize(e) }} >
) } }