3 Jappix - An open social platform
4 These are the music JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 25/04/11
14 // Opens the music bubble
15 function openMusic() {
16 var path = '.music-content';
18 // Show the music bubble
21 $(document).oneTime(10, function() {
22 $(path + ' input').focus();
28 // Parses the music search XML
29 function parseMusic(xml, type) {
30 var path = '.music-content ';
31 var content = path + '.list';
32 var path_type = content + ' .' + type;
34 // Create the result container
35 if(!exists(path_type)) {
36 var code = '<div class="' + type + '"></div>';
39 $(content).prepend(code);
41 $(content).append(code);
45 $(xml).find('track').each(function() {
47 var id = $(this).find('id').text();
48 var title = $(this).find('name').text();
49 var artist = $(this).find('artist').text();
50 var source = $(this).find('source').text();
51 var duration = $(this).find('duration').text();
52 var uri = $(this).find('url').text();
53 var mime = $(this).find('type').text();
65 uri = generateURL(uri);
67 // Append the HTML code
68 $(path_type).append('<a href="#" class="song" data-id="' + id + '">' + title + '</a>');
70 // Current playing song?
71 var current_song = $(path_type + ' a[data-id=' + id + ']');
73 if(exists('.music-audio[data-id=' + id + ']'))
74 current_song.addClass('playing');
77 current_song.click(function() {
78 return addMusic(id, title, artist, source, duration, uri, mime, type);
82 // The search is finished
83 if(exists(content + ' .jamendo') && exists(content + ' .local')) {
84 // Get the result values
85 var jamendo = $(content + ' .jamendo').text();
86 var local = $(content + ' .local').text();
89 $(path + 'input').val('').removeAttr('disabled');
92 if(!jamendo && !local)
93 $(path + '.no-results').show();
95 // We must put a separator between the categories
97 $(content + ' .local').addClass('special');
101 // Sends the music search requests
102 function searchMusic() {
103 var path = '.music-content ';
105 // We get the input string
106 var string = $(path + 'input').val();
108 // We lock the search input
109 $(path + 'input').attr('disabled', true);
111 // We reset the results
112 $(path + '.list div').remove();
113 $(path + '.no-results').hide();
115 // Get the Jamendo results
116 $.get('./php/music-search.php', {searchquery: string, location: 'jamendo'}, function(data) {
117 parseMusic(data, 'jamendo');
120 // Get the local results
121 $.get('./php/music-search.php', {searchquery: string, location: JAPPIX_LOCATION}, function(data) {
122 parseMusic(data, 'local');
126 // Performs an action on the music player
127 function actionMusic(action) {
130 var playThis = document.getElementById('top-content').getElementsByTagName('audio')[0];
132 // Nothing to play, exit
136 var stopButton = $('#top-content a.stop');
139 if(action == 'play') {
143 playThis.addEventListener('ended', function() {
147 logThis('Music is now playing.');
150 // User stop the song or the song came to its end
151 else if(action == 'stop') {
154 $('#top-content .music').removeClass('actived');
155 $('.music-content .list a').removeClass('playing');
156 $('.music-audio').remove();
159 logThis('Music is now stopped.');
170 // Publishes the current title over PEP
171 function publishMusic(title, artist, source, duration, uri) {
172 // We share the tune on PEP if enabled
174 /* REF: http://xmpp.org/extensions/xep-0118.html */
176 var iq = new JSJaCIQ();
179 // Create the main PubSub nodes
180 var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
181 var publish = pubsub.appendChild(iq.buildNode('publish', {'node': NS_TUNE, 'xmlns': NS_PUBSUB}));
182 var item = publish.appendChild(iq.buildNode('item', {'xmlns': NS_PUBSUB}));
183 var tune = item.appendChild(iq.buildNode('tune', {'xmlns': NS_TUNE}));
186 if(title || artist || source || uri) {
188 var nodes = new Array(
196 var values = new Array(
204 // Create the children nodes
207 tune.appendChild(iq.buildNode(nodes[i], {'xmlns': NS_TUNE}, values[i]));
213 logThis('New tune sent: ' + title, 3);
217 // Adds a music title to the results
218 function addMusic(id, title, artist, source, duration, uri, mime, type) {
219 var path = '.music-content ';
221 // We remove & create a new audio tag
222 $('.music-audio').remove();
223 $(path + '.player').prepend('<audio class="music-audio" type="' + mime + '" data-id="' + id + '" />');
225 // We apply the new source to the player
226 if(type == 'jamendo')
227 $('.music-audio').attr('src', 'http://api.jamendo.com/get2/stream/track/redirect/?id=' + id + '&streamencoding=ogg2');
229 $('.music-audio').attr('src', uri);
231 // We play the target sound
234 // We set the actived class
235 $('#top-content .music').addClass('actived');
237 // We set a current played track indicator
238 $(path + '.list a').removeClass('playing');
239 $(path + 'a[data-id=' + id + ']').addClass('playing');
241 // We publish what we listen
242 publishMusic(title, artist, source, duration, uri);
248 function launchMusic() {
249 // When music search string submitted
250 $('.music-content input').keyup(function(e) {
252 if(e.keyCode == 13 && $(this).val())