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