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.



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

def dojo_tooltip
  @dojo_tooltip
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



228
229
230
231
232
233
234
235
236
# File 'lib/htmlgrid/component.rb', line 228

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
  else
    value.to_s.gsub(@@nl2br_ptrn, '<br>')
  end
end

#autofill?Boolean

delegator to @container, default definition in Form

Returns:

  • (Boolean)


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

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

#css_classObject

gets the ‘class’ html-attribute if defined



146
147
148
# File 'lib/htmlgrid/component.rb', line 146

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

#css_class=(css_class) ⇒ Object

sets the ‘class’ html-attribute



150
151
152
# File 'lib/htmlgrid/component.rb', line 150

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

#css_idObject

gets the ‘id’ html-attribute if defined



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

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

#css_id=(css_id) ⇒ Object

sets the ‘id’ html-attribute



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

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

#dojo_9?Boolean

Returns:

  • (Boolean)


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

def dojo_9?
  defined?(::DOJO_VERSION) && ::DOJO_VERSION >= '0.9'
end

#dojo_dynamic_htmlObject



53
54
55
# File 'lib/htmlgrid/dojotoolkit.rb', line 53

def dynamic_html(context)
  ''
end

#dojo_parse_widgetsObject



47
48
49
50
51
# File 'lib/htmlgrid/dojotoolkit.rb', line 47

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

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/htmlgrid/dojotoolkit.rb', line 15

def dojo_tag(widget, args={}, inner_html='')
  # <dojo:#{widget} ...> does not work on konqueror as of 
  # 02.06.2006. In combination with DOJO_DEBUG = true it even 
  # hangs konqueror.
=begin
  dojo_tag = "<div dojoType=\"#{widget}\""
  args.each { |key, value|
    if(value.is_a?(Array))
      dojo_tag << " #{key}=\"#{value.join(';')}\"" 
    else
      dojo_tag << " #{key}=\"#{value}\"" 
    end
  }
  dojo_tag << "></div>"
=end

    div = HtmlGrid::Div.new(@model, @session, self)
    div.set_attribute('dojoType', widget)
    lim = dojo_9? ? "," : ";"
    args.each { |key, value|
      if(value.is_a?(Array))
        value = value.join(lim)
      end
      div.set_attribute(key, value)
    }
    div.value = inner_html
    div
end

#dojo_title=(value) ⇒ Object



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

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

#dynamic_html(context) ⇒ Object



161
162
163
# File 'lib/htmlgrid/component.rb', line 161

def dynamic_html(context)
  ''
end

#escape(txt) ⇒ Object

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



165
166
167
168
169
170
171
# File 'lib/htmlgrid/component.rb', line 165

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

#escape_symbols(txt) ⇒ Object

escape symbol-font strings



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/htmlgrid/component.rb', line 173

def escape_symbols(txt)
  esc = ''
  txt.to_s.each_byte { |byte|
    esc << if(entity = @@symbol_entities[byte])
      '&' << entity << ';'
    else
        if RUBY_VERSION >= '1.9'
          byte
        else
          byte.chr
        end
    end
  }
  esc
end

#formnameObject

delegator to @container, default definition in Form



189
190
191
# File 'lib/htmlgrid/component.rb', line 189

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

#http_headersObject



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

def http_headers
  self::class::HTTP_HEADERS.dup
end

#label=(boolean) ⇒ Object



199
200
201
# File 'lib/htmlgrid/component.rb', line 199

def label=(boolean)
  @label = boolean
end

#label?Boolean

precede this instance with a label?

Returns:

  • (Boolean)


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

def label?
  @label
end

#onclick=(onclick) ⇒ Object



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

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

#onload=(onload) ⇒ Object

delegator to @container, default definition in Template



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

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

#onsubmit=(onsubmit) ⇒ Object

delegator to @container, default definition in Form



210
211
212
# File 'lib/htmlgrid/component.rb', line 210

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

#set_attribute(key, value) ⇒ Object

set a html attribute



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

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

#tabindex=(tab) ⇒ Object



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

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

#to_html(context) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/htmlgrid/component.rb', line 220

def to_html(context)
  if RUBY_VERSION > "1.9"
    _to_html(context, @value).to_s.force_encoding('utf-8')
  else
    _to_html(context, @value).to_s
  end
end