summaryrefslogtreecommitdiff
path: root/static/player.js
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-08-24 18:49:29 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-08-24 18:49:29 +0200
commit337dab08b623aa0f7cb9ecb112f40a3b1eaab8bb (patch)
treebe30194ccae6e9fa1155bb996b7eea24ffd0f313 /static/player.js
parent8ebb79ffe8f8d6172c7ff664f086ae96863fb4bf (diff)
Moved browser code to the Browser class.old
Diffstat (limited to 'static/player.js')
-rw-r--r--static/player.js205
1 files changed, 105 insertions, 100 deletions
diff --git a/static/player.js b/static/player.js
index eedb943..2c56f5b 100644
--- a/static/player.js
+++ b/static/player.js
@@ -71,7 +71,7 @@ function MusicListing(type, path, name, track, metadata, cached) {
a.onclick = function() {
if(type == 'dir') {
- list(path);
+ browser.list(path);
} else if(type == 'file') {
playlist.add(ml);
}
@@ -81,131 +81,132 @@ function MusicListing(type, path, name, track, metadata, cached) {
}
}
-function set_current(path) {
- document.getElementById('current-dir').innerHTML = 'Directory: ' + path;
-}
-
function log(s) {
logbox = document.getElementById('logbox');
logbox.value += s + '\n';
logbox.scrollTop = logbox.scrollHeight;
}
-function change_url(url) {
- audio.set_src(url);
+function Browser(songs, current, audio) {
+ this.songs = songs;
+ this.current = current;
+ this.audio = audio;
- var audio_src_url = document.getElementById('audio-src-url');
- audio_src_url.innerHTML = 'Playing: ' + url.replace('&', '&amp;');
-}
+ this.set_current = function(path) {
+ this.current.innerHTML = 'Directory: ' + path;
+ }
-function output_link(obj) {
- var a = obj.get_anchor();
+ this.output_link = function(obj) {
+ var a = obj.get_anchor();
- var li = document.createElement('li');
- li.appendChild(a);
+ var li = document.createElement('li');
+ li.appendChild(a);
- var song_links = document.getElementById('song-links');
- song_links.appendChild(li);
-}
+ this.songs.appendChild(li);
+ }
-function output_album(album) {
- var li = document.createElement('li');
- li.setAttribute('class', 'album');
- li.appendChild(document.createTextNode(album ? album : '(No album)'));
+ this.output_album = function(album) {
+ var li = document.createElement('li');
+ li.setAttribute('class', 'album');
+ li.appendChild(document.createTextNode(album ? album : '(No album)'));
- var song_links = document.getElementById('song-links');
- song_links.appendChild(li);
-}
+ var song_links = document.getElementById('song-links');
+ song_links.appendChild(li);
+ }
-function add_results(json) {
- var lastalbum = false;
- for(var i = 0; i < json.length; i++) {
- var type = json[i]["type"];
- var path = json[i]["name"];
- var track = json[i]["track"];
- var name = path.substring(path.lastIndexOf('/')+1);
- var cached = type == "file" ? json[i]["cached"] : false;
- var metadata = json[i]["metadata"];
-
- if(type == 'file' && metadata.album != lastalbum) {
- output_album(metadata.album);
- lastalbum = metadata.album;
- }
+ this.add_results = function(json) {
+ var lastalbum = false;
+ for(var i = 0; i < json.length; i++) {
+ var type = json[i]["type"];
+ var path = json[i]["name"];
+ var track = json[i]["track"];
+ var name = path.substring(path.lastIndexOf('/')+1);
+ var cached = type == "file" ? json[i]["cached"] : false;
+ var metadata = json[i]["metadata"];
- var l = new MusicListing(type, path, name, track, metadata, cached);
- output_link(l);
+ if(type == 'file' && metadata.album != lastalbum) {
+ this.output_album(metadata.album);
+ lastalbum = metadata.album;
+ }
+
+ var l = new MusicListing(type, path, name, track, metadata, cached);
+ this.output_link(l);
+ }
+ log('add_results: ' + lastalbum);
}
- log('add_results: ' + lastalbum);
-}
-function list(root) {
- log('listing ' + root);
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if(xmlhttp.readyState == 4) {
- set_current(root);
- var json = JSON.parse(xmlhttp.responseText);
- document.getElementById('song-links').innerHTML = '';
- // add "up" link
- if(root.length > 1) {
- up = root.substr(0, root.lastIndexOf('/'));
- if(up.length == 0)
- up = '/';
- l = new MusicListing('dir', up, '..');
- output_link(l);
+ this.list = function(root) {
+ var b = this;
+
+ log('listing ' + root);
+ var xmlhttp = new XMLHttpRequest();
+ xmlhttp.onreadystatechange = function() {
+ if(xmlhttp.readyState == 4) {
+ b.set_current(root);
+ var json = JSON.parse(xmlhttp.responseText);
+ document.getElementById('song-links').innerHTML = '';
+ // add "up" link
+ if(root.length > 1) {
+ up = root.substr(0, root.lastIndexOf('/'));
+ if(up.length == 0)
+ up = '/';
+ l = new MusicListing('dir', up, '..');
+ b.output_link(l);
+ }
+ b.add_results(json);
}
- add_results(json);
}
+
+ path = '/list?directory=' + encodeURIComponent(root);
+ xmlhttp.open('GET', path);
+ xmlhttp.send();
}
- path = '/list?directory=' + encodeURIComponent(root);
- xmlhttp.open('GET', path);
- xmlhttp.send();
-}
+ this.do_search = function(e) {
+ var b = this;
+ if(e.keyCode != 13)
+ return;
-function do_search(e) {
- if(e.keyCode != 13)
- return;
-
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.onreadystatechange = function() {
- if(xmlhttp.readyState == 4) {
- var json = JSON.parse(xmlhttp.responseText);
- document.getElementById('song-links').innerHTML = '';
- output_link(new MusicListing('dir', '/', 'Go to root directory'));
- add_results(json);
+ var xmlhttp = new XMLHttpRequest();
+ xmlhttp.onreadystatechange = function() {
+ if(xmlhttp.readyState == 4) {
+ var json = JSON.parse(xmlhttp.responseText);
+ document.getElementById('song-links').innerHTML = '';
+ b.output_link(new MusicListing('dir', '/', 'Go to root directory'));
+ b.add_results(json);
+ }
}
+
+ var query = document.getElementById('search-query').value;
+ var path = '/search?query=' + encodeURIComponent(query);
+ xmlhttp.open('GET', path);
+ xmlhttp.send();
}
- var query = document.getElementById('search-query').value;
- var path = '/search?query=' + encodeURIComponent(query);
- xmlhttp.open('GET', path);
- xmlhttp.send();
-}
+ this.add_directory = function() {
+ var songs = document.getElementById('song-links').getElementsByTagName('a');
+ for(var i = 0; i < songs.length; i++) {
+ var a = songs[i];
+ log(a);
+ var ml = a.ml;
+ log(ml);
+ if(ml.type && ml.type == 'file')
+ playlist.add(ml);
+ }
+ }
-function add_directory() {
- var songs = document.getElementById('song-links').getElementsByTagName('a');
- for(var i = 0; i < songs.length; i++) {
- var a = songs[i];
- log(a);
- var ml = a.ml;
- log(ml);
- if(ml.type && ml.type == 'file')
- playlist.add(ml);
+ this.get_a = function(path, track) {
+ var as = document.getElementById('song-links').getElementsByTagName('a');
+ for(var i = 0; i < as.length; i++) {
+ var a = as[i];
+ if(a.ml.path == path && (!track || track == a.ml.track))
+ return a;
+ }
}
}
var source = null;
-function get_a(path, track) {
- var as = document.getElementById('song-links').getElementsByTagName('a');
- for(var i = 0; i < as.length; i++) {
- var a = as[i];
- if(a.ml.path == path && (!track || track == a.ml.track))
- return a;
- }
-}
-
function event_handler(event) {
data = JSON.parse(event.data);
switch(data['type']) {
@@ -217,7 +218,7 @@ function event_handler(event) {
track = data['track'];
log('track: ' + track);
// update directory browser
- var a = get_a(data['path'], track);
+ var a = browser.get_a(data['path'], track);
if(a)
a.setAttribute('class', 'file file-' + data['type']);
// update song queue
@@ -237,7 +238,7 @@ function event_handler(event) {
break;
case 'play':
log('[play] ' + data['path']);
- change_url(data['path']);
+ browser.audio.set_src(data['path']);
audio.play();
break;
default:
@@ -258,8 +259,12 @@ function event_open() {
window.onload = function() {
event_open();
+ var song_links = document.getElementById('song-links');
+ var current = document.getElementById('current-dir');
+
audio = new Audio();
- playlist = new Playlist(document.getElementById('playlist'), audio);
+ browser = new Browser(song_links, current, audio);
+ playlist = new Playlist(document.getElementById('playlist'));
audio.audio.addEventListener('ended', function() { playlist.next(); });
- list('/');
+ browser.list('/');
}