motion-prime/sections/form.rb in motion-prime-0.8.1 vs motion-prime/sections/form.rb in motion-prime-0.8.2

- old
+ new

@@ -27,35 +27,30 @@ else fields.values end end - def reload_cell(section) + def reload_cell_section(section) field = section.name.to_sym - index = field_indexes[field].split('_').map(&:to_i) - path = NSIndexPath.indexPathForRow(index.last, inSection: index.first) + path = field_indexes[field] section.cell.try(:removeFromSuperview) fields[field] = load_field(self.class.fields_options[field]) - fields[field].load_section + fields[field].create_elements if flat_data? @data[path.row] = fields[field] else @data[path.section][path.row] = fields[field] end - set_data_stamp(field_indexes[field]) + set_data_stamp(fields[field].object_id) # table_view.beginUpdates table_view.reloadRowsAtIndexPaths([path], withRowAnimation: UITableViewRowAnimationNone) # table_view.endUpdates end - def reset_data_stamps - set_data_stamp(self.field_indexes.values) - end - # Returns element based on field name and element name # # Examples: # form.element("email:input") # @@ -167,58 +162,44 @@ end end def reload_data @groups_count = nil + init_form_fields # must be before resetting to reflect changes on @data reset_data - init_form_fields reload_table_data end - def reset_data - super - self.fields.values.each(&:clear_observers) - end - def has_many_sections? - section_header_options.present? || grouped_data.count > 1 + group_header_options.present? || grouped_data.count > 1 end def render_table init_form_fields unless self.fields.present? super end - 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 - # Table View Delegate # --------------------- - def number_of_sections(table = nil) + def number_of_groups(table = nil) has_many_sections? ? grouped_data.compact.count : 1 end - def height_for_index(table, index) - section = load_cell_by_index(index, preload: false) - section.container_height - end - class << self 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 + def async_table_data(options = {}) + super + self.send :include, Prime::AsyncFormMixin + end + def field(name, options = {}, &block) options[:name] = name options[:type] ||= :string options[:block] = block self.fields_options ||= {} @@ -235,13 +216,15 @@ self.text_view_limits[name] = limit end end private - def load_sections; end + def create_section_elements; end def init_form_fields + self.fields.values.each(&:clear_observers) if fields.present? + self.fields = {} self.field_indexes = {} self.grouped_data = [] section_indexes = [] (self.class.fields_options || {}).each do |key, field| @@ -252,20 +235,21 @@ grouped_data[section_id] ||= [] section_indexes[section_id] ||= 0 section = load_field(field) self.fields[key] = section - self.field_indexes[key] = "#{section_id}_#{section_indexes[section_id]}" + self.field_indexes[key] = NSIndexPath.indexPathForRow(section_indexes[section_id], inSection: section_id) grouped_data[section_id][section_indexes[section_id]] = section section_indexes[section_id] += 1 end init_form_headers reset_data_stamps end def init_form_headers - options = Array.wrap(self.class.section_header_options).clone - self.section_header_options = options.delete_if.each_with_index { |opts, id| grouped_data[id].nil? } + options = Array.wrap(self.class.group_header_options).clone + options.compact.each { |opts| normalize_options(opts) } + self.group_header_options = options.delete_if.each_with_index { |opts, id| grouped_data[id].nil? } end end end \ No newline at end of file