InternetRadio: fix webinterface (follow latest webinterface changes)
[enigma2-plugins.git] / internetradio / src / web-data / internetradio.js
1 URL.internetradioFavorites = "/internetradio/web/getfavoriteslist";
2 URL.internetradioAddFavorite = "/internetradio/web/addfavorite";
3 URL.internetradioRemoveFavorite = "/internetradio/web/removefavorite";
4 URL.internetradioRenameFavorite = "/internetradio/web/renamefavorite";
5 URL.internetradioStatus= "/internetradio/web/status";
6 URL.internetradioPlay = "/internetradio/web/play?";
7 URL.internetradioStop = "/internetradio/web/stopplaying";
8
9 var InternetRadioFavorite = Class.create({
10         initialize: function(xml){
11                 this.name = getNodeContent(xml, 'e2favoritename', "");
12                 this.text = getNodeContent(xml, 'e2favoritetext', "");
13                 this.type = getNodeContent(xml, 'e2favoritetype', "");
14                 this.tags = getNodeContent(xml, 'e2favoritetags', "").split(' ');
15                 this.country = getNodeContent(xml, 'e2favoritecountry');
16                 this.homepage = getNodeContent(xml, 'e2favoritehomepage', "");
17
18                 this.json = {
19                         'name' : this.name,
20                         'text' : this.text,
21                         'type' : this.type,
22                         'tags' : this.tags,
23                         'country' : this.country,
24                         'homepage' : this.homepage
25                 };
26         },
27
28         toJSON: function(){
29                 return this.json;
30         }
31
32 });
33
34 var InternetRadioFavoriteList = Class.create({
35         initialize: function(xml){
36                 this.list = this.parse(xml);
37         },
38
39         parse: function(xml){
40                 var items = xml.getElementsByTagName("e2internetradio_favorite_item");
41                 var list = [];
42                 var len = items.length;
43                 for(var i = 0; i < len; i++){
44                         var fav = new InternetRadioFavorite(items[i]).toJSON();
45                         list.push(fav);
46                 }
47                 return list;
48         },
49
50         getArray: function(){
51                 return this.list;
52         }
53 });
54
55 /**
56  * MovieListProvider
57  * Handles a list of movies including deleting actions
58  */
59 var InternetRadioFavoritesProvider = Class.create(AbstractContentProvider, {
60         /**
61          * initialize
62          * See the description in AbstractContentProvider
63          */
64         initialize: function($super, showFnc){
65                 $super(URL.internetradioFavorites, showFnc);
66         },
67
68         /**
69          * renderXML
70          * See the description in AbstractContentProvider
71          */
72         renderXML: function(xml){
73                 var list = new InternetRadioFavoriteList(xml).getArray();
74                 return {favorites : list};
75         }
76 });
77
78 var radioTemplateEngine = new TemplateEngine('/internetradio/tpl/')
79 var AbstractRadioContentHandler = Class.create(AbstractContentHandler,  {
80         show: function(data){
81                 this.data = data;
82                 radioTemplateEngine.process(this.tpl, data, this.target, this.finished.bind(this));
83         }
84 });
85
86 var InternetRadioFavoritesHandler  = Class.create(AbstractRadioContentHandler, {
87         initialize: function($super, target, statusTarget, stopTarget){
88                 $super('tplFavorites', target);
89                 this.provider = new InternetRadioFavoritesProvider(this.show.bind(this));
90                 this.ajaxload = true;
91                 this.refresh = false;
92                 this.statusTarget = statusTarget;
93                 this.stopTarget = stopTarget;
94         },
95         getData: function(element){
96                 var fav = {
97                         name: unescape(element.readAttribute('data-name')),
98                         text: unescape(element.readAttribute('data-text')),
99                         type: unescape(element.readAttribute('data-type')),
100                         tags: unescape(element.readAttribute('data-tags')),
101                         country: unescape(element.readAttribute('data-country')),
102                         homepage: unescape(element.readAttribute('data-homepage'))
103                 };
104                 return fav;
105         },
106
107         add: function(form){
108                 var values = form.serialize(true);
109                 this.provider.refresh = true;
110                 this.provider.simpleResultQuery(
111                         URL.internetradioAddFavorite,
112                         {
113                                 name : values.name,
114                                 text: values.text,
115                                 favoritetype : values.favoritetype,
116                                 tags : values.tags,
117                                 country : values.country,
118                                 homepage : values.homepage
119                         },
120                         this.simpleResultCallback.bind(this)
121                 );
122         },
123         
124         rename: function(form){
125                 var values = form.serialize(true);
126                 
127                 this.provider.refresh = false
128                 if(values.newname != null && values.newname != ""  && values.newname != values.name){
129                         this.provider.refresh = true;
130                         this.provider.simpleResultQuery(
131                                 URL.internetradioRenameFavorite, 
132                                 {
133                                         name : values.name, 
134                                         text: values.text, 
135                                         favoritetype : values.favoritetype, 
136                                         newname : values.newname
137                                 },
138                                 this.simpleResultCallback.bind(this)
139                         );
140                 }
141         },
142         
143         remove: function(element){
144                 favorite = this.getData(element);
145
146                 var result = confirm( "Are you sure want to remove the internet Radiostation\n\n" +
147                                 favorite.name + "\n\n" + 
148                                 " from your favorites?");
149
150                 this.provider.refresh = result;
151                 if(result){
152                         this.provider.simpleResultQuery(
153                                 URL.internetradioRemoveFavorite, 
154                                 {
155                                         name : favorite.name,
156                                         text: favorite.text,
157                                         favoritetype :favorite.type
158                                 },
159                                 this.simpleResultCallback.bind(this)
160                         );
161                 }
162         },
163         
164         play: function(element) {
165                 favorite = this.getData(element);
166                 this.provider.simpleResultQuery(
167                         URL.internetradioPlay, 
168                         {
169                                 name : favorite.name,
170                                 url: favorite.text
171                         },
172                         this.simpleResultCallback.bind(this)
173                 );
174         },
175         
176         quickPlay: function(form){
177                 var values = form.serialize(true);
178                 this.provider.simpleResultQuery(
179                         URL.internetradioPlay, 
180                         {
181                                 name : values.name,
182                                 url: values.text
183                         },
184                         this.simpleResultCallback.bind(this)
185                 );
186         },
187         
188         stop: function(){
189                 this.provider.simpleResultQuery(
190                         URL.internetradioStop, 
191                         {},
192                         this.simpleResultCallback.bind(this)
193                 );
194         },
195         
196         getStatus: function(){
197                 this.provider.simpleResultQuery(
198                         URL.internetradioStatus, 
199                         {},
200                         this.onStatusReady.bind(this)
201                 );
202         },
203         
204         onStatusReady: function(transport){
205                 var result = this.provider.simpleResultRenderXML(this.provider.getXML(transport));
206                 $(this.statusTarget).update(result.getStateText());
207                 var stop = $(this.stopTarget);
208                 if(result.getState()){
209                         $(this.stopTarget).show();
210                 } else {
211                         $(this.stopTarget).hide();
212                 }
213         }
214         
215 });
216
217 var Controller = Class.create({
218         initialize: function(handler){
219                 this.handler = handler;
220                 this.handler.onFinished.push(this.registerEvents.bind(this));
221                 this.handler.onFinished.push(this.onFinished.bind(this));
222                 this.eventsregistered = false;
223         },
224
225         registerEvents: function(){
226                 this.eventsregistered = true;
227         },
228
229         onFinished: function(){}
230 });
231
232 var InternetRadio = Class.create(Controller, {
233         initialize: function($super, target, statusTarget, stopTarget){
234                 $super(new InternetRadioFavoritesHandler(target, statusTarget, stopTarget));
235         },
236         
237         load: function(){
238                 this.handler.load();
239         },
240
241         play: function(element){
242                 this.handler.play(element);
243         },
244         
245         quickPlay: function(form){
246                 this.handler.quickPlay(form);
247         },
248
249         stop: function(element){
250                 this.handler.stop(element);
251         },
252
253         add: function(form){
254                 this.handler.add(form);
255         },
256
257         remove: function(element){
258                 this.handler.remove(element);
259         },
260
261         rename: function(element){
262                 this.handler.rename(element);
263         },
264         
265         getStatus: function(){
266                 this.handler.getStatus();
267         }
268 });
269
270 var Radio = Class.create({
271         initialize: function(){
272                 this.internetRadio = new InternetRadio('contentMain', 'currentText', 'stop');
273                 this.sessionProvider = new SessionProvider( this.onSessionAvailable.bind(this) );
274                 this.poller = null;
275         },
276         
277         run: function(){
278                 debug("[Radio].run");
279                 this.sessionProvider.load({});
280         },
281
282         onSessionAvailable: function(sid){
283                 debug("[Radio].onSessionAvailable, " + sid)
284                 global_sessionid = sid;
285
286                 this.internetRadio.load();
287                 this.registerListeners();
288                 this.pollStatus();
289         },
290         
291         pollStatus: function(){
292                 if(this.poller == null){
293                         var _this = this;
294                         this.poller = setInterval(_this.pollStatus.bind(this), 5000);
295                 }
296                 this.internetRadio.getStatus();
297         },
298
299         registerListeners: function(){
300                 var content =  $('contentMain');
301                 content.on(
302                         'click',
303                         '.item',
304                         function(event,element){
305                                 event.stop();
306                                 this.internetRadio.play(element);
307                         }.bind(this)
308                 );
309                 content.on(
310                         'click',
311                         '.edit',
312                         function(event, element){
313                                 event.stop();
314                                 var edit = element.up('.listItem').down('.editEntry');
315                                 if(edit.visible())
316                                         edit.hide();
317                                 else
318                                         edit.show();
319                         }
320                 );
321                 content.on(
322                         'change',
323                         '.removeConfirm',
324                         function(event, element){
325                                 event.stop();
326                                 var button = element.next();
327                                 if(element.checked){
328                                         button.enable();
329                                 } else {
330                                         button.disable();
331                                 }
332                         }
333                 );
334                 content.on(
335                         'click',
336                         '.removeFav',
337                         function(event, element){
338                                 event.stop();
339                                 var entry = element.up('.listItem').down('.irListEntry');
340                                 this.internetRadio.remove(entry);
341                         }.bind(this)
342                 );
343                 
344                 content.on(
345                         'click',
346                         '.renameFav',
347                         function(event, element){
348                                 event.stop();
349                                 var id = element.readAttribute("data-form-id");
350                                 var form = $(id);
351                                 this.internetRadio.rename(form);
352                         }.bind(this)
353                 );
354                 content.on(
355                         'click',
356                         '.quickplay',
357                         function(event, element){
358                                 event.stop();
359                                 var form = $('new');
360                                 this.internetRadio.quickPlay(form);
361                         }.bind(this)
362                 );
363                 content.on(
364                         'click',
365                         '.savenew',
366                         function(event, element){
367                                 event.stop();
368                                 var form = $('new');
369                                 this.internetRadio.add(form);
370                         }.bind(this)
371                 );
372                 $('stop').on(
373                         'click',
374                         function(event, element){
375                                 event.stop();
376                                 this.internetRadio.stop();
377                         }.bind(this)
378                 );
379                 $('newStation').on(
380                         'click',
381                         function(event, element){
382                                 event.stop();
383                                 var form = $('new');
384                                 if(form.visible()){
385                                         form.hide();
386                                 } else {
387                                         form.show();
388                                         $('contentMain').scrollTop = 0;
389                                 }
390                         }.bind(this)
391                 );
392         }
393 });
394 var radio = new Radio();
395