]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
category support for OPML export (fix again)
[tt-rss.git] / tt-rss.js
1 /*
2         This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>              
3         Licensed under GPL v.2 or (at your preference) any later version.
4 */
5
6 var xmlhttp = false;
7
8 var total_unread = 0;
9 var first_run = true;
10
11 var display_tags = false;
12
13 var global_unread = 0;
14
15 var active_title_text = "";
16
17 var current_subtitle = "";
18
19 /*@cc_on @*/
20 /*@if (@_jscript_version >= 5)
21 // JScript gives us Conditional compilation, we can cope with old IE versions.
22 // and security blocked creation of the objects.
23 try {
24         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
25 } catch (e) {
26         try {
27                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
28         } catch (E) {
29                 xmlhttp = false;
30         }
31 }
32 @end @*/
33
34 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
35         xmlhttp = new XMLHttpRequest();
36 }
37
38 function toggleTags() {
39         display_tags = !display_tags;
40
41         var p = document.getElementById("dispSwitchPrompt");
42
43         if (display_tags) {
44                 p.innerHTML = "display feeds";
45         } else {
46                 p.innerHTML = "display tags";
47         }
48         
49         updateFeedList();
50 }
51
52 function dlg_frefresh_callback() {
53         if (xmlhttp.readyState == 4) {
54                 updateFeedList(false, false);
55                 closeDlg();
56         } 
57 }
58
59 function dialog_refresh_callback() {
60         if (xmlhttp.readyState == 4) {
61                 var dlg = document.getElementById("userDlg");
62
63                 dlg.innerHTML = xmlhttp.responseText;
64                 dlg.style.display = "block";
65         } 
66 }
67
68 function refetch_callback() {
69         if (xmlhttp.readyState == 4) {
70
71 //              document.title = "Tiny Tiny RSS";
72
73                 if (!xmlhttp.responseXML) {
74                         notify("refetch_callback: backend did not return valid XML");
75                         return;
76                 }
77         
78                 var reply = xmlhttp.responseXML.firstChild;
79
80                 if (!reply) {
81                         notify("refetch_callback: backend did not return expected XML object");
82                         return;
83                 } 
84
85                 var error_code = reply.getAttribute("error-code");
86         
87                 if (error_code && error_code != 0) {
88                         return fatalError(error_code);
89                 }
90
91                 var f_document = window.frames["feeds-frame"].document;
92
93                 for (var l = 0; l < reply.childNodes.length; l++) {
94                         var id = reply.childNodes[l].getAttribute("id");
95                         var ctr = reply.childNodes[l].getAttribute("counter");
96
97                         var feedctr = f_document.getElementById("FEEDCTR-" + id);
98                         var feedu = f_document.getElementById("FEEDU-" + id);
99                         var feedr = f_document.getElementById("FEEDR-" + id);
100
101 /*                      TODO figure out how to update this from viewfeed.js->view()
102                         disabled for now...
103
104                         if (id == "global-unread") {
105                                 global_unread = ctr;
106                         } */
107
108                         if (feedctr && feedu && feedr) {
109
110                                 feedu.innerHTML = ctr;
111         
112                                 if (ctr > 0) {
113                                         feedctr.className = "odd";
114                                         if (!feedr.className.match("Unread")) {
115                                                 feedr.className = feedr.className + "Unread";
116                                         }
117                                 } else {
118                                         feedctr.className = "invisible";
119                                         feedr.className = feedr.className.replace("Unread", "");
120                                 }
121                         }
122                 }  
123
124                 updateTitle("");
125                 notify("All feeds updated.");
126
127         }
128 }
129
130 function backend_sanity_check_callback() {
131
132         if (xmlhttp.readyState == 4) {
133                 
134                 if (!xmlhttp.responseXML) {
135                         fatalError(3);
136                         return;
137                 }
138
139                 var reply = xmlhttp.responseXML.firstChild;
140
141                 if (!reply) {
142                         fatalError(3);
143                         return;
144                 }
145
146                 var error_code = reply.getAttribute("error-code");
147         
148                 if (error_code && error_code != 0) {
149                         return fatalError(error_code);
150                 }
151
152                 init_second_stage();
153         } 
154 }
155
156 /* wtf this is obsolete
157 function updateFeed(feed_id) {
158
159         var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
160
161         if (xmlhttp_ready(xmlhttp)) {
162                 xmlhttp.open("GET", query_str, true);
163                 xmlhttp.onreadystatechange=feed_update_callback;
164                 xmlhttp.send(null);
165         } else {
166                 printLockingError();
167         }   
168
169 }
170 */
171
172 function scheduleFeedUpdate(force) {
173
174         notify("Updating feeds in background...");
175
176 //      document.title = "Tiny Tiny RSS - Updating...";
177
178         updateTitle("Updating");
179
180         var query_str = "backend.php?op=rpc&subop=";
181
182         if (force) {
183                 query_str = query_str + "forceUpdateAllFeeds";
184         } else {
185                 query_str = query_str + "updateAllFeeds";
186         }
187
188         var omode;
189
190         if (display_tags) {
191                 omode = "t";
192         } else {
193                 omode = "fl";
194         }
195
196         query_str = query_str + "&omode=" + omode;
197
198         if (xmlhttp_ready(xmlhttp)) {
199                 xmlhttp.open("GET", query_str, true);
200                 xmlhttp.onreadystatechange=refetch_callback;
201                 xmlhttp.send(null);
202         } else {
203                 printLockingError();
204         }   
205 }
206
207 function updateFeedList(silent, fetch) {
208
209 //      if (silent != true) {
210 //              notify("Loading feed list...");
211 //      }
212
213         var query_str = "backend.php?op=feeds";
214
215         if (display_tags) {
216                 query_str = query_str + "&tags=1";
217         }
218
219         if (getActiveFeedId()) {
220                 query_str = query_str + "&actid=" + getActiveFeedId();
221         }
222
223         if (fetch) query_str = query_str + "&fetch=yes";
224
225         var feeds_frame = document.getElementById("feeds-frame");
226
227         feeds_frame.src = query_str;
228 }
229
230 function catchupAllFeeds() {
231
232         var query_str = "backend.php?op=feeds&subop=catchupAll";
233
234         notify("Marking all feeds as read...");
235
236         var feeds_frame = document.getElementById("feeds-frame");
237
238         feeds_frame.src = query_str;
239
240         global_unread = 0;
241         updateTitle("");
242
243 }
244
245 function viewCurrentFeed(skip, subop) {
246
247         if (getActiveFeedId()) {
248                 viewfeed(getActiveFeedId(), skip, subop);
249         } else {
250                 disableContainerChildren("headlinesToolbar", false, document);
251                 viewfeed(-1, skip, subop); // FIXME
252         }
253 }
254
255 function viewfeed(feed, skip, subop) {
256         var f = window.frames["feeds-frame"];
257         f.viewfeed(feed, skip, subop);
258 }
259
260 function timeout() {
261         scheduleFeedUpdate(false);
262         setTimeout("timeout()", 1800*1000);
263 }
264
265 function resetSearch() {
266         var searchbox = document.getElementById("searchbox")
267
268         if (searchbox.value != "" && getActiveFeedId()) {       
269                 searchbox.value = "";
270                 viewfeed(getActiveFeedId(), 0, "");
271         }
272 }
273
274 function search() {
275         viewCurrentFeed(0, "");
276 }
277
278 function localPiggieFunction(enable) {
279         if (enable) {
280                 var query_str = "backend.php?op=feeds&subop=piggie";
281
282                 if (xmlhttp_ready(xmlhttp)) {
283
284                         xmlhttp.open("GET", query_str, true);
285                         xmlhttp.onreadystatechange=feedlist_callback;
286                         xmlhttp.send(null);
287                 }
288         }
289 }
290
291 function localHotkeyHandler(keycode) {
292
293 /*      if (keycode == 78) {
294                 return moveToPost('next');
295         }
296
297         if (keycode == 80) {
298                 return moveToPost('prev');
299         } */
300
301         if (keycode == 82) { // r
302                 return scheduleFeedUpdate(true);
303         }
304
305         if (keycode == 85) { // u
306                 if (getActiveFeedId()) {
307                         return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
308                 }
309         }
310
311         if (keycode == 65) { // a
312                 return toggleDispRead();
313         }
314
315 //      notify("KC: " + keycode);
316
317 }
318
319 function updateTitle(s) {
320         var tmp = "Tiny Tiny RSS";
321
322         if (s && s.length > 0) {
323                 current_subtitle = s;
324         }
325
326         if (global_unread > 0) {
327                 tmp = tmp + " (" + global_unread + ")";
328         }
329
330         if (s) {
331                 tmp = tmp + " - " + current_subtitle;
332         }
333
334         if (active_title_text.length > 0) {
335                 tmp = tmp + " > " + active_title_text;
336         }
337
338         document.title = tmp;
339 }
340
341 function genericSanityCheck() {
342
343         if (!xmlhttp) fatalError(1);
344
345         setCookie("ttrss_vf_test", "TEST");
346         
347         if (getCookie("ttrss_vf_test") != "TEST") {
348                 fatalError(2);
349         }
350
351 /*      if (!xmlhttp) {
352                 document.getElementById("headlines").innerHTML = 
353                         "<b>Fatal error:</b> This program requires XmlHttpRequest " + 
354                         "to function properly. Your browser doesn't seem to support it.";
355                 return false;
356         }
357
358         setCookie("ttrss_vf_test", "TEST");
359         if (getCookie("ttrss_vf_test") != "TEST") {
360
361                 document.getElementById("headlines").innerHTML = 
362                         "<b>Fatal error:</b> This program requires cookies " + 
363                         "to function properly. Your browser doesn't seem to support them.";
364
365                 return false;
366         } */
367
368         return true;
369 }
370
371 function init() {
372
373         disableContainerChildren("headlinesToolbar", true);
374
375         if (!genericSanityCheck()) 
376                 return;
377
378         xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
379         xmlhttp.onreadystatechange=backend_sanity_check_callback;
380         xmlhttp.send(null);
381
382 }
383
384 function init_second_stage() {
385
386         setCookie("ttrss_vf_actfeed", "");
387
388         updateFeedList(false, false);
389         document.onkeydown = hotkey_handler;
390
391         var content = document.getElementById("content");
392
393         if (getCookie("ttrss_vf_vmode")) {
394                 var viewbox = document.getElementById("viewbox");
395                 viewbox.value = getCookie("ttrss_vf_vmode");
396         }
397
398         if (getCookie("ttrss_vf_limit")) {
399                 var limitbox = document.getElementById("limitbox");
400                 limitbox.value = getCookie("ttrss_vf_limit");
401         }
402
403 //      if (getCookie("ttrss_vf_actfeed")) {
404 //              viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
405 //      }
406
407 //      setTimeout("timeout()", 2*1000);
408 //      scheduleFeedUpdate(true);
409
410         var splash = document.getElementById("splash");
411
412         if (splash) {
413                 splash.style.display = "none";
414         }
415
416 }
417
418 function quickMenuGo() {
419
420         var chooser = document.getElementById("quickMenuChooser");
421         var opid = chooser[chooser.selectedIndex].id;
422
423         if (opid == "qmcPrefs") {
424                 gotoPreferences();
425         }
426
427         if (opid == "qmcAdvSearch") {
428                 displayDlg("search");
429                 return;
430         }
431
432         if (opid == "qmcAddFeed") {
433                 displayDlg("quickAddFeed");
434                 return;
435         }
436
437         if (opid == "qmcRemoveFeed") {
438                 var actid = getActiveFeedId();
439
440                 if (!actid) {
441                         notify("Please select some feed first.");
442                         return;
443                 }
444         
445                 displayDlg("quickDelFeed", actid);
446                 return;
447         }
448
449         if (opid == "qmcUpdateFeeds") {
450                 scheduleFeedUpdate(true);
451                 return;
452         }
453
454         if (opid == "qmcCatchupAll") {
455                 catchupAllFeeds();
456                 return;
457         }
458
459         if (opid == "qmcShowOnlyUnread") {
460                 toggleDispRead();
461                 return;
462         }
463
464 }
465
466 function qafAdd() {
467
468         if (!xmlhttp_ready(xmlhttp)) {
469                 printLockingError();
470                 return
471         }
472
473         var link = document.getElementById("qafInput");
474
475         if (link.value.length == 0) {
476                 notify("Missing feed URL.");
477         } else {
478                 notify("Adding feed...");
479                 
480                 var feeds_doc = window.frames["feeds-frame"].document;
481
482                 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
483
484                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
485                         param_escape(link.value), true);
486                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
487                 xmlhttp.send(null);
488
489                 link.value = "";
490
491         }
492 }
493
494 function displayDlg(id, param) {
495
496         notify("");
497
498         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
499                 param_escape(id) + "&param=" + param_escape(param), true);
500         xmlhttp.onreadystatechange=dialog_refresh_callback;
501         xmlhttp.send(null);
502
503 }
504
505 function closeDlg() {
506         var dlg = document.getElementById("userDlg");
507         dlg.style.display = "none";
508 }
509
510 function qfdDelete(feed_id) {
511
512         notify("Removing feed...");
513
514         var feeds_doc = window.frames["feeds-frame"].document;
515         feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
516
517         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids=" + feed_id);
518         xmlhttp.onreadystatechange=dlg_frefresh_callback;
519         xmlhttp.send(null);
520 }
521
522
523 function allFeedsMenuGo() {
524         var chooser = document.getElementById("allFeedsChooser");
525
526         var opname = chooser[chooser.selectedIndex].text;
527
528         if (opname == "Update") {
529                 scheduleFeedUpdate(true);
530                 return;
531         }
532
533         if (opname == "Mark as read") {
534                 catchupAllFeeds();
535                 return;
536         }
537
538         if (opname == "Show only unread") {
539                 toggleDispRead();
540                 return;
541         }
542
543 }
544
545 function updateFeedTitle(t) {
546         active_title_text = t;
547         updateTitle();
548 }
549
550 function toggleDispRead() {
551         var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
552
553         hide_read_feeds = !hide_read_feeds;
554
555         var feeds_doc = window.frames["feeds-frame"].document;
556
557         hideOrShowFeeds(feeds_doc, hide_read_feeds);
558
559         if (hide_read_feeds) {
560                 setCookie("ttrss_vf_hreadf", 1);
561         } else {
562                 setCookie("ttrss_vf_hreadf", 0);
563         }
564
565 }
566
567