From 68f67eda54b4348d8a81bbcd6fdf9fe06684ec77 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 22 Aug 2015 02:23:53 +0200 Subject: [PATCH] registers ticking, but objects not showing --- lib/base/constant_view.rb | 14 ++++++++++++++ lib/base/element_view.rb | 28 ++++++++++++++++++++++++---- lib/base/list_view.rb | 21 +++++++++++++++++---- lib/object_view.rb | 3 --- lib/registers_view.rb | 18 +++++++++++++----- 5 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 lib/base/constant_view.rb diff --git a/lib/base/constant_view.rb b/lib/base/constant_view.rb new file mode 100644 index 0000000..a8ac538 --- /dev/null +++ b/lib/base/constant_view.rb @@ -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 diff --git a/lib/base/element_view.rb b/lib/base/element_view.rb index c3cfc2e..48ac2d2 100644 --- a/lib/base/element_view.rb +++ b/lib/base/element_view.rb @@ -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 diff --git a/lib/base/list_view.rb b/lib/base/list_view.rb index 3c99acc..9dd3c76 100644 --- a/lib/base/list_view.rb +++ b/lib/base/list_view.rb @@ -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 diff --git a/lib/object_view.rb b/lib/object_view.rb index 7a54ece..c474586 100644 --- a/lib/object_view.rb +++ b/lib/object_view.rb @@ -59,7 +59,4 @@ class ObjectView fields end - def is_object?( id ) - Virtual.machine.objects[id] != nil - end end diff --git a/lib/registers_view.rb b/lib/registers_view.rb index 94410c5..360a450 100644 --- a/lib/registers_view.rb +++ b/lib/registers_view.rb @@ -18,8 +18,8 @@ class RegistersView < ListView end def draw - list = super() - list = list.children.each do |reg| + super() + @element.children.each do |reg| elem = div("div.register_view") wrap_node_with reg , elem end @@ -28,9 +28,17 @@ class RegistersView < ListView def register_changed reg , old , value reg = reg.symbol unless reg.is_a? Symbol - return unless reg == register - objects_id! value - calc_fields + index = reg.to_s[1 .. -1 ].to_i + if( is_object? value ) + swap = ObjectView.new( value ) + else + swap = ValueView.new value + end + replace_at index , swap + end + + def is_object?( id ) + Virtual.machine.objects[id] != nil end def calc_fields