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 : / ^ /play . + /
7
+ @stopRegex : / ^ /stop/
8
+ @resumeRegex : / ^ /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"
0 commit comments