-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuxcog.go
217 lines (169 loc) · 4.83 KB
/
uxcog.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
// The UXToolkit Project
// Copyright (c) Wirecog, LLC. All rights reserved.
// Use of this source code is governed by a BSD-style
// license, which can be found in the LICENSE file.
package cog
import (
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/isomorphicgo/isokit"
"github.com/uxtoolkit/reconcile"
_ "golang.org/x/net/html"
"honnef.co/go/js/dom"
)
type UXCog struct {
Cog
cogType reflect.Type
cogPrefixName string
cogPackagePath string
cogTemplatePath string
templateSet *isokit.TemplateSet
Props map[string]interface{}
element *dom.Element
id string
hasBeenRendered bool
parseTree *reconcile.ParseTree
cleanupFunc func()
}
func (u *UXCog) getCogPrefixName() string {
if u.cogType != nil {
result := strings.Split(u.cogType.PkgPath(), string(os.PathSeparator))
return "cog:" + result[len(result)-1]
} else {
return ""
}
}
func (u *UXCog) ID() string {
return u.id
}
func (u *UXCog) SetID(id string) {
u.id = id
}
func (u *UXCog) SetCleanupFunc(cleanupFunc func()) {
u.cleanupFunc = cleanupFunc
}
func (u *UXCog) SetElement(element *dom.Element) {
u.element = element
}
func (u *UXCog) Element() *dom.Element {
return u.element
}
func (u *UXCog) CogInit(ts *isokit.TemplateSet) {
u.hasBeenRendered = false
u.Props = make(map[string]interface{})
if ts != nil {
u.templateSet = ts
}
u.cogTemplatePath = filepath.Join(DefaultGoSourcePath, u.cogType.PkgPath(), DefaultTemplatesDirectoryName)
u.cogPrefixName = u.getCogPrefixName()
if isokit.OperatingEnvironment() == isokit.ServerEnvironment {
u.RegisterCogTemplates()
}
}
func (u *UXCog) TemplateSet() *isokit.TemplateSet {
return u.templateSet
}
func (u *UXCog) SetTemplateSet(ts *isokit.TemplateSet) {
u.templateSet = ts
}
func (u *UXCog) CogType() reflect.Type {
return u.cogType
}
func (u *UXCog) SetCogType(cogType reflect.Type) {
u.cogType = cogType
}
func (u *UXCog) CogTemplatePath() string {
return u.cogTemplatePath
}
func (u *UXCog) SetCogTemplatePath(path string) {
u.cogTemplatePath = path
}
func (u *UXCog) RegisterCogTemplates() {
u.templateSet.GatherCogTemplates(u.cogTemplatePath, u.cogPrefixName, ".tmpl")
}
func (u *UXCog) GetProps() map[string]interface{} {
return u.Props
}
func (u *UXCog) SetProp(key string, value interface{}) {
u.Props[key] = value
if ReactivityEnabled == true && u.hasBeenRendered == true {
u.Render()
}
}
func (u *UXCog) BatchPropUpdate(props map[string]interface{}) {
for k, v := range props {
u.Props[k] = v
}
if ReactivityEnabled == true && u.hasBeenRendered == true {
u.Render()
}
}
func (u *UXCog) RenderCogTemplate() {
var populateRenderedContent bool
if u.hasBeenRendered == false {
populateRenderedContent = true
} else {
populateRenderedContent = false
}
rp := isokit.RenderParams{Data: u.Props, Disposition: isokit.PlacementReplaceInnerContents, Element: *u.element, ShouldPopulateRenderedContent: populateRenderedContent}
u.templateSet.Render(filepath.Join(u.getCogPrefixName(), strings.Split(u.getCogPrefixName(), ":")[1]), &rp)
if u.hasBeenRendered == false {
u.hasBeenRendered = true
D := dom.GetWindow().Document()
cogRoot := D.GetElementByID(u.id).FirstChild().(*dom.HTMLDivElement)
contents := cogRoot.InnerHTML()
parseTree, err := reconcile.NewParseTree([]byte(contents))
if err != nil {
println("Encountered an error: ", err)
} else {
u.parseTree = parseTree
}
}
}
func (u *UXCog) Render() error {
document := dom.GetWindow().Document()
e := document.GetElementByID(u.ID())
if u.hasBeenRendered == true && e == nil {
if u.cleanupFunc != nil {
u.cleanupFunc()
return nil
}
}
if strings.ToLower(e.GetAttribute("data-component")) != "cog" {
return errors.New("The cog container div must have a \"data-component\" attribute with a value specified as \"cog\".")
}
if u.hasBeenRendered == false {
// Initial Render
u.SetElement(&e)
u.RenderCogTemplate()
return nil
} else if u.element != nil {
// Re-render
if VDOMEnabled == true {
rp := isokit.RenderParams{Data: u.Props, Disposition: isokit.PlacementReplaceInnerContents, Element: *u.element, ShouldPopulateRenderedContent: true, ShouldSkipFinalRenderStep: true}
u.templateSet.Render(filepath.Join(u.getCogPrefixName(), strings.Split(u.getCogPrefixName(), ":")[1]), &rp)
D := dom.GetWindow().Document()
cogRoot := D.GetElementByID(u.id).FirstChild().(*dom.HTMLDivElement)
//contents := cogRoot.InnerHTML()
newTree, err := reconcile.NewParseTree([]byte(rp.RenderedContent))
if err != nil {
println("Encountered an error: ", err)
}
changes, err := u.parseTree.Compare(newTree)
if err != nil {
println("Encountered an error: ", err)
}
if len(changes) > 0 {
changes.ApplyChanges(cogRoot)
u.parseTree = newTree
}
} else {
u.RenderCogTemplate()
}
return nil
}
return nil
}