Class: Glimmer::SWT::TableProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/swt/table_proxy.rb

Constant Summary

Constants inherited from WidgetProxy

WidgetProxy::DEFAULT_INITIALIZERS

Instance Attribute Summary collapse

Attributes inherited from WidgetProxy

#args, #background, #children, #enabled, #focus, #font, #foreground, #parent, #path

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_css_class, #add_css_classes, #add_observer, #apply_property_type_converters, #build_dom, #can_handle_observation_request?, #clear_css_classes, #content, #css_classes, #dispose, #dom_element, for, #handle_observation_request, #has_style?, #id, #id=, #listener_dom_element, #listener_path, max_id_number_for, max_id_numbers, #name, next_id_number_for, #parent_dom_element, #parent_path, #remove_css_class, #remove_css_classes, #render, reset_max_id_numbers!, #set_attribute, #set_focus, #style_element, underscored_widget_name, widget_class, widget_exists?, #widget_property_listener_installers

Methods included from PropertyOwner

#attribute_getter, #attribute_setter, #get_attribute, #set_attribute

Constructor Details

#initialize(parent, args, block) ⇒ TableProxy

Returns a new instance of TableProxy.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/glimmer/swt/table_proxy.rb', line 13

def initialize(parent, args, block)
  super(parent, args, block)
  @columns = []
  @children = []
  @selection = []
  if editable?
    on_mouse_up { |event|
      edit_table_item(event.table_item, event.column_index)
    }
  end
end

Instance Attribute Details

#additional_sort_propertiesObject

Returns the value of attribute additional_sort_properties.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def additional_sort_properties
  @additional_sort_properties
end

#column_propertiesObject

Returns the value of attribute column_properties.



9
10
11
# File 'lib/glimmer/swt/table_proxy.rb', line 9

def column_properties
  @column_properties
end

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def columns
  @columns
end

#dataObject Also known as: model_binding

Returns the value of attribute data.



9
10
11
# File 'lib/glimmer/swt/table_proxy.rb', line 9

def data
  @data
end

#item_countObject

Returns the value of attribute item_count.



9
10
11
# File 'lib/glimmer/swt/table_proxy.rb', line 9

def item_count
  @item_count
end

#selectionObject

Returns the value of attribute selection.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def selection
  @selection
end

#sort_blockObject

Returns the value of attribute sort_block.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def sort_block
  @sort_block
end

#sort_by_blockObject

Returns the value of attribute sort_by_block.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def sort_by_block
  @sort_by_block
end

#sort_columnObject (readonly)

Returns the value of attribute sort_column.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def sort_column
  @sort_column
end

#sort_propertyObject

Returns the value of attribute sort_property.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def sort_property
  @sort_property
end

#sort_typeObject (readonly)

Returns the value of attribute sort_type.



7
8
9
# File 'lib/glimmer/swt/table_proxy.rb', line 7

def sort_type
  @sort_type
end

Instance Method Details

#cells_for(model) ⇒ Object



76
77
78
# File 'lib/glimmer/swt/table_proxy.rb', line 76

def cells_for(model)
  column_properties.map {|property| model.send(property)}
end

#column_sort_propertiesObject



132
133
134
135
136
# File 'lib/glimmer/swt/table_proxy.rb', line 132

def column_sort_properties
  column_properties.zip(columns.map(&:sort_property)).map do |pair|
    [pair.compact.last].flatten.compact
  end
end

#columns_domObject



316
317
318
319
# File 'lib/glimmer/swt/table_proxy.rb', line 316

def columns_dom
  tr {
  }
end

#columns_dom_elementObject



304
305
306
# File 'lib/glimmer/swt/table_proxy.rb', line 304

def columns_dom_element
  Document.find(columns_path)
end

#columns_pathObject



300
301
302
# File 'lib/glimmer/swt/table_proxy.rb', line 300

def columns_path
  path + ' thead tr'
end

#detect_sort_typeObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/glimmer/swt/table_proxy.rb', line 116

def detect_sort_type
  @sort_type = sort_property.size.times.map { String }
  array = model_binding.evaluate_property
  sort_property.each_with_index do |a_sort_property, i|
    values = array.map { |object| object.send(a_sort_property) }
    value_classes = values.map(&:class).uniq
    if value_classes.size == 1
      @sort_type[i] = value_classes.first
    elsif value_classes.include?(Integer)
      @sort_type[i] = Integer
    elsif value_classes.include?(Float)
      @sort_type[i] = Float
    end
  end
end

#domObject



336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/glimmer/swt/table_proxy.rb', line 336

def dom
  table_id = id
  table_id_style = css
  table_id_css_classes = css_classes
  table_id_css_classes << 'table'
  table_id_css_classes_string = table_id_css_classes.to_a.join(' ')
  @dom ||= html {
    table(id: table_id, style: table_id_style, class: table_id_css_classes_string) {
      thead_dom
      items_dom
    }
  }.to_s
end

#edit_table_item(table_item, column_index) ⇒ Object



226
227
228
# File 'lib/glimmer/swt/table_proxy.rb', line 226

def edit_table_item(table_item, column_index)
  table_item&.edit(column_index) unless column_index.nil?
end

#editable?Boolean Also known as: editable

Returns:

  • (Boolean)


50
51
52
# File 'lib/glimmer/swt/table_proxy.rb', line 50

def editable?
  args.include?(:editable)
end

#elementObject



296
297
298
# File 'lib/glimmer/swt/table_proxy.rb', line 296

def element
  'table'
end

#get_data(key = nil) ⇒ Object



41
42
43
# File 'lib/glimmer/swt/table_proxy.rb', line 41

def get_data(key=nil)
  data
end

#header_visibleObject



239
240
241
# File 'lib/glimmer/swt/table_proxy.rb', line 239

def header_visible
  @header_visible
end

#header_visible=(value) ⇒ Object



230
231
232
233
234
235
236
237
# File 'lib/glimmer/swt/table_proxy.rb', line 230

def header_visible=(value)
  @header_visible = value
  if @header_visible
    thead_dom_element.remove_class('hide')
  else
    thead_dom_element.add_class('hide')
  end
end

#index_of(item) ⇒ Object



84
85
86
# File 'lib/glimmer/swt/table_proxy.rb', line 84

def index_of(item)
  items.index(item)
end

#initial_sort!Object



189
190
191
# File 'lib/glimmer/swt/table_proxy.rb', line 189

def initial_sort!
  sort_by_column!
end

#items=(new_items) ⇒ Object



66
67
68
69
# File 'lib/glimmer/swt/table_proxy.rb', line 66

def items=(new_items)
  @children = new_items
  redraw
end

#items_domObject



331
332
333
334
# File 'lib/glimmer/swt/table_proxy.rb', line 331

def items_dom
  tbody {
  }
end

#items_dom_elementObject



312
313
314
# File 'lib/glimmer/swt/table_proxy.rb', line 312

def items_dom_element
  Document.find(items_path)
end

#items_pathObject



308
309
310
# File 'lib/glimmer/swt/table_proxy.rb', line 308

def items_path
  path + ' tbody'
end

#observation_request_to_event_mappingObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/glimmer/swt/table_proxy.rb', line 247

def observation_request_to_event_mapping
  mouse_handler = -> (event_listener) {
    -> (event) {
      event.singleton_class.send(:define_method, :table_item=) do |item|
        @table_item = item
      end
      event.singleton_class.send(:define_method, :table_item) do
        @table_item
      end
      table_row = event.target.parents('tr').first
      table_data = event.target.parents('td').first
      event.table_item = items.detect {|item| item.id == table_row.attr('id')}
      event.singleton_class.send(:define_method, :column_index) do
        (table_data || event.target).attr('data-column-index')
      end
      event_listener.call(event)
    }
  }

  {
    'on_mouse_down' => {
      event: 'mousedown',
      event_handler: mouse_handler,
    },
    'on_mouse_up' => {
      event: 'mouseup',
      event_handler: mouse_handler,
    },
    'on_widget_selected' => {
      event: 'mouseup',
    }
  }
end

#post_add_contentObject



35
36
37
38
39
# File 'lib/glimmer/swt/table_proxy.rb', line 35

def post_add_content
  return if @initially_sorted
  initial_sort!
  @initially_sorted = true
end

#post_initialize_child(child) ⇒ Object

Only table_columns may be added as children



26
27
28
29
30
31
32
33
# File 'lib/glimmer/swt/table_proxy.rb', line 26

def post_initialize_child(child)
  if child.is_a?(TableColumnProxy)
    @columns << child
  else
    @children << child
  end
  child.redraw
end

#redrawObject



281
282
283
284
285
# File 'lib/glimmer/swt/table_proxy.rb', line 281

def redraw
  super()
  @columns.to_a.each(&:redraw)
  redraw_empty_items
end

#redraw_empty_itemsObject



287
288
289
290
291
292
293
294
# File 'lib/glimmer/swt/table_proxy.rb', line 287

def redraw_empty_items
  if @children&.size.to_i < item_count.to_i
    item_count.to_i.times do
      empty_columns = column_properties&.size.to_i.times.map { |i| "<td data-column-index='#{i}'></td>" }
      items_dom_element.append("<tr class='table-item empty-table-item'>#{empty_columns}</tr>")
    end
  end
end

#remove_allObject



45
46
47
48
# File 'lib/glimmer/swt/table_proxy.rb', line 45

def remove_all
  items.clear
  redraw
end

#search(&condition) ⇒ Object



80
81
82
# File 'lib/glimmer/swt/table_proxy.rb', line 80

def search(&condition)
  items.select {|item| condition.nil? || condition.call(item)}
end

#select(index, meta = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/glimmer/swt/table_proxy.rb', line 88

def select(index, meta = false)
  new_selection = @selection.clone
  selected_item = items[index]
  if @selection.include?(selected_item)
    new_selection.delete(selected_item) if meta
  else
    new_selection = [] if !meta || (!has_style?(:multi) && @selection.to_a.size >= 1)
    new_selection << selected_item
  end
  self.selection = new_selection
end

#selectorObject



243
244
245
# File 'lib/glimmer/swt/table_proxy.rb', line 243

def selector
  super + ' tbody'
end

#sort!Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/glimmer/swt/table_proxy.rb', line 193

def sort!
  return unless sort_property && (sort_type || sort_block || sort_by_block)
  array = model_binding.evaluate_property
  array = array.sort_by(&:hash) # this ensures consistent subsequent sorting in case there are equivalent sorts to avoid an infinite loop
  # Converting value to_s first to handle nil cases. Should work with numeric, boolean, and date fields
  if sort_block
    sorted_array = array.sort(&sort_block)
  elsif sort_by_block
    sorted_array = array.sort_by(&sort_by_block)
  else
    sorted_array = array.sort_by do |object|
      sort_property.each_with_index.map do |a_sort_property, i|
        value = object.send(a_sort_property)
        # handle nil and difficult to compare types gracefully
        if sort_type[i] == Integer
          value = value.to_i
        elsif sort_type[i] == Float
          value = value.to_f
        elsif sort_type[i] == String
          value = value.to_s
        end
        value
      end
    end
  end
  sorted_array = sorted_array.reverse if @sort_direction == :descending
  model_binding.call(sorted_array)
end

#sort_by_column!(table_column_proxy = nil) ⇒ Object

Sorts by specified TableColumnProxy object. If nil, it uses the table default sort instead.



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/glimmer/swt/table_proxy.rb', line 147

def sort_by_column!(table_column_proxy=nil)
  index = columns.to_a.index(table_column_proxy) unless table_column_proxy.nil?
  new_sort_property = table_column_proxy.nil? ? @sort_property : table_column_proxy.sort_property || [column_properties[index]]
  
  return if table_column_proxy.nil? && new_sort_property.nil? && @sort_block.nil? && @sort_by_block.nil?
  if new_sort_property && table_column_proxy.nil? && new_sort_property.size == 1 && (index = column_sort_properties.index(new_sort_property))
    table_column_proxy = columns[index]
  end
  if new_sort_property && new_sort_property.size == 1 && !additional_sort_properties.to_a.empty?
    selected_additional_sort_properties = additional_sort_properties.clone
    if selected_additional_sort_properties.include?(new_sort_property.first)
      selected_additional_sort_properties.delete(new_sort_property.first)
      new_sort_property += selected_additional_sort_properties
    else
      new_sort_property += additional_sort_properties
    end
  end
  
  new_sort_property = [new_sort_property].flatten.compact unless new_sort_property.is_a?(Array)
  @sort_direction = @sort_direction.nil? || @sort_property.first != new_sort_property.first || @sort_direction == :descending ? :ascending : :descending
  
  @sort_property = new_sort_property
  table_column_index = column_properties.index(new_sort_property.to_s.to_sym)
  table_column_proxy ||= columns[table_column_index] if table_column_index
  @sort_column = table_column_proxy if table_column_proxy
          
  if table_column_proxy
    @sort_by_block = nil
    @sort_block = nil
  end
  @sort_type = nil
  if table_column_proxy&.sort_by_block
    @sort_by_block = table_column_proxy.sort_by_block
  elsif table_column_proxy&.sort_block
    @sort_block = table_column_proxy.sort_block
  else
    detect_sort_type
  end
          
  sort!
end

#sort_directionObject



138
139
140
# File 'lib/glimmer/swt/table_proxy.rb', line 138

def sort_direction
  @sort_direction == :ascending ? SWTProxy[:up] : SWTProxy[:down]
end

#sort_direction=(value) ⇒ Object



142
143
144
# File 'lib/glimmer/swt/table_proxy.rb', line 142

def sort_direction=(value)
  @sort_direction = value == SWTProxy[:up] ? :ascending : :descending
end

#thead_domObject



321
322
323
324
325
# File 'lib/glimmer/swt/table_proxy.rb', line 321

def thead_dom
  thead {
    columns_dom
  }
end

#thead_dom_elementObject



327
328
329
# File 'lib/glimmer/swt/table_proxy.rb', line 327

def thead_dom_element
  dom_element.find('thead')
end