app/assets/javascripts/livelist.coffee in livelist-rails-0.0.10 vs app/assets/javascripts/livelist.coffee in livelist-rails-0.0.11
- old
+ new
@@ -2,108 +2,99 @@
setOptions: (options, context=@) =>
_.each( options, (value, option) => context[option] = value )
class window.LiveList extends Utilities
constructor: (options) ->
- @globalOptions.listSelector = options.list.renderTo
- @globalOptions.eventName = "livelist:#{options.global.resourceName}"
- @globalOptions.urlPrefix = "/#{options.global.resourceName}"
+ @listSelector = options.list.renderTo
+ @resourceName = options.global.resourceName
+ @resourceNameSingular = options.global.resourceNameSingular
+ @eventName = "livelist:#{@resourceName}"
+ @urlPrefix = "/#{@resourceName}"
+ @search = new Search(options.search, @)
+ @filters = new Filters(options.filters, @)
+ @pagination = new Pagination(options.pagination, @)
+ @list = new List(options.list, @)
- @setOptions(options.global, @globalOptions)
-
- @search = new Search(@globalOptions, options.search)
- @filters = new Filters(@globalOptions, options.filters)
- @pagination = new Pagination(@globalOptions, options.pagination)
- @list = new List(@search, @filters, @pagination, @globalOptions, options.list)
-
- globalOptions:
- data: null
- resourceName: 'items'
- resourceNameSingular: 'item'
-
class window.List extends Utilities
- constructor: (search, filters, pagination, globalOptions, options = {}) ->
- @data = globalOptions.data
+ constructor: (options, livelist) ->
@fetchRequest = null
- @search = search
- @filters = filters
- @pagination = pagination
+ @livelist = livelist
- @setOptions(globalOptions)
- @listTemplate = "{{##{@resourceName}}}{{>#{@resourceNameSingular}}}{{/#{@resourceName}}}"
+ @listTemplate = "{{##{@livelist.resourceName}}}{{>#{@livelist.resourceNameSingular}}}{{/#{@livelist.resourceName}}}"
@listItemTemplate = '<li>{{id}}</li>'
@fetchingIndicationClass = 'updating'
+ @renderTo = "ul##{@livelist.resourceName}"
+
@setOptions(options)
- $(@renderTo).bind(@eventName, (event, params) => @fetch(presets: null, page: params?.page))
+ $(@renderTo).bind(@livelist.eventName, (event, params) => @fetch(presets: null, page: params?.page))
+ @fetch(presets: @livelist.filters.getPresets())
- presets = @filters.presets()
- @fetch(presets: presets)
-
displayFetchingIndication: => $(@renderTo).addClass(@fetchingIndicationClass)
removeFetchingIndication: => $(@renderTo).removeClass(@fetchingIndicationClass)
renderIndex: (data, textStatus, jqXHR) =>
- @data = data
+ @livelist.data = data
@render()
- @pagination.render(@data)
- @filters.render(@data)
+ @livelist.pagination.render(@livelist.data)
+ @livelist.filters.render(@livelist.data)
fetch: (options) ->
@fetchRequest.abort() if @fetchRequest
- searchTerm = @search.searchTerm()
+ searchTerm = @livelist.search.searchTerm()
params = {}
- params.filters = @filters.setPresets(options.presets)
+ params.filters = @livelist.filters.setPresets(options.presets)
if searchTerm
params.q = searchTerm
if options.page
params.page = options.page
@fetchRequest = $.ajax(
- url : @urlPrefix
+ url : @livelist.urlPrefix
+ type : @livelist.httpMethod
dataType : 'json'
data : params
- type : @httpMethod
beforeSend : @displayFetchingIndication
success : @renderIndex
)
render: ->
partials = {}
- partials[@resourceNameSingular] = @listItemTemplate
- listHTML = Mustache.to_html(@listTemplate, @data, partials)
+ partials[@livelist.resourceNameSingular] = @listItemTemplate
+ listHTML = Mustache.to_html(@listTemplate, @livelist.data, partials)
$(@renderTo).html( listHTML )
@removeFetchingIndication()
window.LiveList.version = '0.0.5'
class window.Filters extends Utilities
- constructor: (globalOptions, options = {}) ->
- @setOptions(globalOptions)
+ constructor: (options, livelist) ->
+ @livelist = livelist
@filters = if options.presets then _.keys(options.presets) else []
@initializeCookies()
@setOptions(options)
- $('input.filter_option', @renderTo).live( 'change', => $(@listSelector).trigger(@eventName) )
+
+ $('input.filter_option', @renderTo).live( 'change', => $(@livelist.listSelector).trigger(@livelist.eventName) )
$(@advancedOptionsToggleSelector).click(@handleAdvancedOptionsClick)
initializeCookies: ->
if jQuery.cookie && @useCookies && @cookieName
@cookieName = 'livelist_filter_presets'
- presets: ->
+ getPresets: ->
cookie = jQuery.cookie(@cookieName) if jQuery.cookie && @useCookies
if @useCookies && cookie
JSON.parse(cookie)
else
@presets
setPresets: (presets) ->
filters = {}
if jQuery.isEmptyObject(presets)
filters = @selections()
- @setCookie() if jQuery.cookie
+ @setCookie(filters) if jQuery.cookie
else
filters = presets
filters
setCookie: (params_filters) ->
@@ -149,29 +140,43 @@
_.all( filter.options, (option) ->
not option.selected
)
)
+ sortOptions: (filters) ->
+ _.map( filters, (filter) ->
+ filter.options = _.sortBy( filter.options, (option) -> option.name)
+ filter
+ )
+
+ sort: (filters) ->
+ _.sortBy( filters, (filter) -> filter.name )
+
render: (data) ->
+ #What is this for?
@filters = _.pluck( data.filters, 'filter_slug' )
+
+ @sort(data.filters)
+ @sortOptions(data.filters)
+
filtersHTML = Mustache.to_html(@template, data)
$(@renderTo).html( filtersHTML )
- if @noFiltersSelected(data) && data[@resourceName].length > 0
+ if @noFiltersSelected(data) && data[@livelist.resourceName].length > 0
$('input[type="checkbox"]', @renderTo).attr('checked', 'checked')
handleAdvancedOptionsClick: (event) =>
event.preventDefault()
$(@renderTo).slideToggle()
class window.Pagination extends Utilities
- constructor: (globalOptions, options = {}) ->
+ constructor: (options, livelist) ->
+ @livelist = livelist
@pagination = null
@maxPages = 30
- @setOptions(globalOptions)
- @emptyListMessage = "<p>No #{@resourceName} matched your filter criteria</p>"
+ @emptyListMessage = "<p>No #{@livelist.resourceName} matched your filter criteria</p>"
@setOptions(options)
$("#{@renderTo} a").live( 'click', (event) -> event.preventDefault() )
$("#{@renderTo} li:not(.disabled) a").live('click', @handlePaginationLinkClick)
@@ -199,47 +204,47 @@
</div>
{{/isEmpty}}
'''
pagesJSON: (currentPage, totalPages) ->
- groupSize = @maxPages / 2
- firstPage = if currentPage < groupSize then 1 else currentPage - groupSize
+ groupSize = Math.floor(@maxPages / 2)
+ firstPage = if currentPage <= groupSize then 1 else currentPage - groupSize
previousPage = firstPage + groupSize * 2 - 1
lastPage = if previousPage >= totalPages then totalPages else previousPage
_.map([firstPage..lastPage], (page) ->
page: page
- currentPage: -> currentPage is page
+ currentPage: currentPage is page
)
paginationJSON: (pagination) ->
{
isEmpty : pagination.total_pages == 0
emptyListMessage : @emptyListMessage
currentPage : pagination.current_page
nextPage : pagination.next_page
previousPage : pagination.previous_page
- urlPrefix : @urlPrefix
+ urlPrefix : @livelist.urlPrefix
pages : @pagesJSON(pagination.current_page, pagination.total_pages)
}
render: (data) ->
@pagination = @paginationJSON(data.pagination)
paginationHTML = Mustache.to_html(@template, @pagination)
$(@renderTo).html( paginationHTML )
handlePaginationLinkClick: (event) =>
event.preventDefault()
- $(@listSelector).trigger(@eventName, {page: $(event.target).data('page')})
+ $(@livelist.listSelector).trigger(@livelist.eventName, {page: $(event.target).data('page')})
class window.Search extends Utilities
- constructor: (globalOptions, options = {}) ->
- @setOptions(globalOptions)
+ constructor: (options, livelist) ->
+ @livelist = livelist
@setOptions(options)
$(@formSelector).submit( (event) => @handleSearchFormSubmit(event) )
searchTerm: ->
q = $(@searchTextInputSelector).val()
if !q or (q is '') then null else q
handleSearchFormSubmit: (event) =>
event.preventDefault()
- $(@listSelector).trigger(@eventName)
+ $(@livelist.listSelector).trigger(@livelist.eventName)