Class: Aurita::GUI::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/aurita-gui/element.rb

Overview

GUI::Element is the base class for any rendering implementation. It consists of the following members:

* @tag: The HTML tag to render. 
* @attrib: A hash storing tag attributes, like 
           { :href => '/link/to/somewhere' }
* @content: Content this element is wrapping. 
            Content can be set in the constructor 
            via parameter :content or using a 
            block or by #content and #content=.

Most methods invoked on an Element instance are redirected to return or set a tag attribute. Example:

link = Element(:tag => :a) { 'klick me' }
link.href = '/link/to/somewhere'

Same as

link = Element(:tag => :a, 
               :content => 'click me', 
               :href => '/link/to/somewhere')

An Element instance can wrap one or more other elements:

image_link = Element.new(:tag => :a, :href => '/link/') { 
               Element.new(:tag => :img, :src => '/an_image.png')
             }

In case an element has no content, it will render a self-closing tag, like <img … />.

In most cases you won’t use class Element directly, but by using a factory like Aurita::GUI::HTML or by any derived class like Aurita::GUI::Form or Aurita::GUI::Table.

Constant Summary collapse

@@element_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Element

Returns a new instance of Element.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aurita-gui/element.rb', line 51

def initialize(params={}, &block) 
  @@element_count += 1
  @id          = @@element_count
  @parent      = params[:parent]
  @force_close = params[:force_closing_tag]
  params.delete(:parent)
  params.delete(:force_closing_tag)

  params[:tag] = :div if params[:tag].nil?

  if block_given? then
    @content = yield
  else
    @content = params[:content]
    @content = [ @content ] unless @content.kind_of? Array
  end
  params.delete(:content)
  @tag     = params[:tag]
  params.delete(:tag)
  # params[:id] = self.class.to_s.split('::')[-1].downcase + '_' << @@element_count.to_s if params[:id].nil?
  params[:onclick] << ';' unless params[:onclick].nil? or params[:onclick].include?(';')
  
  @attrib = params
  # @attrib[:id] = @attrib[:id].to_s 
  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, value = nil) ⇒ Object

Redirect methods to setting or retreiving tag attributes.



103
104
105
106
# File 'lib/aurita-gui/element.rb', line 103

def method_missing(meth, value=nil)
  return @attrib[meth] unless value or meth.to_s.include? '='
  @attrib[meth.to_s.gsub('=','').intern] = value
end

Instance Attribute Details

#attribObject

Returns the value of attribute attrib.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def attrib
  @attrib
end

#contentObject

Returns the value of attribute content.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def content
  @content
end

#parentObject

Returns the value of attribute parent.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def parent
  @parent
end

#tagObject

Returns the value of attribute tag.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



49
50
51
# File 'lib/aurita-gui/element.rb', line 49

def type
  @type
end

Instance Method Details

#+(other) ⇒ Object Also known as: <<

Render this element to a string and append another element.



89
90
91
92
# File 'lib/aurita-gui/element.rb', line 89

def +(other)
#     return string << other.string if other.kind_of? Element
  return [ self, other ]
end

#[](index) ⇒ Object

Do not redirect random access operators.



125
126
127
128
# File 'lib/aurita-gui/element.rb', line 125

def [](index)
  return @content[index]
   #   raise ::Exception.new('Undefined method [] for ' << self.class.to_s)
end

#[]=(index, element) ⇒ Object

Do not redirect random access operators.



130
131
132
133
# File 'lib/aurita-gui/element.rb', line 130

def []=(index,element)
  @content[index] = element
   #  raise ::Exception.new('Undefined method []= for ' << self.class.to_s)
end

#clear_floatingObject

Static helper definition for clearing CSS floats.



143
144
145
# File 'lib/aurita-gui/element.rb', line 143

def clear_floating
  '<div style="clear: both;" />'
end

#dom_idObject

Return DOM id of this element.



79
80
81
# File 'lib/aurita-gui/element.rb', line 79

def dom_id
  @attrib[:id]
end

#dom_id=(value) ⇒ Object

Set DOM id of this element.



83
84
85
# File 'lib/aurita-gui/element.rb', line 83

def dom_id=(value)
  @attrib[:id] = value
end

#each(&block) ⇒ Object



174
175
176
# File 'lib/aurita-gui/element.rb', line 174

def each(&block)
  @content.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/aurita-gui/element.rb', line 137

def empty? 
  @content.length == 0
end

#idObject

Alias definition for #dom_id()



120
121
122
# File 'lib/aurita-gui/element.rb', line 120

def id
  @attrib[:id]
end

#id=(value) ⇒ Object

Alias definition for #dom_id=(value) Define explicitly so built-in method #id is not invoked instead



116
117
118
# File 'lib/aurita-gui/element.rb', line 116

def id=(value)
  @attrib[:id] = value
end

#lengthObject

raise ::Exception.new(‘Undefined method []= for ’ << self.class.to_s)



134
135
136
# File 'lib/aurita-gui/element.rb', line 134

def length
  @content.length
end

#stringObject Also known as: to_s

Render this element to a string.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/aurita-gui/element.rb', line 148

def string
  attrib_string = ''
  @attrib.each_pair { |name,value|
    if value.instance_of?(Array) then
      value = value.join(' ')
    elsif
      value.instance_of?(TrueClass) then
      value = name
    end
    if !value.nil? then
      value = value.to_s
      attrib_string << name.to_s + '="' << value + '" '
    end
  }
  
  if @force_close || content.to_s != '' then
    '<' << @tag.to_s << ' ' << attrib_string << '>' << "\n  " << 
    content.to_s.gsub("\n","\n  ") + "\n" << 
    '</' << @tag.to_s << '>'
  else
    '<' << @tag.to_s << ' ' << attrib_string << '/>' 
  end
  
end

#to_aryObject Also known as: to_a



95
96
97
# File 'lib/aurita-gui/element.rb', line 95

def to_ary
  [ self ]
end