Class: Formotion::Row

Inherits:
Base show all
Defined in:
lib/formotion/row/row.rb

Constant Summary collapse

PROPERTIES =
[
  # @form.render will contain row's value as the value for this key.
  :key,
  # the user's (or configured) value for this row.
  :value,
  # set as cell.titleLabel.text
  :title,
  # set as cell.imageView.image
  :image,
  # an image placeholder for cell.imageView.image when using remote images
  :image_placeholder,
  # set as cell.detailLabel.text
  :subtitle,
  # configures the type of input this is (string, phone, switch, etc)
  # either Formotion::RowType or a string/symbol representation of one
  # see row_type.rb
  :type,
  # Stores possible date pickers mode; corresponds to UIDatePickerMode______
  # OPTIONS: :time, :date, :date_time, :countdown
  # DEFAULT is :date
  :picker_mode,
  # Stores possible formatting information (used by date pickers, etc)
  #   if :type == :date, accepts values in [:short, :medium, :long, :full]
  :format,
  # alternative title for row (only used in EditRow for now)
  :alt_title,
  # determines if the user can edit the row
  # OPTIONS: true, false
  # DEFAULT: true
  :editable,

  # The following apply only to text-input fields

  # text alignment of the input field
  # OPTIONS: :left, :right, :center
  # DEFAULT is :right
  :text_alignment,
  # placeholder text
  :placeholder,
  # accessibility label
  :accessibility,
  # whether or not the entry field is secure (like a password)
  :secure,
  # given by a UIReturnKey___ integer, string, or symbol
  # EX :default, :google
  :return_key,
  # given by a  UITextAutocorrectionType___ integer, string, or symbol
  # EX :yes, :no, :default
  :auto_correction,
  # given by a UITextAutocapitalizationType___ integer, string, or symbol
  # EX :none, :words
  :auto_capitalization,
  # field.clearButtonMode; given by a UITextFieldViewMode__ integer, string, symbol
  # EX :never, :while_editing
  # DEFAULT is nil, which is used as :while_editing
  :clear_button,
  # row height as integer; used for heightForRowAtIndexPath
  # EX 200
  # DEFAULT is nil, which is used as the tableView.row_height
  :row_height,
  # range used for slider min and max value
  # EX (1..100)
  # DEFAULT is (1..10)
  :range,
  # Array used for control row items
  # EX ['free', 'pro']
  # DEFAULT is []
  :items,
  # A hash for a Form used for subforms
  # DEFAULT is nil
  :subform,
  # Used in a subform row; when given,
  # will display the value of the matching key
  # of the subform's rendering.
  # DEFAULT is nil
  :display_key,
  # A hash for a Row used for templates
  # DEFAULT is nil
  :template,
  # Indents row when set to true
  # DEFAULT is false
  :indented,
  # Shows a delete sign next to the row
  # DEFAULT is false
  :deletable,
  # When a row is deleted, actually remove the row from UI
  # instead of just nil'ing the value.
  # DEFAULT is false EXCEPT for template-generated rows
  :remove_on_delete,
  # In a date/time or time picker, the minute interval can
  # be set. That allows picking by every 15 minutes, etc.
  :minute_interval,
  #-Resize image when needed (size as Array [1500,1500])
  :max_image_size,
  # Font for String and Text rows
  :font,
  # Display an inputAccessoryView when editing a StringRow.
  # OPTIONS: :done (a black translucent toolbar with a right-aligned "Done" button)
  # DEFAULT is nil
  :input_accessory,
  # Call a method when the input accessory is tapped.
  # Default is nil
  :done_action,
  # Cell selection style
  # OPTIONS: :blue, :gray, :none
  # DEFAULT is :blue
  :selection_style,

  # The following apply only to weblink rows

  # Whether or not to display a warning to the user before leaving the app.
  # DEFAULT is false
  :warn
]
BOOLEAN_PROPERTIES =
[:secure, :indented, :deletable, :remove_on_delete]
SERIALIZE_PROPERTIES =
PROPERTIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#copyWithZone, #encodeWithCoder, #hash, #initWithCoder, #isEqual

Constructor Details

#initialize(params = {}) ⇒ Row

Returns a new instance of Row.



165
166
167
168
169
170
171
172
# File 'lib/formotion/row/row.rb', line 165

def initialize(params = {})
  super

  BOOLEAN_PROPERTIES.each { |prop|
    Formotion::Conditions.assert_nil_or_boolean(self.send(prop))
  }
  @template_children = []
end

Instance Attribute Details

#indexObject

Index of the row in the section



132
133
134
# File 'lib/formotion/row/row.rb', line 132

def index
  @index
end

#objectObject

RowType object



159
160
161
# File 'lib/formotion/row/row.rb', line 159

def object
  @object
end

#on_begin_callbackObject

callback for what happens when the user starts editing #text_field.



149
150
151
# File 'lib/formotion/row/row.rb', line 149

def on_begin_callback
  @on_begin_callback
end

#on_delete_callbackObject

callback for when a row is tapped



154
155
156
# File 'lib/formotion/row/row.rb', line 154

def on_delete_callback
  @on_delete_callback
end

#on_end_callbackObject

callback for when a row is exited



156
157
158
# File 'lib/formotion/row/row.rb', line 156

def on_end_callback
  @on_end_callback
end

#on_enter_callbackObject

callback for what happens when the user hits the enter key while editing #text_field



146
147
148
# File 'lib/formotion/row/row.rb', line 146

def on_enter_callback
  @on_enter_callback
end

#on_tap_callbackObject

callback for what happens when the user taps a ButtonRow



152
153
154
# File 'lib/formotion/row/row.rb', line 152

def on_tap_callback
  @on_tap_callback
end

#reuse_identifierObject

The reuse-identifier used in UITableViews By default is a stringification of section.index and row.index, thus is unique per row (bad for memory, to fix later.)



137
138
139
# File 'lib/formotion/row/row.rb', line 137

def reuse_identifier
  @reuse_identifier
end

#sectionObject

Reference to the row’s section



129
130
131
# File 'lib/formotion/row/row.rb', line 129

def section
  @section
end

#template_childrenObject

Returns the value of attribute template_children.



163
164
165
# File 'lib/formotion/row/row.rb', line 163

def template_children
  @template_children
end

#template_parentObject

Owning template row, if applicable



162
163
164
# File 'lib/formotion/row/row.rb', line 162

def template_parent
  @template_parent
end

#text_fieldObject (readonly)

reference to the row’s UITextField.



142
143
144
# File 'lib/formotion/row/row.rb', line 142

def text_field
  @text_field
end

Instance Method Details

#after_createObject

called after section and index have been assigned



175
176
177
178
179
# File 'lib/formotion/row/row.rb', line 175

def after_create
  if self.type == :template
    self.object.update_template_rows
  end
end

#auto_capitalization=(value) ⇒ Object



273
274
275
# File 'lib/formotion/row/row.rb', line 273

def auto_capitalization=(value)
  @auto_capitalization = const_int_get("UITextAutocapitalizationType", value)
end

#auto_correction=(value) ⇒ Object



269
270
271
# File 'lib/formotion/row/row.rb', line 269

def auto_correction=(value)
  @auto_correction = const_int_get("UITextAutocorrectionType", value)
end

#button?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/formotion/row/row.rb', line 223

def button?
  object.button?
end

#clear_button=(value) ⇒ Object



277
278
279
# File 'lib/formotion/row/row.rb', line 277

def clear_button=(value)
  @clear_button = const_int_get("UITextFieldViewMode", value)
end

#editable=(editable) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/formotion/row/row.rb', line 289

def editable=(editable)
  case editable
  when TrueClass
  when FalseClass
  when NSNull
    editable = false
  when NilClass
    editable = false
  when NSString
    editable = (editable == "true")
  else
    raise Formotion::InvalidClassError, "Invalid class for `Row#editable`: #{editable.inspect}; should be TrueClass, FalseCLass, or NSString"
  end
  @editable = editable
end

#editable?Boolean

Returns:

  • (Boolean)


305
306
307
308
309
310
# File 'lib/formotion/row/row.rb', line 305

def editable?
  if !self.editable.nil?
    return self.editable
  end
  true
end

#formObject



196
197
198
# File 'lib/formotion/row/row.rb', line 196

def form
  self.section.form
end

#index_pathObject



192
193
194
# File 'lib/formotion/row/row.rb', line 192

def index_path
  NSIndexPath.indexPathForRow(self.index, inSection:self.section.index)
end

#itemsObject

getter overrides



237
238
239
240
241
242
# File 'lib/formotion/row/row.rb', line 237

def items
  if @items.respond_to?(:call)
    @items = @items.call
  end
  @items
end

#make_cellObject

Methods for making cells Called in UITableViewDataSource methods in form_delegate.rb



352
353
354
355
356
357
358
359
360
# File 'lib/formotion/row/row.rb', line 352

def make_cell
  if self.object.nil?
    raise Formotion::NoRowTypeError, "No row type specified for row #{self.index_path.row} in section #{self.index_path.section}; specify a :type"
  end
  cell, text_field = Formotion::RowCellBuilder.make_cell(self)
  @text_field = text_field
  self.object.after_build(cell)
  cell
end

#next_rowObject



204
205
206
207
208
209
210
211
212
# File 'lib/formotion/row/row.rb', line 204

def next_row
  # if there are more rows in this section, use that.
  return self.section.rows[self.index + 1] if self.index < (self.section.rows.count - 1)

  # if there are more sections, then use the first row of that section.
  return self.section.next_section.rows[0] if self.section.next_section

  nil
end

#on_begin(&block) ⇒ Object



319
320
321
# File 'lib/formotion/row/row.rb', line 319

def on_begin(&block)
  self.on_begin_callback = block
end

#on_delete(&block) ⇒ Object



338
339
340
341
342
343
344
345
346
# File 'lib/formotion/row/row.rb', line 338

def on_delete(&block)
  self.on_delete_callback = block
  # set on_tap for all template children
  if self.type == :template
    for templ in self.template_children do
      templ.on_delete_callback = block
    end
  end
end

#on_end(&block) ⇒ Object



323
324
325
# File 'lib/formotion/row/row.rb', line 323

def on_end(&block)
  self.on_end_callback = block
end

#on_enter(&block) ⇒ Object

setters for callbacks



315
316
317
# File 'lib/formotion/row/row.rb', line 315

def on_enter(&block)
  self.on_enter_callback = block
end

#on_tap(&block) ⇒ Object

Used in :button type rows



328
329
330
331
332
333
334
335
336
# File 'lib/formotion/row/row.rb', line 328

def on_tap(&block)
  self.on_tap_callback = block
  # set on_tap for all template children
  if self.type == :template
    for templ in self.template_children do
      templ.on_tap_callback = block
    end
  end
end

#previous_rowObject



214
215
216
217
218
219
220
221
# File 'lib/formotion/row/row.rb', line 214

def previous_row
  return self.section.rows[self.index - 1] if self.index > 0

  # if there are more sections, then use the first row of that section.
  return self.section.previous_section.rows[-1] if self.section.previous_section

  nil
end

#range=(range) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/formotion/row/row.rb', line 251

def range=(range)
  if range
    case range
    when Range
      # all good
    when Array
      range = Range.new(range[0], range[1])
    else
      raise Formotion::InvalidClassError, "Attempted Row.range = #{range.inspect} should be of type Range or Array"
    end
  end
  @range = range
end

#return_key=(value) ⇒ Object



265
266
267
# File 'lib/formotion/row/row.rb', line 265

def return_key=(value)
  @return_key = const_int_get("UIReturnKey", value)
end

#selection_style=(style) ⇒ Object



285
286
287
# File 'lib/formotion/row/row.rb', line 285

def selection_style=(style)
  @selection_style = const_int_get("UITableViewCellSelectionStyle", style || :blue)
end

#subform=(subform) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/formotion/row/row.rb', line 382

def subform=(subform)
  @subform = subform
  # enables you do to row.subform.to_form
  @subform.instance_eval do
    def to_form
      return @hash_subform if @hash_subform
      if self.is_a? Hash
        @hash_subform = Formotion::Form.new(self)
      elsif not self.is_a? Formotion::Form
        raise Formotion::InvalidClassError, "Attempted subform = '#{self.inspect}' should be of type Formotion::Form or Hash"
      end
      @hash_subform ||= self
    end
  end
  @subform
end

#subform?Boolean

Returns:

  • (Boolean)


227
228
229
# File 'lib/formotion/row/row.rb', line 227

def subform?
  self.type.to_s == "subform"
end

#templated?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/formotion/row/row.rb', line 231

def templated?
  !!self.template_parent
end

#text_alignment=(alignment) ⇒ Object



281
282
283
# File 'lib/formotion/row/row.rb', line 281

def text_alignment=(alignment)
  @text_alignment = const_int_get("NSTextAlignment", alignment)
end

#to_hashObject

Retreiving data



371
372
373
374
375
376
377
378
379
380
# File 'lib/formotion/row/row.rb', line 371

def to_hash
  h = super
  if h[:range] && h[:range].is_a?(Range)
    h[:range] = [self.range.begin, self.range.end]
  end
  if subform?
    h[:subform] = self.subform.to_form.to_hash
  end
  h
end

#type=(type) ⇒ Object

setter overrides



246
247
248
249
# File 'lib/formotion/row/row.rb', line 246

def type=(type)
  @object = Formotion::RowType.for(type).new(self)
  @type = type
end

#update_cell(cell) ⇒ Object

Called on every tableView:cellForRowAtIndexPath: so keep implementation details minimal



364
365
366
367
# File 'lib/formotion/row/row.rb', line 364

def update_cell(cell)
  self.object.update_cell(cell)
  cell
end

#value_for_save_hashObject

pseudo-properties



184
185
186
187
188
189
190
# File 'lib/formotion/row/row.rb', line 184

def value_for_save_hash
  if self.object.respond_to? :value_for_save_hash
    self.object.value_for_save_hash
  else
    self.value
  end
end