-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathhelloRactive.js
More file actions
187 lines (164 loc) · 5.17 KB
/
Copy pathhelloRactive.js
File metadata and controls
187 lines (164 loc) · 5.17 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
/// <reference path="typings/ue.d.ts">/>
// ; typing info for auto-completion in Visual Studio Code
let UMG = require('UMG')
let instantiator = require('instantiator')
let _ = require('lodash')
let Ractive = require('ractive')
function GetPC() {
return PlayerController.C(GWorld.GetAllActorsOfClass(PlayerController).OutActors[0])
}
(function (global) {
"use strict"
function dyn_wrap(ractive,prop,bindings,design) {
let elem
let observe
function update(value) {
let bag = {}
_.each(bindings,(v,k) => {
bag[k] = _.get(v(value), prop)
})
elem.set_attrs(bag)
}
function listener(cur, prev, keyPath) {
update(ractive.get())
}
function link() {
observe = ractive.observe(prop, listener)
}
function unlink() {
observe.cancel()
}
let old = design.$link
design.$link = _elem => {
elem = _elem
old && old()
update(ractive)
link()
}
let old_unlink = design.$unlink
design.$unlink = _ => {
old_unlink && old_unlink()
unlink()
}
return design
}
function dyn_object(ractive, prop, fn) {
let elem
let observe
let old = _.clone(ractive.get(prop))
function update(data) {
elem.remove_children()
let widget = fn(data)
if(widget) {
elem.add_child(widget)
}
}
function listener(cur, prev, keyPath) {
if(ractive.get(prop) != old) {
update(ractive.get(prop))
}
old = _.clone(ractive.get(prop))
}
function link(_elem) {
elem = _elem
update(ractive.get(prop))
observe = ractive.observe(prop, listener)
}
function unlink() {
observe.cancel()
}
return UMG.div({$link:link,$unlink:unlink})
}
function dyn_array(ractive,array,prop,fn) {
let elem
let observe
let map = new Map()
let old = [...ractive.get(array)]
function add(added) {
added.forEach(x => {
let data = _.get(x, prop)
map.set(data,elem.add_child(fn(data)))
})
}
function remove(removed) {
removed.forEach(x => {
let data = _.get(x, prop)
elem.remove_child(map.get(data))
map.delete(x)
})
}
function listener(cur, prev, keyPath) {
let added = _.difference(ractive.get(array),old)
let removed = _.difference(old,ractive.get(array))
old = [...ractive.get(array)]
add(added)
remove(removed)
}
function link(_elem) {
elem = _elem
add(ractive.get(array))
observe = ractive.observe(array, listener)
}
function unlink() {
observe.cancel()
}
return UMG.div({$link:link,$unlink:unlink})
}
function main() {
let content = {hello: 'hello'}
let people = [
{name: 'Rich Harris'},
{name: 'Marty Nelson'}
]
let obj = { a: { b: { c: 1 } } };
let ractive = new Ractive({
data : {
content: content,
people: people,
obj : obj
}})
let PC = GetPC()
let design = UMG.div({},
dyn_object(ractive, 'content.hello', x => {
return UMG.text({}, `${x}`)
}),
dyn_array(ractive, 'people', 'name', x => {
return UMG.text({}, `${x}`)
}, UMG.span({})),
dyn_wrap(ractive, 'obj.a.b.c', {Text:x => x}, UMG.text({}, ""))
)
let i = 0
let j = 100
function tick() {
people.push({name: `h ${i++}`})
people.splice(0, 1)
obj.a.b.c = `${j--}`
content.hello += `${i}`
ractive.update('content.hello')
ractive.update('obj.a.b.c')
setTimeout(__ => tick(), 1000)
}
setTimeout(__ => tick(), 1000)
let page = instantiator(design)
let widget = GWorld.CreateWidget(JavascriptWidget, PC)
widget.SetRootWidget(page)
widget.AddToViewport()
// clean up the mess
return function () {
widget.RemoveFromViewport()
}
}
// bootstrap to initiate live-reloading dev env.
try {
module.exports = () => {
let cleanup = null
// wait for map to be loaded.
process.nextTick(() => cleanup = main());
// live-reloadable function should return its cleanup function
return () => cleanup()
}
}
catch (e) {
require('bootstrap')('helloRactive')
}
})(this)