jam-cloud/web/app/assets/javascripts/jamtrack.coffee

242 lines
7.2 KiB
CoffeeScript

$ = jQuery
context = window
context.JK ||= {}
context.JK.JamTrackScreen=class JamTrackScreen
LIMIT = 10
instrument_logo_map = context.JK.getInstrumentIconMap24()
constructor: (@app) ->
@logger = context.JK.logger
@screen = null
@content = null
@scroller = null
@genre = null
@artist = null
@instrument = null
@availability = null
@nextPager = null
@noMoreJamtracks = null
@currentPage = 0
@next = null
@currentQuery = this.defaultQuery()
@expanded = false
beforeShow:(data) =>
this.setFilterFromURL()
this.refresh()
afterShow:(data) =>
events:() =>
@genre.on 'change', this.search
@artist.on 'change', this.search
@instrument.on 'change', this.search
@availability.on 'change', this.search
clearResults:() =>
#$logger.debug("CLEARING CONTENT")
@currentPage = 0
@content.empty()
@noMoreJamtracks.hide()
@next = null
setFilterFromURL:() =>
# Grab parms from URL for artist, instrument, and availability
parms=this.getParams()
this.logger.debug("parms", parms)
if(parms.artist?)
@artist.val(parms.artist)
if(parms.instrument?)
@instrument.val(parms.instrument)
if(parms.availability?)
@availability.val(parms.availability)
getParams:() =>
params = {}
q = window.location.href.split("?")[1]
if q?
q = q.split('#')[0]
raw_vars = q.split("&")
for v in raw_vars
[key, val] = v.split("=")
params[key] = decodeURIComponent(val)
params
refresh:() =>
@currentQuery = this.buildQuery()
that = this
rest.getJamtracks(@currentQuery).done((response) ->
that.clearResults()
that.handleJamtrackResponse(response)
).fail (jqXHR) ->
that.clearResults()
that.noMoreJamtracks.show()
that.app.notifyServerError jqXHR, 'Jamtrack Unavailable'
search:() =>
this.refresh()
false
defaultQuery:() =>
query =
limit: LIMIT
page: @currentPage
if @next
query.since = @next
query
buildQuery:() =>
@currentQuery = this.defaultQuery()
# genre filter
# var genres = @screen.find('#jamtrack_genre').val()
# if (genres !== undefined) {
# @currentQuery.genre = genres
# }
# instrument filter
instrument = @instrument.val()
if instrument?
@currentQuery.instrument = instrument
# artist filter
art = @artist.val()
if art?
@currentQuery.artist = art
# availability filter
availability = @availability.val()
if availability?
@currentQuery.availability = availability
@currentQuery
handleJamtrackResponse:(response) =>
#logger.debug("Handling response", JSON.stringify(response))
@next = response.next
this.renderJamtracks(response)
if response.next == null
# if we less results than asked for, end searching
@scroller.infinitescroll 'pause'
if @currentPage == 0 and response.jamtracks.length == 0
@content.append '<div class=\'no-jamtracks-msg\'>There\'s no jamtracks.</div>'
if @currentPage > 0
@noMoreJamtracks.show()
# there are bugs with infinitescroll not removing the 'loading'.
# it's most noticeable at the end of the list, so whack all such entries
$('.infinite-scroll-loader').remove()
else
@currentPage++
this.buildQuery()
this.registerInfiniteScroll()
registerInfiniteScroll:() =>
@scroller.infinitescroll {
behavior: 'local'
navSelector: '#jamtrackScreen .btn-next-pager'
nextSelector: '#jamtrackScreen .btn-next-pager'
binder: @scroller
dataType: 'json'
appendCallback: false
prefill: false
bufferPx: 100
loading:
msg: $('<div class="infinite-scroll-loader">Loading ...</div>')
img: '/assets/shared/spinner.gif'
path: (page) ->
'/api/jamtracks?' + $.param(this.buildQuery())
}, (json, opts) ->
this.handleJamtrackResponse(json)
@scroller.infinitescroll 'resume'
playJamtrack:(e) =>
e.preventDefault()
addToCartJamtrack:(e) =>
e.preventDefault()
params = id: $(e.target).attr('data-jamtrack-id')
rest.addJamtrackToShoppingCart(params).done((response) ->
context.location = '/client#/shoppingCart'
).fail @app.ajaxError
licenseUSWhy:(e) =>
e.preventDefault()
@app.layout.showDialog 'jamtrack-availability-dialog'
registerEvents:() =>
@screen.find('.jamtrack-detail-btn').on 'click', this.showJamtrackDescription
@screen.find('.play-button').on 'click', this.playJamtrack
@screen.find('.jamtrack-add-cart').on 'click', this.addToCartJamtrack
@screen.find('.license-us-why').on 'click', this.licenseUSWhy
@screen.find('.jamtrack-detail-btn').on 'click', this.toggleExpanded
renderJamtracks:(data) =>
that = this
for jamtrack in data.jamtracks
for track in jamtrack.tracks
inst = '../assets/content/icon_instrument_default24.png'
if track.instrument.id in instrument_logo_map
inst = instrument_logo_map[track.instrument.id].asset
track.instrument_url = inst
track.instrument_desc = track.instrument.description
if track.part != ''
track.instrument_desc += ' (' + track.part + ')'
options =
jamtrack: jamtrack
expanded: that.expanded
@jamtrackItem = $(context._.template($('#template-jamtrack').html(), options, variable: 'data'))
that.renderJamtrack(@jamtrackItem)
this.registerEvents()
showJamtrackDescription:(e) =>
e.preventDefault()
@description = $(e.target).parent('.detail-arrow').next()
if @description.css('display') == 'none'
@description.show()
else
@description.hide()
toggleExpanded:() =>
this.expanded = !this.expanded
this.refresh()
renderJamtrack:(jamtrack) =>
@content.append jamtrack
initialize:() =>
screenBindings =
'beforeShow': this.beforeShow
'afterShow': this.afterShow
@app.bindScreen 'jamtrack', screenBindings
@screen = $('#jamtrack-find-form')
@scroller = @screen.find('.content-body-scroller')
@content = @screen.find('.jamtrack-content')
@genre = @screen.find('#jamtrack_genre')
@artist = @screen.find('#jamtrack_artist')
@instrument = @screen.find('#jamtrack_instrument')
@availability = @screen.find('#jamtrack_availability')
@nextPager = @screen.find('a.btn-next-pager')
@noMoreJamtracks = @screen.find('.end-of-jamtrack-list')
if @screen.length == 0
throw new Error('@screen must be specified')
if @scroller.length == 0
throw new Error('@scroller must be specified')
if @content.length == 0
throw new Error('@content must be specified')
if @noMoreJamtracks.length == 0
throw new Error('@noMoreJamtracks must be specified')
#if(@genre.length == 0) throw new Error("@genre must be specified")
if @artist.length == 0
throw new Error('@artist must be specified')
if @instrument.length == 0
throw new Error('@instrument must be specified')
if @availability.length == 0
throw new Error('@availability must be specified')
this.events()