Class: Glimmer::LibUI::CustomControl::RefinedTable

Inherits:
Object
  • Object
show all
Includes:
Glimmer::LibUI::CustomControl
Defined in:
lib/glimmer/libui/custom_control/refined_table.rb

Constant Summary collapse

FILTER_DEFAULT =
lambda do |row_hash, query|
  text = row_hash.values.map(&:to_s).map(&:downcase).join(' ')
  if query != @last_query
    @last_query = query
    @query_words = []
    query_text = query.strip
    until query_text.empty?
      exact_term_double_quoted_regexp = /"[^"]+"/
      specific_column_double_quoted_column_name_regexp = /"[^":]+":[^": ]+/
      specific_column_double_quoted_column_value_regexp = /[^": ]+:"[^":]+"/
      specific_column_double_quoted_column_name_and_value_regexp = /"[^":]+":"[^":]+"/
      single_word_regexp = /\S+/
      query_match = (query_text + ' ').match(/^(#{exact_term_double_quoted_regexp}|#{specific_column_double_quoted_column_name_regexp}|#{specific_column_double_quoted_column_value_regexp}|#{specific_column_double_quoted_column_name_and_value_regexp}|#{single_word_regexp})\s+/)
      if query_match && query_match[1]
        query_word = query_match[1]
        query_text = query_text.sub(query_word, '').strip
        query_word = query_word.sub(/^"/, '').sub(/"$/, '') if query_word.start_with?('"') && query_word.end_with?('"') && !query_word.include?(':')
        @query_words << query_word
      end
    end
  end
  @query_words.all? do |word|
    if word.include?(':')
      column_name, column_value = word.split(':')
      column_value = column_value.to_s
      column_name = column_name.sub(/^"/, '').sub(/"$/, '') if column_name.start_with?('"') && column_name.end_with?('"')
      column_value = column_value.sub(/^"/, '').sub(/"$/, '') if column_value.start_with?('"') && column_value.end_with?('"')
      column_human_name = row_hash.keys.find do |table_column_name|
        table_column_name.underscore.start_with?(column_name.underscore)
      end
      if column_human_name
        row_hash[column_human_name].downcase.include?(column_value.downcase)
      else
        text.downcase.include?(word.downcase)
      end
    else
      text.downcase.include?(word.downcase)
    end
  end
end

Instance Attribute Summary collapse

Attributes included from Glimmer::LibUI::CustomControl

#args, #body_root, #content, #keyword, #libui, #options, #parent, #parent_proxy

Instance Method Summary collapse

Methods included from Glimmer::LibUI::CustomControl

add_custom_control_namespaces_for, after_body, before_body, body, #can_handle_listener?, custom_control_namespaces, custom_controls_being_interpreted, def_option_attr_accessors, flyweight_custom_control_classes, for, #handle_listener, #has_instance_method?, #initialize, keyword, namespaces_for_class, #observer_registrations, option, options, #post_add_content, #post_initialize_child, reset_custom_control_namespaces, shortcut_keyword

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 260

def method_missing(method_name, *args, &block)
  if @table_proxy&.respond_to?(method_name, *args, &block)
    @table_proxy&.send(method_name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#filtered_model_arrayObject

filtered model array (intermediary, non-paginated)



84
85
86
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 84

def filtered_model_array
  @filtered_model_array
end

#refined_model_arrayObject

paginated filtered model array



85
86
87
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 85

def refined_model_array
  @refined_model_array
end

#table_proxyObject (readonly)

Returns the value of attribute table_proxy.



86
87
88
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 86

def table_proxy
  @table_proxy
end

Instance Method Details

#correct_page(page) ⇒ Object



250
251
252
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 250

def correct_page(page)
  [[page, 1].max, page_count].min
end

#filter_model_arrayObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 215

def filter_model_array
  return unless (@last_filter_query.nil? || filter_query != @last_filter_query)
  if !@filtered_model_array_stack.key?(filter_query)
    table_column_names = @table_proxy.columns.map(&:name)
    @filtered_model_array_stack[filter_query] = model_array.dup.filter do |model|
      row_values = @table_proxy.expand_cell_row(model).map(&:to_s)
      row_hash = Hash[table_column_names.zip(row_values)]
      filter.call(row_hash, filter_query)
    end
  end
  @filtered_model_array = @filtered_model_array_stack[filter_query]
  if @last_filter_query.nil? || filter_query.size > @last_filter_query.size
    @filter_query_page_stack[filter_query] = correct_page(page)
  end
  self.page = @filter_query_page_stack[filter_query] || correct_page(page)
  paginate_model_array
  @last_filter_query = filter_query
end

#indexObject



238
239
240
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 238

def index
  [per_page * (page - 1), 0].max
end

#init_model_arrayObject



206
207
208
209
210
211
212
213
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 206

def init_model_array
  @last_filter_query = nil
  @filter_query_page_stack = {}
  @filtered_model_array = model_array.dup
  @filtered_model_array_stack = {'' => @filtered_model_array}
  self.page = correct_page(page)
  filter_model_array if @table_proxy
end

#limitObject



242
243
244
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 242

def limit
  [(filtered_model_array.count - index), per_page].min
end

#page_countObject



246
247
248
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 246

def page_count
  (filtered_model_array.count.to_f / per_page.to_f).ceil
end

#paginate_model_arrayObject



234
235
236
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 234

def paginate_model_array
  self.refined_model_array = filtered_model_array[index, limit]
end

#respond_to?(method_name, *args, &block) ⇒ Boolean

Ensure proxying properties to @table_proxy if body_root (vertical_box) doesn’t support them

Returns:

  • (Boolean)


256
257
258
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 256

def respond_to?(method_name, *args, &block)
  super || @table_proxy&.respond_to?(method_name, *args, &block)
end

#table_filterObject



135
136
137
138
139
140
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 135

def table_filter
  search_entry {
    stretchy false
    text <=> [self, :filter_query]
  }
end

#table_paginatorObject



142
143
144
145
146
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/glimmer/libui/custom_control/refined_table.rb', line 142

def table_paginator
  horizontal_box {
    stretchy false
    
    button('<<') {
      enabled <= [self, :page, on_read: ->(val) {val > 1}]
      
      on_clicked do
        unless self.page == 0
          self.page = 1
          paginate_model_array
        end
      end
    }
    
    button('<') {
      enabled <= [self, :page, on_read: ->(val) {val > 1}]
      
      on_clicked do
        unless self.page == 0
          self.page = [page - 1, 1].max
          paginate_model_array
        end
      end
    }
    
    entry {
      text <=> [self, :page,
                on_read: :to_s,
                on_write: ->(val) { correct_page(val.to_i) },
                after_write: ->(val) { paginate_model_array },
               ]
    }
    
    if visible_page_count
      label {
        text <= [self, :refined_model_array, on_read: ->(val) {"of #{page_count} pages"}]
      }
    end
    
    button('>') {
      enabled <= [self, :page, on_read: ->(val) {val < page_count}]
      
      on_clicked do
        unless self.page == 0
          self.page = [page + 1, page_count].min
          paginate_model_array
        end
      end
    }
    
    button('>>') {
      enabled <= [self, :page, on_read: ->(val) {val < page_count}]
      
      on_clicked do
        unless self.page == 0
          self.page = page_count
          paginate_model_array
        end
      end
    }
  }
end