]> git.wh0rd.org - chrome-ext/music-player-client.git/blob - main.js
only refresh metadata/playlist tabs when the underlying data has changed
[chrome-ext/music-player-client.git] / main.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
2
3 /* Globals to allow easy manipulation via javascript console */
4 var mpc;
5 var tcpclient;
6 var refresh_id = NaN;
7
8 function TcpClientSender(tcpclient) {
9 this.tcpclient = tcpclient;
10 }
11 TcpClientSender.prototype.send = function(data, cb) {
12 this.tcpclient.sendMessage(data, cb);
13 }
14 TcpClientSender.prototype.poll = function() {
15 this.tcpclient.poll();
16 }
17 TcpClientSender.prototype.reconnect = function() {
18 this.tcpclient.disconnect();
19 this.tcpclient.connect();
20 }
21
22 function tramp_mpc_recv(data) {
23 mpc.recv(data);
24 }
25
26 function sync_storage(sync) {
27 return sync ? chrome.storage.sync : chrome.storage.local;
28 }
29
30 window.onload = function() {
31 var local_keys = [
32 'sync',
33 ];
34 var sync_keys = [
35 'host', 'port', 'refresh',
36 ];
37 var options = {
38 'host': '192.168.0.2',
39 'port': 6600,
40 'sync': true,
41 'refresh': 5,
42 };
43
44 chrome.storage.local.get(local_keys, function(settings) {
45 local_keys.forEach(function(key) {
46 if (key in settings)
47 options[key] = settings[key]
48 });
49
50 var storage = sync_storage(options['sync']);
51 storage.get(sync_keys, function(settings) {
52 sync_keys.forEach(function(key) {
53 if (key in settings)
54 options[key] = settings[key];
55 });
56
57 init_ui(local_keys, sync_keys, options);
58 mpc_connect();
59 });
60 });
61 };
62
63 window.onkeypress = function(e) {
64 switch (e.keyCode) {
65 case 49: // 1
66 show_page('controls');
67 break;
68 case 50: // 1
69 show_page('metadata');
70 break;
71 case 51: // 1
72 show_page('playlist');
73 break;
74 case 52: // 1
75 show_page('options');
76 break;
77 }
78 };
79
80 function mpc_refresh() {
81 mpc.status();
82 }
83
84 function mpc_connect(host, port) {
85 if (typeof(host) != 'string') {
86 host = window['opts_host'].value;
87 port = parseInt(window['opts_port'].value);
88 }
89
90 if (mpc != undefined) {
91 console.log('disconnecting');
92 update_ui('disconnect');
93 delete mpc;
94 tcpclient.disconnect();
95 delete tcpclient;
96 }
97
98 update_ui('init');
99 tcpclient = new TcpClient(host, port);
100 tcpclient.connect(function() {
101 var mpc_sender = new TcpClientSender(tcpclient);
102 tcpclient.addResponseListener(tramp_mpc_recv);
103 mpc = new Mpc(mpc_sender, update_ui);
104 console.log('connected to ' + host + ':' + port);
105 console.log('protip: use the "mpc" object to poke mpd directly.\n' +
106 'you can also do mpc.set_debug(3) to see traffic');
107 mpc_refresh();
108 update_refresh_timer();
109 });
110 }
111
112 function tramp_mpc_consume() {
113 var val = zo(!getToggleButton(this));
114 mpc.consume(val);
115 setToggleButton(this, val);
116 }
117 function tramp_mpc_deleteid() { mpc.deleteid(this.title); }
118 function tramp_mpc_next() { mpc.next(); }
119 function tramp_mpc_pause() { mpc.pause(); }
120 function tramp_mpc_play() { mpc.play(); }
121 function tramp_mpc_previous() { mpc.previous(); }
122 function tramp_mpc_random() {
123 var val = zo(!getToggleButton(this));
124 mpc.random(val);
125 setToggleButton(this, val);
126 }
127 function tramp_mpc_repeat() {
128 var val = zo(!getToggleButton(this));
129 mpc.repeat(val);
130 setToggleButton(this, val);
131 }
132 function tramp_mpc_seekcur() { mpc.seekcur(this.value); }
133 function tramp_mpc_setvol() { mpc.setvol(this.value); }
134 function tramp_mpc_single() {
135 var val = zo(!getToggleButton(this));
136 mpc.single(val);
137 setToggleButton(this, val);
138 }
139 function tramp_mpc_stop() { mpc.stop(); }
140
141 function zo(val) {
142 return val ? 1 : 0;
143 }
144 function szo(val) {
145 return val == '0' ? 0 : 1;
146 }
147 function getToggleButton(btn) {
148 return btn.style.borderStyle == 'inset';
149 }
150 function setToggleButton(btn, val) {
151 if (val === undefined)
152 val = !getToggleButton(btn);
153 btn.style.borderStyle = val ? 'inset' : '';
154 }
155
156 function show_page(page) {
157 if (typeof(page) != 'string')
158 page = this.id.split('.')[1];
159
160 switch (page) {
161 case 'playlist':
162 mpc.playlistinfo();
163 // Fallthrough.
164 case 'metadata':
165 mpc.currentsong();
166 break;
167 }
168
169 var eles = document.getElementsByClassName('main');
170 for (var i = 0; i < eles.length; ++i) {
171 var ele = eles[i];
172 var dis = 'none';
173 var cls = '';
174 if (ele.id == 'main.' + page) {
175 dis = '';
176 cls = 'selected';
177 }
178 ele.style.display = dis;
179 document.getElementById('tab.' + ele.id.split('.')[1]).className = cls;
180 }
181 }
182
183 function do_refresh() {
184 mpc_refresh();
185 refresh_id = window.setTimeout(do_refresh, window['opts_refresh'].value * 1000);
186 }
187
188 function update_refresh_timer() {
189 if (!isNaN(refresh_id))
190 window.clearTimeout(refresh_id);
191 var rate = window['opts_refresh'].value * 1000;
192 if (rate > 0)
193 refresh_id = window.setTimeout(do_refresh, rate);
194 }
195
196 function update_local_settings() {
197 var setting = {};
198 setting[this.id] = this.checked;
199 chrome.storage.local.set(setting);
200 }
201
202 function update_sync_settings() {
203 var setting = {};
204 setting[this.id] = this.value;
205 var storage = sync_storage(window['opts_sync'].checked);
206 storage.set(setting);
207
208 switch (this.id) {
209 case 'refresh':
210 update_refresh_timer();
211 break;
212 }
213 }
214
215 function init_ui(local_keys, sync_keys, options) {
216 var ele, i;
217
218 /* Setup footer */
219 i = 1;
220 [
221 'controls', 'metadata', 'playlist', 'options',
222 ].forEach(function(id) {
223 var ele = document.getElementById('tab.' + id);
224 ele.onclick = show_page;
225 ele.title = id + ' [' + i + ']';
226 ++i;
227 });
228
229 /* Setup control tab */
230 ui_mpc_status = document.getElementById('status');
231 [
232 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
233 'seekcur', 'setvol', 'single', 'stop',
234 ].forEach(function(id) {
235 var ele = window['ui_mpc_' + id] = document.getElementById(id);
236 ele.onchange = ele.onclick = window['tramp_mpc_' + id];
237 ele.title = id;
238 if (ele.accessKey)
239 ele.title += ' [' + ele.accessKey + ']'
240 });
241 window['ui_mpc_currtime'] = document.getElementById('currtime');
242
243 /* Setup metadata tab */
244 [
245 'album', 'artist', 'date', 'file', 'title',
246 ].forEach(function(id) {
247 window['ui_mpc_metadata_' + id] = document.getElementById('metadata.' + id);
248 });
249
250 /* Setup playlist tab */
251 window['ui_mpc_playlist'] = document.getElementById('playlist');
252
253 /* Setup options tab */
254 document.getElementById('connect').onclick = mpc_connect;
255 local_keys.forEach(function(id) {
256 var ele = window['opts_' + id] = document.getElementById(id);
257 ele.checked = options[id];
258 ele.onchange = update_local_settings;
259 });
260 sync_keys.forEach(function(id) {
261 var ele = window['opts_' + id] = document.getElementById(id);
262 ele.value = options[id];
263 ele.oninput = update_sync_settings;
264 });
265 }
266
267 function pretty_time(time) {
268 var sec, min, hrs, ret = '';
269 time = parseInt(time);
270 sec = time % 60;
271 min = parseInt((time / 60) % 60);
272 hrs = parseInt((time / 3600) % 3600);
273 if (hrs)
274 ret = hrs + ':' + ("00" + min).substr(-2) + ':';
275 else
276 ret = min + ':';
277 return ret + ("00" + sec).substr(-2);
278 }
279
280 function playlist_del() {
281 mpc.deleteid(this.song_id);
282 this.parentNode.remove();
283 }
284
285 function playlist_play() {
286 mpc.playid(this.song_id);
287 this.parentNode.style.fontWeight = 'bold';
288 }
289
290 function update_ui(state, cmd) {
291 if (typeof(state) == 'string') {
292 ui_mpc_status.innerText = ({
293 'disconnect': 'Disconnecting...',
294 'init': 'Connecting...',
295 })[state];
296 return;
297 }
298
299 if (Array.isArray(state)) {
300 /*
301 switch (cmd[0]) {
302 case 'setvol':
303 case 'seekcur':
304 break;
305 default:
306 mpc_refresh();
307 }
308 */
309 return;
310 }
311
312 /* Update the metadata tab only when things have changed. */
313 if ('Currentsong' in state && ui_mpc_metadata_file.lastUpdate != state.Currentsong.lastUpdate) {
314 var currentsong = state.Currentsong;
315 ui_mpc_metadata_album.innerText = currentsong.Album;
316 ui_mpc_metadata_artist.innerText = currentsong.Artist;
317 ui_mpc_metadata_title.innerText = currentsong.Title;
318 ui_mpc_metadata_date.innerText = currentsong.Date;
319 ui_mpc_metadata_file.innerText = currentsong.file;
320 }
321
322 /* Update the playlist tab only when things have changed. */
323 if ('Playlist' in state && ui_mpc_playlist.lastUpdate != state.Playlist.lastUpdate) {
324 var playlist = state.Playlist;
325
326 ui_mpc_playlist.innerHTML = '';
327 playlist.forEach(function(song) {
328 var cell, row = ui_mpc_playlist.insertRow(-1);
329 if (song.Pos == currentsong.Pos)
330 row.style.fontWeight = 'bold';
331
332 cell = row.insertCell(-1);
333 cell.id = 'playlist_del';
334 cell.innerHTML = '&#164;';
335 cell.song_id = song.Id;
336 cell.title = 'delete';
337 cell.onclick = playlist_del;
338
339 cell = row.insertCell(-1);
340 cell.innerText = song.Pos;
341 cell.style.textAlign = 'right';
342 cell.song_id = song.Id;
343 cell.title = 'play';
344 cell.onclick = playlist_play;
345
346 if ('Artist' in song) {
347 row.insertCell(-1).innerText = song.Artist;
348 row.insertCell(-1).innerText = song.Album;
349 row.insertCell(-1).innerText = song.Title;
350 } else {
351 cell = row.insertCell(-1);
352 cell.innerText = song.file;
353 cell.colSpan = 3;
354 }
355 row.insertCell(-1).innerText = pretty_time(song.Time);
356 });
357
358 ui_mpc_playlist.lastUpdate = playlist.lastUpdate;
359 }
360
361 /* Update the status tab. */
362 var time, percent;
363 if ('time' in state) {
364 // When stopped, there is no time field at all.
365 time = state.time.split(':');
366 percent = Math.floor((0.0 + time[0]) * 100 / (0.0 + time[1]));
367 } else {
368 time = [0, 0];
369 percent = 0;
370 }
371 ui_mpc_seekcur.max = time[1];
372 ui_mpc_seekcur.value = time[0];
373 ui_mpc_seekcur.title = 'seekcur (' + percent + '%)';
374 ui_mpc_currtime.innerText = [pretty_time(time[0]), pretty_time(time[1]), percent + '%'].join(' / ');
375
376 ui_mpc_setvol.title = 'setvol';
377 if ('volume' in state) {
378 ui_mpc_setvol.value = state.volume;
379 ui_mpc_setvol.title += ' (' + state.volume + '%)';
380 }
381
382 [
383 'consume', 'random', 'repeat', 'single',
384 ].forEach(function(id) {
385 setToggleButton(window['ui_mpc_' + id], szo(state[id]));
386 });
387
388 ui_mpc_status.innerText = ({
389 'play': 'Playing',
390 'pause': 'Paused',
391 'stop': 'Stopped',
392 })[state.state];
393 }