Class: Formotion::RowType::TemplateRow

Inherits:
Base show all
Includes:
BubbleWrap::KVO
Defined in:
lib/formotion/row_type/template_row.rb

Instance Attribute Summary

Attributes inherited from Base

#row, #tableView

Instance Method Summary collapse

Methods inherited from Base

#_on_select, #after_build, #after_delete, #break_with_semaphore, #button?, #cell_style, #done_editing, field_buffer, #initialize, #input_accessory_view, #on_delete, #update_cell, #with_semaphore

Constructor Details

This class inherits a constructor from Formotion::RowType::Base

Instance Method Details

#build_cell(cell) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/formotion/row_type/template_row.rb', line 29

def build_cell(cell)
  cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
  @add_button ||= begin
    button = UIButton.buttonWithType(UIButtonTypeContactAdd)
    button.when(UIControlEventTouchUpInside) do
      self._on_select(nil, nil)
    end
    button
  end
  cell.accessoryView = @add_button
  nil
end

#build_new_row(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/formotion/row_type/template_row.rb', line 52

def build_new_row(options = {})
  # build row
  new_row = row.section.create_row(row.template.merge(options))
  new_row.object.instance_eval do
    def after_delete
      template_value = row.template_parent.value
      template_value.delete_at(row.index)
      row.template_parent.value = template_value
      row.template_parent.template_children.delete_at(row.index)
    end
  end
  
  # inherit some infos to template child
  if row.on_tap_callback
    new_row.on_tap_callback = row.on_tap_callback
  end
  if row.on_delete_callback
    new_row.on_delete_callback = row.on_delete_callback
  end
  new_row.key = "#{row.key}_template".to_sym
  
  new_row.remove_on_delete = true
  new_row.template_parent = row
  row.template_children ||= []
  row.template_children << new_row
  observe(new_row, "value") do |old_value, new_value|
    template_value = row.value.dup
    template_value[new_row.index] = new_row.value
    row.value = template_value
  end
  new_row
end

#cellEditingStyleObject



21
22
23
# File 'lib/formotion/row_type/template_row.rb', line 21

def cellEditingStyle
  UITableViewCellEditingStyleInsert
end

#delete_row(template_row, with_animation = true) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/formotion/row_type/template_row.rb', line 102

def delete_row(template_row, with_animation = true)
  animation = with_animation ? UITableViewRowAnimationTop : UITableViewRowAnimationNone
  index_path = NSIndexPath.indexPathForRow(template_row.index, inSection:row.section.index)
  tableView.beginUpdates
  tableView.deleteRowsAtIndexPaths [index_path], withRowAnimation:animation
  tableView.endUpdates
end

#indentWhileEditing?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/formotion/row_type/template_row.rb', line 25

def indentWhileEditing?
  true
end

#insert_row(template_row, with_animation = true) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/formotion/row_type/template_row.rb', line 94

def insert_row(template_row, with_animation = true)
  animation = with_animation ? UITableViewRowAnimationBottom : UITableViewRowAnimationNone
  index_path = NSIndexPath.indexPathForRow(template_row.index, inSection:row.section.index)
  tableView.beginUpdates
  tableView.insertRowsAtIndexPaths [index_path], withRowAnimation:animation
  tableView.endUpdates
end

#move_row_in_list(new_row) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/formotion/row_type/template_row.rb', line 85

def move_row_in_list(new_row)
  # move to top
  row.section.rows.pop
  row.section.rows.insert(row.template_children.count - 1, new_row)

  # reset indexes
  row.section.refresh_row_indexes
end

#on_insert(tableView, tableViewDelegate) ⇒ Object



46
47
48
49
50
# File 'lib/formotion/row_type/template_row.rb', line 46

def on_insert(tableView, tableViewDelegate)
  new_row = build_new_row
  move_row_in_list(new_row)
  insert_row(new_row)
end

#on_select(tableView, tableViewDelegate) ⇒ Object



42
43
44
# File 'lib/formotion/row_type/template_row.rb', line 42

def on_select(tableView, tableViewDelegate)
  on_insert(tableView, tableViewDelegate)
end

#update_template_rowsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/formotion/row_type/template_row.rb', line 110

def update_template_rows
  row.value ||= []
  if row.value.count > row.template_children.count
    row.value[row.template_children.count..-1].each do |value|
      new_row = build_new_row({:value => value})
      move_row_in_list(new_row)
      insert_row(new_row) if tableView
    end
  elsif row.value.count < row.template_children.count
    row.template_children[row.value.count..-1].each do |row_to_delete|
      row.section.rows.delete(row_to_delete)
      row.template_children.delete(row_to_delete)
      row.section.refresh_row_indexes
      delete_row(row_to_delete) if tableView
    end
  end

  row.value.dup.each_with_index do |new_value, index|
    template_child = row.template_children[index]
    old_value = template_child.value
    if old_value != new_value
      template_child.value = new_value
    end
  end
end