Class: ProMotion::XLForm

Inherits:
Object
  • Object
show all
Defined in:
lib/ProMotion/XLForm/xl_form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form_data, opts = {}) ⇒ XLForm

Returns a new instance of XLForm.



6
7
8
9
10
# File 'lib/ProMotion/XLForm/xl_form.rb', line 6

def initialize(form_data, opts={})
  @form_data = form_data
  @title     = opts[:title] || ''
  @required  = opts[:required]
end

Instance Attribute Details

#form_dataObject (readonly)

Returns the value of attribute form_data.



4
5
6
# File 'lib/ProMotion/XLForm/xl_form.rb', line 4

def form_data
  @form_data
end

Instance Method Details

#buildObject



12
13
14
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
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ProMotion/XLForm/xl_form.rb', line 12

def build
  form                                = XLFormDescriptor.formDescriptorWithTitle(@title)
  form.addAsteriskToRequiredRowsTitle = (@required == :asterisks)

  form_data.each do |section_data|
    title = section_data[:title]

    options     = parse_section_options(section_data[:options])
    insert_mode = section_insert_mode(section_data[:insert_mode])

    section = XLFormSectionDescriptor.formSectionWithTitle(title, sectionOptions: options, sectionInsertMode: insert_mode)
    if options.nonzero?
      tag = section_data[:name]
      mp("MutliValued section with no :name option", force_color: :red) unless tag
      if tag.respond_to? :to_s
        tag = tag.to_s
      end
      section.multivaluedTag = tag
    end
    section.footerTitle = section_data[:footer] if section_data[:footer]

    add_proc section, :on_add, section_data[:on_add] if section_data[:on_add]
    add_proc section, :on_remove, section_data[:on_remove] if section_data[:on_remove]

    form.addFormSection(section)

    section_data[:cells].each do |cell_data|
      tag = cell_data[:name]
      mp("Cell with no :name option", force_color: :red) unless tag
      if tag.respond_to? :to_s
        tag = tag.to_s
      end
      title = cell_data[:title]
      type  = cell_data[:type]
      if type.nil? and cell_data[:cells]
        type = :selector_push
      end

      cell = XLFormRowDescriptor.formRowDescriptorWithTag(tag, rowType: row_type(type), title: title)

      cell.required = cell_data[:required]

      properties = cell_data[:properties] || {}

      # placeholder
      cell.cellConfigAtConfigure.setObject(cell_data[:placeholder], forKey: "textField.placeholder") if cell_data[:placeholder]

      # step_counter
      if cell_data[:type] == :step_counter
        min  = properties[:min]
        max  = properties[:max]
        step = properties[:step]

        cell.cellConfigAtConfigure.setObject(true, forKey: "stepControl.wraps")
        cell.cellConfigAtConfigure.setObject(min, forKey: "stepControl.minimumValue") if min
        cell.cellConfigAtConfigure.setObject(max, forKey: "stepControl.maximumValue") if max
        cell.cellConfigAtConfigure.setObject(step, forKey: "stepControl.maximumValue") if step
      end

      # slider
      if cell_data[:type] == :slider
        min  = properties[:min]
        max  = properties[:max]
        step = properties[:step]
        cell.cellConfigAtConfigure.setObject(min, forKey: "slider.minimumValue") if min
        cell.cellConfigAtConfigure.setObject(max, forKey: "slider.maximumValue") if max
        cell.cellConfigAtConfigure.setObject(step, forKey: "steps") if step
      end

      # dates
      if [:date_inline, :datetime_inline, :time_inline, :date, :datetime, :time, :datepicker].include? cell_data[:type]
        min = properties[:min]
        max = properties[:max]
        cell.cellConfigAtConfigure.setObject(min, forKey: "minimumDate") if min
        cell.cellConfigAtConfigure.setObject(max, forKey: "maximumDate") if max
      end

      cell_class = cell_data[:cell_class]

      # image
      if cell_data[:type] == :image
        cell_class = XLFormImageSelectorCell if cell_class.nil?
      elsif cell_data[:type] == :color
        cell_class = XLFormColorSelectorCell if cell_class.nil?
      end

      cell.cellClass = cell_class if cell_class

      # subcells
      if cell_data[:cells]
        cell.action.viewControllerClass = ProMotion::XLSubFormScreen
        cell.action.cells               = cell_data[:cells]
        cell.valueTransformer           = ProMotion::ValueTransformer
      end

      # also accept default XLForm viewControllerClass
      cell.action.viewControllerClass = cell_data[:view_controller_class] if cell_data[:view_controller_class]
      cell.valueTransformer           = cell_data[:value_transformer] if cell_data[:value_transformer]

      # callbacks
      add_proc cell, :on_change, cell_data[:on_change] if cell_data[:on_change]
      add_proc cell, :on_add, cell_data[:on_add] if cell_data[:on_add]
      add_proc cell, :on_remove, cell_data[:on_remove] if cell_data[:on_remove]

      cell.selectorTitle   = cell_data[:selector_title] if cell_data[:selector_title]
      cell.options = cell_data[:options]

      cell.disabled = !cell_data.fetch(:enabled, true)

      # row visible
      if cell_data[:hidden]
        predicate = cell_data[:hidden]
        if predicate.is_a?(Hash)
          tag = predicate[:name]
          operand = case predicate[:is]
            when :equal
              '=='
            when :not_equal
              '!='
            when :contains
              'contains'
            when :not_contains
              'not contains'
            else
              predicate[:is]
            end
          value = predicate[:value]
        else
          match = /(:?[a-zA-Z_]+)\s+(==|!=|contains|not contains)\s+(.*)/.match(predicate)
          if match and match.length == 4
            # todo better than ignore ?
            tag = match[1]
            operand = match[2]
            value = match[3]
            if value =~ /"(.*)"/
              value = value[1, value.length - 2]
            end
          end
        end

        if tag and operand
          if tag.is_a?(Symbol)
            tag = tag.to_s
          elsif tag.start_with?(':')
            tag[0] = ''
          end
          value = case value
            when 'true', :true, true
              0
            when 'false', :false, false
              1
            when String
              "\"#{value}\""
            else
              value
            end

          if operand == 'contains'
            cell.hidden = "$#{tag} contains[c] #{value}"
          elsif operand == 'not contains'
            cell.hidden = "not($#{tag} contains[c] #{value})"
          else
            cell.hidden = "$#{tag} #{operand} #{value}"
          end
        else
          mp "#{cell_data[:visible]} can not be parsed", force_color: :red
        end
      end

      # validators
      if cell_data[:validators]
        validators = cell_data[:validators]
        validators.each do |key, value|
          validator = case key
          when :email
            XLFormValidator.emailValidator
          when :regex
            regex = value[:regex]
            if regex.is_a?(String)
              XLFormRegexValidator.formRegexValidatorWithMsg(value[:message], regex: regex)
            elsif regex.is_a?(Regexp)
              ProMotion::RegexValidator.validator(value[:message], regex)
            else
              mp "Invalid regex : #{regex.inspect}. Please provides a Regexp or a String", force_color: :red
              nil
            end
          when :url
            ProMotion::UrlValidator.validator
          else
            if value.is_a?(ProMotion::Validator) or value.respond_to?(:isValid)
              value
            else
              mp "Invalid validator : #{key}", force_color: :red
              nil
            end
          end

          if validator
            cell.addValidator(validator)
          end
        end
      end

      # customization
      appearance = cell_data[:appearance]
      if appearance
        cell.cellConfig["textLabel.font"] = appearance[:font] if appearance[:font]
        cell.cellConfig["textLabel.textColor"] = appearance[:color] if appearance[:color]
        cell.cellConfig["detailTextLabel.font"] = appearance[:detail_font] if appearance[:detail_font]
        cell.cellConfig["detailTextLabel.textColor"] = appearance[:detail_color] if appearance[:detail_color]
        cell.cellConfig["backgroundColor"] = appearance[:background_color] if appearance[:background_color]

        appearance.delete_if {|k,v| k.is_a?(Symbol)}.each do |k,v|
          cell.cellConfig[k] = v
        end
      end

      value = cell_data[:value]
      if value and cell.selectorOptions
        cell.selectorOptions.each do |opt|
          if opt.formValue == value
            value = opt
            break
          end
        end
      end

      if value === true or value === false
        value = value ? 1 : 0
      end

      cell.value = value

      section.addFormRow(cell)

      # multi sections
      if section.multivaluedTag
        cell.action.required           = @required
        section.multivaluedRowTemplate = cell.copy
      end
    end
  end

  form
end

#get_callback(row, event) ⇒ Object



258
259
260
261
262
# File 'lib/ProMotion/XLForm/xl_form.rb', line 258

def get_callback(row, event)
  return if @blocks.nil? or @blocks[row].nil? or @blocks[row][event].nil?

  @blocks[row][event]
end