]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
remove xmlhttp.abort() from viewfeed
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17
18 var xmlhttp = Ajax.getTransport();
19 var xmlhttp_ctr = Ajax.getTransport();
20
21 var init_params = new Object();
22
23 function toggleTags() {
24 display_tags = !display_tags;
25
26 var p = document.getElementById("dispSwitchPrompt");
27
28 if (display_tags) {
29 p.innerHTML = "display feeds";
30 } else {
31 p.innerHTML = "display tags";
32 }
33
34 notify("Loading, please wait...");
35
36 updateFeedList();
37 }
38
39 function dlg_frefresh_callback() {
40 if (xmlhttp.readyState == 4) {
41 // notify(xmlhttp.responseText);
42
43 if (getActiveFeedId() == _qfd_deleted_feed) {
44 var h = document.getElementById("headlines-frame");
45 if (h) {
46 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
47 }
48 }
49
50 setTimeout('updateFeedList(false, false)', 50);
51 closeInfoBox();
52 }
53 }
54
55 function refetch_callback() {
56 if (xmlhttp_ctr.readyState == 4) {
57 try {
58
59 var date = new Date();
60
61 last_refetch = date.getTime() / 1000;
62
63 if (!xmlhttp_ctr.responseXML) {
64 notify("refetch_callback: backend did not return valid XML", true, true);
65 return;
66 }
67
68 var reply = xmlhttp_ctr.responseXML.firstChild;
69
70 if (!reply) {
71 notify("refetch_callback: backend did not return expected XML object", true, true);
72 updateTitle("");
73 return;
74 }
75
76 var error_code = false;
77 var error_msg = false;
78
79 if (reply.firstChild) {
80 error_code = reply.firstChild.getAttribute("error-code");
81 error_msg = reply.firstChild.getAttribute("error-msg");
82 }
83
84 if (!error_code) {
85 error_code = reply.getAttribute("error-code");
86 error_msg = reply.getAttribute("error-msg");
87 }
88
89 if (error_code && error_code != 0) {
90 debug("refetch_callback: got error code " + error_code);
91 return fatalError(error_code, error_msg);
92 }
93
94 var counters = reply.firstChild;
95
96 parse_counters(counters, true);
97
98 var runtime_info = counters.nextSibling;
99
100 parse_runtime_info(runtime_info);
101
102 debug("refetch_callback: done");
103
104 if (!daemon_enabled && !daemon_refresh_only) {
105 notify("All feeds updated.");
106 updateTitle("");
107 } else {
108 notify("");
109 }
110 } catch (e) {
111 exception_error("refetch_callback", e);
112 updateTitle("");
113 }
114 }
115 }
116
117 function backend_sanity_check_callback() {
118
119 if (xmlhttp.readyState == 4) {
120
121 try {
122
123 if (!xmlhttp.responseXML) {
124 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
125 return;
126 }
127
128 var reply = xmlhttp.responseXML.firstChild.firstChild;
129
130 if (!reply) {
131 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
132 return;
133 }
134
135 var error_code = reply.getAttribute("error-code");
136
137 if (error_code && error_code != 0) {
138 return fatalError(error_code, reply.getAttribute("error-msg"));
139 }
140
141 debug("sanity check ok");
142
143 var params = reply.nextSibling;
144
145 if (params) {
146 debug('reading init-params...');
147 var param = params.firstChild;
148
149 while (param) {
150 var k = param.getAttribute("key");
151 var v = param.getAttribute("value");
152 debug(k + " => " + v);
153 init_params[k] = v;
154 param = param.nextSibling;
155 }
156 }
157
158 init_second_stage();
159
160 } catch (e) {
161 exception_error("backend_sanity_check_callback", e);
162 }
163 }
164 }
165
166 function scheduleFeedUpdate(force) {
167
168 if (!daemon_enabled && !daemon_refresh_only) {
169 notify("Updating feeds, please wait.", true);
170 updateTitle("Updating");
171 }
172
173 var query_str = "backend.php?op=rpc&subop=";
174
175 if (force) {
176 query_str = query_str + "forceUpdateAllFeeds";
177 } else {
178 query_str = query_str + "updateAllFeeds";
179 }
180
181 var omode;
182
183 if (firsttime_update && !navigator.userAgent.match("Opera")) {
184 firsttime_update = false;
185 omode = "T";
186 } else {
187 if (display_tags) {
188 omode = "t";
189 } else {
190 omode = "flc";
191 }
192 }
193
194 query_str = query_str + "&omode=" + omode;
195 query_str = query_str + "&uctr=" + global_unread;
196
197 debug("in scheduleFeedUpdate");
198
199 var date = new Date();
200
201 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
202 debug("<b>xmlhttp seems to be stuck, aborting</b>");
203 xmlhttp_ctr.abort();
204 }
205
206 debug("REFETCH query: " + query_str);
207
208 if (xmlhttp_ready(xmlhttp_ctr)) {
209 xmlhttp_ctr.open("GET", query_str, true);
210 xmlhttp_ctr.onreadystatechange=refetch_callback;
211 xmlhttp_ctr.send(null);
212 } else {
213 debug("xmlhttp_ctr busy");
214 //printLockingError();
215 }
216 }
217
218 function updateFeedList(silent, fetch) {
219
220 // if (silent != true) {
221 // notify("Loading feed list...");
222 // }
223
224 var query_str = "backend.php?op=feeds";
225
226 if (display_tags) {
227 query_str = query_str + "&tags=1";
228 }
229
230 if (getActiveFeedId() && !activeFeedIsCat()) {
231 query_str = query_str + "&actid=" + getActiveFeedId();
232 }
233
234 if (navigator.userAgent.match("Opera")) {
235 var date = new Date();
236 var timestamp = Math.round(date.getTime() / 1000);
237 query_str = query_str + "&ts=" + timestamp
238 }
239
240 if (fetch) query_str = query_str + "&fetch=yes";
241
242 // var feeds_frame = document.getElementById("feeds-frame");
243 // feeds_frame.src = query_str;
244
245 debug("updateFeedList Q=" + query_str);
246
247 if (xmlhttp_ready(xmlhttp)) {
248 xmlhttp.open("GET", query_str, true);
249 xmlhttp.onreadystatechange=feedlist_callback;
250 xmlhttp.send(null);
251 } else {
252 debug("xmlhttp busy");
253 //printLockingError();
254 }
255
256 }
257
258 function catchupAllFeeds() {
259
260 var query_str = "backend.php?op=feeds&subop=catchupAll";
261
262 notify("Marking all feeds as read...");
263
264 debug("catchupAllFeeds Q=" + query_str);
265
266 if (xmlhttp_ready(xmlhttp)) {
267 xmlhttp.open("GET", query_str, true);
268 xmlhttp.onreadystatechange=feedlist_callback;
269 xmlhttp.send(null);
270 } else {
271 debug("xmlhttp busy");
272 //printLockingError();
273 }
274
275 global_unread = 0;
276 updateTitle("");
277
278 }
279
280 function viewCurrentFeed(subop) {
281
282 // if (getActiveFeedId()) {
283 if (getActiveFeedId() != undefined) {
284 viewfeed(getActiveFeedId(), subop);
285 } else {
286 disableContainerChildren("headlinesToolbar", false, document);
287 // viewfeed(-1, subop); // FIXME
288 }
289 return false; // block unneeded form submits
290 }
291
292 function viewfeed(feed, subop) {
293 var f = window.frames["feeds-frame"];
294 f.viewfeed(feed, subop);
295 }
296
297 function timeout() {
298 scheduleFeedUpdate(false);
299
300 var refresh_time = getInitParam("feeds_frame_refresh");
301
302 if (!refresh_time) refresh_time = 600;
303
304 setTimeout("timeout()", refresh_time*1000);
305 }
306
307 function resetSearch() {
308 var searchbox = document.getElementById("searchbox")
309
310 if (searchbox.value != "" && getActiveFeedId()) {
311 searchbox.value = "";
312 viewfeed(getActiveFeedId(), "");
313 }
314 }
315
316 function searchCancel() {
317 closeInfoBox(true);
318 }
319
320 function search() {
321 closeInfoBox();
322 viewCurrentFeed(0, "");
323 }
324
325 function localPiggieFunction(enable) {
326 if (enable) {
327 var query_str = "backend.php?op=feeds&subop=piggie";
328
329 if (xmlhttp_ready(xmlhttp)) {
330
331 xmlhttp.open("GET", query_str, true);
332 xmlhttp.onreadystatechange=feedlist_callback;
333 xmlhttp.send(null);
334 }
335 }
336 }
337
338 // if argument is undefined, current subtitle is not updated
339 // use blank string to clear subtitle
340 function updateTitle(s) {
341 var tmp = "Tiny Tiny RSS";
342
343 if (s != undefined) {
344 current_subtitle = s;
345 }
346
347 if (global_unread > 0) {
348 tmp = tmp + " (" + global_unread + ")";
349 }
350
351 if (current_subtitle) {
352 tmp = tmp + " - " + current_subtitle;
353 }
354
355 if (active_title_text.length > 0) {
356 tmp = tmp + " > " + active_title_text;
357 }
358
359 document.title = tmp;
360 }
361
362 function genericSanityCheck() {
363
364 if (!xmlhttp) fatalError(1);
365
366 setCookie("ttrss_vf_test", "TEST");
367
368 if (getCookie("ttrss_vf_test") != "TEST") {
369 fatalError(2);
370 }
371
372 return true;
373 }
374
375 function init() {
376
377 try {
378
379 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
380
381 if (arguments.callee.done) return;
382 arguments.callee.done = true;
383
384 disableContainerChildren("headlinesToolbar", true);
385
386 Form.disable("main_toolbar_form");
387
388 if (!genericSanityCheck())
389 return;
390
391 if (getURLParam('debug')) {
392 document.getElementById('debug_output').style.display = 'block';
393 debug('debug mode activated');
394 }
395
396 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
397 xmlhttp.onreadystatechange=backend_sanity_check_callback;
398 xmlhttp.send(null);
399
400 } catch (e) {
401 exception_error("init", e);
402 }
403 }
404
405 function init_second_stage() {
406
407 try {
408
409 cookie_lifetime = getCookie("ttrss_cltime");
410
411 delCookie("ttrss_vf_test");
412
413 document.onkeydown = hotkey_handler;
414
415 var tb = parent.document.forms["main_toolbar_form"];
416
417 // dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
418 // dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
419
420 daemon_enabled = getInitParam("daemon_enabled") == 1;
421 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
422
423 setTimeout('updateFeedList(false, false)', 50);
424
425 debug("second stage ok");
426
427 } catch (e) {
428 exception_error("init_second_stage", e);
429 }
430 }
431
432 function quickMenuChange() {
433 var chooser = document.getElementById("quickMenuChooser");
434 var opid = chooser[chooser.selectedIndex].value;
435
436 chooser.selectedIndex = 0;
437 quickMenuGo(opid);
438 }
439
440 function quickMenuGo(opid) {
441 try {
442
443 if (opid == "qmcPrefs") {
444 gotoPreferences();
445 }
446
447 if (opid == "qmcSearch") {
448 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
449 return;
450 }
451
452 if (opid == "qmcAddFeed") {
453 displayDlg("quickAddFeed");
454 return;
455 }
456
457 if (opid == "qmcRemoveFeed") {
458 var actid = getActiveFeedId();
459
460 if (!actid || activeFeedIsCat()) {
461 alert("Please select some feed first.");
462 return;
463 }
464
465 var fn = getFeedName(actid);
466
467 if (confirm("Unsubscribe from " + fn + "?")) {
468 qfdDelete(actid);
469 }
470
471 return;
472 }
473
474 if (opid == "qmcUpdateFeeds") {
475 scheduleFeedUpdate(true);
476 return;
477 }
478
479 if (opid == "qmcCatchupAll") {
480 catchupAllFeeds();
481 return;
482 }
483
484 if (opid == "qmcShowOnlyUnread") {
485 toggleDispRead();
486 return;
487 }
488
489 if (opid == "qmcAddFilter") {
490 displayDlg("quickAddFilter", getActiveFeedId());
491 }
492 } catch (e) {
493 exception_error("quickMenuGo", e);
494 }
495 }
496
497 function qfdDelete(feed_id) {
498
499 notify("Removing feed...");
500
501 if (!xmlhttp_ready(xmlhttp)) {
502 printLockingError();
503 return
504 }
505
506 _qfd_deleted_feed = feed_id;
507
508 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
509 xmlhttp.onreadystatechange=dlg_frefresh_callback;
510 xmlhttp.send(null);
511
512 return false;
513 }
514
515
516 function updateFeedTitle(t) {
517 active_title_text = t;
518 updateTitle();
519 }
520
521 function toggleDispRead() {
522 try {
523
524 if (!xmlhttp_ready(xmlhttp)) {
525 printLockingError();
526 return
527 }
528
529 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
530
531 hide_read_feeds = !hide_read_feeds;
532
533 debug("toggle_disp_read => " + hide_read_feeds);
534
535 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
536
537 var query = "backend.php?op=rpc&subop=setpref" +
538 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
539
540 storeInitParam("hide_read_feeds", hide_read_feeds, true);
541
542 new Ajax.Request(query);
543
544 } catch (e) {
545 exception_error("toggleDispRead", e);
546 }
547 }
548
549 function parse_runtime_info(elem) {
550 if (!elem) {
551 debug("parse_runtime_info: elem is null, aborting");
552 return;
553 }
554
555 var param = elem.firstChild;
556
557 debug("parse_runtime_info: " + param);
558
559 while (param) {
560 var k = param.getAttribute("key");
561 var v = param.getAttribute("value");
562
563 debug("RI: " + k + " => " + v);
564
565 var w = document.getElementById("noDaemonWarning");
566
567 if (w) {
568 if (k == "daemon_is_running" && v != 1) {
569 w.style.display = "block";
570 } else {
571 w.style.display = "none";
572 }
573 }
574 param = param.nextSibling;
575 }
576 }
577
578 function catchupCurrentFeed() {
579
580 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
581
582 var str = "Mark all articles in " + fn + " as read?";
583
584 /* if (active_feed_is_cat) {
585 str = "Mark all articles in this category as read?";
586 } */
587
588 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
589 return viewCurrentFeed('MarkAllRead')
590 }
591 }
592
593 function userSwitch() {
594 var chooser = document.getElementById("userSwitch");
595 var user = chooser[chooser.selectedIndex].value;
596 window.location = "tt-rss.php?swu=" + user;
597 }
598
599