]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
perform backend sanity check on startup, update schema version in backend.php
[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 search_query = "";
12 var search_mode = "";
13
14 var display_tags = false;
15 //var display_read_feeds = true;
16
17 var global_unread = 0;
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 f_document = window.frames["feeds-frame"].document;
86
87                 for (var l = 0; l < reply.childNodes.length; l++) {
88                         var id = reply.childNodes[l].getAttribute("id");
89                         var ctr = reply.childNodes[l].getAttribute("counter");
90
91                         var feedctr = f_document.getElementById("FEEDCTR-" + id);
92                         var feedu = f_document.getElementById("FEEDU-" + id);
93                         var feedr = f_document.getElementById("FEEDR-" + id);
94
95 /*                      TODO figure out how to update this from viewfeed.js->view()
96                         disabled for now...
97
98                         if (id == "global-unread") {
99                                 global_unread = ctr;
100                         } */
101
102                         if (feedctr && feedu && feedr) {
103
104                                 feedu.innerHTML = ctr;
105         
106                                 if (ctr > 0) {
107                                         feedctr.className = "odd";
108                                         if (!feedr.className.match("Unread")) {
109                                                 feedr.className = feedr.className + "Unread";
110                                         }
111                                 } else {
112                                         feedctr.className = "invisible";
113                                         feedr.className = feedr.className.replace("Unread", "");
114                                 }
115                         }
116                 }  
117
118                 updateTitle("");
119                 notify("All feeds updated.");
120
121         }
122 }
123
124 function backend_sanity_check_callback() {
125
126         if (xmlhttp.readyState == 4) {
127                 
128                 if (!xmlhttp.responseXML) {
129                         fatalError(3);
130                         return;
131                 }
132
133                 var reply = xmlhttp.responseXML.firstChild;
134
135                 if (!reply) {
136                         fatalError(3);
137                         return;
138                 }
139
140                 var error_code = reply.getAttribute("code");
141         
142                 if (error_code && error_code != 0) {
143                         return fatalError(error_code);
144                 }
145
146                 init_second_stage();
147         } 
148 }
149
150 function updateFeed(feed_id) {
151
152         var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
153
154         if (xmlhttp_ready(xmlhttp)) {
155                 xmlhttp.open("GET", query_str, true);
156                 xmlhttp.onreadystatechange=feed_update_callback;
157                 xmlhttp.send(null);
158         } else {
159                 printLockingError();
160         }   
161
162 }
163
164 function scheduleFeedUpdate(force) {
165
166         notify("Updating feeds in background...");
167
168 //      document.title = "Tiny Tiny RSS - Updating...";
169
170         updateTitle("Updating...");
171
172         var query_str = "backend.php?op=rpc&subop=";
173
174         if (force) {
175                 query_str = query_str + "forceUpdateAllFeeds";
176         } else {
177                 query_str = query_str + "updateAllFeeds";
178         }
179
180         var omode;
181
182         if (display_tags) {
183                 omode = "t";
184         } else {
185                 omode = "fl";
186         }
187
188         query_str = query_str + "&omode=" + omode;
189
190         if (xmlhttp_ready(xmlhttp)) {
191                 xmlhttp.open("GET", query_str, true);
192                 xmlhttp.onreadystatechange=refetch_callback;
193                 xmlhttp.send(null);
194         } else {
195                 printLockingError();
196         }   
197 }
198
199 function updateFeedList(silent, fetch) {
200
201 //      if (silent != true) {
202 //              notify("Loading feed list...");
203 //      }
204
205         var query_str = "backend.php?op=feeds";
206
207         if (display_tags) {
208                 query_str = query_str + "&tags=1";
209         }
210
211         if (getActiveFeedId()) {
212                 query_str = query_str + "&actid=" + getActiveFeedId();
213         }
214
215         if (fetch) query_str = query_str + "&fetch=yes";
216
217         var feeds_frame = document.getElementById("feeds-frame");
218
219         feeds_frame.src = query_str;
220 }
221
222 function catchupAllFeeds() {
223
224         var query_str = "backend.php?op=feeds&subop=catchupAll";
225
226         notify("Marking all feeds as read...");
227
228         var feeds_frame = document.getElementById("feeds-frame");
229
230         feeds_frame.src = query_str;
231
232         global_unread = 0;
233         updateTitle();
234
235 }
236
237 function viewCurrentFeed(skip, subop) {
238
239         if (getActiveFeedId()) {
240                 viewfeed(getActiveFeedId(), skip, subop);
241         } else {
242                 disableContainerChildren("headlinesToolbar", false, document);
243                 viewfeed(-1, skip, subop); // FIXME
244         }
245 }
246
247 function viewfeed(feed, skip, subop) {
248         var f = window.frames["feeds-frame"];
249         f.viewfeed(feed, skip, subop);
250 }
251
252 function timeout() {
253         scheduleFeedUpdate(true);
254         setTimeout("timeout()", 1800*1000);
255 }
256
257 function resetSearch() {
258         var searchbox = document.getElementById("searchbox")
259
260         if (searchbox.value != "" && getActiveFeedId()) {       
261                 searchbox.value = "";
262                 viewfeed(getActiveFeedId(), 0, "");
263         }
264 }
265
266 function search() {
267         viewCurrentFeed(0, "");
268 }
269
270 function localPiggieFunction(enable) {
271         if (enable) {
272                 var query_str = "backend.php?op=feeds&subop=piggie";
273
274                 if (xmlhttp_ready(xmlhttp)) {
275
276                         xmlhttp.open("GET", query_str, true);
277                         xmlhttp.onreadystatechange=feedlist_callback;
278                         xmlhttp.send(null);
279                 }
280         }
281 }
282
283 function localHotkeyHandler(keycode) {
284
285 /*      if (keycode == 78) {
286                 return moveToPost('next');
287         }
288
289         if (keycode == 80) {
290                 return moveToPost('prev');
291         } */
292
293         if (keycode == 82) { // r
294                 return scheduleFeedUpdate(true);
295         }
296
297         if (keycode == 85) { // u
298                 if (getActiveFeedId()) {
299                         return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
300                 }
301         }
302
303         if (keycode == 65) { // a
304                 return toggleDispRead();
305         }
306
307 //      notify("KC: " + keycode);
308
309 }
310
311 function updateTitle(s) {
312         var tmp = "Tiny Tiny RSS";
313         
314         if (global_unread > 0) {
315                 tmp = tmp + " (" + global_unread + ")";
316         }
317
318         if (s) {
319                 tmp = tmp + " - " + s;
320         }
321         document.title = tmp;
322 }
323
324 function genericSanityCheck() {
325
326         if (!xmlhttp) fatalError(1);
327
328         setCookie("ttrss_vf_test", "TEST");
329         
330         if (getCookie("ttrss_vf_test") != "TEST") {
331                 fatalError(2);
332         }
333
334 /*      if (!xmlhttp) {
335                 document.getElementById("headlines").innerHTML = 
336                         "<b>Fatal error:</b> This program requires XmlHttpRequest " + 
337                         "to function properly. Your browser doesn't seem to support it.";
338                 return false;
339         }
340
341         setCookie("ttrss_vf_test", "TEST");
342         if (getCookie("ttrss_vf_test") != "TEST") {
343
344                 document.getElementById("headlines").innerHTML = 
345                         "<b>Fatal error:</b> This program requires cookies " + 
346                         "to function properly. Your browser doesn't seem to support them.";
347
348                 return false;
349         } */
350
351         return true;
352 }
353
354 function init() {
355
356         disableContainerChildren("headlinesToolbar", true);
357
358         if (!genericSanityCheck()) 
359                 return;
360
361         xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
362         xmlhttp.onreadystatechange=backend_sanity_check_callback;
363         xmlhttp.send(null);
364
365 }
366
367 function init_second_stage() {
368
369         setCookie("ttrss_vf_actfeed", "");
370
371         updateFeedList(false, false);
372         document.onkeydown = hotkey_handler;
373
374         var content = document.getElementById("content");
375
376         if (getCookie("ttrss_vf_vmode")) {
377                 var viewbox = document.getElementById("viewbox");
378                 viewbox.value = getCookie("ttrss_vf_vmode");
379         }
380
381         if (getCookie("ttrss_vf_limit")) {
382                 var limitbox = document.getElementById("limitbox");
383                 limitbox.value = getCookie("ttrss_vf_limit");
384         }
385
386 //      if (getCookie("ttrss_vf_actfeed")) {
387 //              viewfeed(getCookie("ttrss_vf_actfeed"), 0, '');
388 //      }
389
390         setTimeout("timeout()", 2*1000);
391 //      scheduleFeedUpdate(true);
392
393
394 }
395
396 function quickMenuGo() {
397
398         var chooser = document.getElementById("quickMenuChooser");
399         var opid = chooser[chooser.selectedIndex].id;
400
401         if (opid == "qmcPrefs") {
402                 gotoPreferences();
403         }
404
405         if (opid == "qmcAdvSearch") {
406                 displayDlg("search");
407                 return;
408         }
409
410         if (opid == "qmcAddFeed") {
411                 displayDlg("quickAddFeed");
412                 return;
413         }
414
415         if (opid == "qmcRemoveFeed") {
416                 var actid = getActiveFeedId();
417
418                 if (!actid) {
419                         notify("Please select some feed first.");
420                         return;
421                 }
422         
423                 displayDlg("quickDelFeed", actid);
424                 return;
425         }
426
427         if (opid == "qmcUpdateFeeds") {
428                 scheduleFeedUpdate(true);
429                 return;
430         }
431
432         if (opid == "qmcCatchupAll") {
433                 catchupAllFeeds();
434                 return;
435         }
436
437         if (opid == "qmcShowOnlyUnread") {
438                 toggleDispRead();
439                 return;
440         }
441
442 }
443
444 function qafAdd() {
445
446         if (!xmlhttp_ready(xmlhttp)) {
447                 printLockingError();
448                 return
449         }
450
451         var link = document.getElementById("qafInput");
452
453         if (link.value.length == 0) {
454                 notify("Missing feed URL.");
455         } else {
456                 notify("Adding feed...");
457                 
458                 var feeds_doc = window.frames["feeds-frame"].document;
459
460                 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
461
462                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=add&link=" +
463                         param_escape(link.value), true);
464                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
465                 xmlhttp.send(null);
466
467                 link.value = "";
468
469         }
470 }
471
472 function displayDlg(id, param) {
473
474         notify("");
475
476         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
477                 param_escape(id) + "&param=" + param_escape(param), true);
478         xmlhttp.onreadystatechange=dialog_refresh_callback;
479         xmlhttp.send(null);
480
481 }
482
483 function closeDlg() {
484         var dlg = document.getElementById("userDlg");
485         dlg.style.display = "none";
486 }
487
488 function qfdDelete(feed_id) {
489
490         notify("Removing feed...");
491
492         var feeds_doc = window.frames["feeds-frame"].document;
493         feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
494
495         xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=remove&ids=" + feed_id);
496         xmlhttp.onreadystatechange=dlg_frefresh_callback;
497         xmlhttp.send(null);
498 }
499
500
501 function allFeedsMenuGo() {
502         var chooser = document.getElementById("allFeedsChooser");
503
504         var opname = chooser[chooser.selectedIndex].text;
505
506         if (opname == "Update") {
507                 scheduleFeedUpdate(true);
508                 return;
509         }
510
511         if (opname == "Mark as read") {
512                 catchupAllFeeds();
513                 return;
514         }
515
516         if (opname == "Show only read") {
517                 toggleDispRead();
518                 return;
519         }
520
521 }
522
523 function toggleDispRead() {
524         var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
525
526         hide_read_feeds = !hide_read_feeds;
527
528         var feeds_doc = window.frames["feeds-frame"].document;
529
530         hideOrShowFeeds(feeds_doc, hide_read_feeds);
531
532         if (hide_read_feeds) {
533                 setCookie("ttrss_vf_hreadf", 1);
534         } else {
535                 setCookie("ttrss_vf_hreadf", 0);
536         }
537
538 }
539
540