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

- old
+ new

@@ -18,26 +18,27 @@ # end # 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 + attr_accessor :fields, :field_indexes, :keyboard_visible, :rendered_views, :section_headers, :section_header_options def table_data if @groups_count == 1 fields.values else section_indexes = [] - fields.inject([]) do |result, (key, field)| + data = fields.inject([]) do |result, (key, field)| section = self.class.fields_options[key][:group].to_i - section_indexes[section] ||= 0 result[section] ||= [] result[section][section_indexes[section]] = field section_indexes[section] += 1 result end + self.section_header_options.delete_if.each_with_index { |opts, id| data[id].nil? } + data.compact end end def data @data ||= table_data @@ -54,11 +55,11 @@ 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, styles: cell_styles(field).values.flatten, reuse_identifier: cell_name(table, index), parent_view: table_view do |cell_view| + 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) end end @@ -166,28 +167,27 @@ text_field.resignFirstResponder end def textFieldShouldReturn(text_field) on_input_return(text_field) end + def textFieldShouldBeginEditing(text_field) + text_field.respond_to?(:readonly) ? !text_field.readonly : true + end def textFieldDidBeginEditing(text_field) on_input_edit(text_field) end def textViewDidBeginEditing(text_view) on_input_edit(text_view) end def textView(text_view, shouldChangeTextInRange:range, replacementText:string) - limit = (self.class.text_view_limits || {}).find do |field_name, limit| - view("#{field_name}:input") - end.try(:last) - return true unless limit - allow_string_replacement?(text_view, limit, range, string) + textField(text_view, shouldChangeCharactersInRange:range, replacementString:string) end def textField(text_field, shouldChangeCharactersInRange:range, replacementString:string) limit = (self.class.text_field_limits || {}).find do |field_name, limit| - view("#{field_name}:input") + view("#{field_name}:input") == text_field end.try(:last) return true unless limit allow_string_replacement?(text_field, limit, range, string) end @@ -203,22 +203,20 @@ klass = "MotionPrime::#{field[:type].classify}FieldSection".constantize klass.new(field.merge(table: self)) end def render_field?(name, options) - condition = options.delete(:if) - if condition.nil? - true - elsif condition.is_a?(Proc) + return true unless condition = options[:if] + if condition.is_a?(Proc) self.instance_eval(&condition) else - condition + condition.to_proc.call(self) end end def render_header(section) - return unless options = self.class.section_header_options.try(:[], section) + return unless options = self.section_header_options.try(:[], section) self.section_headers[section] ||= BaseHeaderSection.new(options.merge(table: self)) end def header_for_section(section) self.section_headers ||= [] @@ -263,22 +261,34 @@ self.text_view_limits ||= {} self.text_view_limits[name] = limit end end + def reload_data + @groups_count = nil + init_form_fields + super + end + private def init_form_fields self.fields = {} self.field_indexes = {} section_indexes = [] (self.class.fields_options || []).each do |key, field| next unless render_field?(key, field) section_id = field[:group].to_i - section_indexes[section_id] ||= 0 @groups_count = [@groups_count || 1, section_id + 1].max self.fields[key] = load_field(field) + + section_indexes[section_id] ||= 0 self.field_indexes[key] = "#{section_id}_#{section_indexes[section_id]}" section_indexes[section_id] += 1 end + init_form_headers + end + + def init_form_headers + self.section_header_options = Array.wrap(self.class.section_header_options).clone end end end \ No newline at end of file