Skip to content

Commit 49ac70c

Browse files
committed
Merge branch 'master' into emoticon_polish
Conflicts: app/assets/javascripts/backbone/plugins/emoticons.js.coffee
2 parents bc69ef5 + 55f8ecd commit 49ac70c

File tree

10 files changed

+171
-60
lines changed

10 files changed

+171
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Kandan.Helpers.Audio
2+
@audioHolder: ->
3+
$('.audio_channels')[0]
4+
5+
@audioChannels: ->
6+
$('audio')
7+
8+
@createAudioChannel: (id) ->
9+
channel = $("<audio class='audio_#{id}'></audio>")
10+
channel.appendTo(@audioHolder())
11+
12+
@destroyAudioChannel: (id) ->
13+
channel = $(".audio_#{id}")
14+
channel.remove()
15+
16+
@audioChannel: (id) ->
17+
$(".audio_#{id}")[0]
18+
19+
@currentAudioChannel: ->
20+
@audioChannel(Kandan.Helpers.Channels.getActiveChannelId())

app/assets/javascripts/backbone/helpers/utils.js.coffee

+6-11
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,12 @@ class Kandan.Helpers.Utils
33

44
@browserTabFocused: true
55

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

12-
@playAudioNotice: ->
13-
url = @localFileUrl('ding.wav')
14-
player = $('.audio_private')[0]
15-
player.setAttribute('src', url)
16-
player.play()
17-
18-
@localFileUrl: (fileName) ->
19-
return "http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"
2012

2113
@months: [
2214
"January"
@@ -35,3 +27,6 @@ class Kandan.Helpers.Utils
3527

3628
@resetUnreadActivities: () ->
3729
@unreadActivities = 0
30+
31+
@unescape: (string) ->
32+
string.replace(/&#x2F;/g, "/")

app/assets/javascripts/backbone/kandan.js.coffee.erb

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ window.Kandan =
2828
registerPlugins: ->
2929
plugins = [
3030
"UserList"
31+
,"MusicPlayer"
3132
,"YouTubeEmbed"
3233
,"ImageEmbed"
3334
,"LinkEmbed"

app/assets/javascripts/backbone/plugins/emoticons.js.coffee

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Kandan.Plugins.Emoticons
33
@options:
44
regex: /\([a-zA-Z]+\)/g
55
template: _.template '''
6-
<img class="emoticon-embed" src="/assets/emoticons/<%= emoticon %>" title="<%= title %>" height="30" width="30" />
6+
<img class="emoticon-embed" src="/assets/emoticons/<%= emoticon %>" title="<%= title %>" height="40" width="40" />
77
'''
88

99

@@ -40,10 +40,8 @@ class Kandan.Plugins.Emoticons
4040
title = match.replace(/\(|\)/g, "")
4141
needle = match.replace('(', '\\(').replace(')', '\\)')
4242
search = new RegExp(needle, 'g')
43-
console.log(search)
4443
replacement = @options.template({ emoticon: emoticon, title: title})
4544
message.content = message.content.replace(search, replacement) if emoticon
46-
console.log(message.content)
4745
return Kandan.Helpers.Activities.buildFromMessageTemplate(message)
4846

4947
# Kandan.Plugins.register "Kandan.Plugins.Emoticons"
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,179 @@
11
class Kandan.Plugins.MusicPlayer
22

3-
@plugin_namespace: "Kandan.Plugins.MusicPlayer"
4-
@plugin_id: ""
5-
@widget_title: "Player"
6-
@play_regex: /^\/play .+/
7-
@stop_regex: /^\/stop/
8-
@local_song_data: false
3+
@pluginNamespace: "Kandan.Plugins.MusicPlayer"
4+
@pluginId: ""
5+
@widgetTitle: "Player"
6+
@playRegex: /^&#x2F;play .+/
7+
@stopRegex: /^&#x2F;stop/
8+
@resumeRegex: /^&#x2F;resume/
9+
@localSongData: false
910

1011

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

1417

15-
@set_error: (error_message)->
16-
console.log "music player error", error_message
18+
@setError: (errorMessage)->
19+
console.log "music player error", errorMessage
1720

1821

19-
@create_song_list: (songs)->
22+
@createSongList: (songs)->
2023
$songs = $('<ul class="songs"></ul>')
2124
if songs.length == 0
2225
$songs = "No songs! Maybe add some?"
2326
else
24-
$songs.append(@song_template({song: song})) for song in songs
27+
$songs.append(@songTemplate({song: song})) for song in songs
2528
return $songs
2629

2730

28-
@render: ($widget_el)->
29-
$widget_element_class = $widget_el.attr('class')
31+
@render: ($widgetEl)->
32+
$widgetElementClass = $widgetEl.attr('class')
3033

31-
if @local_song_data
32-
$songs = @create_song_list(@local_song_data)
34+
if @localSongData
35+
$songs = @createSongList(@localSongData)
3336
else
34-
@get_songs({
37+
@getSongs({
3538
success: (songs)=>
36-
$songs = @create_song_list(songs)
39+
$songs = @createSongList(songs)
3740

3841
failure: ()->
39-
@set_error("Could not load songs")
42+
@setError("Could not load songs")
4043
})
41-
$widget_el.html($songs)
44+
$widgetEl.html($songs)
4245

4346

44-
# TODO add support for sounds
45-
@init: (plugin_id)->
46-
@plugin_id = plugin_id
47-
@register_modifier()
48-
@register_widget()
47+
# TODO: Add support for sounds
48+
@init: (pluginId)->
49+
@pluginId = pluginId
50+
Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
51+
@registerPlayModifier()
52+
@registerStopModifier()
53+
@registerResumeModifier()
54+
# Disabled for now
55+
#@registerWidget()
4956

5057

51-
@register_widget: ()->
52-
Kandan.Widgets.register @plugin_namespace
58+
@registerWidget: ()->
59+
Kandan.Widgets.register @pluginNamespace
5360

5461

55-
@register_modifier: ()->
56-
Kandan.Modifiers.register @play_regex, (message, state)=>
57-
if state == Kandan.Helpers.Activities.ACTIVE_STATE
58-
console.log "add song to player and play song"
59-
@store_song url
62+
@registerPlayModifier: ()->
63+
Kandan.Modifiers.register @playRegex, (message, state) =>
64+
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
65+
if true and Kandan.Data.Channels.activeChannelId()? # and state == Kandan.Helpers.Activities.ACTIVE_STATE commented out because state == undefined for some reason
66+
rawInput = Kandan.Helpers.Utils.unescape(url)
67+
soundUrl = null
68+
soundUrl = @localSounds(rawInput)
69+
soundUrl ?= rawInput
70+
71+
@playUrl(message.channel_id, soundUrl)
6072
else
61-
console.log "song is history"
73+
console.log "Not playing stale song"
74+
75+
message.content = @playTemplate({url: url})
76+
return Kandan.Helpers.Activities.buildFromBaseTemplate message
77+
78+
@registerStopModifier: ()->
79+
Kandan.Modifiers.register @stopRegex, (message, state) =>
80+
url = $.trim(message.content.substr(message.content.indexOf(" ") + 1));
81+
if true and Kandan.Data.Channels.activeChannelId()?
82+
@stopSound(message.channel_id)
6283

63-
message.content = @play_template({url: message.content.split @play_regex})
64-
return Kandan.Helpers.Activities.build_from_base_template message
84+
message.content = @stopTemplate()
85+
return Kandan.Helpers.Activities.buildFromBaseTemplate message
86+
87+
@registerResumeModifier: ()->
88+
Kandan.Modifiers.register @resumeRegex, (message, state) =>
89+
if true and Kandan.Data.Channels.activeChannelId()?
90+
@play(message.channel_id)
91+
92+
message.content = @resumeTemplate()
93+
return Kandan.Helpers.Activities.buildFromBaseTemplate message
6594

6695

6796
# TODO display error about song not being added by creating an activity locally
68-
@store_song: (url)->
69-
@get_songs({
97+
@storeSong: (url)->
98+
@getSongs({
7099
success: (data)=>
71100
data.push url
72-
Kandan.Store.set @plugin_id, {
101+
Kandan.Store.set @pluginId, {
73102
success: (data)->
74-
@local_song_data = data
75-
Kandan.Widgets.render_widget @plugin_namespace
103+
@localSongData = data
104+
Kandan.Widgets.renderWidget @pluginNamespace
76105
}
77106
})
78107

79108

80-
@get_songs: (callbacks)->
81-
Kandan.Store.get @plugin_id, callbacks
109+
@getSongs: (callbacks)->
110+
Kandan.Store.get @pluginId, callbacks
111+
112+
@localFileUrl: (fileName) ->
113+
"http://#{ window.location.hostname }:#{ window.location.port }/sounds/#{ fileName }"
114+
115+
@localSounds: (name) ->
116+
sounds = {
117+
'claps' : @localFileUrl('golfclap.mp3')
118+
'cheers' : @localFileUrl('cheers.mp3')
119+
'ding' : @localFileUrl('ding.wav')
120+
'gong' : @localFileUrl('gong.mp3')
121+
}
122+
123+
sounds[name]
124+
125+
@audioChannels: ->
126+
Kandan.Helpers.Audio.audioChannels()
127+
128+
@audioChannel: (id) ->
129+
Kandan.Helpers.Audio.audioChannel(id)
130+
131+
@mute: (channelId) ->
132+
@setVolume(channelId, 0)
133+
134+
@unmute: (channelId) ->
135+
@setVolume(channelId, 1)
136+
137+
@toggle: (channelId) ->
138+
if @audioChannel(channelId).volume == 0
139+
@unmute(channelId)
140+
else
141+
@mute(channelId)
142+
143+
@setVolume: (channelId, volume) ->
144+
@audioChannel(channelId).volume = volume
145+
146+
@setAudioUrl: (channelId, url) ->
147+
@audioChannel(channelId).setAttribute('src', url)
148+
149+
@playUrl: (channelId, url) ->
150+
@setAudioUrl(channelId, url)
151+
@play(channelId)
152+
153+
@play: (channelId) ->
154+
@audioChannel(channelId).play()
155+
156+
@stopSound: (channelId) ->
157+
@audioChannel(channelId).pause()
158+
159+
@currentChannel: () ->
160+
Kandan.Data.Channels.activeChannelId()
161+
162+
@onChannelChange: () ->
163+
channelId = @currentChannel()
164+
for channel in @audioChannels()
165+
raw = $(channel).attr('class').split("_")[1]
166+
id = parseInt(raw)
167+
continue if isNaN(id)
168+
@mute(id)
169+
170+
if @audioChannel(channelId)?
171+
@unmute(channelId)
82172

173+
@playAudioNotice: ->
174+
url = @localFileUrl('ding.wav')
175+
player = $('.audio_private')[0]
176+
player.setAttribute('src', url)
177+
player.play()
83178

84-
# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
179+
# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"

app/assets/javascripts/backbone/views/channel_pane.js.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ class Kandan.Views.ChannelPane extends Backbone.View
33

44
render: (container)->
55
container = container || $(@el)
6-
console.log("channel view render", container);
76
$(container).html @paginatedActivitiesView()
87
$(container).append @chatboxView()
98
@setIdAndData(container)
9+
Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
1010
@
1111

1212
setIdAndData: (container)->

app/views/layouts/application.html.erb

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
<%= yield %>
3535

36-
<audio class="audio_private"></audio>
37-
<audio class="audio_common"></audio>
36+
<ul class="audio_channels" style="display:none;">
37+
<audio class="audio_private"></audio>
38+
<audio class="audio_common"></audio>
39+
</ul>
3840
</body>
3941
</html>

public/sounds/cheers.mp3

134 KB
Binary file not shown.

public/sounds/golfclap.mp3

118 KB
Binary file not shown.

public/sounds/gong.mp3

53 KB
Binary file not shown.

0 commit comments

Comments
 (0)