rubyx-debugger/lib/base/list_view.rb

45 lines
1.1 KiB
Ruby
Raw Normal View History

2015-08-21 20:05:49 +02:00
require_relative "element_view"
2015-08-20 14:48:45 +02:00
class ListView < ElementView
2015-08-20 19:40:47 +02:00
def initialize children
@children = children
2015-08-20 16:07:44 +02:00
@elements = []
2015-08-20 14:48:45 +02:00
end
# can be overriden to return a string that will be passed to div to create the root
# element for the list. See "div" in ElementView for possible strings
def root
"div"
end
# create a root node acording to the "root" function (default div)
2015-08-22 18:37:42 +02:00
# draw all children and keep the elements as @elements
# return (as per base class) the single root of the collection
def draw
@element = div(self.root)
2015-08-20 19:40:47 +02:00
@elements = @children.collect do | c |
add_element c.draw
2015-08-20 19:40:47 +02:00
end
@element
2015-08-20 19:40:47 +02:00
end
# replace the child at index with the given one (second arg)
# The child must be an ElementView , which will be rendered and
# the old node will be replaces in the live dom
def replace_at index , with
old = @elements[index]
@children[index] = with
rendered = with.draw
@elements[index] = rendered
old.replace_with rendered
end
2015-08-22 18:37:42 +02:00
def append view
@children << view
rendered = view.draw
@elements << rendered
@element << rendered
end
2015-08-20 14:48:45 +02:00
end