2015-08-21 20:05:49 +02:00
|
|
|
require_relative "element_view"
|
2015-08-20 14:48:45 +02:00
|
|
|
|
2015-10-07 11:24:02 +02:00
|
|
|
# Listviews hold an array of elements and are responsible for drawing (and re-drawing them)
|
|
|
|
#
|
|
|
|
# A ListView hold the elements, but also the drawn html divs. You can change the element
|
|
|
|
# structure by adding/removing/replacing and the ListView will take care of redrawing the html
|
|
|
|
#
|
|
|
|
# A ListView is itself an ElementView so one can build recursive structures.
|
|
|
|
#
|
|
|
|
# Also one can easily change the root html element, or by deriving wrap or edit the resulting html
|
|
|
|
#
|
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 19:28:09 +02:00
|
|
|
# create a root node acording to the tag given (default div)
|
|
|
|
# The tag name will be passed to the div function, so class and id may be set as well (see there)
|
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
|
2015-08-22 19:28:09 +02:00
|
|
|
def draw root = "div"
|
|
|
|
@element = div(root)
|
2015-08-20 19:40:47 +02:00
|
|
|
@elements = @children.collect do | c |
|
2015-08-22 20:11:33 +02:00
|
|
|
append_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
|
2015-10-04 21:39:02 +02:00
|
|
|
# the old node will be replaced in the live dom
|
2016-12-22 20:18:51 +01:00
|
|
|
def replace_at( index , node)
|
2015-08-22 02:23:53 +02:00
|
|
|
old = @elements[index]
|
2016-12-22 20:18:51 +01:00
|
|
|
@children[index] = node
|
|
|
|
rendered = node.draw
|
2015-08-22 02:23:53 +02:00
|
|
|
@elements[index] = rendered
|
2016-12-22 20:18:51 +01:00
|
|
|
old.replace_with(rendered) if old
|
2015-08-22 02:23:53 +02:00
|
|
|
end
|
2015-08-22 18:37:42 +02:00
|
|
|
|
2015-08-24 02:04:07 +02:00
|
|
|
# remove the first child and element (from view)
|
|
|
|
def remove_first
|
|
|
|
remove_at 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# remove both child and element at given position
|
|
|
|
def remove_at index
|
2016-12-22 20:18:51 +01:00
|
|
|
raise "index out of bounds #{index} => #{@children.length}" if(index >= @children.length or index < 0)
|
2015-08-24 02:04:07 +02:00
|
|
|
@children.delete_at( index )
|
|
|
|
element = @elements.delete_at(index)
|
2016-12-22 20:18:51 +01:00
|
|
|
element.remove if element
|
2015-08-24 02:04:07 +02:00
|
|
|
end
|
|
|
|
|
2015-10-21 13:03:23 +02:00
|
|
|
# remove all elements and views, basically resetting the list to empty
|
|
|
|
def clear_view
|
|
|
|
remove_first while( ! @children.empty? )
|
|
|
|
end
|
|
|
|
|
2015-08-24 02:04:07 +02:00
|
|
|
# append a View instnace to the children array
|
|
|
|
# render it and append it to the html element
|
|
|
|
# and keep a copy in @elements
|
|
|
|
def append_view view
|
2015-08-22 18:37:42 +02:00
|
|
|
@children << view
|
|
|
|
rendered = view.draw
|
2015-08-24 01:44:50 +02:00
|
|
|
@elements << rendered # add to internal array
|
|
|
|
@element << rendered # add to html children
|
|
|
|
rendered
|
2015-08-22 18:37:42 +02:00
|
|
|
end
|
2015-08-20 14:48:45 +02:00
|
|
|
end
|