Class: Glimmer::DataBinding::TableItemsBinding

Inherits:
Object
  • Object
show all
Includes:
DataBinding::Observable, DataBinding::Observer
Defined in:
lib/glimmer/data_binding/table_items_binding.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, model_binding, column_properties) ⇒ TableItemsBinding

Returns a new instance of TableItemsBinding.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 14

def initialize(parent, model_binding, column_properties)
  @last_model_collection = nil
  @table = parent
  @model_binding = model_binding
  @column_properties = column_properties
  if @table.respond_to?(:column_properties=)
    @table.column_properties = @column_properties
  ##else # assume custom widget
  ##  @table.body_root.column_properties = @column_properties
  end
  call(@model_binding.evaluate_property)
  model = model_binding.base_model
  observe(model, model_binding.property_name_expression)
  ##@table.on_widget_disposed do |dispose_event| # doesn't seem needed within Opal
  ##  unregister_all_observables
  ##end
end

Instance Method Details

#call(new_model_collection = nil) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 32

def call(new_model_collection=nil)
  if new_model_collection and new_model_collection.is_a?(Array)
    observe(new_model_collection, @column_properties)
    @model_collection = new_model_collection
  end
  populate_table(@model_collection, @table, @column_properties)
  sort_table(@model_collection, @table, @column_properties)
end

#populate_table(model_collection, parent, column_properties) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 41

def populate_table(model_collection, parent, column_properties)
  return if model_collection&.sort_by(&:hash) == @last_model_collection&.sort_by(&:hash)
  @last_model_collection = model_collection
  # TODO improve performance
  selected_table_item_models = parent.selection.map(&:get_data)
  old_items = parent.items
  old_item_ids_per_model = old_items.reduce({}) {|hash, item| hash.merge(item.get_data.hash => item.id) }
  parent.remove_all
  model_collection.each do |model|
    table_item = Glimmer::SWT::TableItemProxy.new(parent)
    for index in 0..(column_properties.size-1)
      table_item.set_text(index, model.send(column_properties[index]).to_s)
    end
    table_item.set_data(model)
    table_item.id = old_item_ids_per_model[model.hash] if old_item_ids_per_model[model.hash]
  end
  selected_table_items = parent.search {|item| selected_table_item_models.include?(item.get_data) }
  selected_table_items = [parent.items.first] if selected_table_items.empty? && !parent.items.empty?
  parent.selection = selected_table_items unless selected_table_items.empty?
  parent.redraw
end

#sort_table(model_collection, parent, column_properties) ⇒ Object



63
64
65
66
67
# File 'lib/glimmer/data_binding/table_items_binding.rb', line 63

def sort_table(model_collection, parent, column_properties)
  return if model_collection == @last_model_collection
  parent.items = parent.items.sort_by { |item| model_collection.index(item.get_data) }
  @last_model_collection = model_collection
end