Class: Objs::Element

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/objs/element.rb

Constant Summary collapse

KEYWORD =
'element'.freeze
KEYWORD_SHORT =
'e'.freeze
ID =

Element

'id'.freeze
CLASSES =
'classes'.freeze
ATTRIBUTES =
'attributes'.freeze
CONTENT =
'content'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.doc_dataObject

Get the object's documentation data.



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/objs/element.rb', line 258

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'An HTML element.',
    :children => [
      'attributes (container) — All attributes are optional. The attributes of the HTML element, e.g. id (string) — the element id; classes (string) — element classes and styles (css).',
      'content (container) — The element contents.'
    ],
    :messages => [
      'render — Manually render the HTML element. Normally the render is called by the web server when the page containing the element is requested.'
    ],
    :examples => <<~EXAMPLES.strip
      div_sub [e] :
        attributes [can] :
          id [string] : sub_area
          class [string] : mt-3
        content [can] :
          h2 [e] : Sub
          p [e] :
            attributes [can] :
              class [string] : lead
            content [can] :
              msg [string] : Sub area text goes here.
    EXAMPLES
  }
end

.messagesObject

Get a list of message names that this object receives.



167
168
169
# File 'lib/objs/element.rb', line 167

def self.messages
  return super + [ 'render' ]
end

.render_obj(obj, render_ƒ, engine) ⇒ Object

Render an object which might be an element, a container of items, or something else.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/objs/element.rb', line 218

def self.render_obj obj, render_ƒ, engine
  rendered_obj_content = ''
  return nil unless obj

  if obj.children.size > 0
    obj.children.each do |e|

      e = Gloo::Objs::Alias.resolve_alias( engine, e )
      if e.class == Element
        rendered_obj_content << e.send( render_ƒ )
      elsif e.class == Form
        rendered_obj_content << e.render
      elsif e
        data = render_thing e, render_ƒ, engine
        ( rendered_obj_content << data ) if data # e.render( render_ƒ )
      end
    end
  else
    rendered_obj_content << obj.value
  end

  return rendered_obj_content
end

.render_thing(e, render_ƒ, engine) ⇒ Object



242
243
244
245
246
247
248
249
# File 'lib/objs/element.rb', line 242

def self.render_thing e, render_ƒ, engine
  begin
    return e.render( render_ƒ )
  rescue => e
    engine.log_exception e
    return ''
  end
end

.short_typenameObject

The short name of the object type.



39
40
41
# File 'lib/objs/element.rb', line 39

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



32
33
34
# File 'lib/objs/element.rb', line 32

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


139
140
141
# File 'lib/objs/element.rb', line 139

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



148
149
150
151
152
153
154
155
156
157
# File 'lib/objs/element.rb', line 148

def add_default_children
  fac = @engine.factory

  # Create attributes with ID and Classes
  attr = fac.create_can ATTRIBUTES, self
  fac.create_string ID, '', attr
  fac.create_string CLASSES, '', attr

  fac.create_can CONTENT, self
end

#attributes_hashObject

Return the array of attributes if there are any.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/objs/element.rb', line 61

def attributes_hash
  attr_can = find_child ATTRIBUTES

  if attr_can && attr_can.children.size > 0
    h = {}
    attr_can.children.each do |o|
      h[ o.name ] = o.value
    end
    return h
  end

  return {}
end

#content_childObject

Get all the children elements of the content.



126
127
128
# File 'lib/objs/element.rb', line 126

def content_child
  return find_child CONTENT
end

#msg_renderObject

Get the expiration date for the certificate.



174
175
176
177
178
# File 'lib/objs/element.rb', line 174

def msg_render
  content = self.render_html
  @engine.heap.it.set_to content 
  return content
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:

  • (Boolean)


54
55
56
# File 'lib/objs/element.rb', line 54

def multiline_value?
  return false
end

#render_content(render_ƒ) ⇒ Object

Render the element content using the specified render function. This is a recursive function (through one of the other render functions).



207
208
209
210
211
212
# File 'lib/objs/element.rb', line 207

def render_content render_ƒ
  obj = content_child
  obj = self if obj.nil?

  return Element.render_obj( obj, render_ƒ, @engine )
end

#render_htmlObject

Render the element as HTML.



188
189
190
191
192
# File 'lib/objs/element.rb', line 188

def render_html
  content_text = render_content :render_html

  return "#{tag_open}#{content_text}#{tag_close}"
end

#render_textObject

Render the element as text, without tags.



197
198
199
200
201
# File 'lib/objs/element.rb', line 197

def render_text
  content_text = render_content :render_text

  return "#{content_text}"
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



46
47
48
# File 'lib/objs/element.rb', line 46

def set_value( new_value )
  self.value = new_value.to_s
end

#tagObject

Get the tag. This is the name, up until an '_' char. Because name must be unique in the parent, and with HTML we need a way to have multiple of the same tag at the same level.



99
100
101
102
# File 'lib/objs/element.rb', line 99

def tag
  i = self.name.index( '_' )        
  return i ? self.name[ 0..(i-1) ] : self.name
end

#tag_attributesObject

Get all attributes of the tag.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/objs/element.rb', line 78

def tag_attributes
  attr_h = attributes_hash
  return nil unless attr_h && attr_h.size > 0

  attr_str = ''
  attr_h.each do |k,v|
    unless v.blank?
      attr_str << " #{k}=\"#{v}\"" 
    end
  end

  return attr_str
end

#tag_closeObject

Get the closing tag.



119
120
121
# File 'lib/objs/element.rb', line 119

def tag_close
  return "</#{tag}>"
end

#tag_openObject

Get the opening tag.



107
108
109
110
111
112
113
114
# File 'lib/objs/element.rb', line 107

def tag_open
  tag_attributes = self.tag_attributes
  if tag_attributes
    return "<#{tag}#{tag_attributes}>"
  else
    return "<#{tag}>"
  end
end