From 16fed27a8a9085d8df7ca5e332c5bf303304772c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sat, 15 Jun 2019 17:49:45 +0200 Subject: [PATCH] SwitchView, to alternate between different views --- src/ui/web/SwitchView.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/ui/web/SwitchView.js diff --git a/src/ui/web/SwitchView.js b/src/ui/web/SwitchView.js new file mode 100644 index 00000000..7789cb1e --- /dev/null +++ b/src/ui/web/SwitchView.js @@ -0,0 +1,36 @@ +export default class SwitchView { + constructor(defaultView) { + this._childView = defaultView; + } + + mount() { + return this._childView.mount(); + } + + unmount() { + return this._childView.unmount(); + } + + root() { + return this._childView.root(); + } + + update() { + return this._childView.update(); + } + + switch(newView) { + const oldRoot = this.root(); + this._childView.unmount(); + this._childView = newView; + const newRoot = this._childView.mount(); + const parent = oldRoot.parentElement; + if (parent) { + parent.replaceChild(newRoot, oldRoot); + } + } + + get childView() { + return this._childView; + } +}