Class: HtmlGrid::Grid::Row::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlgrid/grid.rb

Constant Summary collapse

ALLOWED_ATTRIBUTES =
[
  'align',
  'class',
  'colspan',
  'style',
  'tag',
  'title',
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeField

Returns a new instance of Field.



42
43
44
45
46
# File 'lib/htmlgrid/grid.rb', line 42

def initialize
  @components = []
  @attributes = {}
  @tag = nil
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



33
34
35
# File 'lib/htmlgrid/grid.rb', line 33

def components
  @components
end

#tagObject

Returns the value of attribute tag.



33
34
35
# File 'lib/htmlgrid/grid.rb', line 33

def tag
  @tag
end

Instance Method Details

#add(item) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/htmlgrid/grid.rb', line 47

def add(item)
  if(item.is_a? Array)
    @components += item
  else
    @components.push item
  end
end

#add_attribute(key, value) ⇒ Object



65
66
67
# File 'lib/htmlgrid/grid.rb', line 65

def add_attribute(key, value)
  @attributes.store(key.to_s, value.to_s)
end

#add_backgroundObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/htmlgrid/grid.rb', line 54

def add_background
  compose
  @components.each { |component| 
    component.add_background
  }
  if(@attributes["class"])
    @attributes["class"] += "-bg" unless @attributes["class"] =~ /-bg/
  else
    @attributes["class"] = "bg"
  end
end

#add_component_style(style) ⇒ Object



68
69
70
71
72
# File 'lib/htmlgrid/grid.rb', line 68

def add_component_style(style)
  @components.each { |cmp| 
    cmp.set_attribute("class", style) if cmp.respond_to?(:set_attribute)
  }
end

#add_style(style) ⇒ Object



73
74
75
# File 'lib/htmlgrid/grid.rb', line 73

def add_style(style)
  @attributes["class"] = style
end

#attribute(key) ⇒ Object



76
77
78
# File 'lib/htmlgrid/grid.rb', line 76

def attribute(key)
  @attributes[key]
end

#colspanObject



79
80
81
# File 'lib/htmlgrid/grid.rb', line 79

def colspan
  @attributes.fetch("colspan", 1).to_i
end

#colspan=(span) ⇒ Object



82
83
84
# File 'lib/htmlgrid/grid.rb', line 82

def colspan=(span)
  @attributes["colspan"] = span.to_s if span.to_i > 1
end

#component_html(cgi) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/htmlgrid/grid.rb', line 85

def component_html(cgi)
  html = ''
  @components.each { |component|
    if component.respond_to? :to_html
      html << component.to_html(cgi).to_s.force_encoding('utf-8')
    else
      html << component.to_s
    end
  }
  html = "&nbsp;" if html.empty?
  html
end

#to_html(context) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/htmlgrid/grid.rb', line 100

def to_html(context)
  if(@tag && context.respond_to?(@tag))
    context.send(@tag, @attributes) { 
      component_html(context) }
  else
    context.td(@attributes) {
      component_html(context) }
  end
end