-
Notifications
You must be signed in to change notification settings - Fork 6
/
dom.go
461 lines (376 loc) · 16.7 KB
/
dom.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
Copyright 2015 Palm Stone Games, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package polymer
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jsbuiltin"
"honnef.co/go/js/dom"
"reflect"
)
var domAPI *js.Object
type WrappedElement struct {
dom.Element
UnwrappedElement dom.Element
}
// FlushDOM flushes pending changes to the DOM
// Insert, append, and remove operations are transacted lazily in certain cases for performance.
// In order to interrogate the DOM (for example, offsetHeight, getComputedStyle, etc.) immediately after one of these operations, call this function first.
func FlushDOM() {
js.Global.Get("Polymer").Get("dom").Call("flush")
}
func WrapDOMElement(el dom.Element) Element {
if el == nil {
return nil
}
// Check if the element is already wrapped, if so, avoid double-wrapping it
if isWrapped(el.Underlying()) {
// Try and cast the dom.Element to a polymer.Element, if it works, just return that
// Otherwise, wrap it in a PolymerWrappedElement directly without wrapping the js element, as it's already wrapped
if newEl, ok := el.(Element); ok {
return newEl
} else {
return typeDomElement(el)
}
}
return WrapJSElement(el.Underlying())
}
func WrapJSElement(obj *js.Object) Element {
if obj == nil || obj == js.Undefined {
return nil
}
return typeDomElement(dom.WrapElement(polymerDOM(obj)))
}
func typeDomElement(el dom.Element) Element {
wrapped := &WrappedElement{el, dom.WrapElement(unwrap(el.Underlying()))}
switch wrapped.TagName() {
// We put the template case last so we can continue into the default case if we don't hit a template of a type we care about
case "TEMPLATE":
switch wrapped.GetAttribute("is") {
case "dom-bind-go":
return &AutoBindGoTemplate{wrapped}
}
fallthrough
default:
return wrapped
}
}
func polymerDOM(obj *js.Object) *js.Object {
// Check if the element is already wrapped, if so, avoid double-wrapping it
if isWrapped(obj) {
return obj
}
return js.Global.Get("Polymer").Call("dom", obj)
}
func isWrapped(obj *js.Object) bool {
if domAPI == nil || domAPI == js.Undefined {
domAPI = js.Global.Get("Polymer").Get("DomApi")
}
if domAPI == nil || domAPI == js.Undefined {
panic("Polymer has not correctly initialized yet")
}
return jsbuiltin.InstanceOf(obj, domAPI)
}
func unwrap(obj *js.Object) *js.Object {
if isWrapped(obj) {
return obj.Get("node")
}
return obj
}
func objToNodeSlice(obj *js.Object) []dom.Node {
if obj == nil || obj == js.Undefined {
return nil
}
nodes := make([]dom.Node, obj.Length())
for i := 0; i < obj.Length(); i++ {
nodes[i] = dom.WrapNode(obj.Index(i))
}
return nodes
}
func objToElementSlice(obj *js.Object) []Element {
if obj == nil || obj == js.Undefined {
return nil
}
nodes := make([]Element, obj.Length())
for i := 0; i < obj.Length(); i++ {
nodes[i] = WrapJSElement(obj.Index(i))
}
return nodes
}
func GetWindow() Window {
w := dom.GetWindow()
return &window{
Window: w,
wrappedElement: WrapJSElement(js.Global.Get("window")),
}
}
func GetDocument() dom.Document {
return GetWindow().Document()
}
type Window interface {
dom.Window
// SubscribeEvent subscribes to an event using the passed callback or channel
// The callback/channel may be strongly typed, the types will be automatically decoded
SubscribeEvent(event string, callback interface{}) *EventSubscription
// UnsubscribeEvent unsubscribes from the event with the passed handle
UnsubscribeEvent(sub *EventSubscription)
}
type window struct {
dom.Window
wrappedElement Element
}
func (w *window) Document() dom.Document {
doc := w.Window.Document()
return &document{
Document: doc,
wrappedElement: WrapDOMElement(doc.DocumentElement()),
}
}
func (w *window) SubscribeEvent(event string, callback interface{}) *EventSubscription {
return w.wrappedElement.SubscribeEvent(event, callback)
}
func (w *window) UnsubscribeEvent(sub *EventSubscription) {
w.wrappedElement.UnsubscribeEvent(sub)
}
type document struct {
dom.Document
wrappedElement dom.Element
}
func (d *document) CreateElement(name string) dom.Element {
return WrapDOMElement(d.Document.CreateElement(name))
}
func (d *document) CreateElementNS(namespace, name string) dom.Element {
return WrapDOMElement(d.Document.CreateElementNS(namespace, name))
}
func (d *document) ElementFromPoint(x, y int) dom.Element {
return WrapDOMElement(d.Document.ElementFromPoint(x, y))
}
func (d *document) GetElementByID(id string) dom.Element {
return d.wrappedElement.QuerySelector("#" + id)
}
func (d *document) GetElementsByClassName(name string) []dom.Element {
return d.wrappedElement.QuerySelectorAll("." + name)
}
func (d *document) GetElementsByTagName(name string) []dom.Element {
return d.wrappedElement.QuerySelectorAll(name)
}
func (d *document) GetElementsByTagNameNS(ns, name string) []dom.Element {
panic("Operation not supported")
}
func (d *document) QuerySelector(sel string) dom.Element {
return d.wrappedElement.QuerySelector(sel)
}
func (d *document) QuerySelectorAll(sel string) []dom.Element {
return d.wrappedElement.QuerySelectorAll(sel)
}
type Element interface {
dom.Element
// Root returns the local DOM root of the current element
Root() Element
// GetDistributedNodes returns the nodes distributed to a <content> insertion point
// only returns useful results when called on a <content> element
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
GetDistributedNodes() []dom.Node
// GetDestinationInsertionPoints returns the <content> nodes this element will be distributed to
// only returns useful results when called on an element that’s being distributed.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
GetDestinationInsertionPoints() []dom.Node
// GetContentChildNodes accepts a css selector that points to a <content> node and returns all nodes that have been distributed to it
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
GetContentChildNodes(selector string) []dom.Node
// GetContentChildNodes accepts a css selector that points to a <content> node and returns all elements that have been distributed to it
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
GetContentChildren(selector string) []Element
// GetEffectiveChildNodes returns a list of effective child nodes for this element.
// Effective child nodes are the child nodes of the element, with any insertion points replaced by their distributed child nodes.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
GetEffectiveChildNodes() []dom.Node
// GetEffectiveChildren returns a list of effective children for this element.
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
GetEffectiveChildren() []Element
// QueryEffectiveChildren returns the first effective child that matches the selector
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
QueryEffectiveChildren(selector string) Element
// QueryAllEffectiveChildren returns a slice of effective children that match selector
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
QueryAllEffectiveChildren(selector string) []Element
// ObserveNodes sets up an observer that will be called when new nodes are added or removed from this Element
// ObserveNodes behaves slightly differently depending on the node being observed:
// - If the node being observed is a content node, the callback is called when the content node’s distributed children change.
// - For any other node, the callback is called when the node’s effective children change.
ObserveNodes(func(*ObservationInfo)) *Observer
// UnobserveNodes stops an observer from receiving notifications
UnobserveNodes(*Observer)
// SubscribeEvent subscribes to an event using the passed callback or channel
// The callback/channel may be strongly typed, the types will be automatically decoded
SubscribeEvent(event string, callback interface{}) *EventSubscription
// UnsubscribeEvent unsubscribes from the event with the passed handle
UnsubscribeEvent(sub *EventSubscription)
}
func (el *WrappedElement) TagName() string {
return el.UnwrappedElement.TagName()
}
func (el *WrappedElement) GetAttribute(name string) string {
return el.UnwrappedElement.GetAttribute(name)
}
func (el *WrappedElement) GetAttributeNS(ns string, name string) string {
return el.UnwrappedElement.GetAttributeNS(ns, name)
}
func (el *WrappedElement) ParentElement() dom.Element {
return WrapDOMElement(el.UnwrappedElement.ParentElement())
}
func (el *WrappedElement) InsertBefore(which dom.Node, before dom.Node) {
el.Underlying().Call("insertBefore", unwrap(which.Underlying()), unwrap(before.Underlying()))
}
func (el *WrappedElement) GetElementsByClassName(name string) []dom.Element {
return el.QuerySelectorAll("." + name)
}
func (el *WrappedElement) GetElementsByTagName(name string) []dom.Element {
return el.QuerySelectorAll(name)
}
func (el *WrappedElement) GetElementsByTagNameNS(ns, name string) []dom.Element {
panic("Operation not supported")
}
func (el *WrappedElement) AppendChild(node dom.Node) {
el.Underlying().Call("appendChild", unwrap(node.Underlying()))
}
func (el *WrappedElement) QuerySelector(sel string) dom.Element {
return WrapDOMElement(el.Element.QuerySelector(sel))
}
func (el *WrappedElement) QuerySelectorAll(sel string) []dom.Element {
elements := el.Element.QuerySelectorAll(sel)
for i, el := range elements {
elements[i] = WrapDOMElement(el)
}
return elements
}
func (el *WrappedElement) GetBoundingClientRect() dom.ClientRect {
return el.UnwrappedElement.GetBoundingClientRect()
}
func (el *WrappedElement) ID() string {
return el.UnwrappedElement.ID()
}
type EventSubscription struct {
event string
funcObj *js.Object
chanRefVal reflect.Value
}
func (el *WrappedElement) SubscribeEvent(event string, callback interface{}) *EventSubscription {
refVal := reflect.ValueOf(callback)
sub := &EventSubscription{event: event}
switch refVal.Kind() {
case reflect.Func:
sub.funcObj = eventHandlerCallback(refVal)
case reflect.Chan:
sub.funcObj = eventChanCallback(refVal)
sub.chanRefVal = refVal
default:
panic(fmt.Sprint("Expected callback of kind %s or %s, but got %s", reflect.Func, reflect.Chan, refVal.Kind()))
}
el.Underlying().Get("node").Call("addEventListener", event, sub.funcObj)
return sub
}
func (el *WrappedElement) UnsubscribeEvent(sub *EventSubscription) {
el.Underlying().Call("removeEventListener", sub.event, sub.funcObj)
if sub.chanRefVal.IsValid() {
sub.chanRefVal.Close()
}
}
// Root returns the local DOM root of the current element
func (el *WrappedElement) Root() Element {
// root is set on the polymer element, but not on its wrapped equivalent, so drill through the wrapper to get the root
return WrapJSElement(el.Underlying().Get("node").Get("root"))
}
// ObservationInfo is the structure used to hand data to ObserveNodes callbacks
type ObservationInfo struct {
Observer *Observer
AddedNodes, RemovedNodes []dom.Node
}
// Observer is the structure used to track an observation using ObserveNodes/UnobserveNodes
type Observer struct {
Element Element
object *js.Object
}
// GetDistributedNodes returns the nodes distributed to a <content> insertion point
// only returns useful results when called on a <content> element
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
func (el *WrappedElement) GetDistributedNodes() []dom.Node {
return objToNodeSlice(el.Underlying().Call("getDistributedNodes"))
}
// GetDestinationInsertionPoints returns the <content> nodes this element will be distributed to
// only returns useful results when called on an element that’s being distributed.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
func (el *WrappedElement) GetDestinationInsertionPoints() []dom.Node {
return objToNodeSlice(el.Underlying().Call("getDestinationInsertionPoints"))
}
// GetContentChildNodes accepts a css selector that points to a <content> node and returns all nodes that have been distributed to it
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
func (el *WrappedElement) GetContentChildNodes(selector string) []dom.Node {
return objToNodeSlice(el.Underlying().Call("getContentChildNodes"))
}
// GetContentChildNodes accepts a css selector that points to a <content> node and returns all elements that have been distributed to it
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#distributed-children
func (el *WrappedElement) GetContentChildren(selector string) []Element {
return objToElementSlice(el.Underlying().Call("getContentChildren"))
}
// GetEffectiveChildNodes returns a list of effective child nodes for this element.
// Effective child nodes are the child nodes of the element, with any insertion points replaced by their distributed child nodes.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
func (el *WrappedElement) GetEffectiveChildNodes() []dom.Node {
return objToNodeSlice(el.Underlying().Call("getEffectiveChildNodes"))
}
// GetEffectiveChildren returns a list of effective children for this element.
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
func (el *WrappedElement) GetEffectiveChildren() []Element {
return objToElementSlice(el.Underlying().Call("getEffectiveChildren"))
}
// QueryEffectiveChildren returns the first effective child that matches the selector
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
func (el *WrappedElement) QueryEffectiveChildren(selector string) Element {
return WrapJSElement(el.Underlying().Call("queryEffectiveChildren"))
}
// QueryAllEffectiveChildren returns a slice of effective children that match selector
// Effective children are the children of the element, with any insertion points replaced by their distributed children.
// Details can be found at https://www.polymer-project.org/1.0/docs/devguide/local-dom.html#effective-children
func (el *WrappedElement) QueryAllEffectiveChildren(selector string) []Element {
return objToElementSlice(el.Underlying().Call("queryAllEffectiveChildren"))
}
// ObserveNodes sets up an observer that will be called when new nodes are added or removed from this Element
// ObserveNodes behaves slightly differently depending on the node being observed:
// - If the node being observed is a content node, the callback is called when the content node’s distributed children change.
// - For any other node, the callback is called when the node’s effective children change.
func (el *WrappedElement) ObserveNodes(f func(*ObservationInfo)) *Observer {
obs := &Observer{}
obs.Element = el
obs.object = el.Underlying().Call("observeNodes", wrapObserveNodesCallback(obs, f))
return obs
}
func wrapObserveNodesCallback(obs *Observer, f func(*ObservationInfo)) func(*js.Object) {
return func(obj *js.Object) {
info := ObservationInfo{
Observer: obs,
AddedNodes: objToNodeSlice(obj.Get("addedNodes")),
RemovedNodes: objToNodeSlice(obj.Get("removedNodes")),
}
f(&info)
}
}
// UnobserveNodes stops an observer from receiving notifications
func (el *WrappedElement) UnobserveNodes(obs *Observer) {
el.Underlying().Call("unobserveNodes", obs.object)
}