Class: MagicGrid::HtmlGrid

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_grid/html_grid.rb

Instance Method Summary collapse

Constructor Details

#initialize(grid_definition, view, controller = nil) ⇒ HtmlGrid

Returns a new instance of HtmlGrid.



9
10
11
12
13
14
# File 'lib/magic_grid/html_grid.rb', line 9

def initialize(grid_definition, view, controller = nil)
  @grid = grid_definition
  @spinner_drawn = false
  @view ||= view
  @current_url = controller && controller.request.fullpath
end

Instance Method Details



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/magic_grid/html_grid.rb', line 160

def column_link_params(col)
  id = col.id
  my_params = grid.base_params.merge(grid.param_key(:col) => id)
  params = HashWithIndifferentAccess.new(my_params)
  if id.to_s == grid.current_sort_col.to_s
    params[grid.param_key(:order)] = grid.current_order.reverse.to_param
  else
    params.delete(grid.param_key(:order))
  end
  params
end

#filler_block(&block) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/magic_grid/html_grid.rb', line 90

def filler_block(&block)
  view. 'tr' do
    view.('td', nil,
                      :class => 'full-width ui-widget-header',
                      :colspan => grid.columns.count,
                      &block)
  end
end

#grid_cell(column, record) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/magic_grid/html_grid.rb', line 143

def grid_cell(column, record)
  view.('td', :class => column.html_classes) do
    method = column.reader
    if method.respond_to? :call
      method.call(record).to_s
    elsif record.respond_to? method
      record.send(method).to_s
    else
      ""
    end
  end
end

#grid_row(record) ⇒ Object



137
138
139
140
141
# File 'lib/magic_grid/html_grid.rb', line 137

def grid_row(record)
  view. 'tr', :class => view.cycle('odd', 'even') do
    grid.columns.map { |c| grid_cell(c, record) }.join.html_safe
  end
end

#magic_column_headersObject



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/magic_grid/html_grid.rb', line 99

def magic_column_headers
  view. 'tr' do
    grid.columns.reduce(''.html_safe) do |acc, col|
      classes = ['ui-state-default'] << col.html_classes
      acc <<
      if col.sortable?
        sortable_header(col)
      else
        view. 'th', col.label.html_safe, :class => classes.join(' ')
      end
    end
  end
end

#magic_grid_footObject



82
83
84
85
86
87
88
# File 'lib/magic_grid/html_grid.rb', line 82

def magic_grid_foot
  if grid.options[:per_page] and grid.options[:bottom_pager]
    magic_pager_block
  elsif not grid.options[:collapse_emtpy_footer]
    filler_block
  end
end

#magic_grid_headObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/magic_grid/html_grid.rb', line 56

def magic_grid_head
  thead = []
  if grid.has_title?
    thead << render_title(grid.title)
  end
  if grid.needs_searcher?
    thead << searcher_block
  end
  if grid.options[:per_page] and grid.options[:top_pager]
    thead << magic_pager_block(true)
  end
  if thead.empty? and not grid.options[:collapse_emtpy_header]
    thead << filler_block(&method(:render_spinner))
  end
  thead << magic_column_headers
  thead.join.html_safe
end

#magic_pager(collection, opts = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/magic_grid/html_grid.rb', line 214

def magic_pager(collection, opts={})
  if view.respond_to? :will_paginate
    # WillPaginate
    view.will_paginate collection.collection, opts
  elsif view.respond_to? :paginate
    #Kaminari, or something else..
    view.paginate collection.collection, opts
  else
    ("<!-- page #{collection.current_page} of #{collection.total_pages} -->" +
     '<!-- INSTALL WillPaginate or Kaminari for a pager! -->').html_safe
  end
end

#magic_pager_block(spinner = false) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/magic_grid/html_grid.rb', line 227

def magic_pager_block(spinner = false)
  view.('tr') do
    view.('td', :class => 'full-width ui-widget-header magic-pager',
                     :colspan => grid.columns.count) do
      pager = magic_pager(grid.magic_collection,
                          :param_name => grid.param_key(:page),
                          :params => grid.base_params)
      if spinner
        pager << render_spinner
      end
      pager
    end
  end
end

#magic_rowsObject



113
114
115
116
117
118
119
120
# File 'lib/magic_grid/html_grid.rb', line 113

def magic_rows
  rows = grid.collection.map(&@row_renderer)
  if rows.empty?
    render_empty_collection(grid.options[:if_empty]).html_safe
  else
    rows.join.html_safe
  end
end

#order_icon(order = Order::Unordered) ⇒ Object



156
157
158
# File 'lib/magic_grid/html_grid.rb', line 156

def order_icon(order = Order::Unordered)
  view. 'span', '', :class => "ui-icon #{order.icon_class}"
end

#render(&row_renderer) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/magic_grid/html_grid.rb', line 16

def render(&row_renderer)
  @row_renderer = row_renderer || method(:grid_row)
  table_options = {
    :class => "magic_grid #{grid.options[:class]}",
    :id    => grid.magic_id,
    :data  => {
      :searcher             => grid.searcher,
      :current              => @current_url,
      :live_search          => grid.options[:live_search],
      :listeners            => grid.options[:listeners],
      :remote               => grid.options[:remote],
      :default_ajax_handler => grid.options[:default_ajax_handler],
      :params               => grid.base_params,
    }.reject {|_,v| v.nil? }
  }
  table(table_options)
end

#render_empty_collection(fallback) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/magic_grid/html_grid.rb', line 122

def render_empty_collection(fallback)
  if fallback
    view. 'tr' do
      view.('td', :colspan => grid.columns.count,
                        :class => 'if-empty') do
        if fallback.respond_to? :call
          fallback.call(grid).to_s
        else
          fallback
        end
      end
    end
  end
end

#render_spinnerObject



47
48
49
50
51
52
53
54
# File 'lib/magic_grid/html_grid.rb', line 47

def render_spinner
  unless @spinner_drawn
    @spinner_drawn = true
    view.tag('span',
              :id => (grid.magic_id.to_s + "_spinner"),
              :class => "magic_grid_spinner")
  end
end

#render_title(title) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/magic_grid/html_grid.rb', line 74

def render_title(title)
  view. 'tr' do
    view.('th', title,
                      :class => 'full-width ui-widget-header',
                      :colspan => grid.columns.count)
  end
end

#searcher_blockObject



185
186
187
188
189
190
191
192
# File 'lib/magic_grid/html_grid.rb', line 185

def searcher_block
  view.('tr') do
    view.('td', :class => 'searcher full-width ui-widget-header',
                     :colspan => grid.columns.count) do
      searcher_input
    end
  end
end

#searcher_inputObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/magic_grid/html_grid.rb', line 194

def searcher_input
  searcher_data = {
    :min_length => grid.options[:min_search_length],
    :current    => grid.current_search,
  }
  searcher = view.label_tag(grid.searcher.to_sym,
                            grid.options[:searcher_label])
  searcher << view.search_field_tag(grid.searcher.to_sym,
                                    grid.param(:q),
                                    :placeholder => grid.options[:searcher_tooltip],
                                    :size        => grid.options[:searcher_size],
                                    :data        => searcher_data,
                                    :form        => "a form that doesn't exist")
  if grid.options[:search_button]
    searcher << view.button_tag(grid.options[:searcher_button],
                                :class => 'magic-grid-search-button')
  end
  searcher << render_spinner
end

#sortable_header(col) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/magic_grid/html_grid.rb', line 172

def sortable_header(col)
  id = col.id
  label = col.label || id.titleize
  params = column_link_params(col)
  classes = %w{sorter ui-state-default} << col.html_classes
  classes << 'sort-current' if col.order.sorted?
  view. 'th', :class => classes.join(' ') do
    view.link_to params, :remote => grid.options[:remote] do
      label.html_safe << order_icon(col.order)
    end
  end
end

#table(options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/magic_grid/html_grid.rb', line 34

def table(options)
  view.('table', options) do
    view.('thead',
                     :data => {:params => grid.base_params},
                     &method(:magic_grid_head)) <<
    view.('tbody',
                     :class => "ui-widget-content",
                     &method(:magic_rows)) <<
    view.('tfoot',
                     &method(:magic_grid_foot))
  end
end