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
|
# File 'ui/src/hokusai/mounting/loop_entry.rb', line 97
def update_loop
block.node.meta.on_update(target) do |ublock, uparent, utarget|
values = utarget.public_send(ast.loop.method)
unless values.is_a?(Enumerable)
raise Hokusai::Error.new("Loop directive `#{ast.loop.method}` on #{target.class} must return an Enumerable")
end
key_prop = ast.props["key"]
raise Hokusai::Error.new("Loop children must have a :key method defined") if key_prop.nil?
key_ctx = LoopContext.new
new_values = []
index_key = "index".freeze
values.each_with_index do |value, index|
key_ctx.add_entry(ast.loop.var, value)
key_ctx.add_entry(index_key, index)
if key_prop.value.args.size > 0
key = key_ctx.send_target(utarget, key_prop.value)
elsif key_ctx.table[key_prop.value.method]
key = key_ctx.table[key_prop.value.method]
else
key = utarget.public_send(key_prop.value.method)
end
new_values << [key, value]
end
previous_values = []
children = []
loop_var = ast.loop.var.to_sym
ublock.children?&.each do |child|
if key = child.node.meta.get_prop(:key)
raise Hokusai::Error.new("Loop children must use :key field") unless key
previous_values << [key, child.node.meta.get_prop(loop_var)]
end
children << child
end
next if new_values == previous_values
Diff.new(previous_values, new_values).patch do |patch|
case patch
when UpdatePatch
ctx = LoopContext.new
ctx.add_entry("index", patch.target)
ctx.add_entry(ast.loop.var, patch.value)
children[patch.target].node.add_styles(target.class)
children[patch.target].node.add_props_from_block(target, context: ctx)
children[patch.target].node.meta.set_prop(ast.loop.var.to_sym, patch.value)
UpdateEntry.new(children[patch.target], uparent, utarget).register(context: ctx)
when MovePatch
if patch.delete
from = children[patch.from]
children[patch.to] = from
children[patch.from].public_send(:on_destroy) if children[patch.from].respond_to? :on_destroy
children[patch.from].node.destroy
children[patch.from] = nil
else
from = children[patch.from]
to = children[patch.to]
children[patch.to] = from
children[patch.from] = to
end
ctx = LoopContext.new
ctx.add_entry(INDEX_KEY, patch.to)
children[patch.to].node.meta.props.each do |k, v|
ctx.add_entry(k.to_s, v)
end
children[patch.to].node.add_styles(target.class)
children[patch.to].node.add_props_from_block(target, context: ctx)
when InsertPatch
target_ast = ast
ctx = LoopContext.new
ctx.add_entry(INDEX_KEY, patch.target)
ctx.add_entry(ast.loop.var, patch.value)
if ast.has_if_condition?
if ast.if.args.size > 0
condition = ctx.send_target(target, ast.if.method)
else
condition = target.public_send(ast.if.method)
end
if !condition && ast.has_else_condition?
target_ast = ast.else_ast
elsif !condition
children[patch.target].public_send(:on_destroy) if children[patch.target].respond_to? :on_destroy
children[patch.target].node.destroy
children[patch.target] = nil
next
end
end
child_block_class = utarget.class.use(target_ast.type)
portal = Node.new(ast)
node = child_block_class.compile(target_ast.type, portal)
node.add_props_from_block(target, context: ctx)
child_block = NodeMounter.new(node, child_block_class).mount(context: ctx)
child_block.node.add_styles(target.class)
child_block.node.meta.publisher.add(target)
child_block.class.provides.merge!(parent.class.provides)
if patch.delete
children[patch.target] = child_block
else
children.insert(patch.target, child_block)
end
when DeletePatch
children[patch.target].public_send(:on_destroy) if children[patch.target].respond_to? :on_destroy
children[patch.target].node.destroy
children[patch.target] = nil
end
end
ublock.node.meta.children = children.reject(&:nil?)
end
end
|