Skip to content

Commit 9e0454f

Browse files
sqakidennwc
authored andcommitted
Added Window.Open and window.SetLocation methods for the Window struct (dennwc#22)
* Added Window.Open and window.SetLocation methods for the Window struct + `window.open` : https://developer.mozilla.org/en-US/docs/Web/API/Window/open#Syntax + `window.location = someURL` * Accept the window features as a key value map pair in the Open function * Changes for the review + string type for the other params + Use strings.Join to make the windowFeatures string * fix code style
1 parent 1d43fa8 commit 9e0454f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

window.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package dom
22

3-
import "github.com/dennwc/dom/js"
3+
import (
4+
"strings"
5+
6+
"github.com/dennwc/dom/js"
7+
)
48

59
func GetWindow() *Window {
610
win := js.Get("window")
@@ -26,6 +30,26 @@ func (w *Window) AddEventListener(typ string, h EventHandler) {
2630
}))
2731
}
2832

33+
func (w *Window) Open(url, windowName string, windowFeatures map[string]string) {
34+
w.v.Call("open", url, windowName, joinKeyValuePairs(windowFeatures, ","))
35+
}
36+
37+
func (w *Window) SetLocation(url string) {
38+
w.v.Set("location", url)
39+
}
40+
2941
func (w *Window) OnResize(fnc func(e Event)) {
3042
w.AddEventListener("resize", fnc)
3143
}
44+
45+
func joinKeyValuePairs(kvpair map[string]string, joiner string) string {
46+
if kvpair == nil {
47+
return ""
48+
}
49+
50+
pairs := make([]string, 0, len(kvpair))
51+
for k, v := range kvpair {
52+
pairs = append(pairs, k+"="+v)
53+
}
54+
return strings.Join(pairs, joiner)
55+
}

0 commit comments

Comments
 (0)