more work on js-rewrite
[enigma2-plugins.git] / webinterface / src / web-data / provider.js
1 /**
2  * AbstractContentProvider
3  * 
4  * Abstract Class for "AbstractContentProvider" Classes 
5  * A Content handler is a class that provides content for the webpage 
6  * e.g. a list of channels, or a list of recordings and/or is able of
7  * doing the handling of all events for that content (e.g. deleting a recording)
8  */
9
10 var AbstractContentProvider = Class.create(AjaxThing, {
11         /**
12          * initialize
13          * Default constructor
14          * Parameters:
15          * @tpl - Name of the template to use
16          * @url - name of the url to use for requests
17          * @target - target html id for the content
18          * @onFinished - an array of functions that should be called after "this.show()" has finished
19          */
20         initialize: function(url, showFnc, onFinished){
21                 this.url = url;
22                 this.request = '';
23                 this.onFinished = onFinished;
24                 this.show = showFnc;
25                 this.parms = {};
26                 this.refresh = false;
27                 this.eventsRegistered = false;
28         },
29                 
30         /**
31          * getXML
32          * Converts the incoming transport result into a DOM object
33          * Parameters:
34          * @transport - the xmlhttp transport object
35          * 
36          **/
37         getXML: function(transport){
38                 var xmlDoc = "";
39
40                 if(window.ActiveXObject){ // we're on IE
41                         xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
42                         xmlDoc.async="false";
43                         xmlDoc.loadXML(transport.responseText);
44                 } else { //we're not on IE
45                         if (!window.google || !google.gears){
46                                 xmlDoc = transport.responseXML;
47                         } else { //no responseXML on gears
48                                 xmlDoc = (new DOMParser()).parseFromString(transport.responseText, "text/xml");
49                         }
50                 }
51
52                 return xmlDoc;
53         },
54         
55         /**
56          * renderXML
57          * renders the XML and returns what's required by this.show();
58          */
59         renderXML: function(xml){
60                 debug('[AbstractContentProvider] ERROR: renderXML not implemented in derived class!');
61                 return {};
62         },
63         
64         /**
65          * callback
66          * The default function that is being called for the onSuccess event of this.load();
67          * Parameters:
68          * @transport - the xmlhttp transport object
69          **/
70         callback: function(transport){
71                 var data = this.renderXML(this.getXML(transport));
72                 this.show(data);
73         },
74         
75         /**
76          * errorback
77          * The default function that is being called for the onError event of this.load();
78          * Parameters
79          * @transport - the xmlhttp transport object
80          */
81         errorback: function(transport){
82                 var notif = "Request failed for:  " + transport.request.url + "<br>Status: " + transport.status + " " + transport.statusText;
83                 notify(notif, false);
84         },
85         
86         /**
87          * load
88          * Calls this.getURL 
89          * Parameters
90          * @parms - an json object containing  {parameter : value} pairs for the request
91          * @fnc - function to replace this.callback (which is being called @ onSuccess)
92          */
93         load: function(parms, fnc){
94                 this.parms = parms;
95                 if(fnc !== undefined){
96                         this.callback = fnc;
97                 }
98                 
99                 this.getUrl(this.url, parms, this.callback.bind(this), this.errorback.bind(this));
100         },
101         
102         registerEvents : function(){
103                 debug('[AbstractContentProvider] WARNING: registerEvents not implemented in derived class!');
104         },
105         
106         /**
107          * finished
108          * Calls all functions this.onFinished contains this.registerEvents
109          * Is usually called after this.show() has finished
110          */
111         finished : function(){
112                 if(!this.eventsRegistered){
113                         this.registerEvents();
114                         this.eventsRegistered = true;
115                 }
116                 
117                 if(this.onFinished !== undefined){
118                         for(var i = 0; i < this.onFinished.length; i++){
119                                 var fnc = this.onFinished[i];
120                                 if(typeof(fnc) === 'function'){
121                                         fnc();
122                                 }
123                         }
124                 }
125         },
126         
127         /**
128          * reload
129          * rexecute this.load() using this.parms and set this.refresh to false
130          * Parameters:
131          * @fnc - function to call @ onSuccess (passed through to this.load() )
132          */     
133         reload: function(fnc){
134                 this.refresh = false;
135                 this.load(this.parms, fnc);
136         },
137         
138         /**
139          * simpleResultQuery
140          * Call any URL that returns a SimpleXMLResult with this.simpleResultCallback for
141          * @onSuccess
142          * Parameters:
143          * @url - the url to call
144          * @parms - an json object containing  {parameter : value} pairs for the request
145          */
146         simpleResultQuery: function(url, parms, callback){
147                 this.getUrl(url, parms, callback);              
148         },
149         
150         /**
151          * simpleResultCallback
152          * Callback for @ onSuccess of this.simpleResultQuery()
153          * if this.refresh == true this.reload is being called
154          * Parameters:
155          * @transport - the xmlhttp transport object
156          */
157         simpleResultCallback: function(transport, callback){
158                 var result = this.simpleResultRenderXML(this.getXML(transport));
159                 if(typeof(callback) == "function"){
160                         callback(result);
161                 }
162                 
163                 if(this.refresh){
164                         this.reload();
165                 }
166                 
167         },
168         
169         /**
170          * simpleResultRenderXML
171          * Renders the result of this.simpleResultQuery() and returns an SimpleXMLResult object for it
172          * Parameters:
173          * @xml - a DOM object containing the XML to render
174          */
175         simpleResultRenderXML: function(xml){
176                 var result = new SimpleXMLResult(xml);
177                 return result;
178         }
179
180         
181 });
182
183 /**
184  * ServiceListProvider
185  * ContentHandler for service lists.
186  */
187 var ServiceListProvider = Class.create(AbstractContentProvider, {
188         /**
189          * initialize
190          * Parameters:
191          * @target: the html target id
192          */
193         initialize: function($super, showFnc){
194                 $super(URL.getservices, showFnc );
195         },
196         
197         /**
198          * renderXML
199          * See the description in AbstractContentProvider
200          */
201         renderXML: function(xml){
202                 var list = new ServiceList(xml).getArray();
203                 return {services : list};       
204         }
205 });
206
207 /**
208  * BouquetListProvider
209  * ContentHandler for bouquet lists.
210  */
211 var BouquetListProvider = Class.create(ServiceListProvider, {
212         /**
213          * renderXML
214          * See the description in AbstractContentProvider
215          */
216         renderXML: function(xml){
217                 var list = new ServiceList(xml).getArray();
218                 return {bouquets : list};       
219         }
220 });
221
222 var ServiceEpgListProvider = Class.create(AbstractContentProvider, {
223         /**
224          * initialize
225          * Parameters:
226          * @target: the html target id
227          */
228         initialize: function($super, showFnc){
229                 $super(URL.epgservice, showFnc );
230         },
231
232         /**
233          * renderXML
234          * See the description in AbstractContentProvider
235          */
236         renderXML: function(xml){
237                 var list = new EPGList(xml).getArray();
238                 return {epg : list};    
239         }
240 });
241
242
243 /**
244  * ServiceListEpgProvider
245  * Handles EPG now/next for a ServiceListProvider
246  */
247 var ServiceListEpgProvider = Class.create(AbstractContentProvider, {
248         //Constants
249         NOW : 'NOW',
250         NEXT : 'NEXT',
251         
252         /**
253          * initialize
254          * See the description in AbstractContentProvider
255          */     
256         initialize: function($super, showFnc){
257                 $super(URL.epgnow, showFnc);
258                 this.type = this.NOW;
259         },
260
261         /**
262          * renderXML
263          * See the description in AbstractContentProvider
264          */
265         renderXML: function(xml){
266                 var list = new EPGList(xml).getArray();
267                 return list;
268         },
269         
270         /**
271          * callback
272          * custom callback
273          * Parameters:
274          * @transport - xmlhttp transport object
275          */
276         callback: function(transport){
277                 var data = this.renderXML(this.getXML(transport));
278                 this.show(data, this.type);
279                 if(this.callbackType !== undefined){
280                         this.get(this.callbackType);
281                 }
282         },      
283         
284         /**
285          * getNowNext
286          * call this.get to show epg-now and epg-next
287          * Parameters:
288          * @parms - an json object containing  {parameter : value} pairs for the request
289          */
290         getNowNext: function(parms){
291                 this.parms = parms;
292                 this.get(this.NOW, this.NEXT);          
293         },
294         
295         /**
296          * get
297          * Load epg information for type and - if set - callbackType 
298          * (ServiceListEpgProvider.NOW or ServiceListEpgProvider.NEXT)
299          * Parameters:
300          * @type - ServiceListEpgProvider.NOW or ServiceListEpgProvider.NEXT
301          * @callbackType - ServiceListEpgProvider.NOW or ServiceListEpgProvider.NEXT
302          */
303         get: function(type, callbackType){              
304                 this.type = type;
305                 //just in case... don't do it twice...
306                 if(type != callbackType){
307                         this.callbackType = callbackType;
308                 }
309                 
310                 switch(this.type){
311                         case this.NOW:
312                                 this.url = URL.epgnow;
313                                 break;
314                         case this.NEXT:
315                                 this.url = URL.epgnext;
316                                 break;
317                 }
318                 
319                 this.load(this.parms);
320         }
321 });
322
323 /**
324  * ServiceListSubserviceProvider
325  * Handles EPG now/next for a ServiceListProvider
326  */
327 var ServiceListSubserviceProvider = Class.create(AbstractContentProvider, {
328         /**
329          * initialize
330          * See the description in AbstractContentProvider
331          */             
332         initialize: function($super, showFnc){
333                 $super(URL.subservices, showFnc);
334         },
335
336         /**
337          * renderXML
338          * See the description in AbstractContentProvider
339          */             
340         renderXML: function(xml){
341                 var list = new ServiceList(xml).getArray();
342                 return list;
343         }
344 });
345
346 /**
347  * MovieListProvider
348  * Handles a list of movies including deleting actions
349  */
350 var MovieListProvider = Class.create(AbstractContentProvider, {
351         /**
352          * initialize
353          * See the description in AbstractContentProvider
354          */
355         initialize: function($super, showFnc){
356                 $super(URL.movielist, showFnc);         
357         },
358         
359         /**
360          * renderXML
361          * See the description in AbstractContentProvider
362          */     
363         renderXML: function(xml){
364                 var list = new MovieList(xml).getArray();
365                 return {movies : list};
366         }
367 });
368
369 var TimerListProvider = Class.create(AbstractContentProvider, {
370         /**
371          * initialize
372          * See the description in AbstractContentProvider
373          */
374         initialize: function($super, showFnc){
375                 $super(URL.timerlist, showFnc);
376         },
377         
378         /**
379          * renderXML
380          * See the description in AbstractContentProvider
381          */     
382         renderXML: function(xml){
383                 var list = new TimerList(xml).getArray();
384                 return {timer : list};
385         }
386 });