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