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