-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
109 lines (97 loc) · 2.6 KB
/
event.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
package objhtml
import (
"sync"
velox "github.com/linkonoid/velox"
)
type (
//EventElement represents the DOM element sending an event
EventElement struct {
Properties map[string]string `json:"properties"`
}
//Event represents a DOM event
Event struct {
//Сlid string `json:"clientid"`
Name string `json:"name"`
Sender EventElement `json:"sender"`
//Change Element `json:"change"`
Inputs []EventElement `json:"inputs"`
Data string `json:"data"`
//Id string `json:"id"`
//Jscript string `json:"jscript"`
//Reply bool `json:"reply"`
}
Sse struct {
velox.State
sync.Mutex
Type string `json:"type"` // "innerhtml", "setvalue", "jseval", "jsalert", "json", "jqid"
SenderId string `json:"senderid"`
ChangeId string `json:"changeid"`
Data interface{} `json:"data"`
}
)
var (
SseData = &Sse{}
)
const (
OnClick = "onclick"
OnChange = "onchange"
OnKeyPress = "onkeypress"
OnAbort = "onabort"
OnBlur = "onblur"
OnDblclick = "ondblclick"
OnError = "onerror"
OnFocus = "onfocus"
OnKeydown = "onkeydown"
OnKeyup = "onkeyup"
OnLoad = "onload"
OnMousedown = "onmousedown"
OnMousemove = "onmousemove"
OnMouseout = "onmouseout"
OnMouseover = "onmouseover"
OnMouseup = "onmouseup"
OnReset = "onreset"
OnResize = "onresize"
OnSelect = "onselect"
OnSubmit = "onsubmit"
OnUnload = "onunload"
)
//GetID get the id of the event sender.
func (e *EventElement) GetID() string {
id, _ := e.Properties["id"]
return id
}
//GetValue gets the value of the event sender.
func (e *EventElement) GetValue() string {
id, _ := e.Properties["value"]
return id
}
//Makes an event that can be send to the browser
func EventSend(clientid, js string, reply chan string) event {
return event{clientid, replyid.New(""), js, reply}
}
//Sends and Event to the browser
func SendEvent(clientid, js string, reply chan string) {
events <- EventSend(clientid, js, reply)
}
//func sendEventDataToClient(eventtype string, data interface{}, elementrender *wgui.Element, sender, change *wgui.Element) {
func SendEventDataToClient(eventtype string, data interface{}, renderelement *Element, sender, change *Element) {
SseData.Lock()
SseData.Type = eventtype
SseData.SenderId = sender.GetID()
SseData.ChangeId = change.GetID()
switch eventtype {
case "innerhtml":
buf := Output
Output.Reset()
err := renderelement.Render()
if err == nil {
SseData.Data = Output.String()
}
Output = buf
buf.Reset()
default:
SseData.Data = data
}
SseData.Unlock()
SseData.Push()
}