/
focus.go
40 lines (33 loc) · 940 Bytes
/
focus.go
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
package controls
import (
"syscall/js"
"github.com/QuestScreen/api/web"
)
// FocusHolder is a UI component that needs to react when the focus leaves its
// root element.
type FocusHolder interface {
// Root returns the root element of the component.
Root() js.Value
// FocusLeaving is called when the focus is leaving the root element, but
// before the focus has actually left.
FocusLeaving()
}
var curHolder FocusHolder
func SetFocusHolder(holder FocusHolder) {
if curHolder != nil && holder != nil {
web.Log(web.LogWarn, "SetHolder called when curHolder was not nil")
}
curHolder = holder
}
var cb = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if curHolder != nil {
if !curHolder.Root().Call("contains", args[0].Get("relatedTarget")).Bool() {
curHolder.FocusLeaving()
curHolder = nil
}
}
return nil
})
func init() {
js.Global().Get("document").Call("addEventListener", "focusout", cb)
}