-
Notifications
You must be signed in to change notification settings - Fork 0
/
js_events.go
162 lines (151 loc) · 4.2 KB
/
js_events.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package alat
import (
"syscall/js"
)
func AttachFocusEvents(widget FocusableWidget, obj js.Value, rownum int) {
obj.Set("onkeydown",js.FuncOf(func(this js.Value, args []js.Value) interface{} {
event := args[0]
keycode := event.Get("keyCode").Int()
shiftkey := event.Get("shiftKey").Bool()
ctrlkey := event.Get("ctrlKey").Bool()
//fmt.Println("OnKeyDown:",keycode,shiftkey,ctrlkey,rownum)
switch keycode {
case 9: // Tab
event.Call("preventDefault")
if shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
widget.Block().Refresh()
widget.Block().PrevWidget().SetFocus()
} else if !shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
widget.Block().Refresh()
widget.Block().NextWidget().SetFocus()
}
case 38: // Up
event.Call("preventDefault")
if !shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
buffer := widget.Block().Buffer()
lastPos := buffer.pos
buffer.Goto(lastPos-1)
if lastPos != buffer.pos {
widget.Block().Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
case 40: // Down
event.Call("preventDefault")
if !shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
buffer := widget.Block().Buffer()
lastPos := buffer.pos
buffer.Goto(lastPos+1)
if lastPos != buffer.pos {
widget.Block().Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
case 33: // PgUp
event.Call("preventDefault")
if !shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
block := widget.Block()
buffer := block.Buffer()
lastPos := buffer.pos
if lastPos == block.viewBegin {
buffer.Goto(lastPos-block.visibleRows)
} else {
buffer.Goto(block.viewBegin)
}
if lastPos != buffer.pos {
block.Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
case 34: // PgDn
event.Call("preventDefault")
if !shiftkey && !ctrlkey {
widget.WriteIfChanged(obj,rownum)
block := widget.Block()
buffer := block.Buffer()
lastPos := buffer.pos
if lastPos == block.viewEnd {
buffer.Goto(lastPos+block.visibleRows)
} else {
buffer.Goto(block.viewEnd)
}
if lastPos != buffer.pos {
block.Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
case 36: // Home
if !shiftkey && ctrlkey {
event.Call("preventDefault")
widget.WriteIfChanged(obj,rownum)
block := widget.Block()
buffer := block.Buffer()
lastPos := buffer.pos
buffer.Goto(0)
if lastPos != buffer.pos {
block.Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
case 35: // End
if !shiftkey && ctrlkey {
event.Call("preventDefault")
widget.WriteIfChanged(obj,rownum)
block := widget.Block()
buffer := block.Buffer()
lastPos := buffer.pos
buffer.Goto(len(buffer.rows)-1)
if lastPos != buffer.pos {
block.Refresh()
widget.SetFocus()
}
widget.SelectAll()
}
}
return nil
}))
/*
obj.Set("onmousedown",js.FuncOf(func(this js.Value, args []js.Value) interface{} {
fmt.Printf("OnClick:%p,%d\n",widget,rownum)
return nil
}))
*/
obj.Set("onfocus",js.FuncOf(func(this js.Value, args []js.Value) interface{} {
block := widget.Block()
//fmt.Printf("OnFocus:%p,%d (from %p,%d)\n",widget,block.Pos(),
// block.lastFocusOutWidget,block.lastFocusOutPos)
if widget.IsInMultiRow() {
buffer := block.Buffer()
bufferPos,_ := block.ViewRow2BufferPos(rownum)
if bufferPos != buffer.pos {
buffer.Goto(bufferPos)
widget.Block().Refresh()
widget.SetFocus()
}
}
widget.Block().OnFocusToWidget(widget)
widget.Block().RefreshCurrentRow()
widget.SelectAll()
return nil
}))
obj.Call("addEventListener","focusout",js.FuncOf(func(this js.Value, args []js.Value) interface{} {
block := widget.Block()
//fmt.Printf("OnFocusOut:%p,%d\n",widget,block.Pos())
widget.WriteIfChanged(obj,rownum)
block.RefreshCurrentRow()
block.lastFocusOutWidget = widget
block.lastFocusOutPos = block.Pos()
// preventing focusout: widget.SetFocus()
return nil
}))
}