-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhyperlink_js.go
More file actions
72 lines (60 loc) · 1.86 KB
/
hyperlink_js.go
File metadata and controls
72 lines (60 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//go:build js
package hyperlink
import (
"net/url"
"syscall/js"
)
var (
_document = js.Global().Get("document")
_body = js.Global().Get("document").Get("body")
_actives = make([]js.Value, 0, 32)
)
type driver struct {
closeFn js.Func
}
func attachDriver(house *Hyperlink, config Config) {
d := driver{}
d.closeFn = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
configureDriver(&d, Config{Blur: true})
return nil
})
js.Global().Get("document").Call("addEventListener", "visibilitychange", d.closeFn)
house.driver = d
}
func configureDriver(driver *driver, config Config) {
if config.Blur {
for i := 0; i < len(_actives); i++ {
_body.Call("removeChild", _actives[i])
}
_actives = _actives[:0]
}
}
func (d *driver) closeFunc() js.Func {
return d.closeFn
}
func (d *driver) open(u *url.URL, preferredPackage string) error {
if ok := js.Global().Call("open", u.String(), "_blank", "noreferrer,noopener").Truthy(); !ok {
// If there's a error let's use the hacky way:
// It will create a "fullscreen <a>", which clicking will
// open the URL.
// Generally, it will need two clicks to open the URL.
// We remove this `a` when the app lost focus (based on Page Visibility API, which Gio relies on),
// or when the user clicks on the `a` element, if the `onclick` works.
a := _document.Call("createElement", "a")
a.Set("href", u.String())
a.Set("target", "_blank")
a.Set("rel", "noreferrer,noopener")
a.Set("innerText", " ")
a.Get("classList").Call("add", "hyperlink")
a.Get("style").Set("display", "block")
a.Get("style").Set("width", "100vw")
a.Get("style").Set("height", "100vh")
a.Get("style").Set("position", "fixed")
a.Get("style").Set("top", "0")
a.Get("style").Set("z-index", "2147483647")
a.Set("onclick", d.closeFunc())
_body.Call("appendChild", a)
_actives = append(_actives, a)
}
return nil
}