rubyx-debugger/lib/base/list_view.rb

71 lines
2.2 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
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
# 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
# return (as per base class) the single root of the collection
def draw root = "div"
@element = div(root)
2015-08-20 19:40:47 +02:00
@elements = @children.collect do | c |
append_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
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)
old = @elements[index]
2016-12-22 20:18:51 +01:00
@children[index] = node
rendered = node.draw
@elements[index] = rendered
2016-12-22 20:18:51 +01:00
old.replace_with(rendered) if old
end
2015-08-22 18:37:42 +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)
@children.delete_at( index )
element = @elements.delete_at(index)
2016-12-22 20:18:51 +01:00
element.remove if element
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
# 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