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