registers ticking, but objects not showing

This commit is contained in:
Torsten Ruger
2015-08-22 02:23:53 +02:00
parent ed2a054ca6
commit 68f67eda54
5 changed files with 68 additions and 16 deletions

14
lib/base/constant_view.rb Normal file
View File

@ -0,0 +1,14 @@
require_relative "element_view"
class ConstantView < ElementView
def initialize class_or_id , text = nil
@class_or_id = class_or_id
@text = text
end
def draw
@element = div(@class_or_id , @text)
end
end

View File

@ -4,10 +4,20 @@ class ElementView
@element = nil
end
#abstract function that should return the single element that is being represented
# the element is also stored in @element
def draw
raise "implement me to return an Element"
end
# helper function to create an element with possible classes, id and text
# The first argument is a bit haml inspired, so "tagname.classname" is the format
# but if tagname is ommited it will default to div
# also several classnames may be given
# if one of the names ends in a ! (bang) it will be assigned as the id
# second argument is optional, but if given will be added as text (content) to the newly
# created Element
# return the new Element, which is not linked into the dom at that point (see << and add*)
def div name_class , text = nil
name , clazz = name_class.split(".")
name = "div" if name.empty?
@ -46,9 +56,19 @@ class ElementView
wrapper << node
end
def add class_or_id , tex = nil
element = div( class_or_id , tex)
element.append_to @element
element
# add the given element the @element
# return the div that was passed in (use << to return the @element)
def add_element div
div.append_to @element
div
end
# create a new element with class and possibly text
# add that new element to the @element
# return the newly created element
def add class_or_id , tex = nil
add_element div( class_or_id , tex)
end
end

View File

@ -7,18 +7,31 @@ class ListView < ElementView
@elements = []
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
def draw on
# create a root node acording to the "root" function (default div)
# draw all chilren and keep the elements as @elements
# return (as per base class) the single root of the collection
def draw
@element = div(self.root)
@elements = @children.collect do | c |
elem = c.draw(@element)
elem.append_to(@element)
elem
add_element c.draw
end
@element
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]
@chilren[index] = with
rendered = with.draw
@elements[index] = rendered
old.replace_with rendered
end
end