class HtmlGrid::Grid
Attributes
height[RW]
width[RW]
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/htmlgrid/grid.rb, line 188 def initialize(attributes = {}) @height = 1 @width = 1 @rows = [Row.new] @attributes = { "cellspacing" => "0" }.update(attributes) end
Public Instance Methods
[](x, y)
click to toggle source
# File lib/htmlgrid/grid.rb, line 329 def [](x, y) @rows[y][x] rescue nil end
add(arg, x, y, col = false)
click to toggle source
# File lib/htmlgrid/grid.rb, line 208 def add(arg, x, y, col = false) if arg.is_a?(Enumerable) if arg.is_a?(String) add_field(arg, x, y) elsif arg.is_a?(Array) arg.each do |item| add_field(item, x || "", y) end elsif col add_column(arg, x, y) else add_row(arg, x, y) end else add_field(arg, x, y) end end
add_attribute(key, value, x, y, w = 1, h = 1)
click to toggle source
# File lib/htmlgrid/grid.rb, line 226 def add_attribute(key, value, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_attribute(key, value) } end
add_background(x, y, w = 1, h = 1)
click to toggle source
# File lib/htmlgrid/grid.rb, line 232 def add_background(x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_background(x, w) } end
add_column(arg, x, y)
click to toggle source
# File lib/htmlgrid/grid.rb, line 238 def add_column(arg, x, y) offset = 0 arg.each do |item| add_field(item, x, y + offset) offset = offset.next end end
add_component_style(style, x, y, w = 1, h = 1)
click to toggle source
# File lib/htmlgrid/grid.rb, line 246 def add_component_style(style, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_component_style(style) } end
add_field(arg, x, y)
click to toggle source
# File lib/htmlgrid/grid.rb, line 252 def add_field(arg, x, y) initialize_grid(x + 1, y + 1) (@rows[y] ||= Row.new).add(arg, x) end
add_row(arg, x, y)
click to toggle source
# File lib/htmlgrid/grid.rb, line 257 def add_row(arg, x, y) offset = 0 arg.each do |item| add_field(item, x + offset, y) offset = offset.next end end
add_style(style, x, y, w = 1, h = 1)
click to toggle source
# File lib/htmlgrid/grid.rb, line 265 def add_style(style, x, y, w = 1, h = 1) each_field(x, y, w, h) { |field| field.add_style(style) } end
add_tag(tag, x, y, w = 1, h = 1)
click to toggle source
# File lib/htmlgrid/grid.rb, line 271 def add_tag(tag, x, y, w = 1, h = 1) initialize_grid(x + w, y + h) each_field(x, y, w, h) { |field| field.tag = tag } end
each_field(x, y, w = 1, h = 1) { |field| ... }
click to toggle source
# File lib/htmlgrid/grid.rb, line 278 def each_field(x, y, w = 1, h = 1) y.upto([y + h, @height].min - 1) { |yy| @rows[yy].each_field(x, w) { |field| yield(field) } } end
field_attribute(key, x = 0, y = 0)
click to toggle source
# File lib/htmlgrid/grid.rb, line 286 def field_attribute(key, x = 0, y = 0) @rows[y].field_attribute(key, x) end
initialize_grid(w, h)
click to toggle source
# File lib/htmlgrid/grid.rb, line 197 def initialize_grid(w, h) if w > @width || h > @height floor = w > @width ? 0 : @height @width = [w, @width].max @height = [h, @height].max floor.upto(@height - 1) { |ii| (@rows[ii] ||= Row.new).initialize_row(@width) } end end
insert_row(y = 0, arg = nil)
click to toggle source
# File lib/htmlgrid/grid.rb, line 290 def insert_row(y = 0, arg = nil) @rows[y, 0] = Row.new @height += 1 add(arg, 0, y) end
push(arg, x = 0, y = height)
click to toggle source
# File lib/htmlgrid/grid.rb, line 296 def push(arg, x = 0, y = height) add(arg, x, y) set_colspan(x, y) end
set_attribute(key, value)
click to toggle source
# File lib/htmlgrid/grid.rb, line 301 def set_attribute(key, value) @attributes[key] = value end
set_attributes(hash)
click to toggle source
# File lib/htmlgrid/grid.rb, line 305 def set_attributes(hash) @attributes.update(hash) end
set_colspan(x = 0, y = 0, span = (@width - x))
click to toggle source
# File lib/htmlgrid/grid.rb, line 309 def set_colspan(x = 0, y = 0, span = (@width - x)) span = span.to_i initialize_grid(x + span, y + 1) self[x, y].colspan = span end
set_row_attributes(attr = {}, y = 0, offset = 0)
click to toggle source
# File lib/htmlgrid/grid.rb, line 315 def set_row_attributes(attr = {}, y = 0, offset = 0) # TODO # At the moment, offset value is not used # But probably offset value is used in grid.c initialize_grid(offset, y + 1) @rows[y].set_attributes(attr) end
to_html(cgi)
click to toggle source
# File lib/htmlgrid/grid.rb, line 323 def to_html(cgi) cgi.table(@attributes) { @rows.collect { |row| row.to_html(cgi).force_encoding("utf-8") }.join } end