Class: MotionPrime::FormSection

Inherits:
TableSection show all
Defined in:
motion-prime/sections/form.rb

Constant Summary

Constants inherited from Section

Section::DEFAULT_CONTENT_HEIGHT, Section::KEYBOARD_HEIGHT_LANDSCAPE, Section::KEYBOARD_HEIGHT_PORTRAIT

Instance Attribute Summary collapse

Attributes inherited from TableSection

#decelerating, #did_appear, #group_header_options, #group_header_sections, #table_element

Attributes inherited from Section

#elements, #model, #name, #options, #screen, #section_styles

Attributes included from DrawSectionMixin

#cached_draw_image, #container_element, #container_gesture_recognizers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TableSection

#add_cell_sections, #cell_for_index, #cell_name, #cell_section_by_index, #cell_section_styles, #cell_sections_for_group, #data, #dealloc, #delete_cell_sections, #delete_from_data, #delete_from_flat_data, #delete_from_groped_data, #fixed_table_data, #flat_data?, group_header, #header_cell_in_group, #header_section_for_group, #height_for_header_in_group, #height_for_index, #hide, #index_for_cell_section, #on_appear, #on_click, pull_to_refresh, #refresh_if_needed, #reload, #reload_table_data, #render_cell, #render_header, #reset_data, #scroll_view_did_end_decelerating, #scroll_view_did_end_dragging, #scroll_view_did_scroll, #scroll_view_will_begin_dragging, #show, #table_element_options, #table_styles, #table_view

Methods included from HasSearchBar

#add_search_bar, #create_search_bar, #dealloc, #searchBar, #searchBarSearchButtonClicked

Methods included from HasStyleChainBuilder

#build_styles_chain

Methods included from TableSectionRefreshMixin

#add_pull_to_refresh, #finish_pull_to_refresh

Methods inherited from Section

#add_element, after_initialize, after_render, before_initialize, before_render, bind_keyboard_close, #bind_keyboard_events, #cell, container, #container_bounds, #container_height, #container_options, #create_elements, #current_input_view_height, #dealloc, #default_name, element, #elements_options, #elements_to_draw, #elements_to_render, #has_container_bounds?, #hide, #hide_keyboard, #initialize, #on_keyboard_hide, #on_keyboard_show, #reload, #reload_section, #render, #render!, #render_container, #render_element?, #screen?, #show, #strong_references, #view

Methods included from DelegateMixin

#clear_delegated, #delegated_by

Methods included from DrawSectionMixin

#bind_gesture_on_container_for, #clear_gesture_for_receiver, #draw_in, #prerender_elements_for_state, #prerender_enabled?

Methods included from SectionWithContainerMixin

#container_view, #init_container_element, #load_container_with_elements

Methods included from FrameCalculatorMixin

#calculate_frome_for

Methods included from HasStyles

#prepare_gradient

Methods included from HasClassFactory

#camelize_factory, #class_factory, #low_camelize_factory

Methods included from HasNormalizer

#normalize_object, #normalize_options

Methods included from HasAuthorization

#api_client, #current_user, #reset_current_user, #user_signed_in?

Constructor Details

This class inherits a constructor from MotionPrime::Section

Instance Attribute Details

#field_indexesObject

Returns the value of attribute field_indexes.



22
23
24
# File 'motion-prime/sections/form.rb', line 22

def field_indexes
  @field_indexes
end

#fieldsObject

Returns the value of attribute fields.



22
23
24
# File 'motion-prime/sections/form.rb', line 22

def fields
  @fields
end

#grouped_dataObject

Returns the value of attribute grouped_data.



22
23
24
# File 'motion-prime/sections/form.rb', line 22

def grouped_data
  @grouped_data
end

#keyboard_visibleObject

Returns the value of attribute keyboard_visible.



22
23
24
# File 'motion-prime/sections/form.rb', line 22

def keyboard_visible
  @keyboard_visible
end

#rendered_viewsObject

Returns the value of attribute rendered_views.



22
23
24
# File 'motion-prime/sections/form.rb', line 22

def rendered_views
  @rendered_views
end

Class Method Details

.async_table_data(options = {}) ⇒ Object



212
213
214
215
# File 'motion-prime/sections/form.rb', line 212

def async_table_data(options = {})
  super
  self.send :include, Prime::AsyncFormMixin
end

.field(name, options = {}, &block) ⇒ Object



217
218
219
220
221
222
223
224
# File 'motion-prime/sections/form.rb', line 217

def field(name, options = {}, &block)
  options[:name] = name
  options[:type] ||= :string
  options[:block] = block
  self.fields_options ||= {}
  self.fields_options[name] = options
  self.fields_options[name]
end

.inherited(subclass) ⇒ Object



205
206
207
208
209
210
# File 'motion-prime/sections/form.rb', line 205

def inherited(subclass)
  super
  subclass.fields_options = self.fields_options.try(:clone)
  subclass.text_field_limits = self.text_field_limits.try(:clone)
  subclass.text_view_limits = self.text_view_limits.try(:clone)
end

.limit_text_field_length(name, limit) ⇒ Object



226
227
228
229
# File 'motion-prime/sections/form.rb', line 226

def limit_text_field_length(name, limit)
  self.text_field_limits ||= {}
  self.text_field_limits[name] = limit
end

.limit_text_view_length(name, limit) ⇒ Object



230
231
232
233
# File 'motion-prime/sections/form.rb', line 230

def limit_text_view_length(name, limit)
  self.text_view_limits ||= {}
  self.text_view_limits[name] = limit
end

Instance Method Details

#allow_string_replacement?(target, limit, range, string) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
# File 'motion-prime/sections/form.rb', line 151

def allow_string_replacement?(target, limit, range, string)
  if string.length.zero? || (range.length + limit - target.text.length) >= string.length
    true
  else
    target.text.length < limit
  end
end

#element(name) ⇒ Object

Returns element based on field name and element name

Examples:

form.element("email:input")

Parameters:

  • String

    name with format “fieldname:elementname”

Returns:

  • MotionPrime::BaseElement element



59
60
61
62
63
64
65
66
# File 'motion-prime/sections/form.rb', line 59

def element(name)
  field_name, element_name = name.split(':')
  if element_name.present?
    field(field_name).element(element_name.to_sym)
  else
    super(field_name)
  end
end

#field(field_name) ⇒ Object

Returns field by name

Examples:

form.field(:email)

Parameters:

  • String

    field name

Returns:

  • MotionPrime::BaseFieldSection field



75
76
77
# File 'motion-prime/sections/form.rb', line 75

def field(field_name)
  self.fields[field_name.to_sym]
end

#field_valuesObject



83
84
85
86
87
88
89
# File 'motion-prime/sections/form.rb', line 83

def field_values
  values = {}
  fields.each do |field_name, field|
    values[field_name.to_sym] = field.value if field.input?
  end
  values
end

#fields_hashObject



79
80
81
# File 'motion-prime/sections/form.rb', line 79

def fields_hash
  fields.to_hash
end

#focus_on(field_name, animated = true) ⇒ Object

Set focus on field cell

Examples:

form.focus_on(:title)

Parameters:

  • String

    field name

Returns:

  • MotionPrime::BaseFieldSection field



105
106
107
108
109
110
111
112
# File 'motion-prime/sections/form.rb', line 105

def focus_on(field_name, animated = true)
  # unfocus other field
  data.flatten.each do |item|
    item.blur
  end
  # focus on field
  field(field_name).focus
end

#has_many_sections?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'motion-prime/sections/form.rb', line 180

def has_many_sections?
  group_header_options.present? || grouped_data.count > 1
end

#keyboard_will_hideObject



133
134
135
136
137
# File 'motion-prime/sections/form.rb', line 133

def keyboard_will_hide
  current_inset = table_view.contentInset
  current_inset.bottom = self.table_element.computed_options[:bottom_content_inset] || 0
  table_view.contentInset = current_inset
end

#keyboard_will_showObject



126
127
128
129
130
131
# File 'motion-prime/sections/form.rb', line 126

def keyboard_will_show
  return if table_view.contentSize.height + table_view.top <= UIScreen.mainScreen.bounds.size.height - KEYBOARD_HEIGHT_PORTRAIT
  current_inset = table_view.contentInset
  current_inset.bottom = KEYBOARD_HEIGHT_PORTRAIT + (self.table_element.computed_options[:bottom_content_inset] || 0)
  table_view.contentInset = current_inset
end

#load_field(field) ⇒ Object



159
160
161
162
# File 'motion-prime/sections/form.rb', line 159

def load_field(field)
  field_class = class_factory("#{field[:type]}_field_section", true)
  field_class.new(field.merge(screen: screen, table: self.weak_ref))
end

#number_of_groups(table = nil) ⇒ Object

Table View Delegate




200
201
202
# File 'motion-prime/sections/form.rb', line 200

def number_of_groups(table = nil)
  has_many_sections? ? grouped_data.compact.count : 1
end

#on_cell_render(cell, index) ⇒ Object



189
190
191
192
193
194
195
# File 'motion-prime/sections/form.rb', line 189

def on_cell_render(cell, index)
  options = data[index.row].try(:options)
  if options && options[:after_render]
    self.send(options[:after_render])
  end
  super
end

#on_input_change(text_field) ⇒ Object

ALIASES



144
# File 'motion-prime/sections/form.rb', line 144

def on_input_change(text_field); end

#on_input_edit_begin(text_field) ⇒ Object



145
# File 'motion-prime/sections/form.rb', line 145

def on_input_edit_begin(text_field); end

#on_input_edit_end(text_field) ⇒ Object



146
# File 'motion-prime/sections/form.rb', line 146

def on_input_edit_end(text_field); end

#on_input_return(text_field) ⇒ Object



147
148
149
# File 'motion-prime/sections/form.rb', line 147

def on_input_return(text_field)
  text_field.resignFirstResponder
end

#register_elements_from_section(section) ⇒ Object



91
92
93
94
95
96
# File 'motion-prime/sections/form.rb', line 91

def register_elements_from_section(section)
  self.rendered_views ||= {}
  section.elements.values.each do |element|
    self.rendered_views[element.view] = {element: element, section: section}
  end
end

#reload_cell_section(section) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'motion-prime/sections/form.rb', line 32

def reload_cell_section(section)
  field = section.name.to_sym
  path = field_indexes[field]
  section.cell.try(:removeFromSuperview)

  fields[field] = load_field(self.class.fields_options[field])
  fields[field].create_elements
  if flat_data?
    @data[path.row] = fields[field]
  else
    @data[path.section][path.row] = fields[field]
  end

  set_data_stamp(fields[field].object_id)

  # table_view.beginUpdates
  table_view.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimationNone)
  # table_view.endUpdates
end

#reload_dataObject



173
174
175
176
177
178
# File 'motion-prime/sections/form.rb', line 173

def reload_data
  @groups_count = nil
  init_form_fields # must be before resetting to reflect changes on @data
  reset_data
  reload_table_data
end

#render_field?(name, options) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
170
171
# File 'motion-prime/sections/form.rb', line 164

def render_field?(name, options)
  return true unless condition = options[:if]
  if condition.is_a?(Proc)
    self.instance_eval(&condition)
  else
    condition.to_proc.call(self)
  end
end

#render_tableObject



184
185
186
187
# File 'motion-prime/sections/form.rb', line 184

def render_table
  init_form_fields unless self.fields.present?
  super
end

#set_height_with_keyboardObject



114
115
116
117
118
# File 'motion-prime/sections/form.rb', line 114

def set_height_with_keyboard
  return if keyboard_visible
  self.table_view.height -= KEYBOARD_HEIGHT_PORTRAIT
  self.keyboard_visible = true
end

#set_height_without_keyboardObject



120
121
122
123
124
# File 'motion-prime/sections/form.rb', line 120

def set_height_without_keyboard
  return unless keyboard_visible
  self.table_view.height += KEYBOARD_HEIGHT_PORTRAIT
  self.keyboard_visible = false
end

#table_dataObject



24
25
26
27
28
29
30
# File 'motion-prime/sections/form.rb', line 24

def table_data
  if has_many_sections?
    grouped_data.compact
  else
    fields.values
  end
end

#table_delegateObject



139
140
141
# File 'motion-prime/sections/form.rb', line 139

def table_delegate
  @table_delegate ||= FormDelegate.new(section: self)
end