Skip to content

Commit

Permalink
Merge branch 'master' into emoticon_polish
Browse files Browse the repository at this point in the history
Conflicts:
	app/assets/javascripts/backbone/plugins/emoticons.js.coffee
  • Loading branch information
sgrove committed Apr 13, 2012
2 parents bc69ef5 + 55f8ecd commit 49ac70c
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 60 deletions.
20 changes: 20 additions & 0 deletions app/assets/javascripts/backbone/helpers/audio.js.coffee
@@ -0,0 +1,20 @@
class Kandan.Helpers.Audio
@audioHolder: ->
$('.audio_channels')[0]

@audioChannels: ->
$('audio')

@createAudioChannel: (id) ->
channel = $("<audio class='audio_#{id}'></audio>")
channel.appendTo(@audioHolder())

@destroyAudioChannel: (id) ->
channel = $(".audio_#{id}")
channel.remove()

@audioChannel: (id) ->
$(".audio_#{id}")[0]

@currentAudioChannel: ->
@audioChannel(Kandan.Helpers.Channels.getActiveChannelId())
17 changes: 6 additions & 11 deletions app/assets/javascripts/backbone/helpers/utils.js.coffee
Expand Up @@ -3,20 +3,12 @@ class Kandan.Helpers.Utils

@browserTabFocused: true

@notifyInTitleIfRequired: ->
if @browserTabFocused != true
@playAudioNotice()
@notifyInTitleIfRequired: (activityAttributes) ->
if Kandan.Data.Channels.activeChannelId() == activityAttributes.channel_id and activityAttributes.action == "message" and @browserTabFocused != true
Kandan.Plugins.MusicPlayer.playAudioNotice()
@unreadActivities += 1
$(document).attr('title', "(#{@unreadActivities}) Kandan")

@playAudioNotice: ->
url = @localFileUrl('ding.wav')
player = $('.audio_private')[0]
player.setAttribute('src', url)
player.play()

@localFileUrl: (fileName) ->
return "http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"

@months: [
"January"
Expand All @@ -35,3 +27,6 @@ class Kandan.Helpers.Utils

@resetUnreadActivities: () ->
@unreadActivities = 0

@unescape: (string) ->
string.replace(/&#x2F;/g, "/")
1 change: 1 addition & 0 deletions app/assets/javascripts/backbone/kandan.js.coffee.erb
Expand Up @@ -28,6 +28,7 @@ window.Kandan =
registerPlugins: ->
plugins = [
"UserList"
,"MusicPlayer"
,"YouTubeEmbed"
,"ImageEmbed"
,"LinkEmbed"
Expand Down
4 changes: 1 addition & 3 deletions app/assets/javascripts/backbone/plugins/emoticons.js.coffee
Expand Up @@ -3,7 +3,7 @@ class Kandan.Plugins.Emoticons
@options:
regex: /\([a-zA-Z]+\)/g
template: _.template '''
<img class="emoticon-embed" src="/assets/emoticons/<%= emoticon %>" title="<%= title %>" height="30" width="30" />
<img class="emoticon-embed" src="/assets/emoticons/<%= emoticon %>" title="<%= title %>" height="40" width="40" />
'''


Expand Down Expand Up @@ -40,10 +40,8 @@ class Kandan.Plugins.Emoticons
title = match.replace(/\(|\)/g, "")
needle = match.replace('(', '\\(').replace(')', '\\)')
search = new RegExp(needle, 'g')
console.log(search)
replacement = @options.template({ emoticon: emoticon, title: title})
message.content = message.content.replace(search, replacement) if emoticon
console.log(message.content)
return Kandan.Helpers.Activities.buildFromMessageTemplate(message)

# Kandan.Plugins.register "Kandan.Plugins.Emoticons"
181 changes: 138 additions & 43 deletions app/assets/javascripts/backbone/plugins/music_player.js.coffee
@@ -1,84 +1,179 @@
class Kandan.Plugins.MusicPlayer

@plugin_namespace: "Kandan.Plugins.MusicPlayer"
@plugin_id: ""
@widget_title: "Player"
@play_regex: /^\/play .+/
@stop_regex: /^\/stop/
@local_song_data: false
@pluginNamespace: "Kandan.Plugins.MusicPlayer"
@pluginId: ""
@widgetTitle: "Player"
@playRegex: /^&#x2F;play .+/
@stopRegex: /^&#x2F;stop/
@resumeRegex: /^&#x2F;resume/
@localSongData: false


@play_template: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
@song_template: _.template('<li><%= song.split("/").pop() %></li>')
@playTemplate: _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
@stopTemplate: _.template('<strong><a class="audio-play">stopping</a> the music.')
@resumeTemplate: _.template('<strong><a class="audio-play">resuming</a> the music.')
@songTemplate: _.template('<li><%= song.split("/").pop() %></li>')


@set_error: (error_message)->
console.log "music player error", error_message
@setError: (errorMessage)->
console.log "music player error", errorMessage


@create_song_list: (songs)->
@createSongList: (songs)->
$songs = $('<ul class="songs"></ul>')
if songs.length == 0
$songs = "No songs! Maybe add some?"
else
$songs.append(@song_template({song: song})) for song in songs
$songs.append(@songTemplate({song: song})) for song in songs
return $songs


@render: ($widget_el)->
$widget_element_class = $widget_el.attr('class')
@render: ($widgetEl)->
$widgetElementClass = $widgetEl.attr('class')

if @local_song_data
$songs = @create_song_list(@local_song_data)
if @localSongData
$songs = @createSongList(@localSongData)
else
@get_songs({
@getSongs({
success: (songs)=>
$songs = @create_song_list(songs)
$songs = @createSongList(songs)

failure: ()->
@set_error("Could not load songs")
@setError("Could not load songs")
})
$widget_el.html($songs)
$widgetEl.html($songs)


# TODO add support for sounds
@init: (plugin_id)->
@plugin_id = plugin_id
@register_modifier()
@register_widget()
# TODO: Add support for sounds
@init: (pluginId)->
@pluginId = pluginId
Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
@registerPlayModifier()
@registerStopModifier()
@registerResumeModifier()
# Disabled for now
#@registerWidget()


@register_widget: ()->
Kandan.Widgets.register @plugin_namespace
@registerWidget: ()->
Kandan.Widgets.register @pluginNamespace


@register_modifier: ()->
Kandan.Modifiers.register @play_regex, (message, state)=>
if state == Kandan.Helpers.Activities.ACTIVE_STATE
console.log "add song to player and play song"
@store_song url
@registerPlayModifier: ()->
Kandan.Modifiers.register @playRegex, (message, state) =>
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Data.Channels.activeChannelId()? # and state == Kandan.Helpers.Activities.ACTIVE_STATE commented out because state == undefined for some reason
rawInput = Kandan.Helpers.Utils.unescape(url)
soundUrl = null
soundUrl = @localSounds(rawInput)
soundUrl ?= rawInput

@playUrl(message.channel_id, soundUrl)
else
console.log "song is history"
console.log "Not playing stale song"

message.content = @playTemplate({url: url})
return Kandan.Helpers.Activities.buildFromBaseTemplate message

@registerStopModifier: ()->
Kandan.Modifiers.register @stopRegex, (message, state) =>
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
if true and Kandan.Data.Channels.activeChannelId()?
@stopSound(message.channel_id)

message.content = @play_template({url: message.content.split @play_regex})
return Kandan.Helpers.Activities.build_from_base_template message
message.content = @stopTemplate()
return Kandan.Helpers.Activities.buildFromBaseTemplate message

@registerResumeModifier: ()->
Kandan.Modifiers.register @resumeRegex, (message, state) =>
if true and Kandan.Data.Channels.activeChannelId()?
@play(message.channel_id)

message.content = @resumeTemplate()
return Kandan.Helpers.Activities.buildFromBaseTemplate message


# TODO display error about song not being added by creating an activity locally
@store_song: (url)->
@get_songs({
@storeSong: (url)->
@getSongs({
success: (data)=>
data.push url
Kandan.Store.set @plugin_id, {
Kandan.Store.set @pluginId, {
success: (data)->
@local_song_data = data
Kandan.Widgets.render_widget @plugin_namespace
@localSongData = data
Kandan.Widgets.renderWidget @pluginNamespace
}
})


@get_songs: (callbacks)->
Kandan.Store.get @plugin_id, callbacks
@getSongs: (callbacks)->
Kandan.Store.get @pluginId, callbacks

@localFileUrl: (fileName) ->
"http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"

@localSounds: (name) ->
sounds = {
'claps' : @localFileUrl('golfclap.mp3')
'cheers' : @localFileUrl('cheers.mp3')
'ding' : @localFileUrl('ding.wav')
'gong' : @localFileUrl('gong.mp3')
}

sounds[name]

@audioChannels: ->
Kandan.Helpers.Audio.audioChannels()

@audioChannel: (id) ->
Kandan.Helpers.Audio.audioChannel(id)

@mute: (channelId) ->
@setVolume(channelId, 0)

@unmute: (channelId) ->
@setVolume(channelId, 1)

@toggle: (channelId) ->
if @audioChannel(channelId).volume == 0
@unmute(channelId)
else
@mute(channelId)

@setVolume: (channelId, volume) ->
@audioChannel(channelId).volume = volume

@setAudioUrl: (channelId, url) ->
@audioChannel(channelId).setAttribute('src', url)

@playUrl: (channelId, url) ->
@setAudioUrl(channelId, url)
@play(channelId)

@play: (channelId) ->
@audioChannel(channelId).play()

@stopSound: (channelId) ->
@audioChannel(channelId).pause()

@currentChannel: () ->
Kandan.Data.Channels.activeChannelId()

@onChannelChange: () ->
channelId = @currentChannel()
for channel in @audioChannels()
raw = $(channel).attr('class').split("_")[1]
id = parseInt(raw)
continue if isNaN(id)
@mute(id)

if @audioChannel(channelId)?
@unmute(channelId)

@playAudioNotice: ->
url = @localFileUrl('ding.wav')
player = $('.audio_private')[0]
player.setAttribute('src', url)
player.play()

# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
Expand Up @@ -3,10 +3,10 @@ class Kandan.Views.ChannelPane extends Backbone.View

render: (container)->
container = container || $(@el)
console.log("channel view render", container);
$(container).html @paginatedActivitiesView()
$(container).append @chatboxView()
@setIdAndData(container)
Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
@

setIdAndData: (container)->
Expand Down
6 changes: 4 additions & 2 deletions app/views/layouts/application.html.erb
Expand Up @@ -33,7 +33,9 @@

<%= yield %>

<audio class="audio_private"></audio>
<audio class="audio_common"></audio>
<ul class="audio_channels" style="display:none;">
<audio class="audio_private"></audio>
<audio class="audio_common"></audio>
</ul>
</body>
</html>
Binary file added public/sounds/cheers.mp3
Binary file not shown.
Binary file added public/sounds/golfclap.mp3
Binary file not shown.
Binary file added public/sounds/gong.mp3
Binary file not shown.

0 comments on commit 49ac70c

Please sign in to comment.