2006-03-29 04:02:39 -05:00
|
|
|
// TODO
|
|
|
|
// move stuff to responsehandler
|
2006-03-29 07:35:31 -05:00
|
|
|
// handle source change events (keyPress etc)
|
|
|
|
// If playlist is empty don't confirm delete
|
2006-05-26 18:20:11 -04:00
|
|
|
var CustomDraggable = Class.create();
|
|
|
|
CustomDraggable.removeOnDrop = false;
|
|
|
|
CustomDraggable.revereNamesOnDrop = true;
|
|
|
|
|
|
|
|
CustomDraggable.prototype = (new Rico.Draggable()).extend( {
|
|
|
|
|
|
|
|
initialize: function( htmlElement, name ) {
|
|
|
|
this.type = 'Custom';
|
|
|
|
this.htmlElement = htmlElement;
|
|
|
|
this.name = name;
|
|
|
|
},
|
|
|
|
|
|
|
|
log: function(str) {
|
|
|
|
// alert(str);
|
|
|
|
},
|
|
|
|
|
|
|
|
select: function() {
|
|
|
|
this.selected = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
deselect: function() {
|
|
|
|
this.selected = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
startDrag: function() {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
this.log("startDrag: [" + this.name +"]");
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelDrag: function() {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
this.log("cancelDrag: [" + this.name +"]");
|
|
|
|
},
|
|
|
|
|
|
|
|
endDrag: function() {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
this.log("endDrag: [" + this.name +"]");
|
|
|
|
if ( CustomDraggable.removeOnDrop )
|
|
|
|
this.htmlElement.style.display = 'none';
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
getSingleObjectDragGUI: function() {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
|
|
|
|
var div = document.createElement("div");
|
|
|
|
div.className = 'customDraggable';
|
|
|
|
div.style.width = (this.htmlElement.offsetWidth - 10) + "px";
|
|
|
|
new Insertion.Top( div, "some songs" );
|
|
|
|
return div;
|
|
|
|
},
|
|
|
|
|
|
|
|
getMultiObjectDragGUI: function( draggables ) {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
|
|
|
|
var names = "";
|
|
|
|
names = "some song(s)";
|
|
|
|
|
|
|
|
var div = document.createElement("div");
|
|
|
|
div.className = 'customDraggable';
|
|
|
|
div.style.width = (this.htmlElement.offsetWidth - 10) + "px";
|
|
|
|
new Insertion.Top( div, names );
|
|
|
|
return div;
|
|
|
|
},
|
|
|
|
|
|
|
|
getDroppedGUI: function() {
|
|
|
|
var el = this.htmlElement;
|
|
|
|
|
|
|
|
var div = document.createElement("div");
|
|
|
|
var names = this.name.split(",");
|
|
|
|
if ( CustomDraggable.revereNamesOnDrop )
|
|
|
|
new Insertion.Top( div, "<span class='nameSpan'>[" + names[1].substring(1) + " " + names[0]+ "]</span>" );
|
|
|
|
else
|
|
|
|
new Insertion.Top( div, "<span class='nameSpan'>[" + this.name + "]</span>" );
|
|
|
|
return div;
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return this.name;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
|
|
var CustomDropzone = Class.create();
|
|
|
|
CustomDropzone.prototype = (new Rico.Dropzone()).extend( {
|
|
|
|
initialize: function( htmlElement, header) {
|
|
|
|
this.htmlElement = htmlElement;
|
|
|
|
this.header = header;
|
|
|
|
this.absoluteRect = null;
|
|
|
|
this.acceptedObjects = [];
|
|
|
|
|
|
|
|
this.offset = navigator.userAgent.toLowerCase().indexOf("msie") >= 0 ? 0 : 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
activate: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
deactivate: function() {
|
|
|
|
},
|
|
|
|
|
|
|
|
showHover: function() {
|
|
|
|
if ( this.showingHover )
|
|
|
|
return;
|
|
|
|
this.header.style.color = "#1111bb";
|
|
|
|
this.showingHover = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
hideHover: function() {
|
|
|
|
if ( !this.showingHover )
|
|
|
|
return;
|
|
|
|
this.header.style.color = "#000000";
|
|
|
|
this.showingHover = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
accept: function(draggableObjects) {
|
|
|
|
var songids = '';
|
|
|
|
for (var i = SelectedRows.songId.length - 1; i >= 0; --i) {
|
|
|
|
if (SelectedRows.songId[i]) {
|
|
|
|
if (songids != '') {
|
|
|
|
songids += ",";
|
|
|
|
}
|
|
|
|
songids += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._insert(songids);
|
|
|
|
},
|
|
|
|
|
|
|
|
_insert: function(songids) {
|
|
|
|
var url = '/databases/1/containers/' + this.htmlElement.value + "/items/add?output=xml&dmap.itemid=" + songids;
|
|
|
|
new Ajax.Request(url ,{method: 'get',onComplete:this.responseAdd});
|
|
|
|
},
|
|
|
|
|
|
|
|
responseAdd: function(request) {
|
|
|
|
var status = Element.textContent(request.responseXML.getElementsByTagName('dmap.status')[0]);
|
|
|
|
if ('200' != status) {
|
|
|
|
alert("Couldn't add songs to playlist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
alert("Added songs to playlist");
|
|
|
|
},
|
|
|
|
|
|
|
|
canAccept: function(draggableObjects) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
Event.observe(window,'load',initPlaylist);
|
2006-03-25 07:48:51 -05:00
|
|
|
|
2006-03-24 06:44:50 -05:00
|
|
|
var SEARCH_DELAY = 500; // # ms without typing before the search box searches
|
2006-04-22 17:06:34 -04:00
|
|
|
var BROWSE_TEXT_LEN = 30; // genres/artists/albums select options longer than this will have
|
|
|
|
// a title attribute (to show a tool tip for items that doesn't fit the box
|
2006-04-08 16:54:58 -04:00
|
|
|
var g_myLiveGrid; // the live grid;
|
2006-03-21 16:59:18 -05:00
|
|
|
function initPlaylist() {
|
2006-04-18 16:23:19 -04:00
|
|
|
Ajax.Responders.register({ onCreate: Spinner.incRequestCount,
|
|
|
|
onComplete: Spinner.decRequestCount});
|
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
new Ajax.Request('/databases/1/containers?output=xml',{method: 'get',onComplete:rsSource});
|
|
|
|
Query.send('genres');
|
2006-03-29 17:20:12 -05:00
|
|
|
Event.observe('search','keypress',EventHandler.searchKeyPress);
|
2006-03-23 17:00:38 -05:00
|
|
|
Event.observe('source','change',EventHandler.sourceChange);
|
2006-03-27 15:26:12 -05:00
|
|
|
Event.observe('source','click',EventHandler.sourceClick);
|
2006-03-27 17:19:18 -05:00
|
|
|
Event.observe('source','keypress',EventHandler.sourceKeyPress);
|
2006-03-23 17:00:38 -05:00
|
|
|
Event.observe('genres','change',EventHandler.genresChange);
|
|
|
|
Event.observe('artists','change',EventHandler.artistsChange);
|
|
|
|
Event.observe('albums','change',EventHandler.albumsChange);
|
2006-03-29 17:20:12 -05:00
|
|
|
Event.observe('add_playlist_href','click',EventHandler.addPlaylistHrefClick);
|
2006-03-27 15:26:12 -05:00
|
|
|
|
|
|
|
Event.observe(document,'click',GlobalEvents.click);
|
2006-03-29 17:20:12 -05:00
|
|
|
Event.observe('edit_playlist_name','keypress',EventHandler.editPlaylistNameKeyPress);
|
2006-04-18 16:23:19 -04:00
|
|
|
Event.observe('songs_data','click',SelectedRows.click);
|
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-04-08 16:54:58 -04:00
|
|
|
g_myLiveGrid = new Rico.LiveGrid('songs_data',20,1000,'',{prefetchBuffer: false});
|
2006-05-26 18:20:11 -04:00
|
|
|
|
|
|
|
for (var i = $('songs_data').rows.length - 1; i >= 0; --i) {
|
|
|
|
dndMgr.registerDraggable(new CustomDraggable($('songs_data').rows[i], "bob " + i));
|
|
|
|
}
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
2006-04-18 16:23:19 -04:00
|
|
|
var Spinner = {
|
|
|
|
count: 0,
|
2006-04-20 17:30:47 -04:00
|
|
|
incRequestCount: function (ca) {
|
2006-04-18 16:23:19 -04:00
|
|
|
Spinner.count++;
|
|
|
|
$('spinner').style.visibility = 'visible';
|
|
|
|
},
|
2006-04-20 17:30:47 -04:00
|
|
|
decRequestCount: function (caller) {
|
2006-04-18 16:23:19 -04:00
|
|
|
Spinner.count--;
|
2006-05-03 17:03:10 -04:00
|
|
|
if (/type=browse/.test(caller.url)) {
|
2006-04-18 16:23:19 -04:00
|
|
|
Spinner.count = 0;
|
|
|
|
$('spinner').style.visibility = 'hidden';
|
|
|
|
}
|
|
|
|
}
|
2006-04-20 17:30:47 -04:00
|
|
|
};
|
2006-03-27 15:26:12 -05:00
|
|
|
var GlobalEvents = {
|
2006-03-27 16:56:26 -05:00
|
|
|
_clickListeners: [],
|
2006-03-27 15:26:12 -05:00
|
|
|
click: function (e) {
|
2006-03-27 16:56:26 -05:00
|
|
|
GlobalEvents._clickListeners.each(function (name) {
|
2006-03-27 15:26:12 -05:00
|
|
|
name.click(e);
|
|
|
|
});
|
|
|
|
},
|
2006-03-27 16:56:26 -05:00
|
|
|
addClickListener: function (el) {
|
|
|
|
this._clickListeners.push(el);
|
2006-03-27 15:26:12 -05:00
|
|
|
},
|
2006-03-27 16:56:26 -05:00
|
|
|
removeClickListener: function (el) {
|
|
|
|
this._clickListeners = this._clickListeners.findAll(function (element) {
|
2006-03-27 15:26:12 -05:00
|
|
|
return (element != el);
|
|
|
|
});
|
|
|
|
}
|
2006-04-20 17:30:47 -04:00
|
|
|
};
|
2006-03-24 02:47:43 -05:00
|
|
|
|
2006-03-29 17:20:12 -05:00
|
|
|
var Source = {
|
2006-03-27 15:26:12 -05:00
|
|
|
playlistId: '',
|
|
|
|
playlistName: '',
|
2006-03-27 17:19:18 -05:00
|
|
|
_getOptionElement: function (id) {
|
2006-03-27 15:26:12 -05:00
|
|
|
return option = $A($('source').getElementsByTagName('option')).find(function (el) {
|
2006-03-29 17:20:12 -05:00
|
|
|
return (el.value == id);
|
2006-03-27 15:26:12 -05:00
|
|
|
});
|
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
addPlaylist: function () {
|
2006-03-27 16:56:26 -05:00
|
|
|
var url = '/databases/1/containers/add?output=xml';
|
2006-03-29 07:35:31 -05:00
|
|
|
var name= 'untitled playlist';
|
|
|
|
if (this._playlistExists(name)) {
|
|
|
|
var i=1;
|
|
|
|
while (this._playlistExists(name +' ' + i)) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
name += ' ' +i;
|
|
|
|
}
|
|
|
|
this.playlistName = name;
|
|
|
|
url += '&org.mt-daapd.playlist-type=0&dmap.itemname=' + encodeURIComponent(name);
|
2006-03-29 17:20:12 -05:00
|
|
|
new Ajax.Request(url ,{method: 'get',onComplete:this.responseAdd});
|
2006-03-27 16:56:26 -05:00
|
|
|
},
|
2006-03-29 07:35:31 -05:00
|
|
|
_playlistExists: function (name) {
|
2006-03-29 17:20:12 -05:00
|
|
|
return $A($('source').getElementsByTagName('option')).pluck('firstChild').find(function (el) {
|
|
|
|
return el.nodeValue == name;
|
2006-03-29 07:35:31 -05:00
|
|
|
});
|
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
removePlaylist: function () {
|
2006-03-27 17:19:18 -05:00
|
|
|
if (window.confirm('Really delete playlist?')) {
|
|
|
|
var url = '/databases/1/containers/del?output=xml';
|
|
|
|
url += '&dmap.itemid=' + $('source').value;
|
2006-03-29 17:20:12 -05:00
|
|
|
new Ajax.Request(url ,{method: 'get',onComplete:this.response});
|
|
|
|
var option = this._getOptionElement($('source').value);
|
2006-03-27 17:19:18 -05:00
|
|
|
Element.remove(option);
|
|
|
|
}
|
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
savePlaylistName: function () {
|
2006-03-27 15:26:12 -05:00
|
|
|
input = $('edit_playlist_name');
|
|
|
|
var url = '/databases/1/containers/edit?output=xml';
|
2006-03-29 17:20:12 -05:00
|
|
|
url += '&dmap.itemid=' + Source.playlistId;
|
2006-03-29 06:24:46 -05:00
|
|
|
url += '&dmap.itemname=' + encodeURIComponent(input.value);
|
2006-03-29 17:20:12 -05:00
|
|
|
new Ajax.Request(url ,{method: 'get',onComplete:this.response});
|
|
|
|
var option = this._getOptionElement(Source.playlistId);
|
2006-03-27 15:26:12 -05:00
|
|
|
option.text = input.value;
|
2006-03-29 17:20:12 -05:00
|
|
|
this.hideEditPlaylistName();
|
2006-03-27 15:26:12 -05:00
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
editPlaylistName: function () {
|
2006-03-27 15:26:12 -05:00
|
|
|
input = $('edit_playlist_name');
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.playlistId = $('source').value;
|
2006-04-11 03:22:34 -04:00
|
|
|
playlistName = Element.textContent(this._getOptionElement(Source.playlistId));
|
2006-04-08 15:48:18 -04:00
|
|
|
//###FIXME use prototype Position instead
|
2006-04-01 17:12:03 -05:00
|
|
|
input.style.top = RicoUtil.toDocumentPosition(this._getOptionElement(Source.playlistId)).y + 'px';
|
2006-03-29 17:20:12 -05:00
|
|
|
input.value = playlistName;
|
2006-03-27 15:26:12 -05:00
|
|
|
input.style.display = 'block';
|
|
|
|
Field.activate(input);
|
2006-03-27 16:56:26 -05:00
|
|
|
GlobalEvents.addClickListener(this);
|
2006-03-27 15:26:12 -05:00
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
hideEditPlaylistName: function () {
|
2006-03-27 15:26:12 -05:00
|
|
|
$('edit_playlist_name').style.display = 'none';
|
2006-03-31 02:29:53 -05:00
|
|
|
Field.activate('source');
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.playlistId = '';
|
2006-03-27 16:56:26 -05:00
|
|
|
GlobalEvents.removeClickListener(this);
|
2006-03-27 15:26:12 -05:00
|
|
|
},
|
|
|
|
response: function (request) {
|
2006-03-27 16:12:28 -05:00
|
|
|
// Check that the save gave response 200 OK
|
2006-03-27 15:26:12 -05:00
|
|
|
},
|
2006-03-27 16:56:26 -05:00
|
|
|
responseAdd: function(request) {
|
|
|
|
var status = Element.textContent(request.responseXML.getElementsByTagName('dmap.status')[0]);
|
|
|
|
if ('200' != status) {
|
2006-03-29 07:35:31 -05:00
|
|
|
//###FIXME if someone else adds a playlist with the same name
|
|
|
|
// as mine, (before My page is refreshed) won't happen that often
|
2006-03-29 04:02:39 -05:00
|
|
|
alert('There is a playlist with that name, write some code to handle this');
|
2006-03-27 16:56:26 -05:00
|
|
|
return;
|
|
|
|
}
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.playlistId = Element.textContent(request.responseXML.getElementsByTagName('dmap.itemid')[0]);
|
2006-03-27 16:56:26 -05:00
|
|
|
var o = document.createElement('option');
|
2006-03-29 17:20:12 -05:00
|
|
|
o.value = Source.playlistId;
|
|
|
|
o.text = Source.playlistName;
|
2006-03-27 16:56:26 -05:00
|
|
|
$('static_playlists').appendChild(o);
|
2006-03-29 17:20:12 -05:00
|
|
|
$('source').value = Source.playlistId;
|
|
|
|
Source.editPlaylistName();
|
|
|
|
Query.setSource(Source.playlistId);
|
2006-03-27 16:56:26 -05:00
|
|
|
Query.send('genres');
|
|
|
|
},
|
2006-03-27 15:26:12 -05:00
|
|
|
click: function (e) {
|
2006-04-08 15:48:18 -04:00
|
|
|
//###FIXME use prototype Position instead
|
2006-03-27 16:12:28 -05:00
|
|
|
var x = Event.pointerX(e);
|
|
|
|
var y = Event.pointerY(e);
|
|
|
|
var el = $('edit_playlist_name');
|
|
|
|
var pos = RicoUtil.toViewportPosition(el);
|
|
|
|
if ((x > pos.x) && (x < pos.x + el.offsetWidth) &&
|
|
|
|
(y > pos.y) && (y < pos.y + el.offsetHeight)) {
|
|
|
|
// Click was in input box
|
|
|
|
return;
|
|
|
|
}
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.savePlaylistName();
|
2006-03-27 15:26:12 -05:00
|
|
|
}
|
2006-04-20 17:30:47 -04:00
|
|
|
};
|
|
|
|
|
2006-03-23 17:00:38 -05:00
|
|
|
var EventHandler = {
|
2006-03-29 17:20:12 -05:00
|
|
|
searchTimeOut: '',
|
2006-03-27 15:26:12 -05:00
|
|
|
sourceClickCount: [],
|
|
|
|
sourceClick: function (e) {
|
|
|
|
var playlistId = Event.element(e).value;
|
|
|
|
if (1 == playlistId) {
|
|
|
|
// do nothing for Library
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (EventHandler.sourceClickCount[playlistId]) {
|
2006-04-20 17:30:47 -04:00
|
|
|
EventHandler.sourceClickCount[playlistId]++;
|
2006-03-27 15:26:12 -05:00
|
|
|
} else {
|
|
|
|
EventHandler.sourceClickCount[playlistId] = 1;
|
|
|
|
}
|
|
|
|
if (EventHandler.sourceClickCount[playlistId] > 1) {
|
|
|
|
el = Event.element(e);
|
2006-04-01 17:12:03 -05:00
|
|
|
if (!document.all && !el.text) {
|
|
|
|
// IE sends the select in the event, not the option
|
2006-03-27 15:26:12 -05:00
|
|
|
// Firefox generates and event when clicking in and empty area
|
|
|
|
// of the select box
|
|
|
|
return;
|
|
|
|
}
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.editPlaylistName();
|
2006-03-27 15:26:12 -05:00
|
|
|
}
|
|
|
|
},
|
2006-03-23 17:00:38 -05:00
|
|
|
sourceChange: function (e) {
|
2006-03-27 15:26:12 -05:00
|
|
|
EventHandler.sourceClickCount = [];
|
2006-03-27 04:34:12 -05:00
|
|
|
Field.clear('search');
|
|
|
|
var playlistId = $('source').value;
|
2006-03-27 16:56:26 -05:00
|
|
|
Query.setSource(playlistId);
|
2006-03-27 04:34:12 -05:00
|
|
|
Query.send('genres');
|
2006-03-23 17:00:38 -05:00
|
|
|
},
|
2006-03-27 17:19:18 -05:00
|
|
|
sourceKeyPress: function (e) {
|
|
|
|
if (e.keyCode == Event.KEY_DELETE) {
|
2006-03-29 17:20:12 -05:00
|
|
|
Source.removePlaylist();
|
2006-03-27 17:19:18 -05:00
|
|
|
}
|
2006-03-29 04:02:39 -05:00
|
|
|
if (113 == e.keyCode) {
|
|
|
|
// F2
|
|
|
|
//TODO edit playist, what is the key on a mac?
|
|
|
|
}
|
2006-03-27 17:19:18 -05:00
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
editPlaylistNameKeyPress: function (e) {
|
|
|
|
input = $('edit_playlist_name');
|
|
|
|
if (e.keyCode == Event.KEY_ESC) {
|
|
|
|
Source.hideEditPlaylistName();
|
|
|
|
}
|
|
|
|
if (e.keyCode == Event.KEY_RETURN) {
|
|
|
|
Source.savePlaylistName();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addPlaylistHrefClick: function (e) {
|
|
|
|
Source.addPlaylist();
|
|
|
|
},
|
|
|
|
searchKeyPress: function (e) {
|
|
|
|
if (EventHandler.searchTimeOut) {
|
|
|
|
window.clearTimeout(EventHandler.searchTimeOut);
|
|
|
|
}
|
|
|
|
if (e.keyCode == Event.KEY_RETURN) {
|
|
|
|
EventHandler._search();
|
|
|
|
} else {
|
|
|
|
EventHandler.searchTimeOut = window.setTimeout(EventHandler._search,SEARCH_DELAY);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_search: function () {
|
2006-03-23 17:00:38 -05:00
|
|
|
Query.setSearchString($('search').value);
|
|
|
|
Query.send('genres');
|
|
|
|
},
|
|
|
|
genresChange: function (e) {
|
2006-03-29 17:20:12 -05:00
|
|
|
EventHandler._setSelected('genres');
|
2006-03-23 17:00:38 -05:00
|
|
|
Query.send('artists');
|
|
|
|
},
|
|
|
|
artistsChange: function (e) {
|
2006-03-29 17:20:12 -05:00
|
|
|
EventHandler._setSelected('artists');
|
2006-03-23 17:00:38 -05:00
|
|
|
Query.send('albums');
|
|
|
|
},
|
|
|
|
albumsChange: function (e) {
|
2006-03-29 17:20:12 -05:00
|
|
|
EventHandler._setSelected('albums');
|
2006-04-20 17:30:47 -04:00
|
|
|
SelectedRows.clearAll();
|
2006-04-08 16:54:58 -04:00
|
|
|
g_myLiveGrid.resetContents();
|
|
|
|
g_myLiveGrid.requestContentRefresh(0);
|
|
|
|
// Query.send('songs');
|
2006-03-23 17:00:38 -05:00
|
|
|
},
|
2006-03-29 17:20:12 -05:00
|
|
|
_setSelected: function (type) {
|
2006-03-23 17:00:38 -05:00
|
|
|
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-26 16:24:20 -05:00
|
|
|
};
|
2006-03-26 16:14:03 -05:00
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
var Query = {
|
2006-05-03 15:03:33 -04:00
|
|
|
baseUrl: '/rsp/db/',
|
|
|
|
playlistId: '1',
|
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-27 16:56:26 -05:00
|
|
|
setSource: function (playlistId) {
|
|
|
|
Query.clearSelection('genres');
|
|
|
|
Query.clearSelection('artists');
|
|
|
|
Query.clearSelection('albums');
|
|
|
|
Query.setSearchString('');
|
2006-05-03 15:03:33 -04:00
|
|
|
Query.playlistId = playlistId;
|
2006-03-27 16:56:26 -05:00
|
|
|
},
|
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) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.genres.collect(function(value){return 'genre%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'albums':
|
|
|
|
if (this.artists.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.artists.collect(function(value){return 'artist%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
} else if (this.genres.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.genres.collect(function(value){return 'genre%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'songs':
|
|
|
|
if (this.albums.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.albums.collect(function(value){return 'album%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
} else if (this.artists.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.artists.collect(function(value){return 'artist%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
} else if (this.genres.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
query = this.genres.collect(function(value){return 'genre%3D"'+value.encode()+'"';});
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
break;
|
2006-03-26 16:24:20 -05:00
|
|
|
default:
|
2006-03-26 16:34:57 -05:00
|
|
|
// Do nothing
|
2006-03-26 16:24:20 -05:00
|
|
|
break;
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
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-05-03 15:03:33 -04:00
|
|
|
['genre','artist','album','title'].each(function (item) {
|
|
|
|
search.push(item +' includes "' + string + '"');
|
2006-03-23 17:00:38 -05:00
|
|
|
});
|
|
|
|
if (query.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
return '&query=(' + search.join(' or ') + ') and ('.encode() + query.join(' or ')+ ')';
|
2006-03-23 17:00:38 -05:00
|
|
|
} else {
|
2006-05-03 15:03:33 -04:00
|
|
|
return '&query=' + search.join(' or ');
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
2006-03-24 02:09:31 -05:00
|
|
|
} else {
|
|
|
|
if (query.length > 0) {
|
2006-05-03 15:03:33 -04:00
|
|
|
return '&query=' + query.join(' or ');
|
2006-03-24 02:09:31 -05:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
|
|
|
},
|
2006-04-08 15:48:18 -04:00
|
|
|
getFullUrl: 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':
|
2006-05-03 15:03:33 -04:00
|
|
|
url = '/genre';
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'artists':
|
2006-05-03 15:03:33 -04:00
|
|
|
url = '/artist';
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'albums':
|
2006-05-03 15:03:33 -04:00
|
|
|
url = '/album';
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
|
|
|
case 'songs':
|
2006-05-03 15:03:33 -04:00
|
|
|
url = '';
|
|
|
|
meta = '&type=browse';
|
2006-03-21 16:59:18 -05:00
|
|
|
break;
|
2006-03-26 16:24:20 -05:00
|
|
|
default:
|
|
|
|
alert("Shouldn't happen 2");
|
|
|
|
break;
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
2006-05-03 15:03:33 -04:00
|
|
|
return this.baseUrl + this.playlistId + url + '?dummy=' + meta + this.getUrl(type);
|
2006-04-08 15:48:18 -04:00
|
|
|
},
|
|
|
|
send: function(type) {
|
|
|
|
if (('genres' == type) || ('artists' == type) || ('albums' == type)) {
|
2006-05-03 15:03:33 -04:00
|
|
|
handler = ResponseHandler[type];
|
2006-04-08 15:48:18 -04:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
// handler = rsSongs;
|
|
|
|
}
|
|
|
|
new Ajax.Request(this.getFullUrl(type), {method: 'get',onComplete:handler});
|
2006-03-21 16:59:18 -05:00
|
|
|
}
|
2005-03-16 14:56:40 -05:00
|
|
|
};
|
2006-03-23 17:00:38 -05:00
|
|
|
var ResponseHandler = {
|
2006-05-03 15:03:33 -04:00
|
|
|
genres: function (request) {
|
|
|
|
ResponseHandler._genreAlbumArtist(request,'genres');
|
|
|
|
},
|
|
|
|
artists: function (request) {
|
|
|
|
ResponseHandler._genreAlbumArtist(request,'artists');
|
|
|
|
},
|
|
|
|
albums: function (request) {
|
|
|
|
ResponseHandler._genreAlbumArtist(request,'albums');
|
|
|
|
},
|
|
|
|
_genreAlbumArtist: function (request,type) {
|
|
|
|
var items = $A(request.responseXML.getElementsByTagName('item'));
|
2006-03-23 17:00:38 -05:00
|
|
|
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);
|
2006-03-26 16:14:03 -05:00
|
|
|
var selected = {};
|
|
|
|
Query[type].each(function(item) {
|
|
|
|
selected[item] = true;
|
|
|
|
});
|
|
|
|
Query.clearSelection(type);
|
2006-03-29 17:20:12 -05:00
|
|
|
if (ResponseHandler._addOptions(type,items,selected)) {
|
2006-03-26 16:14:03 -05:00
|
|
|
select.value='all';
|
|
|
|
}
|
2006-03-23 17:00:38 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'genres':
|
|
|
|
Query.send('artists');
|
|
|
|
break;
|
|
|
|
case 'artists':
|
|
|
|
Query.send('albums');
|
|
|
|
break;
|
|
|
|
case 'albums':
|
2006-04-20 17:30:47 -04:00
|
|
|
SelectedRows.clearAll();
|
2006-04-08 16:54:58 -04:00
|
|
|
g_myLiveGrid.resetContents();
|
|
|
|
g_myLiveGrid.requestContentRefresh(0);
|
2006-04-08 15:48:18 -04:00
|
|
|
// Query.send('songs');
|
2006-03-23 17:00:38 -05:00
|
|
|
break;
|
2006-03-26 16:24:20 -05:00
|
|
|
default:
|
|
|
|
alert("Shouldn't happen 3");
|
|
|
|
break;
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
2006-03-29 17:20:12 -05:00
|
|
|
},
|
|
|
|
_addOptions: function (type,options,selected) {
|
|
|
|
el = $(type);
|
|
|
|
var nothingSelected = true;
|
|
|
|
options.each(function (option) {
|
|
|
|
var node;
|
|
|
|
//###FIXME I have no idea why the Builder.node can't create options
|
|
|
|
// with the selected state I want.
|
|
|
|
var o = document.createElement('option');
|
|
|
|
o.value = option;
|
2006-04-22 17:06:34 -04:00
|
|
|
if (option.length >= BROWSE_TEXT_LEN) {
|
2006-03-29 17:20:12 -05:00
|
|
|
o.title = option;
|
|
|
|
}
|
2006-04-22 17:06:34 -04:00
|
|
|
o.appendChild(document.createTextNode(option));
|
2006-03-29 17:20:12 -05:00
|
|
|
if (selected[option]) {
|
|
|
|
o.selected = true;
|
|
|
|
nothingSelected = false;
|
|
|
|
Query.addSelection(type,option);
|
|
|
|
} else {
|
|
|
|
o.selected = false;
|
|
|
|
}
|
|
|
|
el.appendChild(o);
|
|
|
|
});
|
|
|
|
return nothingSelected;
|
2006-03-23 17:00:38 -05:00
|
|
|
}
|
2006-03-26 16:24:20 -05:00
|
|
|
};
|
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) {
|
2006-03-27 16:56:26 -05:00
|
|
|
optgroup = Builder.node('optgroup',{label: 'Static playlists',id: 'static_playlists'});
|
2006-03-21 16:59:18 -05:00
|
|
|
staticPlaylists.each(function (item) {
|
|
|
|
var option = document.createElement('option');
|
|
|
|
optgroup.appendChild(Builder.node('option',{value: item.id},item.name));
|
|
|
|
});
|
|
|
|
sourceSelect.appendChild(optgroup);
|
2006-05-26 18:20:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
var options = $('static_playlists').getElementsByTagName("option");
|
|
|
|
for (var j = 0; j < options.length; j++) {
|
|
|
|
dndMgr.registerDropZone(new CustomDropzone(options[j], $('static_playlists')));
|
|
|
|
}
|
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-04-18 16:23:19 -04:00
|
|
|
SelectedRows = {
|
|
|
|
songId: [],
|
|
|
|
click: function(e) {
|
|
|
|
var tr = Event.findElement(e,'tr');
|
2006-04-20 17:47:58 -04:00
|
|
|
var id = tr.getAttribute('songid');
|
|
|
|
if (!id) {
|
2006-04-20 17:30:47 -04:00
|
|
|
return;
|
2006-04-18 16:23:19 -04:00
|
|
|
}
|
2006-04-20 17:30:47 -04:00
|
|
|
if (e.ctrlKey) {
|
|
|
|
if (SelectedRows.isSelected(tr)) {
|
|
|
|
SelectedRows.unsetSelected(tr);
|
|
|
|
} else {
|
|
|
|
SelectedRows.setSelected(tr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (e.shiftKey) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (SelectedRows.isSelected(tr)) {
|
|
|
|
SelectedRows.clearAll();
|
|
|
|
} else {
|
|
|
|
SelectedRows.clearAll();
|
|
|
|
SelectedRows.setSelected(tr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isSelected: function (tr) {
|
|
|
|
return SelectedRows.songId[tr.getAttribute('songid')];
|
2006-04-18 16:23:19 -04:00
|
|
|
},
|
2006-04-20 17:30:47 -04:00
|
|
|
setSelected: function (tr) {
|
|
|
|
SelectedRows.songId[tr.getAttribute('songid')] = tr.getAttribute('index');
|
|
|
|
tr.style.backgroundColor = '#8CACBB';
|
|
|
|
},
|
|
|
|
unsetSelected: function (tr) {
|
|
|
|
SelectedRows.songId[tr.getAttribute('songid')] = '';
|
|
|
|
tr.style.backgroundColor = '';
|
|
|
|
},
|
|
|
|
clearAll: function () {
|
|
|
|
SelectedRows.songId = [];
|
|
|
|
$A($('songs_data').getElementsByTagName('tr')).each(SelectedRows.unsetSelected);
|
|
|
|
},
|
|
|
|
updateState: function (tr,songId,index) {
|
2006-05-03 17:03:10 -04:00
|
|
|
if (songId && (index || 0 === index)) {
|
|
|
|
// 0 == false but we want to catch index == 0
|
2006-04-20 17:30:47 -04:00
|
|
|
tr.setAttribute('songid',songId);
|
|
|
|
tr.setAttribute('index',index);
|
|
|
|
if (SelectedRows.isSelected(tr)) {
|
|
|
|
SelectedRows.setSelected(tr);
|
|
|
|
} else {
|
|
|
|
SelectedRows.unsetSelected(tr);
|
|
|
|
}
|
|
|
|
} else {
|
2006-05-03 17:03:10 -04:00
|
|
|
tr.setAttribute('songid','');
|
|
|
|
tr.setAttribute('index','');
|
2006-04-20 17:30:47 -04:00
|
|
|
SelectedRows.unsetSelected(tr);
|
|
|
|
}
|
2006-04-18 16:23:19 -04:00
|
|
|
}
|
2006-04-20 17:30:47 -04:00
|
|
|
};
|
|
|
|
|
2006-03-21 16:59:18 -05:00
|
|
|
String.prototype.encode = function () {
|
2006-03-26 16:14:03 -05:00
|
|
|
return encodeURIComponent(this).replace(/\'/g,"\\'");
|
2006-03-21 16:59:18 -05:00
|
|
|
};
|
|
|
|
// 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;
|
|
|
|
};
|