2006-03-21 16:59:18 -05:00
|
|
|
Event.observe(window,'load',initPlaylist);
|
|
|
|
var MAX_SONGS= 400;
|
2006-03-24 06:44:50 -05:00
|
|
|
var SEARCH_DELAY = 500; // # ms without typing before the search box searches
|
2006-03-21 16:59:18 -05:00
|
|
|
Ajax.Responders.register({ onCreate: function () {$('busymsg').style.visibility='visible';},
|
2006-03-24 06:44:50 -05:00
|
|
|
onComplete: function () {if (!Query.busy) {$('busymsg').style.visibility='hidden';}}});
|
2006-03-21 16:59:18 -05:00
|
|
|
function initPlaylist() {
|
|
|
|
new Ajax.Request('/databases/1/containers?output=xml',{method: 'get',onComplete:rsSource});
|
|
|
|
Query.send('genres');
|
2006-03-23 17:00:38 -05:00
|
|
|
Event.observe('search','keypress',Search.keyPress);
|
|
|
|
Event.observe('source','change',EventHandler.sourceChange);
|
|
|
|
Event.observe('genres','change',EventHandler.genresChange);
|
|
|
|
Event.observe('artists','change',EventHandler.artistsChange);
|
|
|
|
Event.observe('albums','change',EventHandler.albumsChange);
|
2006-03-24 02:47:43 -05:00
|
|
|
// Firefox remebers the search box value on page reload
|
2006-03-24 06:44:50 -05:00
|
|
|
Field.clear('search');
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
2006-03-24 02:09:31 -05:00
|
|
|
// TODO busy message
|
|
|
|
// timeout on search box
|
|
|
|
// move stuff to responsehandler
|
|
|
|
// handle source change events
|
2006-03-24 02:47:43 -05:00
|
|
|
|
2006-03-23 17:00:38 -05:00
|
|
|
var Search = {
|
2006-03-24 06:44:50 -05:00
|
|
|
timeOut: '',
|
2006-03-23 17:00:38 -05:00
|
|
|
keyPress: function (e) {
|
|
|
|
if (e.keyCode == Event.KEY_RETURN) {
|
|
|
|
EventHandler.search();
|
2006-03-24 06:44:50 -05:00
|
|
|
} else {
|
|
|
|
if (this.timeOut) {
|
|
|
|
window.clearTimeout(this.timeOut);
|
|
|
|
}
|
|
|
|
this.timeOut = window.setTimeout(EventHandler.search,SEARCH_DELAY);
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var EventHandler = {
|
|
|
|
sourceChange: function (e) {
|
|
|
|
alert('Playlist id:'+$('source').value);
|
|
|
|
},
|
|
|
|
search: function () {
|
|
|
|
Query.setSearchString($('search').value);
|
|
|
|
Query.send('genres');
|
|
|
|
},
|
|
|
|
genresChange: function (e) {
|
|
|
|
EventHandler.setSelected('genres');
|
|
|
|
Query.send('artists');
|
|
|
|
},
|
|
|
|
artistsChange: function (e) {
|
|
|
|
EventHandler.setSelected('artists');
|
|
|
|
Query.send('albums');
|
|
|
|
},
|
|
|
|
albumsChange: function (e) {
|
|
|
|
EventHandler.setSelected('albums');
|
|
|
|
Query.send('songs');
|
|
|
|
},
|
|
|
|
setSelected: function (type) {
|
|
|
|
var options = $A($(type).options);
|
|
|
|
Query.clearSelection(type);
|
|
|
|
if ($(type).value != 'all') {
|
|
|
|
options.each(function (option) {
|
|
|
|
if (option.selected) {
|
|
|
|
Query.addSelection(type,option.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
var Query = {
|
2006-03-23 17:00:38 -05:00
|
|
|
genres: [],
|
|
|
|
artists:[],
|
|
|
|
albums: [],
|
2006-03-24 06:44:50 -05:00
|
|
|
busy: '',
|
2006-03-24 02:09:31 -05:00
|
|
|
searchString: '',
|
2006-03-23 17:00:38 -05:00
|
|
|
clearSelection: function (type) {
|
|
|
|
this[type] = [];
|
2006-03-21 16:59:18 -05:00
|
|
|
},
|
|
|
|
addSelection: function (type,value){
|
|
|
|
this[type].push(value);
|
|
|
|
},
|
2006-03-23 17:00:38 -05:00
|
|
|
setSearchString: function (string) {
|
|
|
|
this.searchString = string;
|
|
|
|
},
|
2006-03-21 16:59:18 -05:00
|
|
|
getUrl: function (type) {
|
2006-03-23 17:00:38 -05:00
|
|
|
var query=[];
|
2006-03-21 16:59:18 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'artists':
|
|
|
|
if (this.genres.length > 0) {
|
|
|
|
query = this.genres.collect(function(value){return "'daap.songgenre:"+value.encode()+"'";});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'albums':
|
|
|
|
if (this.artists.length > 0) {
|
|
|
|
query = this.artists.collect(function(value){return "'daap.songartist:"+value.encode()+"'";});
|
|
|
|
} else if (this.genres.length > 0) {
|
|
|
|
query = this.genres.collect(function(value){return "'daap.songgenre:"+value.encode()+"'";});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'songs':
|
|
|
|
if (this.albums.length > 0) {
|
|
|
|
query = this.albums.collect(function(value){return "'daap.songalbum:"+value.encode()+"'";});
|
|
|
|
} else if (this.artists.length > 0) {
|
|
|
|
query = this.artists.collect(function(value){return "'daap.songartist:"+value.encode()+"'";});
|
|
|
|
} else if (this.genres.length > 0) {
|
|
|
|
query = this.genres.collect(function(value){return "'daap.songgenre:"+value.encode()+"'";});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-03-23 17:00:38 -05:00
|
|
|
if (this.searchString) {
|
|
|
|
var search = [];
|
2006-03-24 02:47:43 -05:00
|
|
|
var string = this.searchString.encode();
|
2006-03-23 17:00:38 -05:00
|
|
|
['daap.songgenre','daap.songartist','daap.songalbum','dmap.itemname'].each(function (item) {
|
|
|
|
search.push("'" + item +':*' + string + "*'");
|
|
|
|
});
|
|
|
|
if (query.length > 0) {
|
2006-03-24 02:47:43 -05:00
|
|
|
return '&query=(' +search.join(',') + ')+('.encode() + query.join(',')+ ')';
|
2006-03-23 17:00:38 -05:00
|
|
|
} else {
|
|
|
|
return '&query=' + search.join(',');
|
|
|
|
}
|
2006-03-24 02:09:31 -05:00
|
|
|
} else {
|
|
|
|
if (query.length > 0) {
|
|
|
|
return '&query=' + query.join(',');
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
send: function (type) {
|
2006-03-24 06:44:50 -05:00
|
|
|
this.busy = true;
|
2006-03-21 16:59:18 -05:00
|
|
|
var url;
|
|
|
|
var handler;
|
|
|
|
var meta = '';
|
|
|
|
switch (type) {
|
|
|
|
case 'genres':
|
|
|
|
url = '/databases/1/browse/genres';
|
2006-03-23 17:00:38 -05:00
|
|
|
handler = ResponseHandler.genreAlbumArtist;
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'artists':
|
|
|
|
url = '/databases/1/browse/artists';
|
2006-03-23 17:00:38 -05:00
|
|
|
handler = ResponseHandler.genreAlbumArtist;
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'albums':
|
|
|
|
url = '/databases/1/browse/albums';
|
2006-03-23 17:00:38 -05:00
|
|
|
handler = ResponseHandler.genreAlbumArtist;
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'songs':
|
|
|
|
url = '/databases/1/items';
|
|
|
|
meta = '&meta=daap.songalbum,daap.songartist,daap.songgenre,dmap.itemid,daap.songtime,dmap.itemname';
|
|
|
|
handler = rsSongs;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
url = url + '?output=xml' + meta + this.getUrl(type);
|
|
|
|
new Ajax.Request(url ,{method: 'get',onComplete:handler});
|
|
|
|
}
|
2005-03-16 14:56:40 -05:00
|
|
|
};
|
2006-03-23 17:00:38 -05:00
|
|
|
var ResponseHandler = {
|
|
|
|
genreAlbumArtist: function (request) {
|
|
|
|
var type;
|
|
|
|
if (request.responseXML.getElementsByTagName('daap.browsegenrelisting').length > 0) {
|
|
|
|
type = 'genres';
|
|
|
|
}
|
|
|
|
if (request.responseXML.getElementsByTagName('daap.browseartistlisting').length > 0) {
|
|
|
|
type = 'artists';
|
|
|
|
}
|
|
|
|
if (request.responseXML.getElementsByTagName('daap.browsealbumlisting').length > 0) {
|
|
|
|
type = 'albums';
|
|
|
|
}
|
|
|
|
|
|
|
|
var items = $A(request.responseXML.getElementsByTagName('dmap.listingitem'));
|
|
|
|
items = items.collect(function (el) {
|
|
|
|
return Element.textContent(el);
|
|
|
|
}).sort();
|
|
|
|
var select = $(type);
|
|
|
|
Element.removeChildren(select);
|
2005-03-05 19:16:58 -05:00
|
|
|
|
2006-03-23 17:00:38 -05:00
|
|
|
var o = document.createElement('option');
|
|
|
|
o.value = 'all';
|
|
|
|
o.appendChild(document.createTextNode('All (' + items.length + ' ' + type + ')'));
|
|
|
|
select.appendChild(o);
|
|
|
|
addOptions(select,items);
|
|
|
|
select.value='all';
|
|
|
|
switch (type) {
|
|
|
|
case 'genres':
|
|
|
|
Query.send('artists');
|
|
|
|
break;
|
|
|
|
case 'artists':
|
|
|
|
Query.send('albums');
|
|
|
|
break;
|
|
|
|
case 'albums':
|
|
|
|
Query.send('songs');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-21 14:53:39 -05:00
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
function rsSource(request) {
|
|
|
|
var items = $A(request.responseXML.getElementsByTagName('dmap.listingitem'));
|
|
|
|
var sourceSelect = $('source');
|
2006-03-23 17:00:38 -05:00
|
|
|
var smartPlaylists = [];
|
|
|
|
var staticPlaylists = [];
|
2006-03-21 16:59:18 -05:00
|
|
|
Element.removeChildren(sourceSelect);
|
2005-03-16 14:56:40 -05:00
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
items.each(function (item,index) {
|
|
|
|
if (0 === index) {
|
|
|
|
// Skip Library
|
2005-03-21 14:53:39 -05:00
|
|
|
return;
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (item.getElementsByTagName('com.apple.itunes.smart-playlist').length > 0) {
|
|
|
|
smartPlaylists.push({name: Element.textContent(item.getElementsByTagName('dmap.itemname')[0]),
|
|
|
|
id: Element.textContent(item.getElementsByTagName('dmap.itemid')[0])});
|
2005-03-21 14:53:39 -05:00
|
|
|
} else {
|
2006-03-21 16:59:18 -05:00
|
|
|
staticPlaylists.push({name: Element.textContent(item.getElementsByTagName('dmap.itemname')[0]),
|
|
|
|
id: Element.textContent(item.getElementsByTagName('dmap.itemid')[0])});
|
2005-03-21 14:53:39 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
});
|
|
|
|
sourceSelect.appendChild(Builder.node('option',{value: '1'},'Library'));
|
|
|
|
if (smartPlaylists.length > 0) {
|
|
|
|
optgroup = Builder.node('optgroup',{label: 'Smart playlists'});
|
|
|
|
smartPlaylists.each(function (item) {
|
|
|
|
var option = document.createElement('option');
|
|
|
|
optgroup.appendChild(Builder.node('option',{value: item.id},item.name));
|
|
|
|
});
|
|
|
|
sourceSelect.appendChild(optgroup);
|
2005-03-21 14:53:39 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
if (staticPlaylists.length > 0) {
|
|
|
|
optgroup = Builder.node('optgroup',{label: 'Static playlists'});
|
|
|
|
staticPlaylists.each(function (item) {
|
|
|
|
var option = document.createElement('option');
|
|
|
|
optgroup.appendChild(Builder.node('option',{value: item.id},item.name));
|
|
|
|
});
|
|
|
|
sourceSelect.appendChild(optgroup);
|
2005-03-21 14:53:39 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
// Select Library
|
|
|
|
sourceSelect.value = 1;
|
2005-03-21 14:53:39 -05:00
|
|
|
}
|
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
function rsSongs(request) {
|
|
|
|
var items = $A(request.responseXML.getElementsByTagName('dmap.listingitem'));
|
|
|
|
var songsTable = $('songs_data');
|
|
|
|
Element.removeChildren(songsTable);
|
|
|
|
if (items.length > MAX_SONGS) {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
tr.appendChild(Builder.node('td',{colspan: '5'},'Search returned > '+MAX_SONGS+' results'));
|
|
|
|
songsTable.appendChild(tr);
|
2006-03-24 06:44:50 -05:00
|
|
|
Query.busy = false;
|
2006-03-21 16:59:18 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
items.each(function (item) {
|
|
|
|
var tr = document.createElement('tr');
|
|
|
|
var time = parseInt(Element.textContent(item.getElementsByTagName('daap.songtime')[0]));
|
|
|
|
time = Math.round(time / 1000);
|
|
|
|
var seconds = time % 60;
|
2005-03-05 19:16:58 -05:00
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
seconds = (seconds < 10) ? '0'+seconds : seconds;
|
|
|
|
timeString = Math.floor(time/60)+ ':' + seconds;
|
2005-03-05 19:16:58 -05:00
|
|
|
|
2006-03-22 03:55:31 -05:00
|
|
|
tr.appendChild(Builder.node('td',{style:'width: 140px;'},Element.textContent(item.getElementsByTagName('dmap.itemname')[0])));
|
2006-03-21 16:59:18 -05:00
|
|
|
tr.appendChild(Builder.node('td',{style:'width: 50px;'},timeString));
|
2006-03-22 03:55:31 -05:00
|
|
|
tr.appendChild(Builder.node('td',{style:'width: 120px;'},Element.textContent(item.getElementsByTagName('daap.songartist')[0])));
|
|
|
|
tr.appendChild(Builder.node('td',{style:'width: 120px;'},Element.textContent(item.getElementsByTagName('daap.songalbum')[0])));
|
2006-03-21 16:59:18 -05:00
|
|
|
tr.appendChild(Builder.node('td',{style:'width: 100px;'},Element.textContent(item.getElementsByTagName('daap.songgenre')[0])));
|
2005-03-05 19:16:58 -05:00
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
songsTable.appendChild(tr);
|
2006-03-24 06:44:50 -05:00
|
|
|
});
|
|
|
|
Query.busy = false;
|
2005-03-05 19:16:58 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
function addOptions(element,options) {
|
|
|
|
options.each(function (option) {
|
|
|
|
var node;
|
|
|
|
var text = option.truncate(25);
|
|
|
|
if (option.length != text.length) {
|
|
|
|
node = Builder.node('option',{value: option, title: option},option.truncate(25));
|
2005-03-16 14:56:40 -05:00
|
|
|
} else {
|
2006-03-21 16:59:18 -05:00
|
|
|
node = Builder.node('option',{value: option},option.truncate(25));
|
2005-03-16 14:56:40 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
node.selected = false;
|
|
|
|
element.appendChild(node);
|
|
|
|
});
|
2005-03-16 17:35:26 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
Object.extend(Element, {
|
|
|
|
removeChildren: function(element) {
|
|
|
|
while(element.hasChildNodes()) {
|
|
|
|
element.removeChild(element.firstChild);
|
2005-03-16 14:56:40 -05:00
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
},
|
|
|
|
textContent: function(node) {
|
|
|
|
if ((!node) || !node.hasChildNodes()) {
|
|
|
|
// Empty text node
|
|
|
|
return '';
|
2005-03-16 14:56:40 -05:00
|
|
|
} else {
|
2006-03-21 16:59:18 -05:00
|
|
|
if (node.textContent) {
|
|
|
|
// W3C ?
|
|
|
|
return node.textContent;
|
|
|
|
} else if (node.text) {
|
|
|
|
// IE
|
|
|
|
return node.text;
|
2005-03-16 14:56:40 -05:00
|
|
|
}
|
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
// We shouldn't end up here;
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
String.prototype.encode = function () {
|
|
|
|
return encodeURIComponent(this).replace(/\'/,"\\'");
|
|
|
|
};
|
|
|
|
// Stolen from prototype 1.5
|
|
|
|
String.prototype.truncate = function(length, truncation) {
|
|
|
|
length = length || 30;
|
|
|
|
truncation = truncation === undefined ? '...' : truncation;
|
|
|
|
var ret = (this.length > length) ? this.slice(0, length - truncation.length) + truncation : this;
|
|
|
|
return '' + ret;
|
|
|
|
};
|
|
|
|
function add() {
|
|
|
|
alert('add');
|
|
|
|
}
|
|
|
|
/* Detta script finns att hamta pa http://www.jojoxx.net och
|
|
|
|
far anvandas fritt sa lange som dessa rader star kvar. */
|
|
|
|
function DataDumper(obj,n,prefix){
|
|
|
|
var str=""; prefix=(prefix)?prefix:""; n=(n)?n+1:1; var ind=""; for(var i=0;i<n;i++){ ind+=" "; }
|
|
|
|
if(typeof(obj)=="string"){
|
|
|
|
str+=ind+prefix+"String:\""+obj+"\"\n";
|
|
|
|
} else if(typeof(obj)=="number"){
|
|
|
|
str+=ind+prefix+"Number:"+obj+"\n";
|
|
|
|
} else if(typeof(obj)=="function"){
|
|
|
|
str+=ind+prefix+"Function:"+obj+"\n";
|
|
|
|
} else if(typeof(obj) == 'boolean') {
|
|
|
|
str+=ind+prefix+"Boolean:" + obj + "\n";
|
|
|
|
} else {
|
|
|
|
var type="Array";
|
|
|
|
for(var i in obj){ type=(type=="Array"&&i==parseInt(i))?"Array":"Object" }
|
|
|
|
str+=ind+prefix+type+"[\n";
|
|
|
|
if(type=="Array"){
|
|
|
|
for(var i in obj){ str+=DataDumper(obj[i],n,i+"=>"); }
|
|
|
|
} else {
|
|
|
|
for(var i in obj){ str+=DataDumper(obj[i],n,i+"=>"); }
|
|
|
|
}
|
|
|
|
str+=ind+"]\n";
|
|
|
|
}
|
|
|
|
return str;
|
2005-03-16 14:56:40 -05:00
|
|
|
}
|