]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
enable search toolbar
[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 _qfd_deleted_feed = 0;
10 var firsttime_update = true;
11 var last_refetch = 0;
12 var cookie_lifetime = 0;
13
14 var xmlhttp = Ajax.getTransport();
15
16 function toggleTags() {
17 display_tags = !display_tags;
18
19 var p = document.getElementById("dispSwitchPrompt");
20
21 if (display_tags) {
22 p.innerHTML = "display feeds";
23 } else {
24 p.innerHTML = "display tags";
25 }
26
27 updateFeedList();
28 }
29
30 function dlg_frefresh_callback() {
31 if (xmlhttp.readyState == 4) {
32 notify(xmlhttp.responseText);
33 updateFeedList(false, false);
34 if (_qfd_deleted_feed) {
35 var hframe = document.getElementById("headlines-frame");
36 if (hframe) {
37 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
38 }
39 }
40 closeInfoBox();
41 }
42 }
43
44 function hide_unread_callback() {
45 if (xmlhttp.readyState == 4) {
46
47 try {
48
49 var reply = xmlhttp.responseXML.firstChild.firstChild;
50 var value = reply.getAttribute("value");
51 var hide_read_feeds = (value != "false")
52 var feeds_doc = window.frames["feeds-frame"].document;
53
54 hideOrShowFeeds(feeds_doc, hide_read_feeds);
55
56 if (hide_read_feeds) {
57 setCookie("ttrss_vf_hreadf", 1);
58 } else {
59 setCookie("ttrss_vf_hreadf", 0);
60 }
61
62 } catch (e) {
63 exception_error("hide_unread_callback", e);
64 }
65
66 }
67 }
68
69 function refetch_callback() {
70 if (xmlhttp.readyState == 4) {
71 try {
72
73 var date = new Date();
74
75 last_refetch = date.getTime() / 1000;
76
77 if (!xmlhttp.responseXML) {
78 notify("refetch_callback: backend did not return valid XML", true, true);
79 return;
80 }
81
82 var reply = xmlhttp.responseXML.firstChild;
83
84 if (!reply) {
85 notify("refetch_callback: backend did not return expected XML object", true, true);
86 updateTitle("");
87 return;
88 }
89
90 var error_code = reply.getAttribute("error-code");
91
92 if (error_code && error_code != 0) {
93 return fatalError(error_code, reply.getAttribute("error-msg"));
94 }
95
96 var f_document = window.frames["feeds-frame"].document;
97
98 parse_counters(reply, f_document, window, true);
99
100 debug("refetch_callback: done");
101
102 if (!daemon_enabled) {
103 notify("All feeds updated.");
104 updateTitle("");
105 } else {
106 notify("");
107 }
108 } catch (e) {
109 exception_error("refetch_callback", e);
110 updateTitle("");
111 }
112 }
113 }
114
115 function backend_sanity_check_callback() {
116
117 if (xmlhttp.readyState == 4) {
118
119 try {
120
121 if (!xmlhttp.responseXML) {
122 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
123 return;
124 }
125
126 var reply = xmlhttp.responseXML.firstChild;
127
128 if (!reply) {
129 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
130 return;
131 }
132
133 var error_code = reply.getAttribute("error-code");
134
135 if (error_code && error_code != 0) {
136 return fatalError(error_code, reply.getAttribute("error-msg"));
137 }
138
139 debug("sanity check ok");
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) {
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("xmlhttp seems to be stuck, aborting");
186 xmlhttp.abort();
187 }
188
189 if (xmlhttp_ready(xmlhttp)) {
190 xmlhttp.open("GET", query_str, true);
191 xmlhttp.onreadystatechange=refetch_callback;
192 xmlhttp.send(null);
193 } else {
194 debug("xmlhttp busy");
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 (navigator.userAgent.match("Opera")) {
216 var date = new Date();
217 var timestamp = Math.round(date.getTime() / 1000);
218 query_str = query_str + "&ts=" + timestamp
219 }
220
221 if (fetch) query_str = query_str + "&fetch=yes";
222
223 var feeds_frame = document.getElementById("feeds-frame");
224
225 feeds_frame.src = query_str;
226 }
227
228 function catchupAllFeeds() {
229
230 var query_str = "backend.php?op=feeds&subop=catchupAll";
231
232 notify("Marking all feeds as read...");
233
234 var feeds_frame = document.getElementById("feeds-frame");
235
236 feeds_frame.src = query_str;
237
238 global_unread = 0;
239 updateTitle("");
240
241 }
242
243 function viewCurrentFeed(skip, subop) {
244
245 if (getActiveFeedId()) {
246 viewfeed(getActiveFeedId(), skip, subop);
247 } else {
248 disableContainerChildren("headlinesToolbar", false, document);
249 viewfeed(-1, skip, subop); // FIXME
250 }
251 return false; // block unneeded form submits
252 }
253
254 function viewfeed(feed, skip, subop) {
255 var f = window.frames["feeds-frame"];
256 f.viewfeed(feed, skip, subop);
257 }
258
259 function timeout() {
260 scheduleFeedUpdate(false);
261
262 var refresh_time = getCookie('ttrss_vf_refresh');
263
264 if (!refresh_time) refresh_time = 600;
265
266 setTimeout("timeout()", refresh_time*1000);
267 }
268
269 function resetSearch() {
270 var searchbox = document.getElementById("searchbox")
271
272 if (searchbox.value != "" && getActiveFeedId()) {
273 searchbox.value = "";
274 viewfeed(getActiveFeedId(), 0, "");
275 }
276 }
277
278 function searchCancel() {
279 closeInfoBox(true);
280 }
281
282 function search() {
283 closeInfoBox();
284 viewCurrentFeed(0, "");
285 }
286
287 function localPiggieFunction(enable) {
288 if (enable) {
289 var query_str = "backend.php?op=feeds&subop=piggie";
290
291 if (xmlhttp_ready(xmlhttp)) {
292
293 xmlhttp.open("GET", query_str, true);
294 xmlhttp.onreadystatechange=feedlist_callback;
295 xmlhttp.send(null);
296 }
297 }
298 }
299
300 function localHotkeyHandler(keycode) {
301
302 if (keycode == 82) { // r
303 return scheduleFeedUpdate(true);
304 }
305
306 if (keycode == 85) { // u
307 if (getActiveFeedId()) {
308 return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
309 }
310 }
311
312 if (keycode == 65) { // a
313 return toggleDispRead();
314 }
315
316 var f_doc = window.frames["feeds-frame"].document;
317 var feedlist = f_doc.getElementById('feedList');
318
319 if (keycode == 74) { // j
320 var feed = getActiveFeedId();
321 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
322 if (new_feed) viewfeed(new_feed, 0, '');
323 }
324
325 if (keycode == 75) { // k
326 var feed = getActiveFeedId();
327 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
328 if (new_feed) viewfeed(new_feed, 0, '');
329 }
330
331 // notify("KC: " + keycode);
332
333 }
334
335 // if argument is undefined, current subtitle is not updated
336 // use blank string to clear subtitle
337 function updateTitle(s) {
338 var tmp = "Tiny Tiny RSS";
339
340 if (s != undefined) {
341 current_subtitle = s;
342 }
343
344 if (global_unread > 0) {
345 tmp = tmp + " (" + global_unread + ")";
346 }
347
348 if (current_subtitle) {
349 tmp = tmp + " - " + current_subtitle;
350 }
351
352 if (active_title_text.length > 0) {
353 tmp = tmp + " > " + active_title_text;
354 }
355
356 document.title = tmp;
357 }
358
359 function genericSanityCheck() {
360
361 if (!xmlhttp) fatalError(1);
362
363 setCookie("ttrss_vf_test", "TEST");
364
365 if (getCookie("ttrss_vf_test") != "TEST") {
366 fatalError(2);
367 }
368
369 return true;
370 }
371
372 function init() {
373
374 try {
375
376 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
377
378 if (arguments.callee.done) return;
379 arguments.callee.done = true;
380
381 disableContainerChildren("headlinesToolbar", true);
382
383 Form.disable("main_toolbar_form");
384
385 if (!genericSanityCheck())
386 return;
387
388 if (getURLParam('debug')) {
389 document.getElementById('debug_output').style.display = 'block';
390 debug('debug mode activated');
391 }
392
393 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
394 xmlhttp.onreadystatechange=backend_sanity_check_callback;
395 xmlhttp.send(null);
396
397 } catch (e) {
398 exception_error("init", e);
399 }
400 }
401
402 function resize_feeds_frame() {
403 var f = document.getElementById("feeds-frame");
404 var tf = document.getElementById("mainFooter");
405 var th = document.getElementById("mainHeader");
406
407 f.style.height = document.body.scrollHeight - tf.scrollHeight -
408 th.scrollHeight - 50 + "px";
409 }
410
411 function init_second_stage() {
412
413 try {
414
415 cookie_lifetime = getCookie("ttrss_cltime");
416
417 delCookie("ttrss_vf_test");
418
419 setCookie("ttrss_vf_actfeed", "");
420
421 updateFeedList(false, false);
422 document.onkeydown = hotkey_handler;
423
424 /* var viewbox = document.getElementById("viewbox");
425 var limitbox = document.getElementById("limitbox");
426
427 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
428 dropboxSelect(limitbox, getCookie("ttrss_vf_limit")); */
429
430 daemon_enabled = getCookie("ttrss_vf_daemon");
431
432 // FIXME should be callled after window resize
433
434 var h = document.getElementById("headlines");
435 var c = document.getElementById("content");
436
437 if (navigator.userAgent.match("Opera")) {
438 resize_feeds_frame();
439
440 /* // fix headlines frame height for Opera
441 var h = document.getElementById("headlines");
442 var c = document.getElementById("content");
443 var nh = document.body.scrollHeight * 0.25;
444
445 h.style.height = nh + "px";
446 c.style.height = c.scrollHeight - nh + "px"; */
447
448 }
449
450 debug("second stage ok");
451
452 } catch (e) {
453 exception_error("init_second_stage", e);
454 }
455 }
456
457 function quickMenuChange() {
458 var chooser = document.getElementById("quickMenuChooser");
459 var opid = chooser[chooser.selectedIndex].value;
460
461 chooser.selectedIndex = 0;
462 quickMenuGo(opid);
463 }
464
465 function quickMenuGo(opid) {
466 try {
467
468 if (opid == "qmcPrefs") {
469 gotoPreferences();
470 }
471
472 if (opid == "qmcSearch") {
473 displayDlg("search", getActiveFeedId());
474 return;
475 }
476
477 if (opid == "qmcAddFeed") {
478 displayDlg("quickAddFeed");
479 return;
480 }
481
482 if (opid == "qmcRemoveFeed") {
483 var actid = getActiveFeedId();
484
485 if (!actid) {
486 alert("Please select some feed first.");
487 return;
488 }
489
490 if (confirm("Unsubscribe from current feed?")) {
491 qfdDelete(actid);
492 }
493
494 return;
495 }
496
497 if (opid == "qmcUpdateFeeds") {
498 scheduleFeedUpdate(true);
499 return;
500 }
501
502 if (opid == "qmcCatchupAll") {
503 catchupAllFeeds();
504 return;
505 }
506
507 if (opid == "qmcShowOnlyUnread") {
508 toggleDispRead();
509 return;
510 }
511
512 if (opid == "qmcAddFilter") {
513 displayDlg("quickAddFilter", getActiveFeedId());
514 }
515 } catch (e) {
516 exception_error("quickMenuGo", e);
517 }
518 }
519
520 function qfdDelete(feed_id) {
521
522 notify("Removing feed...");
523
524 if (!xmlhttp_ready(xmlhttp)) {
525 printLockingError();
526 return
527 }
528
529 // var feeds_doc = window.frames["feeds-frame"].document;
530 // feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
531
532 _qfd_deleted_feed = feed_id;
533
534 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
535 xmlhttp.onreadystatechange=dlg_frefresh_callback;
536 xmlhttp.send(null);
537 }
538
539
540 function updateFeedTitle(t) {
541 active_title_text = t;
542 updateTitle();
543 }
544
545 function toggleDispRead() {
546 try {
547
548 if (!xmlhttp_ready(xmlhttp)) {
549 printLockingError();
550 return
551 }
552
553 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
554
555 hide_read_feeds = !hide_read_feeds;
556
557 var query = "backend.php?op=rpc&subop=setpref" +
558 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
559
560 xmlhttp.open("GET", query);
561 xmlhttp.onreadystatechange=hide_unread_callback;
562 xmlhttp.send(null);
563
564 } catch (e) {
565 exception_error("toggleDispRead", e);
566 }
567 }
568
569 function debug(msg) {
570 var c = document.getElementById('debug_output');
571 if (c && c.style.display == "block") {
572 while (c.lastChild != 'undefined' && c.childNodes.length > 20) {
573 c.removeChild(c.lastChild);
574 }
575
576 var d = new Date();
577 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
578 ":" + leading_zero(d.getSeconds());
579 c.innerHTML = "<li>[" + ts + "] " + msg + "</li>" + c.innerHTML;
580 }
581 }
582
583 function fatalError(code, message) {
584 try {
585 var fe = document.getElementById("fatal_error");
586 var fc = document.getElementById("fatal_error_msg");
587
588 fc.innerHTML = "Code " + code + ": " + message;
589
590 fe.style.display = "block";
591
592 } catch (e) {
593 exception_error("fatalError", e);
594 }
595 }