Class: Storefront::Components::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/storefront/components/table.rb

Constant Summary

Constants included from Helpers::ContentHelper

Helpers::ContentHelper::SCOPES, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL, Helpers::ContentHelper::SCOPES_WITH_NESTED_MODEL

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#component_name, #extract_classes!, #extract_options!, #inside?, #pointer, #render_with_pointer, #to_s

Methods included from Helpers::ContentHelper

#encoded_contents, #font_face_data_uri, #html5_time, #read_binary_file, #read_image_size, #rendered_text, #sanitize, #t?

Constructor Details

#initialize(template, *args) ⇒ Table

Returns a new instance of Table.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/storefront/components/table.rb', line 6

def initialize(template, *args)
  @template         = template
  options           = args.extract_options!
  record_or_key     = args.shift
  @key              = record_key(record_or_key)
  @row_index        = 0
  @cell_index       = 0
  @scope            = :table
  @headers          = []
  options[:summary] ||= "Table for #{key.titleize}"
  options[:class]   = ["data-table", options[:class]].compact.uniq.join(" ")
  options[:role]    = :grid
  data              = options.delete(:data) || {}
  #data[:url]        = options[:url] || @template.controller.request.path
  #data[:for]        = options[:for] || options[:model] || @key
  data[:total]      = options[:total] if options[:total]
  data[:page]       = options[:page] if options[:page]
  data[:count]      = options[:count] if options[:count]
  aria              = options.delete(:aria) || {}
  aria[:"aria-multiselectable"] = false unless aria.has_key?(:"aria-multiselectable") || options[:multiselect] == true
  options[:data]    = data
  options[:id]      ||= "#{record_or_key.to_s}-table"
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Storefront::Components::Base

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/storefront/components/table.rb', line 4

def content
  @content
end

#keyObject (readonly)

Returns the value of attribute key.



4
5
6
# File 'lib/storefront/components/table.rb', line 4

def key
  @key
end

#templateObject (readonly)

Returns the value of attribute template.



4
5
6
# File 'lib/storefront/components/table.rb', line 4

def template
  @template
end

Instance Method Details

#body(attributes = {}, &block) ⇒ Object

scope=‘row’ <td headers=‘x’/>



88
89
90
91
92
93
94
95
96
# File 'lib/storefront/components/table.rb', line 88

def body(attributes = {}, &block)
  template.capture_haml do
    @row_index = 0
    @scope     = :body
    template.haml_tag :tbody, attributes, &block
    @row_index = 0
    @scope     = :table
  end
end

#captionObject



70
71
72
# File 'lib/storefront/components/table.rb', line 70

def caption
    
end

#cell(*args, &block) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/storefront/components/table.rb', line 192

def cell(*args, &block)
  attributes                      = args.extract_options!
  value                           = args.shift
  attributes[:role]               = :gridcell
  attributes[:id]                 ||= id_for(:cell, key, value, @row_index, @cell_index) if config.id_enabled_on.include?("table")
  #attributes[:"aria-describedby"] = @headers[@cell_index]
  attributes[:headers]            = @headers[@cell_index]
  [:width, :height].each do |size|
    attributes[size] = pixelate(attributes[size]) unless attributes[size].nil?
  end
  template.capture_haml do
    if block_given?
      template.haml_tag :td, attributes, &block
    else
      template.haml_tag :td, value, attributes
    end
    @cell_index                     += 1
  end
end

#column(*args, &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/storefront/components/table.rb', line 123

def column(*args, &block)
  attributes = args.extract_options!
  value   = args.shift
  attributes[:id]   ||= id_for(:header, key, value, @row_index, @cell_index) if config.id_enabled_on.include?("table")
  [:width, :height].each do |size|
    attributes[size] = pixelate(attributes[size]) unless attributes[size].nil?
  end
  @headers.push(attributes[:id])

  template.capture_haml do
    template.haml_tag :col, attributes
    @cell_index += 1
  end
end

#current_page_numObject



64
65
66
67
68
# File 'lib/storefront/components/table.rb', line 64

def current_page_num
  page = params[:page] ? params[:page].to_i : 1
  page = 1 if page < 1
  page
end

#first_page_path(collection) ⇒ Object



56
57
58
# File 'lib/storefront/components/table.rb', line 56

def first_page_path(collection)
  with_params(request.path, :page => 1)
end

#foot(attributes = {}, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/storefront/components/table.rb', line 98

def foot(attributes = {}, &block)
  template.capture_haml do
    @row_index = 0
    @scope     = :foot
    template.haml_tag :tfoot, attributes, &block
    @row_index = 0
    @scope     = :table
  end
end

#head(attributes = {}, &block) ⇒ Object

scope=‘col’



75
76
77
78
79
80
81
82
83
84
# File 'lib/storefront/components/table.rb', line 75

def head(attributes = {}, &block)
  @hide_header = attributes.delete(:visible) == false
  template.capture_haml do
    @row_index = 0
    @scope     = :head
    template.haml_tag :thead, attributes, &block
    @row_index = 0
    @scope     = :table
  end
end

#header(*args, &block) ⇒ Object

direction => “ascending” valid directions: ascending, descending, none, other abbr is what the header controls (for sorting)



141
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
# File 'lib/storefront/components/table.rb', line 141

def header(*args, &block)
  attributes = args.extract_options!
  value   = args.shift
  if value.is_a?(::Symbol)
    attributes[:abbr] ||= value.to_s
  end
  # aria-selected if sorted
  attributes[:role] = :columnheader
  attributes[:id]   ||= id_for(:header, key, value, @row_index, @cell_index) if config.id_enabled_on.include?("table")
  attributes[:scope] = :col
  attributes[:abbr] ||= attributes.delete(:for) if attributes.has_key?(:for)
  [:width, :height].each do |size|
    attributes[size] = pixelate(attributes[size]) unless attributes[size].nil?
  end
  sort = attributes.delete(:sort) == true
  label = attributes.delete(:label) || (value.is_a?(::String) ? value.strip : value.to_s.titleize)
    
  if sort
    attributes[:class] = [attributes[:class], attributes.delete(:sort_class) || "sortable"].compact.uniq.join(" ")
    attributes[:direction] ||= template.sort_class(value)
  end
    
  direction = attributes.delete(:direction)
  if direction.present?
    attributes[:"aria-sort"] = direction
    attributes[:class] = [attributes[:class], direction].join(" ")
    attributes[:"aria-selected"] = true
  else
    attributes[:"aria-sort"] = "none"
    attributes[:"aria-selected"] = false
  end
    
  @headers.push(attributes[:id])
  template.capture_haml do
    if block_given?
      template.haml_tag :th, attributes, &block
    else
      if sort
        template.haml_tag :th, attributes do
          template.haml_concat template.link_to_sort(label, value)
        end
      else
        template.haml_tag :th, attributes do
          template.haml_tag :span, label
        end
      end
    end
    @cell_index += 1
  end
end

#id_for(type, key, value, row_index = @row_index, column_index = @column_index) ⇒ Object



220
221
222
223
224
# File 'lib/storefront/components/table.rb', line 220

def id_for(type, key, value, row_index = @row_index, column_index = @column_index)
  [key, type, row_index, column_index].compact.map do |node|
    node.to_s.gsub(/[\s_]/, "-")
  end.join("-")
end

#last_page_path(collection) ⇒ Object



60
61
62
# File 'lib/storefront/components/table.rb', line 60

def last_page_path(collection)
  with_params(request.path, :page => collection.last_page)
end


43
44
45
46
# File 'lib/storefront/components/table.rb', line 43

def link_to_sort(title, attribute, options = {})
  sort_param = sort_value(attribute, opposite_sort_direction(attribute))
  link_to title, with_params(request.path, :sort => sort_param), options
end

#next_page_path(collection) ⇒ Object



48
49
50
# File 'lib/storefront/components/table.rb', line 48

def next_page_path(collection)
  with_params(request.path, :page => collection.next_page)
end

#prev_page_path(collection) ⇒ Object



52
53
54
# File 'lib/storefront/components/table.rb', line 52

def prev_page_path(collection)
  with_params(request.path, :page => collection.prev_page)
end

#record_key(record_or_key) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/storefront/components/table.rb', line 212

def record_key(record_or_key)
  if record_or_key.is_a?(String) || record_or_key.is_a?(Symbol)
    record_or_key.to_s
  else
    record_or_key.class.name
  end
end

#render(&block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/storefront/components/table.rb', line 31

def render(&block)
  template.capture_haml do
    template.haml_tag :table, options do
      yield(self)
    end
  end
end

#row(*args, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/storefront/components/table.rb', line 108

def row(*args, &block)
  attributes = args.extract_options!
  if @scope == :body
    attributes[:class] = [template.cycle("odd", "even"), attributes[:class]].compact.uniq.join(" ")
  end
  attributes[:role] = :row
  attributes[:scope] = :row
  template.capture_haml do
    @row_index += 1
    @cell_index = 0
    template.haml_tag :tr, attributes, &block
    @cell_index = 0
  end
end

#table_query_row_classObject



39
40
41
# File 'lib/storefront/components/table.rb', line 39

def table_query_row_class
  ["search-row", query_params.except("page", "sort").blank? ? nil : "search-results"].compact.join(" ")
end