smart_listing 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,17 @@
1
1
  module SmartListing
2
2
  module Helper
3
3
  module ControllerExtensions
4
- def smart_listing_create name, collection, options = {}
5
- name = name.to_sym
4
+ # Creates new smart listing
5
+ #
6
+ # Possible calls:
7
+ # smart_listing_create name, collection, options = {}
8
+ # smart_listing_create options = {}
9
+ def smart_listing_create *args
10
+ options = args.extract_options!
11
+ name = (args[0] || options[:name] || controller_name).to_sym
12
+ collection = args[1] || options[:collection] || smart_listing_collection
13
+
14
+ options = {:config_profile => view_context.smart_listing_config_profile}.merge(options)
6
15
 
7
16
  list = SmartListing::Base.new(name, collection, options)
8
17
  list.setup(params, cookies)
@@ -16,6 +25,10 @@ module SmartListing
16
25
  def smart_listing name
17
26
  @smart_listings[name.to_sym]
18
27
  end
28
+
29
+ def _prefixes
30
+ super << 'smart_listing'
31
+ end
19
32
  end
20
33
 
21
34
  class Builder
@@ -28,6 +41,10 @@ module SmartListing
28
41
  @smart_listing_name, @smart_listing, @template, @options, @proc = smart_listing_name, smart_listing, template, options, proc
29
42
  end
30
43
 
44
+ def name
45
+ @smart_listing_name
46
+ end
47
+
31
48
  def paginate options = {}
32
49
  if @smart_listing.collection.respond_to? :current_page
33
50
  @template.paginate @smart_listing.collection, {:remote => @smart_listing.remote?, :param_name => @smart_listing.param_name(:page), :params => UNSAFE_PARAMS}.merge(@smart_listing.kaminari_options)
@@ -44,14 +61,14 @@ module SmartListing
44
61
  end
45
62
 
46
63
  def pagination_per_page_links options = {}
47
- container_classes = [SmartListing.config.classes(:pagination_per_page)]
48
- container_classes << SmartListing.config.classes(:hidden) if empty?
64
+ container_classes = [@template.smart_listing_config.classes(:pagination_per_page)]
65
+ container_classes << @template.smart_listing_config.classes(:hidden) if empty?
49
66
 
50
67
  per_page_sizes = @smart_listing.page_sizes.clone
51
68
  per_page_sizes.push(0) if @smart_listing.unlimited_per_page?
52
69
 
53
70
  locals = {
54
- :container_classes => container_classes,
71
+ :container_classes => container_classes,
55
72
  :per_page_sizes => per_page_sizes,
56
73
  }
57
74
 
@@ -64,7 +81,7 @@ module SmartListing
64
81
  end
65
82
 
66
83
  locals = {
67
- :page => page,
84
+ :page => page,
68
85
  :url => url,
69
86
  }
70
87
 
@@ -72,8 +89,9 @@ module SmartListing
72
89
  end
73
90
 
74
91
  def sortable title, attribute, options = {}
75
- dirs = [nil, "asc", "desc"]
76
- next_index = (dirs.index(@smart_listing.sort_order(attribute)) + 1) % dirs.length
92
+ dirs = options[:sort_dirs] || @smart_listing.sort_dirs || [nil, "asc", "desc"]
93
+
94
+ next_index = dirs.index(@smart_listing.sort_order(attribute)).nil? ? 0 : (dirs.index(@smart_listing.sort_order(attribute)) + 1) % dirs.length
77
95
 
78
96
  sort_params = {
79
97
  attribute => dirs[next_index]
@@ -82,7 +100,7 @@ module SmartListing
82
100
  locals = {
83
101
  :order => @smart_listing.sort_order(attribute),
84
102
  :url => @template.url_for(sanitize_params(@template.params.merge(@smart_listing.all_params(:sort => sort_params)))),
85
- :container_classes => [SmartListing.config.classes(:sortable)],
103
+ :container_classes => [@template.smart_listing_config.classes(:sortable)],
86
104
  :attribute => attribute,
87
105
  :title => title
88
106
  }
@@ -117,10 +135,10 @@ module SmartListing
117
135
 
118
136
  # Add new item button & placeholder to list
119
137
  def item_new options = {}, &block
120
- no_records_classes = [SmartListing.config.classes(:no_records)]
121
- no_records_classes << SmartListing.config.classes(:hidden) unless empty?
138
+ no_records_classes = [@template.smart_listing_config.classes(:no_records)]
139
+ no_records_classes << @template.smart_listing_config.classes(:hidden) unless empty?
122
140
  new_item_button_classes = []
123
- new_item_button_classes << SmartListing.config.classes(:hidden) if max_count?
141
+ new_item_button_classes << @template.smart_listing_config.classes(:hidden) if max_count?
124
142
 
125
143
  locals = {
126
144
  :colspan => options.delete(:colspan),
@@ -134,15 +152,15 @@ module SmartListing
134
152
  }
135
153
 
136
154
  unless block_given?
137
- locals[:placeholder_classes] = [SmartListing.config.classes(:new_item_placeholder), SmartListing.config.classes(:hidden)]
138
- locals[:new_item_action_classes] = [SmartListing.config.classes(:new_item_action)]
139
- locals[:new_item_action_classes] << SmartListing.config.classes(:hidden) if !empty? && max_count?
155
+ locals[:placeholder_classes] = [@template.smart_listing_config.classes(:new_item_placeholder), @template.smart_listing_config.classes(:hidden)]
156
+ locals[:new_item_action_classes] = [@template.smart_listing_config.classes(:new_item_action)]
157
+ locals[:new_item_action_classes] << @template.smart_listing_config.classes(:hidden) if !empty? && max_count?
140
158
 
141
159
  @template.render(:partial => 'smart_listing/item_new', :locals => default_locals.merge(locals))
142
160
  else
143
- locals[:placeholder_classes] = [SmartListing.config.classes(:new_item_placeholder)]
144
- locals[:placeholder_classes] << SmartListing.config.classes(:hidden) if !empty? && max_count?
145
- locals[:new_item_action_classes] = [SmartListing.config.classes(:new_item_action), SmartListing.config.classes(:hidden)]
161
+ locals[:placeholder_classes] = [@template.smart_listing_config.classes(:new_item_placeholder)]
162
+ locals[:placeholder_classes] << @template.smart_listing_config.classes(:hidden) if !empty? && max_count?
163
+ locals[:new_item_action_classes] = [@template.smart_listing_config.classes(:new_item_action), @template.smart_listing_config.classes(:hidden)]
146
164
 
147
165
  locals[:new_item_content] = @template.capture(&block)
148
166
  @template.render(:partial => 'smart_listing/item_new', :locals => default_locals.merge(locals))
@@ -170,9 +188,16 @@ module SmartListing
170
188
  end
171
189
  end
172
190
 
191
+ def smart_listing_config_profile
192
+ defined?(super) ? super : :default
193
+ end
194
+
195
+ def smart_listing_config
196
+ SmartListing.config(smart_listing_config_profile)
197
+ end
198
+
173
199
  # Outputs smart list container
174
200
  def smart_listing_for name, *args, &block
175
- puts args.to_yaml
176
201
  raise ArgumentError, "Missing block" unless block_given?
177
202
  name = name.to_sym
178
203
  options = args.extract_options!
@@ -183,17 +208,18 @@ module SmartListing
183
208
  output = ""
184
209
 
185
210
  data = {}
186
- data[SmartListing.config.data_attributes(:max_count)] = @smart_listings[name].max_count if @smart_listings[name].max_count && @smart_listings[name].max_count > 0
187
- data[SmartListing.config.data_attributes(:href)] = @smart_listings[name].href if @smart_listings[name].href
188
- data[SmartListing.config.data_attributes(:callback_href)] = @smart_listings[name].callback_href if @smart_listings[name].callback_href
211
+ data[smart_listing_config.data_attributes(:max_count)] = @smart_listings[name].max_count if @smart_listings[name].max_count && @smart_listings[name].max_count > 0
212
+ data[smart_listing_config.data_attributes(:item_count)] = @smart_listings[name].count
213
+ data[smart_listing_config.data_attributes(:href)] = @smart_listings[name].href if @smart_listings[name].href
214
+ data[smart_listing_config.data_attributes(:callback_href)] = @smart_listings[name].callback_href if @smart_listings[name].callback_href
189
215
  data.merge!(options[:data]) if options[:data]
190
216
 
191
217
  if bare
192
218
  output = capture(builder, &block)
193
219
  else
194
- output = content_tag(:div, :class => SmartListing.config.classes(:main), :id => name, :data => data) do
195
- concat(content_tag(:div, "", :class => SmartListing.config.classes(:loading)))
196
- concat(content_tag(:div, :class => SmartListing.config.classes(:content)) do
220
+ output = content_tag(:div, :class => smart_listing_config.classes(:main), :id => name, :data => data) do
221
+ concat(content_tag(:div, "", :class => smart_listing_config.classes(:loading)))
222
+ concat(content_tag(:div, :class => smart_listing_config.classes(:content)) do
197
223
  concat(capture(builder, &block))
198
224
  end)
199
225
  end
@@ -202,7 +228,7 @@ module SmartListing
202
228
  output
203
229
  end
204
230
 
205
- def smart_listing_render name, *args
231
+ def smart_listing_render name = controller_name, *args
206
232
  smart_listing_for(name, *args) do |smart_listing|
207
233
  concat(smart_listing.render_list)
208
234
  end
@@ -211,9 +237,9 @@ module SmartListing
211
237
  def smart_listing_controls_for name, *args, &block
212
238
  smart_listing = @smart_listings.try(:[], name)
213
239
 
214
- classes = [SmartListing.config.classes(:controls), args.first.try(:[], :class)]
240
+ classes = [smart_listing_config.classes(:controls), args.first.try(:[], :class)]
215
241
 
216
- form_tag(smart_listing.try(:href) || {}, :remote => smart_listing.try(:remote?) || true, :method => :get, :class => classes, :data => {:smart_listing => name}) do
242
+ form_tag(smart_listing.try(:href) || {}, :remote => smart_listing.try(:remote?) || true, :method => :get, :class => classes, :data => {smart_listing_config.data_attributes(:main) => name}) do
217
243
  concat(content_tag(:div, :style => "margin:0;padding:0;display:inline") do
218
244
  concat(hidden_field_tag("#{smart_listing.try(:base_param)}[_]", 1, :id => nil)) # this forces smart_listing_update to refresh the list
219
245
  end)
@@ -228,31 +254,41 @@ module SmartListing
228
254
  next unless action.is_a?(Hash)
229
255
 
230
256
  locals = {
231
- :action_if => action.has_key?(:if) ? action[:if] : true,
257
+ :action_if => action.has_key?(:if) ? action[:if] : true,
232
258
  :url => action.delete(:url),
233
259
  :icon => action.delete(:icon),
234
260
  :title => action.delete(:title),
235
261
  }
236
- locals[:icon] = [locals[:icon], SmartListing.config.classes(:muted)] if !locals[:action_if]
237
262
 
263
+ template = nil
238
264
  action_name = action[:name].to_sym
265
+
239
266
  case action_name
240
267
  when :show
241
- concat(render(:partial => 'smart_listing/action_show', :locals => locals))
268
+ locals[:icon] ||= smart_listing_config.classes(:icon_show)
269
+ template = 'action_show'
242
270
  when :edit
243
- concat(render(:partial => 'smart_listing/action_edit', :locals => locals))
271
+ locals[:icon] ||= smart_listing_config.classes(:icon_edit)
272
+ template = 'action_edit'
244
273
  when :destroy
274
+ locals[:icon] ||= smart_listing_config.classes(:icon_trash)
245
275
  locals.merge!(
246
276
  :confirmation => action.delete(:confirmation),
247
277
  )
248
- concat(render(:partial => 'smart_listing/action_delete', :locals => locals))
278
+ template = 'action_delete'
249
279
  when :custom
250
280
  locals.merge!(
251
281
  :html_options => action,
252
282
  )
253
- concat(render(:partial => 'smart_listing/action_custom', :locals => locals))
283
+ template = 'action_custom'
284
+ end
285
+
286
+ locals[:icon] = [locals[:icon], smart_listing_config.classes(:muted)] if !locals[:action_if]
287
+
288
+ if template
289
+ concat(render(:partial => "smart_listing/#{template}", :locals => locals))
254
290
  else
255
- concat(render(:partial => "smart_listing/action_#{action_name}", :locals => {:action => action}))
291
+ concat(render(:partial => "smart_listing/action_#{action_name}", :locals => {:action => action}))
256
292
  end
257
293
  end
258
294
  end
@@ -268,9 +304,14 @@ module SmartListing
268
304
  #################################################################################################
269
305
  # JS helpers:
270
306
 
271
- # Updates the smart list
272
- def smart_listing_update name, options = {}
273
- name = name.to_sym
307
+ # Updates smart listing
308
+ #
309
+ # Posible calls:
310
+ # smart_listing_update name, options = {}
311
+ # smart_listing_update options = {}
312
+ def smart_listing_update *args
313
+ options = args.extract_options!
314
+ name = (args[0] || options[:name] || controller_name).to_sym
274
315
  smart_listing = @smart_listings[name]
275
316
 
276
317
  # don't update list if params are missing (prevents interfering with other lists)
@@ -280,19 +321,35 @@ module SmartListing
280
321
 
281
322
  builder = Builder.new(name, smart_listing, self, {}, nil)
282
323
  render(:partial => 'smart_listing/update_list', :locals => {
283
- :name => smart_listing.name,
284
- :part => smart_listing.partial,
285
- :smart_listing => builder,
324
+ :name => smart_listing.name,
325
+ :part => smart_listing.partial,
326
+ :smart_listing => builder,
286
327
  :smart_listing_data => {
287
- SmartListing.config.data_attributes(:params) => smart_listing.all_params,
288
- SmartListing.config.data_attributes(:max_count) => smart_listing.max_count,
328
+ smart_listing_config.data_attributes(:params) => smart_listing.all_params,
329
+ smart_listing_config.data_attributes(:max_count) => smart_listing.max_count,
330
+ smart_listing_config.data_attributes(:item_count) => smart_listing.count,
289
331
  }
290
332
  })
291
333
  end
292
334
 
293
335
  # Renders single item (i.e for create, update actions)
294
- def smart_listing_item name, item_action, object = nil, partial = nil, options = {}
295
- name = name.to_sym
336
+ #
337
+ # Possible calls:
338
+ # smart_listing_item name, item_action, object = nil, partial = nil, options = {}
339
+ # smart_listing_item item_action, object = nil, partial = nil, options = {}
340
+ def smart_listing_item *args
341
+ options = args.extract_options!
342
+ if [:create, :create_continue, :destroy, :edit, :new, :remove, :update].include?(args[1])
343
+ name = args[0]
344
+ item_action = args[1]
345
+ object = args[2]
346
+ partial = args[3]
347
+ else
348
+ name = (options[:name] || controller_name).to_sym
349
+ item_action = args[0]
350
+ object = args[1]
351
+ partial = args[2]
352
+ end
296
353
  type = object.class.name.downcase.to_sym if object
297
354
  id = options[:id] || object.try(:id)
298
355
  valid = options[:valid] if options.has_key?(:valid)
@@ -7,8 +7,8 @@
7
7
  paginator: the paginator that renders the pagination tags inside
8
8
  -%>
9
9
  <%= paginator.render do -%>
10
- <div class="text-center">
11
- <ul class="pagination">
10
+ <div class="<%= smart_listing_config.classes(:pagination_wrapper) %>">
11
+ <ul class="<%= smart_listing_config.classes(:pagination_container) %>">
12
12
  <%= first_page_tag unless current_page.first? %>
13
13
  <%= prev_page_tag unless current_page.first? %>
14
14
  <% each_page do |page| -%>
@@ -2,10 +2,11 @@
2
2
  Delete action.
3
3
 
4
4
  Available local variables:
5
+ action_if: false if action is disabled
5
6
  url: destroy url
6
- icon
7
+ icon: icon css classes
7
8
  confirmation: confrimation text
8
9
  title
9
10
  -%>
10
11
 
11
- <%= link_to_if(action_if, content_tag(:i, "", :class => icon || SmartListing.config.classes(:icon_trash)), url, :remote => true, :class => "destroy", :method => :delete, :title => title || t("smart_listing.actions.destroy"), :data => {:confirmation => confirmation || t("smart_listing.msgs.destroy_confirmation")} ) %>
12
+ <%= link_to_if(action_if, content_tag(:i, "", :class => icon), url, :remote => true, :class => "destroy", :method => :delete, :title => title || t("smart_listing.actions.destroy"), :data => {:confirmation => confirmation || t("smart_listing.msgs.destroy_confirmation")} ) %>
@@ -2,9 +2,10 @@
2
2
  Edit action.
3
3
 
4
4
  Available local variables:
5
- url
6
- icon
5
+ action_if: false if action is disabled
6
+ url: edit url
7
+ icon: icon css classes
7
8
  title
8
9
  -%>
9
10
 
10
- <%= link_to_if(action_if, content_tag(:i, '', :class => icon || SmartListing.config.classes(:icon_edit)), url, :remote => true, :class => "edit", :title => title || t("smart_listing.actions.edit") ) %>
11
+ <%= link_to_if(action_if, content_tag(:i, '', :class => icon), url, :remote => true, :class => "edit", :title => title || t("smart_listing.actions.edit") ) %>
@@ -2,9 +2,10 @@
2
2
  Show action.
3
3
 
4
4
  Available local variables:
5
- url
6
- icon
5
+ action_if: false if action is disabled
6
+ url: show url
7
+ icon: icon clss classes
7
8
  title
8
9
  -%>
9
10
 
10
- <%= link_to_if(action_if, content_tag(:i, '', :class => icon || SmartListing.config.classes(:icon_show)), url, :class => "show", :title => title || t("smart_listing.actions.show") ) %>
11
+ <%= link_to_if(action_if, content_tag(:i, '', :class => icon), url, :class => "show", :title => title || t("smart_listing.actions.show") ) %>
@@ -14,7 +14,7 @@
14
14
  new_item_content new item placeholder content (usually some form)
15
15
  -%>
16
16
 
17
- <%= content_tag(:tr, '', :class => placeholder_classes + %w{info}, :data => {SmartListing.config.data_attributes(:autoshow) => new_item_autoshow}) do %>
17
+ <%= content_tag(:tr, '', :class => placeholder_classes + %w{info}, :data => {smart_listing_config.data_attributes(:autoshow) => new_item_autoshow}) do %>
18
18
  <%= new_item_content %>
19
19
  <% end %>
20
20
  <%= content_tag(:tr, :class => new_item_action_classes + %w{info}) do %>
@@ -23,7 +23,7 @@
23
23
  <%= no_items_text %>
24
24
  <% end %>
25
25
  <%= link_to(new_item_button_url, :remote => true, :class => new_item_button_classes + %w{btn pull-right}) do %>
26
- <%= content_tag(:i, '', :class => SmartListing.config.classes(:icon_new)) %>
26
+ <%= content_tag(:i, '', :class => smart_listing_config.classes(:icon_new)) %>
27
27
  <%= new_item_button_text %>
28
28
  <% end %>
29
29
  <% end %>
@@ -11,7 +11,7 @@
11
11
  <%= content_tag(:div, :class => container_classes ) do %>
12
12
  <% if smart_listing.options[:paginate] && (smart_listing.count > smart_listing.page_sizes.first) %>
13
13
  <%= t('views.pagination.per_page') %>
14
- <% per_page_sizes.each do |p| %><%= builder.pagination_per_page_link p %><% break if p > smart_listing.count %><% end %>
14
+ <% per_page_sizes.each do |p| %><%= builder.pagination_per_page_link p %><% break if p >= smart_listing.count %><% end %>
15
15
  |
16
16
  <% end %>
17
17
  <%= t('views.pagination.total') %>
@@ -14,8 +14,8 @@
14
14
  <%= link_to url, :class => container_classes, :data => {:attr => attribute}, :remote => true do %>
15
15
  <%= title %>
16
16
  <% if order %>
17
- <span class="<%= order == "asc" ? SmartListing.config.classes(:icon_sort_up) : SmartListing.config.classes(:icon_sort_down) %>"></span>
17
+ <span class="<%= order == "asc" ? smart_listing_config.classes(:icon_sort_up) : smart_listing_config.classes(:icon_sort_down) %>"></span>
18
18
  <% else %>
19
- <span class="<%= SmartListing.config.classes(:icon_sort_none) %>"></span>
19
+ <span class="<%= smart_listing_config.classes(:icon_sort_none) %>"></span>
20
20
  <% end %>
21
21
  <% end %>
@@ -0,0 +1,2 @@
1
+ <%= smart_listing_item :create, smart_listing_resource,
2
+ smart_listing_resource.valid? ? "item" : "form" %>
@@ -0,0 +1 @@
1
+ <%= smart_listing_item :destroy, smart_listing_resource %>
@@ -0,0 +1 @@
1
+ <%= smart_listing_item :edit, smart_listing_resource, "form" %>
@@ -0,0 +1 @@
1
+ <%= smart_listing_update %>
@@ -1,2 +1,2 @@
1
- var smart_listing = $('#<%= name %>').smart_listing();
1
+ var smart_listing = $('#<%= name.to_s %>').smart_listing();
2
2
  smart_listing.destroy(<%= id %>, <%= object.destroyed? %>);
@@ -0,0 +1 @@
1
+ <%= smart_listing_item :new, smart_listing_resource, "form" %>
@@ -0,0 +1,2 @@
1
+ <%= smart_listing_item :update, smart_listing_resource,
2
+ smart_listing_resource.valid? ? "item" : "form" %>
@@ -12,6 +12,7 @@ SmartListing.configure do |config|
12
12
  #:memorize_per_page => false, # save per page settings in the cookie
13
13
  #:page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array
14
14
  #:kaminari_options => {:theme => "smart_listing"}, # Kaminari's paginate helper options
15
+ #:sort_dirs => [nil, "asc", "desc"], # Default sorting directions cycle of sortables
15
16
  })
16
17
 
17
18
  config.constants :classes, {
@@ -27,8 +28,9 @@ SmartListing.configure do |config|
27
28
  #:hidden => "hidden",
28
29
  #:autoselect => "autoselect",
29
30
  #:callback => "callback",
31
+ #:pagination_wrapper => "text-center",
32
+ #:pagination_container => "pagination",
30
33
  #:pagination_per_page => "pagination-per-page text-center",
31
- #:pagination_count => "count",
32
34
  #:inline_editing => "info",
33
35
  #:no_records => "no-records",
34
36
  #:limit => "smart-listing-limit",
@@ -48,15 +50,18 @@ SmartListing.configure do |config|
48
50
  #:icon_sort_none => "glyphicon glyphicon-resize-vertical",
49
51
  #:icon_sort_up => "glyphicon glyphicon-chevron-up",
50
52
  #:icon_sort_down => "glyphicon glyphicon-chevron-down",
53
+ #:muted => "text-muted",
51
54
  }
52
55
 
53
56
  config.constants :data_attributes, {
54
57
  #:main => "smart-listing",
58
+ #:controls_initialized => "smart-listing-controls-initialized",
55
59
  #:confirmation => "confirmation",
56
60
  #:id => "id",
57
61
  #:href => "href",
58
62
  #:callback_href => "callback-href",
59
63
  #:max_count => "max-count",
64
+ #:item_count => "item-count",
60
65
  #:inline_edit_backup => "smart-listing-edit-backup",
61
66
  #:params => "params",
62
67
  #:observed => "observed",
@@ -71,5 +76,9 @@ SmartListing.configure do |config|
71
76
  #:row => "tr",
72
77
  #:head => "thead",
73
78
  #:filtering_icon => "i"
79
+ #:filtering_button => "button",
80
+ #:filtering_icon => "button span",
81
+ #:filtering_input => ".filter input",
82
+ #:pagination_count => ".pagination-per-page .count",
74
83
  }
75
84
  end
@@ -1,18 +1,20 @@
1
1
  module SmartListing
2
- def self.configure(&block)
3
- yield @config ||= SmartListing::Configuration.new
2
+ mattr_reader :configs
3
+
4
+ def self.configure profile = nil
5
+ profile ||= :default
6
+ @@configs ||= {}
7
+ yield @@configs[profile] ||= SmartListing::Configuration.new
4
8
  end
5
9
 
6
- def self.config
7
- @config ||= SmartListing::Configuration.new
10
+ def self.config profile = nil
11
+ profile ||= :default
12
+ @@configs ||= {}
13
+ @@configs[profile] ||= SmartListing::Configuration.new
8
14
  end
9
15
 
10
16
  class Configuration
11
- if Rails.env.development?
12
- DEFAULT_PAGE_SIZES = [3, 10, 20, 50, 100]
13
- else
14
- DEFAULT_PAGE_SIZES = [10, 20, 50, 100]
15
- end
17
+ DEFAULT_PAGE_SIZES = [10, 20, 50, 100].freeze
16
18
 
17
19
  DEFAULTS = {
18
20
  :global_options => {
@@ -26,8 +28,9 @@ module SmartListing
26
28
  :unlimited_per_page => false, # allow infinite page size
27
29
  :paginate => true, # allow pagination
28
30
  :memorize_per_page => false,
29
- :page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array
31
+ :page_sizes => DEFAULT_PAGE_SIZES.dup, # set available page sizes array
30
32
  :kaminari_options => {:theme => "smart_listing"}, # Kaminari's paginate helper options
33
+ :sort_dirs => [nil, "asc", "desc"], # Default sorting directions cycle of sortables
31
34
  },
32
35
  :constants => {
33
36
  :classes => {
@@ -43,8 +46,9 @@ module SmartListing
43
46
  :hidden => "hidden",
44
47
  :autoselect => "autoselect",
45
48
  :callback => "callback",
49
+ :pagination_wrapper => "text-center",
50
+ :pagination_container => "pagination",
46
51
  :pagination_per_page => "pagination-per-page text-center",
47
- :pagination_count => "count",
48
52
  :inline_editing => "info",
49
53
  :no_records => "no-records",
50
54
  :limit => "smart-listing-limit",
@@ -68,15 +72,16 @@ module SmartListing
68
72
  },
69
73
  :data_attributes => {
70
74
  :main => "smart-listing",
75
+ :controls_initialized => "smart-listing-controls-initialized",
71
76
  :confirmation => "confirmation",
72
77
  :id => "id",
73
78
  :href => "href",
74
79
  :callback_href => "callback-href",
75
80
  :max_count => "max-count",
81
+ :item_count => "item-count",
76
82
  :inline_edit_backup => "smart-listing-edit-backup",
77
83
  :params => "params",
78
84
  :observed => "observed",
79
- :href => "href",
80
85
  :autoshow => "autoshow",
81
86
  :popover => "slpopover",
82
87
  },
@@ -87,13 +92,16 @@ module SmartListing
87
92
  :head => "thead",
88
93
  :filtering_button => "button",
89
94
  :filtering_icon => "button span",
90
- :filtering_input => ".filter input"
95
+ :filtering_input => ".filter input",
96
+ :pagination_count => ".pagination-per-page .count",
91
97
  }
92
98
  }
93
- }
99
+ }.freeze
100
+
101
+ attr_reader :options
94
102
 
95
103
  def initialize
96
- @options = DEFAULTS
104
+ @options = {}
97
105
  end
98
106
 
99
107
  def method_missing(sym, *args, &block)
@@ -101,29 +109,44 @@ module SmartListing
101
109
  end
102
110
 
103
111
  def constants key, value = nil
104
- if value
112
+ if value && !value.empty?
113
+ @options[:constants] ||= {}
114
+ @options[:constants][key] ||= {}
105
115
  @options[:constants][key].merge!(value)
106
116
  end
107
- @options[:constants][key]
117
+ @options[:constants].try(:[], key) || DEFAULTS[:constants][key]
108
118
  end
109
119
 
110
120
  def classes key
111
- @options[:constants][:classes][key]
121
+ @options[:constants].try(:[], :classes).try(:[], key) || DEFAULTS[:constants][:classes][key]
112
122
  end
113
123
 
114
124
  def data_attributes key
115
- @options[:constants][:data_attributes][key]
125
+ @options[:constants].try(:[], :data_attributes).try(:[], key) || DEFAULTS[:constants][:data_attributes][key]
116
126
  end
117
127
 
118
128
  def selectors key
119
- @options[:constants][:selectors][key]
129
+ @options[:constants].try(:[], :selectors).try(:[], key) || DEFAULTS[:constants][:selectors][key]
120
130
  end
121
131
 
122
132
  def global_options value = nil
123
- if value
133
+ if value && !value.empty?
134
+ @options[:global_options] ||= {}
124
135
  @options[:global_options].merge!(value)
125
136
  end
126
- @options[:global_options]
137
+ !@options[:global_options].try(:empty?) ? DEFAULTS[:global_options] : DEFAULTS[:global_options].deep_merge(@options[:global_options])
138
+ end
139
+
140
+ def to_json
141
+ @options.to_json
142
+ end
143
+
144
+ def dump
145
+ DEFAULTS.deep_merge(@options)
146
+ end
147
+
148
+ def dump_json
149
+ dump.to_json
127
150
  end
128
151
  end
129
152
  end
@@ -1,3 +1,3 @@
1
1
  module SmartListing
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end