]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
mobile version uses separate sid
[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 tb = parent.document.forms["main_toolbar_form"];
425
426 dropboxSelect(tb.view_mode, getCookie("ttrss_vf_vmode"));
427 dropboxSelect(tb.limit, getCookie("ttrss_vf_limit"));
428
429 daemon_enabled = getCookie("ttrss_vf_daemon");
430
431 // FIXME should be callled after window resize
432
433 var h = document.getElementById("headlines");
434 var c = document.getElementById("content");
435
436 if (navigator.userAgent.match("Opera")) {
437 resize_feeds_frame();
438
439 /* // fix headlines frame height for Opera
440 var h = document.getElementById("headlines");
441 var c = document.getElementById("content");
442 var nh = document.body.scrollHeight * 0.25;
443
444 h.style.height = nh + "px";
445 c.style.height = c.scrollHeight - nh + "px"; */
446
447 }
448
449 debug("second stage ok");
450
451 } catch (e) {
452 exception_error("init_second_stage", e);
453 }
454 }
455
456 function quickMenuChange() {
457 var chooser = document.getElementById("quickMenuChooser");
458 var opid = chooser[chooser.selectedIndex].value;
459
460 chooser.selectedIndex = 0;
461 quickMenuGo(opid);
462 }
463
464 function quickMenuGo(opid) {
465 try {
466
467 if (opid == "qmcPrefs") {
468 gotoPreferences();
469 }
470
471 if (opid == "qmcSearch") {
472 displayDlg("search", getActiveFeedId());
473 return;
474 }
475
476 if (opid == "qmcAddFeed") {
477 displayDlg("quickAddFeed");
478 return;
479 }
480
481 if (opid == "qmcRemoveFeed") {
482 var actid = getActiveFeedId();
483
484 if (!actid) {
485 alert("Please select some feed first.");
486 return;
487 }
488
489 if (confirm("Unsubscribe from current feed?")) {
490 qfdDelete(actid);
491 }
492
493 return;
494 }
495
496 if (opid == "qmcUpdateFeeds") {
497 scheduleFeedUpdate(true);
498 return;
499 }
500
501 if (opid == "qmcCatchupAll") {
502 catchupAllFeeds();
503 return;
504 }
505
506 if (opid == "qmcShowOnlyUnread") {
507 toggleDispRead();
508 return;
509 }
510
511 if (opid == "qmcAddFilter") {
512 displayDlg("quickAddFilter", getActiveFeedId());
513 }
514 } catch (e) {
515 exception_error("quickMenuGo", e);
516 }
517 }
518
519 function qfdDelete(feed_id) {
520
521 notify("Removing feed...");
522
523 if (!xmlhttp_ready(xmlhttp)) {
524 printLockingError();
525 return
526 }
527
528 // var feeds_doc = window.frames["feeds-frame"].document;
529 // feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
530
531 _qfd_deleted_feed = feed_id;
532
533 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
534 xmlhttp.onreadystatechange=dlg_frefresh_callback;
535 xmlhttp.send(null);
536 }
537
538
539 function updateFeedTitle(t) {
540 active_title_text = t;
541 updateTitle();
542 }
543
544 function toggleDispRead() {
545 try {
546
547 if (!xmlhttp_ready(xmlhttp)) {
548 printLockingError();
549 return
550 }
551
552 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
553
554 hide_read_feeds = !hide_read_feeds;
555
556 var query = "backend.php?op=rpc&subop=setpref" +
557 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
558
559 xmlhttp.open("GET", query);
560 xmlhttp.onreadystatechange=hide_unread_callback;
561 xmlhttp.send(null);
562
563 } catch (e) {
564 exception_error("toggleDispRead", e);
565 }
566 }
567
568 function debug(msg) {
569 var c = document.getElementById('debug_output');
570 if (c && c.style.display == "block") {
571 while (c.lastChild != 'undefined' && c.childNodes.length > 20) {
572 c.removeChild(c.lastChild);
573 }
574
575 var d = new Date();
576 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
577 ":" + leading_zero(d.getSeconds());
578 c.innerHTML = "<li>[" + ts + "] " + msg + "</li>" + c.innerHTML;
579 }
580 }
581
582 function fatalError(code, message) {
583 try {
584 var fe = document.getElementById("fatal_error");
585 var fc = document.getElementById("fatal_error_msg");
586
587 fc.innerHTML = "Code " + code + ": " + message;
588
589 fe.style.display = "block";
590
591 } catch (e) {
592 exception_error("fatalError", e);
593 }
594 }