This repository has been archived by the owner on May 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio-box-client.coffee
170 lines (149 loc) · 6.26 KB
/
radio-box-client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Mojolicious radio box .......................................................
window.App =
info:
status: "-"
position: 0
duration: 0
radio_stations: []
# ...........................................
init: ->
$("#bt_pause").on('click', App.do_pause)
$("#bt_next").on('click', App.do_next)
$("#bt_prev").on('click', App.do_prev)
$("#bt_get_radio").on('click', App.do_get_radio)
$("#bt_get_music").on('click', App.do_get_music)
$("#radio_stations").on('change', App.do_select_radio)
$("#volume_slider").on('change', {absolute: true}, App.do_change_volume)
$("#volume_slider").on('blur', {absolute: true}, App.do_change_volume)
$("#volume_down").on('click', {down: 10}, App.do_change_volume)
$("#volume_up").on('click', {up: 10}, App.do_change_volume)
$(document).ajaxError () ->
$("#div_error").show()
.fadeOut 1500, () ->
$("button.nav_buttons").removeAttr('disabled')
App.update_info()
# update info every 15 seconds (not on dashboard)
window.setInterval App.update_info, 15 * 1000 if ! navigator.userAgent.match(/WebClip/)
# ...........................................
update_info: ->
$.get '/get_info', (info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
render_info: ->
$("button.nav_buttons").removeAttr('disabled')
if App.info.status == 'playing'
$("#bt_pause").html('<i class="icon-pause"> pause')
if App.info.duration == "-1"
$("#bt_prev").attr('disabled', 'disabled')
$("#bt_next").attr('disabled', 'disabled')
else if App.info.status == 'paused' || App.info.status == 'stopped'
$("#bt_pause").html('<i class="icon-play"> play')
if App.info.tag
if App.info.radio_title
$("#div_info").html """
#{App.info.tag.title}<br>
<b>#{App.info.radio_title}</b>
"""
else if App.info.tag.artist && App.info.tag.album
$("#div_info").html """
#{App.info.tag.artist}<br>
<i>#{App.info.tag.album}</i><br>
<b>#{App.info.tag.title}</b>
"""
$("#radio_stations").hide()[0].selectedIndex = 0
else
$("#div_info").html """
<b>#{App.info.tag.title}</b>
"""
if App.info.radio_title && App.info.file.match(/http:\/\//)
$("#radio_stations").show()
if App.radio_stations.length
App.render_select_radio()
else
App.do_get_radio()
if App.info.volume != undefined
$('input#volume_slider').val(App.info.volume)
# ...........................................
render_select_radio: ->
select_input = $('#radio_stations')[0]
select_input.options.length = 0
select_input.options.add(new Option(' - please select station -', ''))
for item in App.radio_stations
new_option = new Option(item.title, item.url)
if App.info.radio_title && App.info.file.match(/http:\/\//) && App.info.file == item.url
new_option.selected = true
select_input.options.add(new_option)
# ...........................................
do_pause: ->
$("#bt_pause").attr('disabled', 'disabled')
if App.info.duration > 0 && ! App.info.radio_title
$.get '/pause', (info_data) ->
App.info = info_data.info
App.render_info()
else if App.info.status == 'playing'
$.get '/stop', (info_data) ->
App.info = info_data.info
App.render_info()
else if App.info.status == 'stopped'
$.get '/play', (info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
do_next: ->
$("#bt_next").attr('disabled', 'disabled')
$.get '/next', (info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
do_prev: ->
$("#bt_prev").attr('disabled', 'disabled')
$.get '/prev', (info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
do_get_radio: ->
$.get '/get_radio', (result) ->
$("#radio_stations").show()
App.radio_stations = result.radio_stations;
App.render_select_radio()
# ...........................................
do_get_music: ->
$.get '/get_music', (info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
do_select_radio: (event) ->
if event.target.value
$.get '/play_radio'
url: event.target.value
(info_data) ->
App.info = info_data.info
App.render_info()
# ...........................................
do_change_volume: (event) ->
if App._change_valume_tid
window.clearTimeout App._change_valume_tid
App._change_valume_tid = undefined
new_volume = 0
if event.data.up
new_volume = App.info.volume + event.data.up
new_volume = 100 if new_volume > 100
else if event.data.down
new_volume = App.info.volume - event.data.down
new_volume = 0 if new_volume < 0
else if event.data.absolute
new_volume = parseInt($("#volume_slider").val())
else
return
App._change_valume_tid = window.setTimeout(
() ->
if new_volume != undefined && new_volume != App.info.volume
App.info.volume = new_volume
$("#volume_slider").val(new_volume)
$.get '/set_volume'
volume: new_volume
200
)
# .............................................................................
$ () -> App.init()