livelist-rails 0.0.12 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,251 +0,0 @@
1
- class window.Utilities
2
- setOptions: (options, context = @) =>
3
- _.each( options, (value, option) => context[option] = value )
4
-
5
- class window.LiveList extends Utilities
6
- constructor: (options) ->
7
- @listSelector = options.list.renderTo
8
- @resourceName = options.global.resourceName
9
- @resourceNameSingular = options.global.resourceNameSingular
10
- @urlPrefix = options.global.urlPrefix || "/#{@resourceName}"
11
- @httpMethod = options.global.httpMethod || 'get'
12
- @eventName = "livelist:#{@resourceName}"
13
- @search = new Search(options.search, @)
14
- @filters = new Filters(options.filters, @)
15
- @pagination = new Pagination(options.pagination, @)
16
- @list = new List(options.list, @)
17
-
18
- class window.List extends Utilities
19
- constructor: (options, livelist) ->
20
- @fetchRequest = null
21
- @livelist = livelist
22
-
23
- @listTemplate = "{{##{@livelist.resourceName}}}{{>#{@livelist.resourceNameSingular}}}{{/#{@livelist.resourceName}}}"
24
- @listItemTemplate = '<li>{{id}}</li>'
25
- @fetchingIndicationClass = 'updating'
26
- @renderTo = "ul##{@livelist.resourceName}"
27
-
28
- @setOptions(options)
29
-
30
- $(@renderTo).bind(@livelist.eventName, (event, params) => @fetch(presets: null, page: params?.page))
31
- @fetch(presets: @livelist.filters.getPresets())
32
-
33
- displayFetchingIndication: => $(@renderTo).addClass(@fetchingIndicationClass)
34
- removeFetchingIndication: => $(@renderTo).removeClass(@fetchingIndicationClass)
35
-
36
- renderIndex: (data, textStatus, jqXHR) =>
37
- @livelist.data = data
38
- @render()
39
- @livelist.pagination.render(@livelist.data)
40
- @livelist.filters.render(@livelist.data)
41
-
42
- fetch: (options) ->
43
- @fetchRequest.abort() if @fetchRequest
44
- searchTerm = @livelist.search.searchTerm()
45
- params = {}
46
- params.filters = @livelist.filters.setPresets(options.presets)
47
-
48
- if searchTerm
49
- params.q = searchTerm
50
- if options.page
51
- params.page = options.page
52
-
53
- @fetchRequest = $.ajax(
54
- url : @livelist.urlPrefix
55
- type : @livelist.httpMethod
56
- dataType : 'json'
57
- data : params
58
- beforeSend : @displayFetchingIndication
59
- success : @renderIndex
60
- )
61
-
62
- render: ->
63
- partials = {}
64
- partials[@livelist.resourceNameSingular] = @listItemTemplate
65
- listHTML = Mustache.to_html(@listTemplate, @livelist.data, partials)
66
- $(@renderTo).html( listHTML )
67
- @removeFetchingIndication()
68
-
69
- window.LiveList.version = '0.0.7'
70
-
71
- class window.Filters extends Utilities
72
- constructor: (options, livelist) ->
73
- @livelist = livelist
74
- @filters = if options.presets then _.keys(options.presets) else []
75
- @initializeCookies()
76
- @setOptions(options)
77
-
78
- $('input.filter_option', @renderTo).live( 'change', => $(@livelist.listSelector).trigger(@livelist.eventName) )
79
- $(@advancedOptionsToggleSelector).click(@handleAdvancedOptionsClick)
80
-
81
- initializeCookies: ->
82
- if jQuery.cookie && @useCookies && @cookieName
83
- @cookieName = 'livelist_filter_presets'
84
-
85
- getPresets: ->
86
- cookie = jQuery.cookie(@cookieName) if jQuery.cookie && @useCookies
87
- if @useCookies && cookie
88
- JSON.parse(cookie)
89
- else
90
- @presets
91
-
92
- setPresets: (presets) ->
93
- filters = {}
94
- if jQuery.isEmptyObject(presets)
95
- filters = @selections()
96
- @setCookie(filters) if jQuery.cookie
97
- else
98
- filters = presets
99
- filters
100
-
101
- setCookie: (params_filters) ->
102
- if not jQuery.isEmptyObject(params_filters)
103
- jQuery.cookie(@cookieName, JSON.stringify(params_filters))
104
-
105
- template: '''
106
- {{#filters}}
107
- <div class='filter'>
108
- <h3>
109
- {{name}}
110
- </h3>
111
- <ul id='{{filter_slug}}_filter_options'>
112
- {{#options}}
113
- <label>
114
- <li>
115
- <input {{#selected}}checked='checked'{{/selected}}
116
- class='left filter_option'
117
- id='filter_{{slug}}'
118
- name='filters[]'
119
- type='checkbox'
120
- value='{{value}}' />
121
- <div class='left filter_name'>{{name}}</div>
122
- <div class='right filter_count'>{{count}}</div>
123
- <div class='clear'></div>
124
- </li>
125
- </label>
126
- {{/options}}
127
- </ul>
128
- </div>
129
- {{/filters}}
130
- '''
131
-
132
- selections: ->
133
- filters = {}
134
- _.each( @filters, (filter) =>
135
- filters[filter] = _.pluck( $("##{filter}_filter_options input.filter_option:checked"), 'value' )
136
- )
137
- filters
138
-
139
- noFiltersSelected: (data) ->
140
- _.all( data.filters, (filter) ->
141
- _.all( filter.options, (option) ->
142
- not option.selected
143
- )
144
- )
145
-
146
- sortOptions: (filters) ->
147
- _.map( filters, (filter) ->
148
- filter.options = _.sortBy( filter.options, (option) -> option.name)
149
- filter
150
- )
151
-
152
- sort: (filters) ->
153
- _.sortBy( filters, (filter) -> filter.name )
154
-
155
- render: (data) ->
156
- #What is this for?
157
- @filters = _.pluck( data.filters, 'filter_slug' )
158
-
159
- @sort(data.filters)
160
- @sortOptions(data.filters)
161
-
162
- filtersHTML = Mustache.to_html(@template, data)
163
- $(@renderTo).html( filtersHTML )
164
- if @noFiltersSelected(data) && data[@livelist.resourceName].length > 0
165
- $('input[type="checkbox"]', @renderTo).attr('checked', 'checked')
166
-
167
-
168
- handleAdvancedOptionsClick: (event) =>
169
- event.preventDefault()
170
- $(@renderTo).slideToggle()
171
-
172
- class window.Pagination extends Utilities
173
- constructor: (options, livelist) ->
174
- @livelist = livelist
175
- @pagination = null
176
- @maxPages = 30
177
-
178
- @emptyListMessage = "<p>No #{@livelist.resourceName} matched your filter criteria</p>"
179
- @setOptions(options)
180
-
181
- $("#{@renderTo} a").live( 'click', (event) -> event.preventDefault() )
182
- $("#{@renderTo} li:not(.disabled) a").live('click', @handlePaginationLinkClick)
183
-
184
- template: '''
185
- {{#isEmpty}}
186
- {{{emptyListMessage}}}
187
- {{/isEmpty}}
188
- {{^isEmpty}}
189
- <div class="pagination">
190
- <ul>
191
- <li class="{{^previousPage}}disabled{{/previousPage}}">
192
- <a href='{{urlPrefix}}?page={{previousPage}}' data-page='{{previousPage}}'>← Previous</a>
193
- </li>
194
-
195
- {{#pages}}
196
- <li class="{{#currentPage}}active disabled{{/currentPage}}">
197
- <a href='{{urlPrefix}}?page={{page}}' data-page='{{page}}'>{{page}}</a>
198
- </li>
199
- {{/pages}}
200
-
201
- <li class="{{^nextPage}}disabled{{/nextPage}}">
202
- <a href='{{urlPrefix}}?page={{nextPage}}' data-page='{{nextPage}}'>Next →</a>
203
- </li>
204
- </ul>
205
- </div>
206
- {{/isEmpty}}
207
- '''
208
-
209
- pagesJSON: (currentPage, totalPages) ->
210
- groupSize = Math.floor(@maxPages / 2)
211
- firstPage = if currentPage <= groupSize then 1 else currentPage - groupSize
212
- previousPage = firstPage + groupSize * 2 - 1
213
- lastPage = if previousPage >= totalPages then totalPages else previousPage
214
- _.map([firstPage..lastPage], (page) ->
215
- page: page
216
- currentPage: currentPage is page
217
- )
218
-
219
- paginationJSON: (pagination) ->
220
- {
221
- isEmpty : pagination.total_pages == 0
222
- emptyListMessage : @emptyListMessage
223
- currentPage : pagination.current_page
224
- nextPage : pagination.next_page
225
- previousPage : pagination.previous_page
226
- urlPrefix : @livelist.urlPrefix
227
- pages : @pagesJSON(pagination.current_page, pagination.total_pages)
228
- }
229
-
230
- render: (data) ->
231
- @pagination = @paginationJSON(data.pagination)
232
- paginationHTML = Mustache.to_html(@template, @pagination)
233
- $(@renderTo).html( paginationHTML )
234
-
235
- handlePaginationLinkClick: (event) =>
236
- event.preventDefault()
237
- $(@livelist.listSelector).trigger(@livelist.eventName, {page: $(event.target).data('page')})
238
-
239
- class window.Search extends Utilities
240
- constructor: (options, livelist) ->
241
- @livelist = livelist
242
- @setOptions(options)
243
- $(@formSelector).submit( (event) => @handleSearchFormSubmit(event) )
244
-
245
- searchTerm: ->
246
- q = $(@searchTextInputSelector).val()
247
- if !q or (q is '') then null else q
248
-
249
- handleSearchFormSubmit: (event) =>
250
- event.preventDefault()
251
- $(@livelist.listSelector).trigger(@livelist.eventName)