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_data ⇒ Object (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

#build ⇒ Object



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
# 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?
      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]
      options              = parse_options(cell_data[:options])
      cell.selectorOptions = options

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

      cell.value    = value if value

      cell.disabled = !cell_data[:enabled] if cell_data[:enabled]

      # 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

      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



179
180
181
182
183
# File 'lib/ProMotion/XLForm/xl_form.rb', line 179

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

  @blocks[row][event]
end