improve DOMWindow

This commit is contained in:
srgooglo 2022-10-12 00:01:03 +02:00
parent 76858e2c94
commit 59432a29a9

View File

@ -6,14 +6,24 @@ import { Icons } from "components/Icons"
import "./index.less" import "./index.less"
class DOMWindow { class DOMWindow {
constructor(props) { constructor(props = {}) {
this.props = { ...props } this.props = props
this.id = this.props.id this.id = this.props.id
this.key = 0 this.key = 0
this.currentRender = null
this.root = document.getElementById("app_windows") this.root = document.getElementById("app_windows")
this.element = document.getElementById(this.id) this.element = document.createElement("div")
this.element.setAttribute("id", this.id)
this.element.setAttribute("key", this.key)
this.element.setAttribute("classname", this.props.className)
// if props clickOutsideToClose is true, add event listener to close window
if (this.props.clickOutsideToClose) {
document.addEventListener("click", this.handleWrapperClick)
}
// handle root container // handle root container
if (!this.root) { if (!this.root) {
@ -33,39 +43,43 @@ class DOMWindow {
this.key = lastChildKey + 1 this.key = lastChildKey + 1
} }
}
this.element = document.createElement("div") handleWrapperClick = (event) => {
if (!this.currentRender) {
return
}
this.element.setAttribute("id", this.id) // if click in not renderer fragment, close window
this.element.setAttribute("key", this.key) if (!this.element.contains(event.target)) {
this.element.setAttribute("classname", this.props.className) this.remove()
}
this.root.appendChild(this.element)
} }
render = (fragment) => { render = (fragment) => {
ReactDOM.render( this.root.appendChild(this.element)
fragment,
this.element,
)
return this this.currentRender = fragment
return ReactDOM.render(fragment, this.element)
} }
create = () => { remove = () => {
// set render this.root.removeChild(this.element)
this.render(<WindowRender {...this.props} id={this.id} key={this.key} destroy={this.destroy} />) this.currentRender = null
return this
} }
destroy = () => { destroy = () => {
this.element.remove() this.element.remove()
return this this.currentRender = null
}
createDefaultWindow = () => {
return this.render(<DefaultWindowRender {...this.props} id={this.id} key={this.key} destroy={this.destroy} />)
} }
} }
class WindowRender extends React.Component { class DefaultWindowRender extends React.Component {
state = { state = {
actions: [], actions: [],
dimensions: { dimensions: {
@ -195,4 +209,4 @@ class WindowRender extends React.Component {
} }
} }
export { DOMWindow, WindowRender } export { DOMWindow, DefaultWindowRender }