Skip to content

Instantly share code, notes, and snippets.

@ttilley
Created March 30, 2012 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttilley/2246910 to your computer and use it in GitHub Desktop.
Save ttilley/2246910 to your computer and use it in GitHub Desktop.
top = this
top.logJPlayerCreate ?= true
top.logJPlayerPublishCreate ?= true
top.logAllJPlayerEvents ?= false
do ($ = jQuery) ->
if !$.subscribe
o = $({})
$.subscribe = ->
o.on.apply o, arguments
$.unsubscribe = ->
o.off.apply o, arguments
$.publish = ->
o.trigger.apply o, arguments
_extends = (child, parent) ->
for own key, value of parent
child[key] = value
ctor = ->
@constructor = child
@
ctor.prototype = parent.prototype
child.prototype = new ctor
child.__super__ = parent.prototype
child
initEventPublishing = (jpe) ->
for own evKey, evName of jQuery.jPlayer.event
if top.logJPlayerPublishCreate
console.log("creating publish handler on #{jpe.attr('id')} for /jplayer/#{evKey}")
data =
key: evKey
name: evName
jq: jpe
handler = (event) ->
if top.logAllJPlayerEvents
console.groupCollapsed("#{event.data.jq.attr('id')} #{event.data.key}")
console.log("jPlayer:")
console.dir(event.jPlayer)
console.log("data:")
console.dir(event.data)
console.groupEnd()
$.publish "/jplayer/#{event.data.key}", [event.jPlayer, event.data, event]
jpe.on evName, data, handler
originalJPlayer = top.jQuery.jPlayer
JPlayerDuckPunch = (options, element) ->
options ||= {}
if top.logJPlayerCreate
console.groupCollapsed(element.id)
console.log("JPlayerDuckPunch")
console.log("options:")
console.dir(options)
console.log("element:")
console.dir(element)
dpopt = jQuery.extend({}, options)
dpopt['solution'] = 'html, flash'
originalReady = options['ready'] || ->
dpopt['ready'] = ->
jpe = $(this);
console.log("jPlayer ready handler for #{jpe.attr('id')}") if top.logJPlayerCreate
initEventPublishing(jpe)
console.log("calling original ready handler...") if top.logJPlayerCreate
originalReady.apply(this, arguments)
if top.logJPlayerCreate
console.log("modified options:")
console.dir(dpopt)
console.groupEnd()
originalJPlayer.apply(this, [dpopt, element])
JPlayerDuckPunch::options.emulateHtml = true
JPlayerDuckPunch::options.solution = 'html, flash'
JPlayerDuckPunch::options.supplied = "m4a, mp3"
JPlayerDuckPunch::options.preload = "auto"
JPlayerDuckPunch::options.errorAlerts = true
JPlayerDuckPunch::options.warningAlerts = true
JPlayerDuckPunch::compatibleEvents = [
'error', 'warning', 'loadstart', 'progress', 'timeupdate',
'play', 'pause', 'ended', 'seeking', 'seeked', 'volumechange'
]
JPlayerDuckPunch::coreEvents = [
'playing', 'timeupdate', 'loadedmetadata', 'canplaythrough',
'progress', 'seeked', 'ended', 'error'
]
top.jQuery.jPlayer = _extends(JPlayerDuckPunch, originalJPlayer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment