motion-prime/sections/form.rb in motion-prime-0.3.3 vs motion-prime/sections/form.rb in motion-prime-0.4.0

- old
+ new

@@ -21,13 +21,11 @@ class_attribute :text_field_limits, :text_view_limits class_attribute :fields_options, :section_header_options attr_accessor :fields, :field_indexes, :keyboard_visible, :rendered_views, :section_headers, :section_header_options def table_data - if @groups_count == 1 - fields.values - else + if @has_groups section_indexes = [] data = fields.inject([]) do |result, (key, field)| section = self.class.fields_options[key][:group].to_i section_indexes[section] ||= 0 result[section] ||= [] @@ -35,48 +33,56 @@ section_indexes[section] += 1 result end self.section_header_options.delete_if.each_with_index { |opts, id| data[id].nil? } data.compact + else + fields.values end end def data @data ||= table_data end def render_table init_form_fields - reset_data_stamps options = { styles: table_styles.values.flatten, delegate: self, dataSource: self, style: (UITableViewStyleGrouped unless flat_data?)} self.table_element = screen.table_view(options) end def render_cell(index, table) field = rows_for_section(index.section)[index.row] - screen.table_view_cell section: field, reuse_identifier: cell_name(table, index), parent_view: table_view do |cell_view| - field.cell_view = cell_view if field.respond_to?(:cell_view) - field.render(to: screen) + screen.table_view_cell section: field, reuse_identifier: cell_name(table, index), parent_view: table_view do |container_view, container_element| + field.container_element = container_element + field.render end end 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) - table_view.beginUpdates section.cell.try(:removeFromSuperview) fields[field] = load_field(self.class.fields_options[field]) - @data = nil + 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 + # table_view.endUpdates end def reset_data_stamps set_data_stamp(self.field_indexes.values) end @@ -176,10 +182,22 @@ on_input_edit(text_field) end def textViewDidBeginEditing(text_view) on_input_edit(text_view) end + def textViewDidChange(text_view) # bug in iOS 7 - cursor is out of textView bounds + line = text_view.caretRectForPosition(text_view.selectedTextRange.start) + overflow = line.origin.y + line.size.height - + (text_view.contentOffset.y + text_view.bounds.size.height - text_view.contentInset.bottom - text_view.contentInset.top) + if overflow > 0 + offset = text_view.contentOffset + offset.y += overflow + text_view.textContainerInset.bottom + UIView.animate(duration: 0.2) do + text_view.setContentOffset(offset) + end + end + end def textView(text_view, shouldChangeTextInRange:range, replacementText:string) textField(text_view, shouldChangeCharactersInRange:range, replacementString:string) end @@ -199,11 +217,11 @@ end end def load_field(field) klass = "MotionPrime::#{field[:type].classify}FieldSection".constantize - klass.new(field.merge(table: self)) + klass.new(field.merge(screen: screen, table: self)) end def render_field?(name, options) return true unless condition = options[:if] if condition.is_a?(Proc) @@ -213,24 +231,24 @@ end end def render_header(section) return unless options = self.section_header_options.try(:[], section) - self.section_headers[section] ||= BaseHeaderSection.new(options.merge(table: self)) + self.section_headers[section] ||= BaseHeaderSection.new(options.merge(screen: screen, table: self)) end def header_for_section(section) self.section_headers ||= [] self.section_headers[section] || render_header(section) end def tableView(table, viewForHeaderInSection: section) return unless header = header_for_section(section) - wrapper = MotionPrime::BaseElement.factory(:view, styles: cell_styles(header).values.flatten, parent_view: table_view) - wrapper.render(to: screen) do |cell_view| - header.cell_view = cell_view if header.respond_to?(:cell_view) - header.render(to: screen) + wrapper = MotionPrime::BaseElement.factory(:view, screen: screen, styles: cell_styles(header).values.flatten, parent_view: table_view) + wrapper.render do |container_view, container_element| + header.container_element = container_element + header.render end end def tableView(table, heightForHeaderInSection: section) header_for_section(section).try(:container_height) || 0 @@ -263,12 +281,13 @@ end end def reload_data @groups_count = nil + reset_data init_form_fields - super + table_view.reloadData end private def init_form_fields self.fields = {} @@ -283,9 +302,11 @@ section_indexes[section_id] ||= 0 self.field_indexes[key] = "#{section_id}_#{section_indexes[section_id]}" section_indexes[section_id] += 1 end init_form_headers + @has_groups = section_header_options.present? || @groups_count > 1 + reset_data_stamps end def init_form_headers self.section_header_options = Array.wrap(self.class.section_header_options).clone end \ No newline at end of file