create view directory for views

This commit is contained in:
Torsten Ruger
2015-10-19 19:56:35 +03:00
parent 37e24cd02a
commit 82b78b8825
9 changed files with 8 additions and 8 deletions

58
lib/views/blocks_view.rb Normal file
View File

@ -0,0 +1,58 @@
class BlocksView < ListView
def initialize interpreter
@interpreter = interpreter
@interpreter.register_event(:instruction_changed, self)
super([BlockView.new(@interpreter.block)])
@method_name = method_name
end
def draw
super()
wrap_element div("div.block_view") << div("h4" , "Method + Block " ) << div("h4.method" , @method_name)
return @element
end
def instruction_changed
new_name = method_name
unless new_name == @method_name
@method_name = new_name
@element.at_css(".method").text = method_name
end
return if @interpreter.block.object_id == @children.last.block.object_id
@elements.last.at_css(".bright").remove_class("bright")
append_view( BlockView.new(@interpreter.block) )
remove_first if( @elements.length > 6)
end
def method_name
bl = @interpreter.block
return "" unless bl
return bl.method if bl.method.is_a? String
"#{bl.method.for_class.name}.#{bl.method.name}"
end
end
class BlockView < ElementView
def initialize block
@block = block
end
attr_reader :block
def draw
@element = div("div") << div("span.bright" , block_name )
end
def method_name
return @block.method if @block.method.is_a? String
@block.method.name
end
def block_name
return @block if @block.is_a? String
"#{method_name}.#{@block.name}"
end
end

37
lib/views/classes_view.rb Normal file
View File

@ -0,0 +1,37 @@
class ClassesView < ListView
def initialize interpreter
@interpreter = interpreter
classes = []
Virtual.machine.space.classes.each do |name , claz|
next if [:Kernel,:Module,:MetaClass,:BinaryCode].index name
classes << claz
end
classes.sort! {|a,b| a.name <=> b.name }
super( classes.collect{|c| ClassView.new(c)})
end
def draw
super()
wrap_element div("ul.nav!")
wrap_element( div(".classes") << div("h4" , "Classes") )
return @element
end
end
class ClassView < ElementView
def initialize clazz
@clazz = clazz
end
def draw
@element = div("li") << div( "a" , @clazz.name ) << (ul = div("ul"))
@clazz.object_layout.object_instance_names.each do |name|
ul << (div("li") << div("a", name ))
end
@element.style["z-index"] = 20
@element
end
end

8
lib/views/file_view.rb Normal file
View File

@ -0,0 +1,8 @@
class FileView < ElementView
def draw
@element = div(".file_view") << div("h4" ,"Future")
end
end

View File

@ -0,0 +1,30 @@
require "base/constant_view"
require "base/list_view"
class InstructionView < ListView
def initialize interpreter
@interpreter = interpreter
super([ConstantView.new( "span.bright" , "starting" )])
@interpreter.register_event(:instruction_changed, self)
end
def instruction_changed
@element.at_css(".bright").remove_class("bright")
instruction = append_view( ConstantView.new( "span.bright" , instruction_text ) )
wrap_node_with instruction , div
remove_first if( @elements.length > 6)
end
def draw
super()
wrap_node_with @elements.first , div
wrap_element div(".source_view") << div("h4" ,"Virtual Machine Instruction")
@element
end
def instruction_text
return "" unless @interpreter.instruction
@interpreter.instruction.to_s
end
end

63
lib/views/object_view.rb Normal file
View File

@ -0,0 +1,63 @@
require_relative "ref_view"
class ObjectView < ListView
# z is the z-index
def initialize object_id , interpreter = nil , z = nil
@object_id = object_id
@z = z
@interpreter = interpreter
@interpreter.register_event(:object_changed, self) if interpreter
super( content_elements )
end
def draw
@element = super(@interpreter ? "ul.nav!" : "ul")
prepend_element div("li" , "-------------------------")
prepend_element div( "li" ) << div("span" , class_header(@object_id) )
return @element
end
def object_changed reg , at
at = at - 1 #take the layout off
#puts "Object changed in #{reg}"
for_object = @interpreter.get_register( reg )
return unless for_object == @object_id
#puts "Object changed #{for_object} , at #{at}"
object = Virtual.machine.objects[@object_id]
raise "error #{@object_id} , #{at}" unless object and ! object.is_a?(String)
variable = object.get_instance_variables.get(at)
#puts "got var name #{variable} for #{at}"
f = object.get_instance_variable(variable)
element = RefView.new( variable , f.object_id , @z )
replace_at at , element
end
def class_header(id)
object = Virtual.machine.objects[id]
clazz = object.class.name.split("::").last
[clazz, id].join " : "
end
def content_elements
object = Virtual.machine.objects[@object_id]
fields = []
if object and ! object.is_a?(String)
fields << RefView.new( "layout" , object.get_layout.object_id , @z )
object.get_instance_variables.each do |variable|
f = object.get_instance_variable(variable)
fields << RefView.new( variable , f.object_id , @z )
end
if( object.is_a?(Parfait::List) )
index = 1
object.each do | o , i|
fields << RefView.new( index.to_s , o.object_id , @z )
index += 1
end
end
end
fields
end
end

59
lib/views/ref_view.rb Normal file
View File

@ -0,0 +1,59 @@
class RefView < ListView
def initialize name , value , z = nil
@name = name
@value = value
@z = z
super []
end
attr_reader :value
def value= val
@value = val
add_hover
end
def draw
@element = div("li") << div("a" , "#{@name} : #{marker(@value)}" )
add_hover
@element.style["z-index"] = @z if @z
@element
end
def add_hover
return if is_string?
@element.on("hover"){ hover } if is_object?(@value)
end
def is_object?( )
Virtual.machine.objects[@value] != nil
end
def is_string?()
Virtual.machine.objects[@value].is_a? String
end
def is_nil?()
Virtual.machine.objects[@value].nil?
end
def hover
#puts "hovering #{@name}"
append_view ObjectView.new(@value)
@element.off("hover")
end
def marker id
if is_string?
str = @value
elsif is_nil?
str = "nil"
else
var = Virtual.machine.objects[id]
str = var.class.name.split("::").last[0,2]
str + " : #{id.to_s}"
end
end
end

View File

@ -0,0 +1,51 @@
require_relative "object_view"
class RegistersView < ListView
def initialize interpreter
@interpreter = interpreter
@interpreter.register_event(:register_changed, self)
kids = []
@interpreter.registers.each do |reg , val|
kids << ValueView.new( val )
end
super(kids)
end
def draw
super( "div.registers_view" )
@element.children.each_with_index do |reg, index|
elem = div("div.register_view")
wrap_node_with reg , elem
end
@element
end
def register_changed reg , old , value
reg = reg.symbol unless reg.is_a? Symbol
index = reg.to_s[1 .. -1 ].to_i
if( is_object? value )
swap = ObjectView.new( value , @interpreter , 16 - index )
else
swap = ValueView.new value
end
replace_at index , swap
# @elements[index].style["z-index"] = -index
end
def is_object?( id )
Virtual.machine.objects[id] != nil
end
end
class ValueView < ElementView
def initialize value
@value = value
end
def draw
@element = div("ul.nav!") << div("li") << div("span", @value)
end
end

42
lib/views/status_view.rb Normal file
View File

@ -0,0 +1,42 @@
class StatusView < ElementView
def initialize interpreter
@interpreter = interpreter
end
def draw
@element = div(".status_view") <<
div("h4" , "Interpreter" ) <<
div("button.act" , "Next") <<
div( "br") <<
div("span.clock" , clock_text) <<
div( "br") <<
div("span.state" , state_text) <<
div( "br") <<
div( "span.link" , link_text) <<
div( "br" , "Stdout") <<
div("span.stdout")
# set up event handler
@element.at_css(".act").on("click") { self.update }
return @element
end
def update
@interpreter.tick
@element.at_css(".clock").text = clock_text
@element.at_css(".link").text = link_text
@element.at_css(".stdout").text = @interpreter.stdout
end
def link_text
"Link #{@interpreter.link}"
end
def state_text
"State #{@interpreter.state}"
end
def clock_text
"Instruction #{@interpreter.clock}"
end
end