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.



44
45
46
47
48
# File 'lib/htmlgrid/grid.rb', line 44

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

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



35
36
37
# File 'lib/htmlgrid/grid.rb', line 35

def components
  @components
end

#tagObject

Returns the value of attribute tag.



35
36
37
# File 'lib/htmlgrid/grid.rb', line 35

def tag
  @tag
end

Instance Method Details

#add(item) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/htmlgrid/grid.rb', line 50

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

#add_attribute(key, value) ⇒ Object



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

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

#add_backgroundObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/htmlgrid/grid.rb', line 58

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

#add_component_style(style) ⇒ Object



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

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

#add_style(style) ⇒ Object



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

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

#attribute(key) ⇒ Object



84
85
86
# File 'lib/htmlgrid/grid.rb', line 84

def attribute(key)
  @attributes[key]
end

#colspanObject



88
89
90
# File 'lib/htmlgrid/grid.rb', line 88

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

#colspan=(span) ⇒ Object



92
93
94
# File 'lib/htmlgrid/grid.rb', line 92

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

#component_html(cgi) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/htmlgrid/grid.rb', line 96

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

#to_html(context) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/htmlgrid/grid.rb', line 113

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