]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/music.js
af86a060c51e14ff65896175162c0897ffe7ca3b
[friendica-addons.git] / jappixmini / jappix / js / music.js
1 /*
2
3 Jappix - An open social platform
4 These are the music JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 25/04/11
11
12 */
13
14 // Opens the music bubble
15 function openMusic() {
16         var path = '.music-content';
17         
18         // Show the music bubble
19         showBubble(path);
20         
21         $(document).oneTime(10, function() {
22                 $(path + ' input').focus();
23         });
24         
25         return false;
26 }
27
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;
33         
34         // Create the result container
35         if(!exists(path_type)) {
36                 var code = '<div class="' + type + '"></div>';
37                 
38                 if(type == 'local')
39                         $(content).prepend(code);
40                 else
41                         $(content).append(code);
42         }
43         
44         // Fill the results
45         $(xml).find('track').each(function() {
46                 // Parse the XML
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();
54                 
55                 // No ID?
56                 if(!id)
57                         id = hex_md5(uri);
58                 
59                 // No MIME?
60                 if(!mime)
61                         mime = 'audio/ogg';
62                 
63                 // Local URL?
64                 if(type == 'local')
65                         uri = generateURL(uri);
66                 
67                 // Append the HTML code
68                 $(path_type).append('<a href="#" class="song" data-id="' + id + '">' + title + '</a>');
69                 
70                 // Current playing song?
71                 var current_song = $(path_type + ' a[data-id=' + id + ']');
72                 
73                 if(exists('.music-audio[data-id=' + id + ']'))
74                         current_song.addClass('playing');
75                 
76                 // Click event
77                 current_song.click(function() {
78                         return addMusic(id,  title, artist, source, duration, uri, mime, type);
79                 });
80         });
81         
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();
87                 
88                 // Enable the input
89                 $(path + 'input').val('').removeAttr('disabled');
90                 
91                 // No result
92                 if(!jamendo && !local)
93                         $(path + '.no-results').show();
94                 
95                 // We must put a separator between the categories
96                 if(jamendo && local)
97                         $(content + ' .local').addClass('special');
98         }
99 }
100
101 // Sends the music search requests
102 function searchMusic() {
103         var path = '.music-content ';
104         
105         // We get the input string
106         var string = $(path + 'input').val();
107         
108         // We lock the search input
109         $(path + 'input').attr('disabled', true);
110         
111         // We reset the results
112         $(path + '.list div').remove();
113         $(path + '.no-results').hide();
114         
115         // Get the Jamendo results
116         $.get('./php/music-search.php', {searchquery: string, location: 'jamendo'}, function(data) {
117                 parseMusic(data, 'jamendo');
118         });
119         
120         // Get the local results
121         $.get('./php/music-search.php', {searchquery: string, location: JAPPIX_LOCATION}, function(data) {
122                 parseMusic(data, 'local');
123         });
124 }
125
126 // Performs an action on the music player
127 function actionMusic(action) {
128         try {
129                 // Initialize
130                 var playThis = document.getElementById('top-content').getElementsByTagName('audio')[0];
131                 
132                 // Nothing to play, exit
133                 if(!playThis)
134                         return false;
135                 
136                 var stopButton = $('#top-content a.stop');
137                 
138                 // User play a song
139                 if(action == 'play') {
140                         stopButton.show();
141                         playThis.load();
142                         playThis.play();
143                         playThis.addEventListener('ended', function() {
144                                 actionMusic('stop');
145                         }, true);  
146                         
147                         logThis('Music is now playing.');
148                 }
149                 
150                 // User stop the song or the song came to its end
151                 else if(action == 'stop') {
152                         stopButton.hide();
153                         playThis.pause();
154                         $('#top-content .music').removeClass('actived');
155                         $('.music-content .list a').removeClass('playing');
156                         $('.music-audio').remove();
157                         publishMusic();
158                         
159                         logThis('Music is now stopped.');
160                 }
161         }
162         
163         catch(e) {}
164         
165         finally {
166                 return false;
167         }
168 }
169
170 // Publishes the current title over PEP
171 function publishMusic(title, artist, source, duration, uri) {
172         // We share the tune on PEP if enabled
173         if(enabledPEP()) {
174                 /* REF: http://xmpp.org/extensions/xep-0118.html */
175                 
176                 var iq = new JSJaCIQ();
177                 iq.setType('set');
178                 
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}));
184                 
185                 // Enough data?
186                 if(title || artist || source || uri) {
187                         // Data array
188                         var nodes = new Array(
189                                         'title',
190                                         'artist',
191                                         'source',
192                                         'length',
193                                         'uri'
194                                     );
195                         
196                         var values = new Array(
197                                         title,
198                                         artist,
199                                         source,
200                                         length,
201                                         uri
202                                      );
203                         
204                         // Create the children nodes
205                         for(i in nodes) {
206                                 if(values[i])
207                                         tune.appendChild(iq.buildNode(nodes[i], {'xmlns': NS_TUNE}, values[i]));
208                         }
209                 }
210                 
211                 con.send(iq);
212                 
213                 logThis('New tune sent: ' + title, 3);
214         }
215 }
216
217 // Adds a music title to the results
218 function addMusic(id, title, artist, source, duration, uri, mime, type) {
219         var path = '.music-content ';
220         
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 + '" />');
224         
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');
228         else
229                 $('.music-audio').attr('src', uri);
230         
231         // We play the target sound
232         actionMusic('play');
233         
234         // We set the actived class
235         $('#top-content .music').addClass('actived');
236         
237         // We set a current played track indicator
238         $(path + '.list a').removeClass('playing');
239         $(path + 'a[data-id=' + id + ']').addClass('playing');
240         
241         // We publish what we listen
242         publishMusic(title, artist, source, duration, uri);
243         
244         return false;
245 }
246
247 // Addon launcher
248 function launchMusic() {
249         // When music search string submitted
250         $('.music-content input').keyup(function(e) {
251                 // Enter : send
252                 if(e.keyCode == 13 && $(this).val())
253                         searchMusic();
254                 
255                 // Escape : quit
256                 if(e.keyCode == 27)
257                         closeBubbles();
258         });
259 }