class Canis::TabularWidget::TableRowSorter

This is our default table row sorter. It does a multiple sort and allows for reverse sort also. It's a pretty simple sorter and uses sort, not sort_by. Improvements welcome. Usage: provide model in constructor or using model method Call toggle_sort_order(column_index) Call sort. Currently, this sorts the provided model in-place. Future versions may maintain a copy, or use a table that provides a mapping of model to result. # TODO check if column_sortable

Attributes

sort_keys[R]

Public Class Methods

new(model=nil) click to toggle source
# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 974
def initialize model=nil
  self.model = model
  @columns_sort = []
  @sort_keys = nil
end

Public Instance Methods

model=(model) click to toggle source
# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 979
def model=(model)
  @model = model
  @sort_keys = nil
end
set_sort_keys(list) click to toggle source
# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 1034
def set_sort_keys list
  @sort_keys = list
end
sort() click to toggle source

sorts the model based on sort keys and reverse flags @sort_keys contains indices to sort on @reverse_flags is an array of booleans, true for reverse, nil or false for ascending

# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 997
def sort
  return unless @model
  return if @sort_keys.empty?
  $log.debug "TABULAR SORT KEYS #{sort_keys} "
  @model.sort!{|x,y| 
    res = 0
    @sort_keys.each { |ee| 
      e = ee.abs-1 # since we had offsetted by 1 earlier
      abse = e.abs
      if ee < 0
        res = y[abse] <=> x[abse]
      else
        res = x[e] <=> y[e]
      end
      break if res != 0
    }
    res
  }
end
sortable(colindex, tf) click to toggle source
# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 983
def sortable colindex, tf
  @columns_sort[colindex] = tf
end
sortable?(colindex) click to toggle source
# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 986
def sortable? colindex
  return false if @columns_sort[colindex]==false
  return true
end
toggle_sort_order(index) click to toggle source

toggle the sort order if given column offset is primary sort key Otherwise, insert as primary sort key, ascending.

# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 1018
def toggle_sort_order index
  index += 1 # increase by 1, since 0 won't multiple by -1
  # internally, reverse sort is maintained by multiplying number by -1
  @sort_keys ||= []
  if @sort_keys.first && index == @sort_keys.first.abs
    @sort_keys[0] *= -1 
  else
    @sort_keys.delete index # in case its already there
    @sort_keys.delete(index*-1) # in case its already there
    @sort_keys.unshift index
    # don't let it go on increasing
    if @sort_keys.size > 3
      @sort_keys.pop
    end
  end
end
use_to_s(colindex) click to toggle source

should to_s be used for this column

# File lib/canis/core/widgets/deprecated/tabularwidget.rb, line 991
def use_to_s colindex
  return true # TODO
end