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, #section_header_options, #section_headers, #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

#async_data?, async_table_data, #cell_for_index, #cell_name, #cell_styles, #data, #dealloc, #flat_data?, group_header, #header_for_section, #height_for_header_in_section, #hide, #on_appear, #on_async_data_loaded, #on_async_data_preloaded, #on_click, #on_row_render, pull_to_refresh, #refresh_if_needed, #render_cell, #render_header, #row_by_index, #rows_for_section, #scroll_view_did_end_decelerating, #scroll_view_did_end_dragging, #scroll_view_did_scroll, #scroll_view_will_begin_dragging, #show, #table_styles, #table_view, #view_for_header_in_section

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, #build_element, #cell, container, #container_bounds, #container_height, #container_options, #create_elements, #dealloc, #default_name, element, #elements_options, #elements_to_draw, #elements_to_render, #hide, #hide_keyboard, #initialize, #load_elements, #load_section, #load_section!, #on_keyboard_hide, #on_keyboard_show, #reload_section, #render, #render!, #render_container, #render_element?, #show, #view

Methods included from DrawSectionMixin

#bind_gesture_on_container_for, #container_view, #draw_in, #init_container_element, #load_container_element, #prerender_elements_for_state, #prerender_enabled?, #strong_references

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

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



213
214
215
216
217
218
219
220
# File 'motion-prime/sections/form.rb', line 213

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

.limit_text_field_length(name, limit) ⇒ Object



222
223
224
225
# File 'motion-prime/sections/form.rb', line 222

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



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

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)


148
149
150
151
152
153
154
# File 'motion-prime/sections/form.rb', line 148

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



64
65
66
67
68
69
70
71
# File 'motion-prime/sections/form.rb', line 64

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



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

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

#fields_hashObject



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

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



102
103
104
105
106
107
108
109
# File 'motion-prime/sections/form.rb', line 102

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)


182
183
184
# File 'motion-prime/sections/form.rb', line 182

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

#height_for_index(table, index) ⇒ Object



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

def height_for_index(table, index)
  section = load_cell_by_index(index, preload: false)
  section.container_height
end

#keyboard_will_hideObject



130
131
132
133
134
# File 'motion-prime/sections/form.rb', line 130

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



123
124
125
126
127
128
# File 'motion-prime/sections/form.rb', line 123

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



156
157
158
159
# File 'motion-prime/sections/form.rb', line 156

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_sections(table = nil) ⇒ Object

Table View Delegate




203
204
205
# File 'motion-prime/sections/form.rb', line 203

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

#on_input_change(text_field) ⇒ Object

ALIASES



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

def on_input_change(text_field); end

#on_input_edit_begin(text_field) ⇒ Object



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

def on_input_edit_begin(text_field); end

#on_input_edit_end(text_field) ⇒ Object



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

def on_input_edit_end(text_field); end

#on_input_return(text_field) ⇒ Object



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

def on_input_return(text_field)
  text_field.resignFirstResponder
end

#register_elements_from_section(section) ⇒ Object



88
89
90
91
92
93
# File 'motion-prime/sections/form.rb', line 88

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) ⇒ Object



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

def reload_cell(section)
  field = section.name.to_sym
  index = field_indexes[field].split('_').map(&:to_i)
  path = NSIndexPath.indexPathForRow(index.last, inSection: index.first)
  section.cell.try(:removeFromSuperview)

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

  set_data_stamp(field_indexes[field])

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

#reload_dataObject



170
171
172
173
174
175
# File 'motion-prime/sections/form.rb', line 170

def reload_data
  @groups_count = nil
  reset_data
  init_form_fields
  reload_table_data
end

#reload_table_dataObject



191
192
193
194
195
196
197
198
# File 'motion-prime/sections/form.rb', line 191

def reload_table_data
  return super unless async_data?
  sections = NSMutableIndexSet.new
  number_of_sections.times do |section_id|
    sections.addIndex(section_id)
  end
  table_view.reloadSections sections, withRowAnimation: UITableViewRowAnimationFade
end

#render_field?(name, options) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
# File 'motion-prime/sections/form.rb', line 161

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



186
187
188
189
# File 'motion-prime/sections/form.rb', line 186

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

#reset_dataObject



177
178
179
180
# File 'motion-prime/sections/form.rb', line 177

def reset_data
  super
  self.fields.values.each(&:clear_observers)
end

#reset_data_stampsObject



53
54
55
# File 'motion-prime/sections/form.rb', line 53

def reset_data_stamps
  set_data_stamp(self.field_indexes.values)
end

#set_height_with_keyboardObject



111
112
113
114
115
# File 'motion-prime/sections/form.rb', line 111

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



117
118
119
120
121
# File 'motion-prime/sections/form.rb', line 117

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



136
137
138
# File 'motion-prime/sections/form.rb', line 136

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