forked from maxence-charriere/go-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.go
More file actions
194 lines (168 loc) · 4.24 KB
/
js.go
File metadata and controls
194 lines (168 loc) · 4.24 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package app
import (
"encoding/json"
"fmt"
"github.com/murlokswarm/log"
"github.com/murlokswarm/markup"
"github.com/satori/go.uuid"
)
const (
jsFmt = `
function Mount(id, markup) {
const sel = '[data-murlok-root="' + id + '"]';
const elem = document.querySelector(sel);
elem.innerHTML = markup;
}
function RenderFull(id, markup) {
const sel = '[data-murlok-id="' + id + '"]';
const elem = document.querySelector(sel);
elem.outerHTML = markup;
}
function RenderAttributes(id, attrs) {
const sel = '[data-murlok-id="' + id + '"]';
const elem = document.querySelector(sel);
for (var name in attrs) {
if (elem.hasAttribute(name) && attrs[name].length == 0) {
elem.removeAttribute(name);
continue;
}
elem.setAttribute(name, attrs[name]);
}
}
function GetAttributeValue(elem, name) {
if (!elem.hasAttribute(name)) {
return null;
}
return elem.getAttribute(name);
}
function CallEvent(id, method, self, event) {
var arg;
var value = null;
if (self.value) {
value = self.value;
}
switch (event.type) {
case "click":
case "contextmenu":
case "dblclick":
case "mousedown":
case "mouseenter":
case "mouseleave":
case "mousemove":
case "mouseover":
case "mouseout":
case "mouseup":
case "drag":
case "dragend":
case "dragenter":
case "dragleave":
case "dragover":
case "dragstart":
case "drop":
arg = MakeMouseArg(event);
break;
case "mousewheel":
arg = MakeWheelArg(event);
break;
case "keydown":
case "keypress":
case "keyup":
arg = MakeKeyboardArg(event);
break;
case "change":
arg = MakeChangeArg(value);
break;
default:
arg = {};
break;
}
arg.Target = {
ID: GetAttributeValue(self, "id"),
Class: GetAttributeValue(self, "class"),
Index: GetAttributeValue(self, "data-murlok-index"),
Value: value,
Tag: self.tagName.toLowerCase()
};
Call(id, method, arg);
}
function MakeMouseArg(event) {
return {
AltKey: event.altKey,
Button: event.button,
ClientX: event.clientX,
ClientY: event.clientY,
CtrlKey: event.ctrlKey,
Detail: event.detail,
MetaKey: event.metaKey,
PageX: event.pageX,
PageY: event.pageY,
ScreenX: event.screenX,
ScreenY: event.screenY,
ShiftKey: event.shiftKey
};
}
function MakeWheelArg(event) {
return {
DeltaX: event.deltaX,
DeltaY: event.deltaY,
DeltaZ: event.deltaZ,
DeltaMode: event.deltaMode
};
}
function MakeKeyboardArg(event) {
return {
AltKey: event.altKey,
CtrlKey: event.ctrlKey,
CharCode: event.charCode,
KeyCode: event.keyCode,
Location: event.location,
MetaKey: event.metaKey,
ShiftKey: event.shiftKey
};
}
function MakeChangeArg(value) {
return {
Value: value
};
}
function Call(id, method, arg) {
let msg = {
ID: id,
Method: method,
Arg: JSON.stringify(arg)
};
msg = JSON.stringify(msg);
%v
}
`
)
// DOMElement represents a DOM element.
type DOMElement struct {
Tag string // The tag of the element. e.g. div.
ID string // The id attribute.
Class string // the class attribute.
Value string // The value attribute.
Index string // The data-murlok-index attribute.
}
type jsMsg struct {
ID uuid.UUID
Method string
Arg string
}
// HandleEvent allows to call the component method or map the component field
// described in msg.
// Should be used only in a driver.
func HandleEvent(msg string) {
var jsMsg jsMsg
if err := json.Unmarshal([]byte(msg), &jsMsg); err != nil {
log.Error(err)
return
}
markup.HandleEvent(jsMsg.ID, jsMsg.Method, jsMsg.Arg)
}
// MurlokJS returns the javascript code allowing bidirectional communication
// between a context and it's webview.
// Should be used only in drivers implementations.
func MurlokJS() string {
return fmt.Sprintf(jsFmt, driver.JavascriptBridge())
}