Class: Hokusai::Mounting::LoopEntry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
ui/src/hokusai/mounting/loop_entry.rb

Constant Summary collapse

INDEX_KEY =
"index".freeze

Instance Method Summary collapse

Constructor Details

#initialize(mount_entry) ⇒ LoopEntry

Returns a new instance of LoopEntry.



32
33
34
# File 'ui/src/hokusai/mounting/loop_entry.rb', line 32

def initialize(mount_entry)
  @entry = mount_entry
end

Instance Method Details

#registerObject



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
# File 'ui/src/hokusai/mounting/loop_entry.rb', line 36

def register
  child_block_class = target.class.use(ast.type)
  values = target.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

  entries_to_return = []
  secondary_entries = []

  # puts "COMPILING #{ast.type}"
  # portal = Node.new(ast)
  # node = child_block_class.compile(ast.type, portal)

  values.each_with_index do |value, index|
    ctx = LoopContext.new
    ctx.add_entry(ast.loop.var, value)
    ctx.add_entry(INDEX_KEY, index)

    if ast.has_if_condition?
      if ast.if.args.size > 0
        ctx.send_target(target, target.if)
      else
        condition = target.public_send(ast.if.method)
      end

      next if condition
    end

    portal = Node.new(ast)
    node = child_block_class.compile(ast.type, portal)
    child_block = child_block_class.new(node: node)
    child_block.node.add_styles(target.class)
    child_block.node.add_props_from_block(target, context: ctx)
    child_block.node.meta.props[ast.loop.var.to_sym] = value
    child_block.node.meta.publisher.add(target)

    UpdateEntry.new(child_block, block, target).register(context: ctx)

    child_block.class.provides.merge!(parent.class.provides)

    block.node.meta << child_block

    node.ast.children.each_with_index do |child, idx|
      entries_to_return << MountEntry.new(index, child, child_block, child_block, child_block, context: nil)
    end

    siblings = []
    portal.ast.children.each_with_index do |child, idx|
      siblings << MountEntry.new(idx, child, child_block, child_block, target, context: ctx)
    end

    secondary_entries << siblings
  end

  update_loop

  [entries_to_return, secondary_entries]
end

#update_loopObject



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
        # TODO: update rest of block props
      end
    end

    ublock.node.meta.children = children.reject(&:nil?)
  end
end