Class: HtmlGrid::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlgrid/component.rb,
lib/htmlgrid/dojotoolkit.rb

Constant Summary collapse

CSS_CLASS =

sets the ‘class’ html-attribute

nil
CSS_ID =

sets the ‘id’ html-attribute

nil
HTML_ATTRIBUTES =

other html-attributes

{}
HTTP_HEADERS =

default http-headers

{
  "Content-Type" => "text/html",
  "Cache-Control" => "no-cache, max-age=3600, must-revalidate"
}
LABEL =

precede instances of this class with a label?

false
@@html_entities =
[
  ["&", "amp"],
  ["<", "lt"],
  [">", "gt"]
]
@@symbol_entities =

:nodoc:

{
  34  => "forall",
  36  => "exist",
  42  => "lowast",
  45  => "minus",
  64  => "cong",
  65  => "Alpha",  97 =>  "alpha",
  66  => "Beta", 98  => "beta",
  67  => "Chi",  99 =>  "chi",
  68  => "Delta",  100  => "delta",
  69  => "Epsilon",  101  => "epsilon",
  70  => "Phi",  102  => "phi",
  71  => "Gamma",  103  => "gamma",
  72  => "Eta",  104  => "eta",
  73  => "Iota", 105 =>  "iota",
  75  => "Kappa",  107  => "kappa",
  76  => "Lambda", 108 =>  "lambda",
  77  => "Mu", 109 =>  "mu",
  78  => "Nu", 110 =>  "nu",
  79  => "Omicron",  111  => "omicron",
  80  => "Pi", 112 =>  "pi",
  81  => "Theta",  113  => "theta",
  82  => "Rho",  114  => "rho",
  83  => "Sigma",  115  => "sigma",
  84  => "Tau",  116  => "tau",
  85  => "Upsilon",  117  => "upsilon",
  86  => "sigmaf",
  87  => "Omega",  119  => "omega",
  88  => "Xi", 120 =>  "xi",
  89  => "Psi",  121  => "psi",
  90  => "Zeta", 122 =>  "zeta",
  94  => "perp", 126 =>  "sim",
  163 =>  "le",
  165 =>  "infin",
  166 =>  "fnof",
  171 =>  "harr",
  172 =>  "larr",
  173 =>  "uarr",
  174 =>  "rarr",
  175 =>  "darr",
  179 =>  "ge",
  181 =>  "prop",
  182 =>  "part",
  185 =>  "ne",
  186 =>  "equiv",
  187 =>  "asymp",
  191 =>  "crarr",
  196 =>  "otimes",
  197 =>  "oplus",
  198 =>  "empty",
  199 =>  "cap",
  200 =>  "cup",
  201 =>  "sup",
  202 =>  "supe",
  203 =>  "nsub",
  204 =>  "sub",
  205 =>  "sube",
  206 =>  "isin",
  207 =>  "notin",
  208 =>  "ang",
  209 =>  "nabla",
  213 =>  "prod",
  214 =>  "radic",
  215 =>  "sdot",
  217 =>  "and",
  218 =>  "or",
  219 =>  "hArr",
  220 =>  "lArr",
  221 =>  "uArr",
  222 =>  "rArr",
  223 =>  "dArr",
  229 =>  "sum",
  242 =>  "int"
}
@@nl2br_ptrn =
/(\r\n)|(\n)|(\r)/
@@msie_ptrn =
/MSIE\s*(\d)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, session = nil, container = nil) ⇒ Component

Returns a new instance of Component.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/htmlgrid/component.rb', line 125

def initialize(model, session = nil, container = nil)
  @model = model
  @session = session
  @lookandfeel = session.lookandfeel if session.respond_to?(:lookandfeel)
  @container = container
  @attributes = self.class::HTML_ATTRIBUTES.dup
  if css_class
    @attributes.store("class", css_class)
  end
  if css_id
    @attributes.store("id", css_id)
  end
  @value = nil
  @label = self.class::LABEL
  init
end

Instance Attribute Details

#attributesObject (readonly)

:nodoc:



123
124
125
# File 'lib/htmlgrid/component.rb', line 123

def attributes
  @attributes
end

#dojo_tooltipObject

Returns the value of attribute dojo_tooltip.



12
13
14
# File 'lib/htmlgrid/dojotoolkit.rb', line 12

def dojo_tooltip
  @dojo_tooltip
end

#label=(value) ⇒ Object (writeonly)

Sets the attribute label

Parameters:

  • value

    the value to set the attribute label to.



206
207
208
# File 'lib/htmlgrid/component.rb', line 206

def label=(value)
  @label = value
end

#modelObject (readonly)

:nodoc:



123
124
125
# File 'lib/htmlgrid/component.rb', line 123

def model
  @model
end

#valueObject

Returns the value of attribute value.



124
125
126
# File 'lib/htmlgrid/component.rb', line 124

def value
  @value
end

Instance Method Details

#_to_html(context, value = @value) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/htmlgrid/component.rb', line 235

def _to_html(context, value = @value)
  if value.is_a?(Array)
    value.collect { |item| _to_html(context, item) }.join(" ")
  elsif value.respond_to?(:to_html)
    value.to_html(context).to_s.dup.force_encoding("utf-8")
  elsif value&.is_a?(String)
    _value = CGI.unescape(value.gsub("+", CGI.escape("+")))
  elsif value&.is_a?(Integer)
    _value = value.to_s
  end
end

#autofill?Boolean

delegator to @container, default definition in Form

Returns:

  • (Boolean)


143
144
145
# File 'lib/htmlgrid/component.rb', line 143

def autofill?
  @container.autofill? if @container.respond_to?(:autofill?)
end

#css_classObject

gets the ‘class’ html-attribute if defined



148
149
150
# File 'lib/htmlgrid/component.rb', line 148

def css_class
  @css_class ||= self.class::CSS_CLASS
end

#css_class=(css_class) ⇒ Object

sets the ‘class’ html-attribute



153
154
155
# File 'lib/htmlgrid/component.rb', line 153

def css_class=(css_class)
  @css_class = @attributes["class"] = css_class
end

#css_idObject

gets the ‘id’ html-attribute if defined



158
159
160
# File 'lib/htmlgrid/component.rb', line 158

def css_id
  @css_id ||= self.class::CSS_ID
end

#css_id=(css_id) ⇒ Object

sets the ‘id’ html-attribute



163
164
165
# File 'lib/htmlgrid/component.rb', line 163

def css_id=(css_id)
  @css_id = @attributes["id"] = css_id
end

#dojo_dynamic_htmlObject



39
40
41
# File 'lib/htmlgrid/dojotoolkit.rb', line 39

def dynamic_html(context)
  ""
end

#dojo_parse_on_loadObject



33
34
35
36
37
# File 'lib/htmlgrid/dojotoolkit.rb', line 33

def dojo_parse_on_load
  if @container.respond_to?(:dojo_parse_on_load)
    @container.dojo_parse_on_load
  end
end

#dojo_tag(widget, args = {}, inner_html = "") ⇒ Object

DOJO_VERSION >= 1.7.0 only (removed old version support)



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/htmlgrid/dojotoolkit.rb', line 14

def dojo_tag(widget, args = {}, inner_html = "")
  div = HtmlGrid::Div.new(@model, @session, self)
  div.set_attribute("data-dojo-type", widget)
  args.each { |key, value|
    if value.is_a?(Array)
      value = value.join(",")
    end
    div.set_attribute(key, value)
  }
  div.value = inner_html
  div
end

#dojo_title=(value) ⇒ Object



27
28
29
30
31
# File 'lib/htmlgrid/dojotoolkit.rb', line 27

def dojo_title=(value)
  tooltip = HtmlGrid::Div.new(@model, @session, self)
  tooltip.value = value
  self.dojo_tooltip = tooltip
end

#dynamic_html(context) ⇒ Object



167
168
169
# File 'lib/htmlgrid/component.rb', line 167

def dynamic_html(context)
  ""
end

#escape(txt) ⇒ Object

escape ‘&’, ‘<’ and ‘>’ characters in txt



172
173
174
175
176
177
# File 'lib/htmlgrid/component.rb', line 172

def escape(txt)
  @@html_entities.each_with_object(txt.to_s.dup) { |map, str|
    char, entity = map
    str.gsub!(char, "&" + entity + ";")
  }
end

#escape_symbols(txt) ⇒ Object

escape symbol-font strings



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/htmlgrid/component.rb', line 180

def escape_symbols(txt)
  esc = ""
  txt.to_s.each_byte { |byte|
    esc += if (entity = @@symbol_entities[byte])
      "&" + entity + ";"
    else
      byte
    end
  }
  esc
end

#formnameObject

delegator to @container, default definition in Form



193
194
195
# File 'lib/htmlgrid/component.rb', line 193

def formname
  @container.formname if @container.respond_to?(:formname)
end

#http_headersObject



197
198
199
# File 'lib/htmlgrid/component.rb', line 197

def http_headers
  self.class::HTTP_HEADERS.dup
end

#label?Boolean

precede this instance with a label?

Returns:

  • (Boolean)


202
203
204
# File 'lib/htmlgrid/component.rb', line 202

def label?
  @label
end

#onclick=(onclick) ⇒ Object



208
209
210
# File 'lib/htmlgrid/component.rb', line 208

def onclick=(onclick)
  @attributes["onclick"] = onclick
end

#onload=(onload) ⇒ Object

delegator to @container, default definition in Template



213
214
215
# File 'lib/htmlgrid/component.rb', line 213

def onload=(onload)
  @container.onload = onload if @container.respond_to? :onload=
end

#onsubmit=(onsubmit) ⇒ Object

delegator to @container, default definition in Form



218
219
220
# File 'lib/htmlgrid/component.rb', line 218

def onsubmit=(onsubmit)
  @container.onsubmit = onsubmit if @container.respond_to? :onsubmit=
end

#set_attribute(key, value) ⇒ Object

set a html attribute



223
224
225
# File 'lib/htmlgrid/component.rb', line 223

def set_attribute(key, value)
  @attributes.store(key, value)
end

#tabindex=(tab) ⇒ Object



227
228
229
# File 'lib/htmlgrid/component.rb', line 227

def tabindex=(tab)
  @attributes.store("tabIndex", tab.to_s)
end

#to_html(context) ⇒ Object



231
232
233
# File 'lib/htmlgrid/component.rb', line 231

def to_html(context)
  _to_html(context, @value).to_s.encode("utf-8")
end