Class: Dima::Html::Field

Inherits:
Element show all
Includes:
Init
Defined in:
lib/dima/html/field.rb

Overview

Field is used in a form.

Direct Known Subclasses

ComplexField, SimpleField

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Init

#initialize

Methods inherited from Element

#id, #id=

Instance Attribute Details

#afterObject

element to be placed AFTER the field content



42
43
44
# File 'lib/dima/html/field.rb', line 42

def after
  @after
end

#alignObject

text align: used for correct display in tables



46
47
48
# File 'lib/dima/html/field.rb', line 46

def align
  @align
end

#beforeObject

element to be placed BEFORE the field content



40
41
42
# File 'lib/dima/html/field.rb', line 40

def before
  @before
end

#editObject

edit mode switch



48
49
50
# File 'lib/dima/html/field.rb', line 48

def edit
  @edit
end

#formObject

form holding this field



30
31
32
# File 'lib/dima/html/field.rb', line 30

def form
  @form
end

#has_errorsObject

Returns the value of attribute has_errors.



50
51
52
# File 'lib/dima/html/field.rb', line 50

def has_errors
  @has_errors
end

#hintObject

hint message



36
37
38
# File 'lib/dima/html/field.rb', line 36

def hint
  @hint
end

#labelObject

label describes this field



32
33
34
# File 'lib/dima/html/field.rb', line 32

def label
  @label
end

#requiredObject

is this field required?



38
39
40
# File 'lib/dima/html/field.rb', line 38

def required
  @required
end

#tooltipObject

tooltip



34
35
36
# File 'lib/dima/html/field.rb', line 34

def tooltip
  @tooltip
end

#urlObject

url property



44
45
46
# File 'lib/dima/html/field.rb', line 44

def url
  @url
end

Instance Method Details

#after_nodeObject

After node.



127
128
129
# File 'lib/dima/html/field.rb', line 127

def after_node
  Node.new(tag: 'span', attributes: { class: 'dim-after' }, text: self.after) if self.after
end

#before_nodeObject

Before node.



122
123
124
# File 'lib/dima/html/field.rb', line 122

def before_node
  Node.new(tag: 'span', attributes: { class: 'dim-before' }, text: self.before) if self.before
end

#edit?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/dima/html/field.rb', line 72

def edit?
  self.edit and not self.readonly
end

#empty_nodeObject

Empty node.



110
111
112
# File 'lib/dima/html/field.rb', line 110

def empty_node
  Node.new(tag: 'span', attributes: { class: 'dim-empty' }, text: '(empty)')
end

#label_nodeObject

Prepare label node for this field.



115
116
117
118
119
# File 'lib/dima/html/field.rb', line 115

def label_node
  unless @label == false
    Node.new(tag: 'div', attributes: { class: 'dim-label' }, text: @label)
  end
end

#optionsObject

Additional properties.



68
69
70
# File 'lib/dima/html/field.rb', line 68

def options
  @options ||= {}
end

#to_nObject

Create html node for the field.



77
78
79
80
81
82
83
84
# File 'lib/dima/html/field.rb', line 77

def to_n
  field = Node.new(tag: 'div', attributes: { id: self.id, class: 'dim-field' })
  field.add_class('dim-required') if required
  field.add_class('dim-error') if has_errors
  field << label_node
  field << value_node
  field
end

#url_nodeObject

Url node.



132
133
134
135
136
137
138
139
140
# File 'lib/dima/html/field.rb', line 132

def url_node
  if self.url
    if self.url.is_a?(Proc) and self.model
      Node.new(tag: 'a', attributes: {href: self.url.call(self.model)})
    else
      Node.new(tag: 'a', attributes: {href: self.url.to_s})
    end
  end
end

#valObject

Get current value of the field.



58
59
60
# File 'lib/dima/html/field.rb', line 58

def val
  @val
end

#val=(v) ⇒ Object

Set the value of the field.



63
64
65
# File 'lib/dima/html/field.rb', line 63

def val=(v)
  @val = v
end

#validateObject



52
53
54
55
# File 'lib/dima/html/field.rb', line 52

def validate
  self.has_errors = false
  self.has_errors = true if self.required and Dima::Utils.empty?(@val)
end

#value_nodeObject

Prepare content node.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dima/html/field.rb', line 87

def value_node
  content = Node.new(tag: 'div', attributes: { class: 'dim-value' })
  if empty? and not self.edit?
    content << empty_node
  else
    content.add_class(align.to_s) if align
    content << before_node
    ## content + link node
    link_node = url_node
    if link_node and not self.edit
      link_node << main_content_node
      content << link_node
    else
      content << main_content_node
    end
    ##
    content << after_node
    content
  end
  content
end