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