Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' into emoticon_polish

Conflicts:
	app/assets/javascripts/backbone/plugins/emoticons.js.coffee
  • Loading branch information...
commit 49ac70c79787891bbe90324e85145f3a5e759c2c 2 parents bc69ef5 + 55f8ecd
Sean Grove authored April 13, 2012
20  app/assets/javascripts/backbone/helpers/audio.js.coffee
... ...
@@ -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())
17  app/assets/javascripts/backbone/helpers/utils.js.coffee
@@ -3,20 +3,12 @@ class Kandan.Helpers.Utils
3 3
 
4 4
   @browserTabFocused: true
5 5
 
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()
9 9
       @unreadActivities += 1
10 10
       $(document).attr('title', "(#{@unreadActivities}) Kandan")
11 11
 
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 }"
20 12
 
21 13
   @months: [
22 14
     "January"
@@ -35,3 +27,6 @@ class Kandan.Helpers.Utils
35 27
 
36 28
   @resetUnreadActivities: () ->
37 29
     @unreadActivities = 0
  30
+
  31
+  @unescape: (string) ->
  32
+    string.replace(/&#x2F;/g, "/")
1  app/assets/javascripts/backbone/kandan.js.coffee.erb
@@ -28,6 +28,7 @@ window.Kandan =
28 28
   registerPlugins: ->
29 29
     plugins = [
30 30
       "UserList"
  31
+      ,"MusicPlayer"
31 32
       ,"YouTubeEmbed"
32 33
       ,"ImageEmbed"
33 34
       ,"LinkEmbed"
4  app/assets/javascripts/backbone/plugins/emoticons.js.coffee
@@ -3,7 +3,7 @@ class Kandan.Plugins.Emoticons
3 3
   @options:
4 4
     regex: /\([a-zA-Z]+\)/g
5 5
     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" />
7 7
     '''
8 8
 
9 9
 
@@ -40,10 +40,8 @@ class Kandan.Plugins.Emoticons
40 40
         title       = match.replace(/\(|\)/g, "")
41 41
         needle      = match.replace('(', '\\(').replace(')', '\\)')
42 42
         search      = new RegExp(needle, 'g')
43  
-        console.log(search)
44 43
         replacement = @options.template({ emoticon: emoticon, title: title})
45 44
         message.content = message.content.replace(search, replacement) if emoticon
46  
-        console.log(message.content)
47 45
       return Kandan.Helpers.Activities.buildFromMessageTemplate(message)
48 46
 
49 47
 # Kandan.Plugins.register "Kandan.Plugins.Emoticons"
181  app/assets/javascripts/backbone/plugins/music_player.js.coffee
... ...
@@ -1,84 +1,179 @@
1 1
 class Kandan.Plugins.MusicPlayer
2 2
 
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
9 10
 
10 11
 
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>')
13 16
 
14 17
 
15  
-  @set_error: (error_message)->
16  
-    console.log "music player error", error_message
  18
+  @setError: (errorMessage)->
  19
+    console.log "music player error", errorMessage
17 20
 
18 21
 
19  
-  @create_song_list: (songs)->
  22
+  @createSongList: (songs)->
20 23
     $songs = $('<ul class="songs"></ul>')
21 24
     if songs.length == 0
22 25
       $songs = "No songs! Maybe add some?"
23 26
     else
24  
-      $songs.append(@song_template({song: song})) for song in songs
  27
+      $songs.append(@songTemplate({song: song})) for song in songs
25 28
     return $songs
26 29
 
27 30
 
28  
-  @render: ($widget_el)->
29  
-    $widget_element_class = $widget_el.attr('class')
  31
+  @render: ($widgetEl)->
  32
+    $widgetElementClass = $widgetEl.attr('class')
30 33
 
31  
-    if @local_song_data
32  
-      $songs = @create_song_list(@local_song_data)
  34
+    if @localSongData
  35
+      $songs = @createSongList(@localSongData)
33 36
     else
34  
-      @get_songs({
  37
+      @getSongs({
35 38
         success: (songs)=>
36  
-          $songs = @create_song_list(songs)
  39
+          $songs = @createSongList(songs)
37 40
 
38 41
         failure: ()->
39  
-          @set_error("Could not load songs")
  42
+          @setError("Could not load songs")
40 43
       })
41  
-    $widget_el.html($songs)
  44
+    $widgetEl.html($songs)
42 45
 
43 46
 
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()
49 56
 
50 57
 
51  
-  @register_widget: ()->
52  
-    Kandan.Widgets.register @plugin_namespace
  58
+  @registerWidget: ()->
  59
+    Kandan.Widgets.register @pluginNamespace
53 60
 
54 61
 
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)
60 72
       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)
62 83
 
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
65 94
 
66 95
 
67 96
   # 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({
70 99
       success: (data)=>
71 100
         data.push url
72  
-        Kandan.Store.set @plugin_id, {
  101
+        Kandan.Store.set @pluginId, {
73 102
           success: (data)->
74  
-            @local_song_data = data
75  
-            Kandan.Widgets.render_widget @plugin_namespace
  103
+            @localSongData = data
  104
+            Kandan.Widgets.renderWidget @pluginNamespace
76 105
         }
77 106
     })
78 107
 
79 108
 
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)
82 172
 
  173
+  @playAudioNotice: ->
  174
+    url    = @localFileUrl('ding.wav')
  175
+    player = $('.audio_private')[0]
  176
+    player.setAttribute('src', url)
  177
+    player.play()
83 178
 
84  
-# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
  179
+# Kandan.Plugins.register "Kandan.Plugins.MusicPlayer"
2  app/assets/javascripts/backbone/views/channel_pane.js.coffee
@@ -3,10 +3,10 @@ class Kandan.Views.ChannelPane extends Backbone.View
3 3
 
4 4
   render: (container)->
5 5
     container = container || $(@el)
6  
-    console.log("channel view render", container);
7 6
     $(container).html @paginatedActivitiesView()
8 7
     $(container).append @chatboxView()
9 8
     @setIdAndData(container)
  9
+    Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
10 10
     @
11 11
 
12 12
   setIdAndData: (container)->
6  app/views/layouts/application.html.erb
@@ -33,7 +33,9 @@
33 33
 
34 34
   <%= yield %>
35 35
 
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>
38 40
 </body>
39 41
 </html>
BIN  public/sounds/cheers.mp3
Binary file not shown
BIN  public/sounds/golfclap.mp3
Binary file not shown
BIN  public/sounds/gong.mp3
Binary file not shown

0 notes on commit 49ac70c

Please sign in to comment.
Something went wrong with that request. Please try again.