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
|
|
|
|
|
2015-08-22 02:23:53 +02:00
|
|
|
# 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
|
2015-08-21 19:07:02 +02:00
|
|
|
def root
|
|
|
|
"div"
|
|
|
|
end
|
|
|
|
|
2015-08-22 02:23:53 +02:00
|
|
|
# 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
|
2015-08-22 02:23:53 +02:00
|
|
|
# return (as per base class) the single root of the collection
|
|
|
|
def draw
|
2015-08-22 01:37:15 +02:00
|
|
|
@element = div(self.root)
|
2015-08-20 19:40:47 +02:00
|
|
|
@elements = @children.collect do | c |
|
2015-08-22 02:23:53 +02:00
|
|
|
add_element c.draw
|
2015-08-20 19:40:47 +02:00
|
|
|
end
|
2015-08-22 01:37:15 +02:00
|
|
|
@element
|
2015-08-20 19:40:47 +02:00
|
|
|
end
|
2015-08-21 19:07:02 +02:00
|
|
|
|
2015-08-22 02:23:53 +02:00
|
|
|
# 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]
|
2015-08-22 02:40:19 +02:00
|
|
|
@children[index] = with
|
2015-08-22 02:23:53 +02:00
|
|
|
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
|