jam-cloud/web/app/assets/javascripts/jam_track_preview.js.coffee

161 lines
4.4 KiB
CoffeeScript

$ = jQuery
context = window
context.JK ||= {};
context.JK.JamTrackPreview = {}
context.JK.JamTrackPreview = class JamTrackPreview
constructor: (app, $root, jamTrack, jamTrackTrack, options) ->
@EVENTS = context.JK.EVENTS
@rest = context.JK.Rest()
@logger = context.JK.logger
@options = options || {master_shows_duration: false, color:'gray'}
@app = app
@jamTrack = jamTrack
@jamTrackTrack = jamTrackTrack
@root = $root
@playButton = null
@stopButton = null
@instrumentIcon = null
@instrumentName = null
@part = null
@loading = null
template = $('#template-jam-track-preview')
throw "no jam track preview template" if not template.exists()
@root.html(context._.template(template.html(), @options, {variable:'data'}))
@playButton = @root.find('.play-button')
@stopButton = @root.find('.stop-button')
@instrumentIcon = @root.find('.instrument-icon')
@instrumentName = @root.find('.instrument-name')
@part = @root.find('.part')
@loading = @root.find('.loading')
@playButton.on('click', @play)
@stopButton.on('click', @stop)
@root.attr('data-track-type', @jamTrackTrack.track_type).attr('data-id', @jamTrackTrack.id)
instrumentId = null
instrumentDescription = '?'
if @jamTrackTrack.track_type == 'Track'
if @jamTrackTrack.instrument
instrumentId = @jamTrackTrack.instrument.id
instrumentDescription = @jamTrackTrack.instrument.description
else
instrumentId = 'other'
instrumentDescription= 'Master Mix'
instrument_src = context.JK.getInstrumentIcon24(instrumentId)
@instrumentIcon.attr('data-instrument-id', instrumentId).attr('src', instrument_src)
@instrumentName.text(instrumentDescription)
#context.JK.bindInstrumentHover(@root)
part = ''
if @jamTrackTrack.track_type == 'Track'
part = "#{@jamTrackTrack.part}" if @jamTrackTrack.part? && @jamTrackTrack.part != instrumentDescription
else
if @options.master_shows_duration
duration = 'entire song'
if @jamTrack.duration
duration = "0:00 - #{context.JK.prettyPrintSeconds(@jamTrack.duration)}"
part = duration
else
part = @jamTrack.name + ' by ' + @jamTrack.original_artist
@part.text("(#{part})") if part != ''
if @jamTrackTrack.preview_mp3_url?
urls = [@jamTrackTrack.preview_mp3_url]
if @jamTrackTrack.preview_ogg_url?
urls.push(@jamTrackTrack.preview_ogg_url)
@urls = urls
@no_audio = false
else
@no_audio = true
if @no_audio
@playButton.addClass('disabled')
@stopButton.addClass('disabled')
onDestroyed: () =>
@sound.unload()
removeNowPlaying: () =>
context.JK.JamTrackPreview.nowPlaying.splice(this)
if context.JK.JamTrackPreview.nowPlaying.length > 0
@logger.warn("multiple jamtrack previews playing")
onHowlerEnd: () =>
@logger.debug("on end $(this)", $(this))
@stopButton.addClass('hidden')
@playButton.removeClass('hidden')
@removeNowPlaying()
onHowlerLoad: () =>
@loading.addClass('hidden')
play: (e) =>
if e?
e.stopPropagation()
if @no_audio
context.JK.prodBubble(@playButton, 'There is no preview available for this track.', {}, {duration:2000})
else
unless @sound?
@root.on('remove', @onDestroyed);
@sound = new Howl({
src: @urls,
autoplay: false,
loop: false,
volume: 1.0,
preload: true,
onload: @onHowlerLoad
onend: @onHowlerEnd})
@loading.removeClass('hidden')
@logger.debug("play issued for jam track preview")
@sound.play()
for playingSound in context.JK.JamTrackPreview.nowPlaying
playingSound.issueStop()
context.JK.JamTrackPreview.nowPlaying = []
context.JK.JamTrackPreview.nowPlaying.push(this)
@playButton.addClass('hidden')
@stopButton.removeClass('hidden')
return false
issueStop: () =>
@logger.debug("pause issued for jam track preview")
@sound.pause() # stop does not actually stop in windows client
@stopButton.addClass('hidden')
@playButton.removeClass('hidden')
stop: (e) =>
if e?
e.stopPropagation()
if @no_audio
context.JK.helpBubble(@playButton, 'There is no preview available for this track.', {}, {duration:2000})
else
@issueStop()
@removeNowPlaying()
return false
context.JK.JamTrackPreview.nowPlaying = []