From eee497f22a3d738e954a83052bcd24c3bdfd6c17 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 2 Aug 2019 09:04:00 +0200 Subject: [PATCH] ui/app: (wasm) create and add container div if one is not provided Signed-off-by: Elias Naur --- ui/app/os_js.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ui/app/os_js.go b/ui/app/os_js.go index 0b5fe9ae..6771336a 100644 --- a/ui/app/os_js.go +++ b/ui/app/os_js.go @@ -1,7 +1,6 @@ package app import ( - "errors" "image" "sync" "syscall/js" @@ -32,14 +31,11 @@ var mainDone = make(chan struct{}) func createWindow(win *Window, opts *WindowOptions) error { doc := js.Global().Get("document") - parent := doc.Call("getElementById", "giowindow") - if parent == js.Null() { - return errors.New("app: #giowindow not found") - } + cont := getContainer(doc) cnv := createCanvas(doc) - parent.Call("appendChild", cnv) + cont.Call("appendChild", cnv) tarea := createTextArea(doc) - parent.Call("appendChild", tarea) + cont.Call("appendChild", tarea) w := &window{ cnv: cnv, tarea: tarea, @@ -64,6 +60,16 @@ func createWindow(win *Window, opts *WindowOptions) error { return nil } +func getContainer(doc js.Value) js.Value { + cont := doc.Call("getElementById", "giowindow") + if cont != js.Null() { + return cont + } + cont = doc.Call("createElement", "DIV") + doc.Get("body").Call("appendChild", cont) + return cont +} + func createTextArea(doc js.Value) js.Value { tarea := doc.Call("createElement", "input") style := tarea.Get("style")