]> git.wh0rd.org - tt-rss.git/blame_incremental - functions.js
getAllCounters: add rate limit (5 sec)
[tt-rss.git] / functions.js
... / ...
CommitLineData
1var hotkeys_enabled = true;
2var xmlhttp_rpc = Ajax.getTransport();
3var notify_silent = false;
4var last_progress_point = 0;
5
6/* add method to remove element from array */
7
8Array.prototype.remove = function(s) {
9 for (var i=0; i < this.length; i++) {
10 if (s == this[i]) this.splice(i, 1);
11 }
12}
13
14function browser_has_opacity() {
15 return navigator.userAgent.match("Gecko") != null ||
16 navigator.userAgent.match("Opera") != null;
17}
18
19function is_msie() {
20 return navigator.userAgent.match("MSIE");
21}
22
23function is_opera() {
24 return navigator.userAgent.match("Opera");
25}
26
27function is_khtml() {
28 return navigator.userAgent.match("KHTML");
29}
30
31function is_safari() {
32 return navigator.userAgent.match("Safari");
33}
34
35function exception_error(location, e, silent) {
36 var msg;
37
38 if (e.fileName) {
39 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
40
41 msg = "Exception: " + e.name + ", " + e.message +
42 "\nFunction: " + location + "()" +
43 "\nLocation: " + base_fname + ":" + e.lineNumber;
44
45 } else {
46 msg = "Exception: " + e + "\nFunction: " + location + "()";
47 }
48
49 debug("<b>EXCEPTION: " + msg + "</b>");
50
51 if (!silent) {
52 alert(msg);
53 }
54}
55
56function disableHotkeys() {
57 hotkeys_enabled = false;
58}
59
60function enableHotkeys() {
61 hotkeys_enabled = true;
62}
63
64function xmlhttp_ready(obj) {
65 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
66}
67
68function open_article_callback(transport) {
69 try {
70
71 if (transport.responseXML) {
72
73 var link = transport.responseXML.getElementsByTagName("link")[0];
74 var id = transport.responseXML.getElementsByTagName("id")[0];
75
76 debug("open_article_callback, received link: " + link);
77
78 if (link && id) {
79
80 var wname = "ttrss_article_" + id.firstChild.nodeValue;
81
82 debug("link url: " + link.firstChild.nodeValue + ", wname " + wname);
83
84 var w = window.open(link.firstChild.nodeValue, wname);
85
86 if (!w) { notify_error("Failed to load article in new window"); }
87
88 if (id) {
89 id = id.firstChild.nodeValue;
90 if (!document.getElementById("headlinesList")) {
91 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
92 }
93 }
94 } else {
95 notify_error("Can't open article: received invalid article link");
96 }
97 } else {
98 notify_error("Can't open article: received invalid XML");
99 }
100
101 } catch (e) {
102 exception_error("open_article_callback", e);
103 }
104}
105
106function param_escape(arg) {
107 if (typeof encodeURIComponent != 'undefined')
108 return encodeURIComponent(arg);
109 else
110 return escape(arg);
111}
112
113function param_unescape(arg) {
114 if (typeof decodeURIComponent != 'undefined')
115 return decodeURIComponent(arg);
116 else
117 return unescape(arg);
118}
119
120function delay(gap) {
121 var then,now;
122 then=new Date().getTime();
123 now=then;
124 while((now-then)<gap) {
125 now=new Date().getTime();
126 }
127}
128
129var notify_hide_timerid = false;
130
131function hide_notify() {
132 var n = document.getElementById("notify");
133 if (n) {
134 n.style.display = "none";
135 }
136}
137
138function notify_silent_next() {
139 notify_silent = true;
140}
141
142function notify_real(msg, no_hide, n_type) {
143
144 if (notify_silent) {
145 notify_silent = false;
146 return;
147 }
148
149 var n = document.getElementById("notify");
150 var nb = document.getElementById("notify_body");
151
152 if (!n || !nb) return;
153
154 if (notify_hide_timerid) {
155 window.clearTimeout(notify_hide_timerid);
156 }
157
158 if (msg == "") {
159 if (n.style.display == "block") {
160 notify_hide_timerid = window.setTimeout("hide_notify()", 0);
161 }
162 return;
163 } else {
164 n.style.display = "block";
165 }
166
167 /* types:
168
169 1 - generic
170 2 - progress
171 3 - error
172 4 - info
173
174 */
175
176 if (typeof __ != 'undefined') {
177 msg = __(msg);
178 }
179
180 if (n_type == 1) {
181 n.className = "notify";
182 } else if (n_type == 2) {
183 n.className = "notifyProgress";
184 msg = "<img src='images/indicator_white.gif'> " + msg;
185 } else if (n_type == 3) {
186 n.className = "notifyError";
187 msg = "<img src='images/sign_excl.gif'> " + msg;
188 } else if (n_type == 4) {
189 n.className = "notifyInfo";
190 msg = "<img src='images/sign_info.gif'> " + msg;
191 }
192
193// msg = "<img src='images/live_com_loading.gif'> " + msg;
194
195 nb.innerHTML = msg;
196
197 if (!no_hide) {
198 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
199 }
200}
201
202function notify(msg, no_hide) {
203 notify_real(msg, no_hide, 1);
204}
205
206function notify_progress(msg, no_hide) {
207 notify_real(msg, no_hide, 2);
208}
209
210function notify_error(msg, no_hide) {
211 notify_real(msg, no_hide, 3);
212
213}
214
215function notify_info(msg, no_hide) {
216 notify_real(msg, no_hide, 4);
217}
218
219function printLockingError() {
220 notify_info("Please wait until operation finishes.");
221}
222
223function cleanSelectedList(element) {
224 var content = document.getElementById(element);
225
226 if (!document.getElementById("feedCatHolder")) {
227 for (i = 0; i < content.childNodes.length; i++) {
228 var child = content.childNodes[i];
229 try {
230 child.className = child.className.replace("Selected", "");
231 } catch (e) {
232 //
233 }
234 }
235 } else {
236 for (i = 0; i < content.childNodes.length; i++) {
237 var child = content.childNodes[i];
238 if (child.id == "feedCatHolder") {
239 debug(child.id);
240 var fcat = child.lastChild;
241 for (j = 0; j < fcat.childNodes.length; j++) {
242 var feed = fcat.childNodes[j];
243 feed.className = feed.className.replace("Selected", "");
244 }
245 }
246 }
247 }
248}
249
250
251function cleanSelected(element) {
252 var content = document.getElementById(element);
253
254 for (i = 0; i < content.rows.length; i++) {
255 content.rows[i].className = content.rows[i].className.replace("Selected", "");
256 }
257}
258
259function getVisibleUnreadHeadlines() {
260 var content = document.getElementById("headlinesList");
261
262 var rows = new Array();
263
264 if (!content) return rows;
265
266 for (i = 0; i < content.rows.length; i++) {
267 var row_id = content.rows[i].id.replace("RROW-", "");
268 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
269 rows.push(row_id);
270 }
271 }
272 return rows;
273}
274
275function getVisibleHeadlineIds() {
276
277 var content = document.getElementById("headlinesList");
278
279 var rows = new Array();
280
281 if (!content) return rows;
282
283 for (i = 0; i < content.rows.length; i++) {
284 var row_id = content.rows[i].id.replace("RROW-", "");
285 if (row_id.length > 0) {
286 rows.push(row_id);
287 }
288 }
289 return rows;
290}
291
292function getFirstVisibleHeadlineId() {
293 if (isCdmMode()) {
294 var rows = cdmGetVisibleArticles();
295 return rows[0];
296 } else {
297 var rows = getVisibleHeadlineIds();
298 return rows[0];
299 }
300}
301
302function getLastVisibleHeadlineId() {
303 if (isCdmMode()) {
304 var rows = cdmGetVisibleArticles();
305 return rows[rows.length-1];
306 } else {
307 var rows = getVisibleHeadlineIds();
308 return rows[rows.length-1];
309 }
310}
311
312function markHeadline(id) {
313 var row = document.getElementById("RROW-" + id);
314 if (row) {
315 var is_active = false;
316
317 if (row.className.match("Active")) {
318 is_active = true;
319 }
320 row.className = row.className.replace("Selected", "");
321 row.className = row.className.replace("Active", "");
322 row.className = row.className.replace("Insensitive", "");
323
324 if (is_active) {
325 row.className = row.className = "Active";
326 }
327
328 var check = document.getElementById("RCHK-" + id);
329
330 if (check) {
331 check.checked = true;
332 }
333
334 row.className = row.className + "Selected";
335
336 }
337}
338
339function getFeedIds() {
340 var content = document.getElementById("feedsList");
341
342 var rows = new Array();
343
344 for (i = 0; i < content.rows.length; i++) {
345 var id = content.rows[i].id.replace("FEEDR-", "");
346 if (id.length > 0) {
347 rows.push(id);
348 }
349 }
350
351 return rows;
352}
353
354function setCookie(name, value, lifetime, path, domain, secure) {
355
356 var d = false;
357
358 if (lifetime) {
359 d = new Date();
360 d.setTime(d.getTime() + (lifetime * 1000));
361 }
362
363 debug("setCookie: " + name + " => " + value + ": " + d);
364
365 int_setCookie(name, value, d, path, domain, secure);
366
367}
368
369function int_setCookie(name, value, expires, path, domain, secure) {
370 document.cookie= name + "=" + escape(value) +
371 ((expires) ? "; expires=" + expires.toGMTString() : "") +
372 ((path) ? "; path=" + path : "") +
373 ((domain) ? "; domain=" + domain : "") +
374 ((secure) ? "; secure" : "");
375}
376
377function delCookie(name, path, domain) {
378 if (getCookie(name)) {
379 document.cookie = name + "=" +
380 ((path) ? ";path=" + path : "") +
381 ((domain) ? ";domain=" + domain : "" ) +
382 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
383 }
384}
385
386
387function getCookie(name) {
388
389 var dc = document.cookie;
390 var prefix = name + "=";
391 var begin = dc.indexOf("; " + prefix);
392 if (begin == -1) {
393 begin = dc.indexOf(prefix);
394 if (begin != 0) return null;
395 }
396 else {
397 begin += 2;
398 }
399 var end = document.cookie.indexOf(";", begin);
400 if (end == -1) {
401 end = dc.length;
402 }
403 return unescape(dc.substring(begin + prefix.length, end));
404}
405
406function disableContainerChildren(id, disable, doc) {
407
408 if (!doc) doc = document;
409
410 var container = doc.getElementById(id);
411
412 if (!container) {
413 //alert("disableContainerChildren: element " + id + " not found");
414 return;
415 }
416
417 for (var i = 0; i < container.childNodes.length; i++) {
418 var child = container.childNodes[i];
419
420 try {
421 child.disabled = disable;
422 } catch (E) {
423
424 }
425
426 if (disable) {
427 if (child.className && child.className.match("button")) {
428 child.className = "disabledButton";
429 }
430 } else {
431 if (child.className && child.className.match("disabledButton")) {
432 child.className = "button";
433 }
434 }
435 }
436
437}
438
439function gotoPreferences() {
440 document.location.href = "prefs.php";
441}
442
443function gotoMain() {
444 document.location.href = "tt-rss.php";
445}
446
447function gotoExportOpml() {
448 document.location.href = "opml.php?op=Export";
449}
450
451function getActiveFeedId() {
452// return getCookie("ttrss_vf_actfeed");
453 try {
454 debug("gAFID: " + active_feed_id);
455 return active_feed_id;
456 } catch (e) {
457 exception_error("getActiveFeedId", e);
458 }
459}
460
461function activeFeedIsCat() {
462 return active_feed_is_cat;
463}
464
465function setActiveFeedId(id) {
466// return setCookie("ttrss_vf_actfeed", id);
467 try {
468 debug("sAFID(" + id + ")");
469 active_feed_id = id;
470 } catch (e) {
471 exception_error("setActiveFeedId", e);
472 }
473}
474
475function parse_counters(reply, scheduled_call) {
476 try {
477
478 var feeds_found = 0;
479
480 var elems = reply.getElementsByTagName("counter");
481
482 for (var l = 0; l < elems.length; l++) {
483
484 var id = elems[l].getAttribute("id");
485 var t = elems[l].getAttribute("type");
486 var ctr = elems[l].getAttribute("counter");
487 var error = elems[l].getAttribute("error");
488 var has_img = elems[l].getAttribute("hi");
489 var updated = elems[l].getAttribute("updated");
490 var title = elems[l].getAttribute("title");
491
492 if (id == "global-unread") {
493 global_unread = ctr;
494 updateTitle();
495 continue;
496 }
497
498 if (id == "subscribed-feeds") {
499 feeds_found = ctr;
500 continue;
501 }
502
503 if (t == "category") {
504 var catctr = document.getElementById("FCATCTR-" + id);
505 if (catctr) {
506 catctr.innerHTML = "(" + ctr + ")";
507 if (ctr > 0) {
508 catctr.className = "catCtrHasUnread";
509 } else {
510 catctr.className = "catCtrNoUnread";
511 }
512 }
513 continue;
514 }
515
516 var feedctr = document.getElementById("FEEDCTR-" + id);
517 var feedu = document.getElementById("FEEDU-" + id);
518 var feedr = document.getElementById("FEEDR-" + id);
519 var feed_img = document.getElementById("FIMG-" + id);
520 var feedlink = document.getElementById("FEEDL-" + id);
521 var feedupd = document.getElementById("FLUPD-" + id);
522
523 if (updated && feedlink) {
524 if (error) {
525 feedlink.title = "Error: " + error + " (" + updated + ")";
526 } else {
527 feedlink.title = "Updated: " + updated;
528 }
529 }
530
531 if (updated && feedupd) {
532 if (error) {
533 feedupd.innerHTML = updated + " (Error)";
534 } else {
535 feedupd.innerHTML = updated;
536 }
537 }
538
539 if (has_img && feed_img && !is_msie()) {
540 if (!feed_img.src.match(id + ".ico")) {
541 feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
542 }
543 }
544
545 if (feedlink && title) {
546 feedlink.innerHTML = title;
547 }
548
549 if (feedctr && feedu && feedr) {
550
551 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
552 viewCurrentFeed();
553 }
554
555 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
556
557 feedu.innerHTML = ctr;
558
559 if (error) {
560 feedr.className = feedr.className.replace("feed", "error");
561 } else if (id > 0) {
562 feedr.className = feedr.className.replace("error", "feed");
563 }
564
565 if (ctr > 0) {
566 feedctr.className = "odd";
567 if (!feedr.className.match("Unread")) {
568 var is_selected = feedr.className.match("Selected");
569
570 feedr.className = feedr.className.replace("Selected", "");
571 feedr.className = feedr.className.replace("Unread", "");
572
573 feedr.className = feedr.className + "Unread";
574
575 if (is_selected) {
576 feedr.className = feedr.className + "Selected";
577 }
578
579 }
580
581 if (row_needs_hl) {
582 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
583 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
584 }
585 } else {
586 feedctr.className = "invisible";
587 feedr.className = feedr.className.replace("Unread", "");
588 }
589 }
590 }
591
592 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
593
594 var feeds_stored = number_of_feeds;
595
596 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
597
598 if (feeds_stored != feeds_found) {
599 number_of_feeds = feeds_found;
600
601 if (feeds_stored != 0 && feeds_found != 0) {
602 debug("Subscribed feed number changed, refreshing feedlist");
603 setTimeout('updateFeedList(false, false)', 50);
604 }
605 }
606
607 } catch (e) {
608 exception_error("parse_counters", e);
609 }
610}
611
612function parse_counters_reply(transport, scheduled_call) {
613
614 if (!transport.responseXML) {
615 notify_error("Backend did not return valid XML", true);
616 return;
617 }
618
619 var reply = transport.responseXML.firstChild;
620
621 if (!reply) {
622 notify_error("Backend did not return expected XML object", true);
623 updateTitle("");
624 return;
625 }
626
627 var error_code = false;
628 var error_msg = false;
629
630 if (reply.firstChild) {
631 error_code = reply.firstChild.getAttribute("error-code");
632 error_msg = reply.firstChild.getAttribute("error-msg");
633 }
634
635 if (!error_code) {
636 error_code = reply.getAttribute("error-code");
637 error_msg = reply.getAttribute("error-msg");
638 }
639
640 if (error_code && error_code != 0) {
641 debug("refetch_callback: got error code " + error_code);
642 return fatalError(error_code, error_msg);
643 }
644
645 var counters = reply.getElementsByTagName("counters")[0];
646
647 parse_counters(counters, scheduled_call);
648
649 var runtime_info = reply.getElementsByTagName("runtime-info")[0];
650
651 parse_runtime_info(runtime_info);
652
653 if (getInitParam("feeds_sort_by_unread") == 1) {
654 resort_feedlist();
655 }
656
657 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
658
659}
660
661function all_counters_callback2(transport) {
662 try {
663 debug("<b>all_counters_callback2 IN: " + transport + "</b>");
664 parse_counters_reply(transport);
665 debug("<b>all_counters_callback2 OUT: " + transport + "</b>");
666
667 } catch (e) {
668 exception_error("all_counters_callback2", e);
669 }
670}
671
672function get_feed_unread(id) {
673 try {
674 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);
675 } catch (e) {
676 exception_error("get_feed_unread", e, true);
677 return -1;
678 }
679}
680
681function get_feed_entry_unread(doc, elem) {
682
683 var id = elem.id.replace("FEEDR-", "");
684
685 if (id <= 0) {
686 return -1;
687 }
688
689 try {
690 return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);
691 } catch (e) {
692 return -1;
693 }
694}
695
696function resort_category(doc, node) {
697 debug("resort_category: " + node);
698
699 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
700 for (i = 0; i < node.childNodes.length; i++) {
701 if (node.childNodes[i].nodeName != "LI") { continue; }
702
703 if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
704 continue;
705 }
706
707 for (j = i+1; j < node.childNodes.length; j++) {
708 if (node.childNodes[j].nodeName != "LI") { continue; }
709
710 var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
711 var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
712
713 if (cur_val > tmp_val) {
714 tempnode_i = node.childNodes[i].cloneNode(true);
715 tempnode_j = node.childNodes[j].cloneNode(true);
716 node.replaceChild(tempnode_i, node.childNodes[j]);
717 node.replaceChild(tempnode_j, node.childNodes[i]);
718 }
719 }
720
721 }
722 }
723
724}
725
726function resort_feedlist() {
727 debug("resort_feedlist");
728
729 var fd = document;
730
731 if (fd.getElementById("feedCatHolder")) {
732
733 var feeds = fd.getElementById("feedList");
734 var child = feeds.firstChild;
735
736 while (child) {
737
738 if (child.id == "feedCatHolder") {
739 resort_category(fd, child.firstChild);
740 }
741
742 child = child.nextSibling;
743 }
744
745 } else {
746 resort_category(fd, fd.getElementById("feedList"));
747 }
748}
749
750/** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
751 * * @author Sundar Dorai-Raj
752 * * Email: sdoraira@vt.edu
753 * * This program is free software; you can redistribute it and/or
754 * * modify it under the terms of the GNU General Public License
755 * * as published by the Free Software Foundation; either version 2
756 * * of the License, or (at your option) any later version,
757 * * provided that any use properly credits the author.
758 * * This program is distributed in the hope that it will be useful,
759 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
760 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
761 * * GNU General Public License for more details at http://www.gnu.org * * */
762
763 var numbers=".0123456789";
764 function isNumeric(x) {
765 // is x a String or a character?
766 if(x.length>1) {
767 // remove negative sign
768 x=Math.abs(x)+"";
769 for(j=0;j<x.length;j++) {
770 // call isNumeric recursively for each character
771 number=isNumeric(x.substring(j,j+1));
772 if(!number) return number;
773 }
774 return number;
775 }
776 else {
777 // if x is number return true
778 if(numbers.indexOf(x)>=0) return true;
779 return false;
780 }
781 }
782
783
784function hideOrShowFeeds(doc, hide) {
785
786 debug("hideOrShowFeeds: " + doc + ", " + hide);
787
788 var fd = document;
789
790 var list = fd.getElementById("feedList");
791
792 if (fd.getElementById("feedCatHolder")) {
793
794 var feeds = fd.getElementById("feedList");
795 var child = feeds.firstChild;
796
797 while (child) {
798
799 if (child.id == "feedCatHolder") {
800 hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
801 }
802
803 child = child.nextSibling;
804 }
805
806 } else {
807 hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
808 }
809}
810
811function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
812
813// debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
814
815 var cat_unread = 0;
816
817 if (!node) {
818 debug("hideOrShowFeeds: passed node is null, aborting");
819 return;
820 }
821
822// debug("cat: " + node.id);
823
824 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
825 for (i = 0; i < node.childNodes.length; i++) {
826 if (node.childNodes[i].nodeName != "LI") { continue; }
827
828 if (node.childNodes[i].style != undefined) {
829
830 var has_unread = (node.childNodes[i].className != "feed" &&
831 node.childNodes[i].className != "label" &&
832 !(!getInitParam("hide_read_shows_special") &&
833 node.childNodes[i].className == "virt") &&
834 node.childNodes[i].className != "error" &&
835 node.childNodes[i].className != "tag");
836
837// debug(node.childNodes[i].id + " --> " + has_unread);
838
839 if (hide && !has_unread) {
840 //node.childNodes[i].style.display = "none";
841 var id = node.childNodes[i].id;
842 Effect.Fade(node.childNodes[i], {duration : 0.3,
843 queue: { position: 'end', scope: 'FFADE-' + id, limit: 1 }});
844 }
845
846 if (!hide) {
847 node.childNodes[i].style.display = "list-item";
848 //Effect.Appear(node.childNodes[i], {duration : 0.3});
849 }
850
851 if (has_unread) {
852 node.childNodes[i].style.display = "list-item";
853 cat_unread++;
854 //Effect.Appear(node.childNodes[i], {duration : 0.3});
855 //Effect.Highlight(node.childNodes[i]);
856 }
857 }
858 }
859 }
860
861// debug("end cat: " + node.id + " unread " + cat_unread);
862
863 if (cat_unread == 0) {
864 if (cat_node.style == undefined) {
865 debug("ERROR: supplied cat_node " + cat_node +
866 " has no styles. WTF?");
867 return;
868 }
869 if (hide) {
870 //cat_node.style.display = "none";
871 Effect.Fade(cat_node, {duration : 0.3,
872 queue: { position: 'end', scope: 'CFADE-' + node.id, limit: 1 }});
873 } else {
874 cat_node.style.display = "list-item";
875 }
876 } else {
877 try {
878 cat_node.style.display = "list-item";
879 } catch (e) {
880 debug(e);
881 }
882 }
883
884// debug("unread for category: " + cat_unread);
885}
886
887function selectTableRow(r, do_select) {
888 r.className = r.className.replace("Selected", "");
889
890 if (do_select) {
891 r.className = r.className + "Selected";
892 }
893}
894
895function selectTableRowById(elem_id, check_id, do_select) {
896
897 try {
898
899 var row = document.getElementById(elem_id);
900
901 if (row) {
902 selectTableRow(row, do_select);
903 }
904
905 var check = document.getElementById(check_id);
906
907 if (check) {
908 check.checked = do_select;
909 }
910 } catch (e) {
911 exception_error("selectTableRowById", e);
912 }
913}
914
915function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
916 classcheck, reset_others) {
917
918 var content = document.getElementById(content_id);
919
920 if (!content) {
921 alert("[selectTableRows] Element " + content_id + " not found.");
922 return;
923 }
924
925 for (i = 0; i < content.rows.length; i++) {
926 if (Element.visible(content.rows[i])) {
927 if (!classcheck || content.rows[i].className.match(classcheck)) {
928
929 if (content.rows[i].id.match(prefix)) {
930 selectTableRow(content.rows[i], do_select);
931
932 var row_id = content.rows[i].id.replace(prefix, "");
933 var check = document.getElementById(check_prefix + row_id);
934
935 if (check) {
936 check.checked = do_select;
937 }
938 } else if (reset_others) {
939 selectTableRow(content.rows[i], false);
940
941 var row_id = content.rows[i].id.replace(prefix, "");
942 var check = document.getElementById(check_prefix + row_id);
943
944 if (check) {
945 check.checked = false;
946 }
947
948 }
949 } else if (reset_others) {
950 selectTableRow(content.rows[i], false);
951
952 var row_id = content.rows[i].id.replace(prefix, "");
953 var check = document.getElementById(check_prefix + row_id);
954
955 if (check) {
956 check.checked = false;
957 }
958
959 }
960 }
961 }
962}
963
964function getSelectedTableRowIds(content_id, prefix) {
965
966 var content = document.getElementById(content_id);
967
968 if (!content) {
969 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
970 return;
971 }
972
973 var sel_rows = new Array();
974
975 for (i = 0; i < content.rows.length; i++) {
976 if (content.rows[i].id.match(prefix) &&
977 content.rows[i].className.match("Selected")) {
978
979 var row_id = content.rows[i].id.replace(prefix + "-", "");
980 sel_rows.push(row_id);
981 }
982 }
983
984 return sel_rows;
985
986}
987
988function toggleSelectRowById(sender, id) {
989 var row = document.getElementById(id);
990
991 if (sender.checked) {
992 if (!row.className.match("Selected")) {
993 row.className = row.className + "Selected";
994 }
995 } else {
996 if (row.className.match("Selected")) {
997 row.className = row.className.replace("Selected", "");
998 }
999 }
1000}
1001
1002function toggleSelectListRow(sender) {
1003 var parent_row = sender.parentNode;
1004
1005 if (sender.checked) {
1006 if (!parent_row.className.match("Selected")) {
1007 parent_row.className = parent_row.className + "Selected";
1008 }
1009 } else {
1010 if (parent_row.className.match("Selected")) {
1011 parent_row.className = parent_row.className.replace("Selected", "");
1012 }
1013 }
1014}
1015
1016function tSR(sender) {
1017 return toggleSelectRow(sender);
1018}
1019
1020function toggleSelectRow(sender) {
1021 var parent_row = sender.parentNode.parentNode;
1022
1023 if (sender.checked) {
1024 if (!parent_row.className.match("Selected")) {
1025 parent_row.className = parent_row.className + "Selected";
1026 }
1027 } else {
1028 if (parent_row.className.match("Selected")) {
1029 parent_row.className = parent_row.className.replace("Selected", "");
1030 }
1031 }
1032}
1033
1034function getRelativeFeedId(list, id, direction, unread_only) {
1035 var rows = list.getElementsByTagName("LI");
1036 var feeds = new Array();
1037
1038 for (var i = 0; i < rows.length; i++) {
1039 if (rows[i].id.match("FEEDR-")) {
1040
1041 if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1042
1043 if (!unread_only ||
1044 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1045 feeds.push(rows[i].id.replace("FEEDR-", ""));
1046 }
1047 }
1048 }
1049 }
1050
1051 if (!id) {
1052 if (direction == "next") {
1053 return feeds.shift();
1054 } else {
1055 return feeds.pop();
1056 }
1057 } else {
1058 if (direction == "next") {
1059 var idx = feeds.indexOf(id);
1060 if (idx != -1 && idx < feeds.length) {
1061 return feeds[idx+1];
1062 } else {
1063 return getRelativeFeedId(list, false, direction, unread_only);
1064 }
1065 } else {
1066 var idx = feeds.indexOf(id);
1067 if (idx > 0) {
1068 return feeds[idx-1];
1069 } else {
1070 return getRelativeFeedId(list, false, direction, unread_only);
1071 }
1072 }
1073
1074 }
1075}
1076
1077function showBlockElement(id, h_id) {
1078 var elem = document.getElementById(id);
1079
1080 if (elem) {
1081 elem.style.display = "block";
1082
1083 if (h_id) {
1084 elem = document.getElementById(h_id);
1085 if (elem) {
1086 elem.style.display = "none";
1087 }
1088 }
1089 } else {
1090 alert("[showBlockElement] can't find element with id " + id);
1091 }
1092}
1093
1094function appearBlockElement_afh(effect) {
1095
1096}
1097
1098function checkboxToggleElement(elem, id) {
1099 if (elem.checked) {
1100 Effect.SlideDown(id, {duration : 0.5});
1101 } else {
1102 Effect.SlideUp(id, {duration : 0.5});
1103 }
1104}
1105
1106function appearBlockElement(id, h_id) {
1107
1108 try {
1109 if (h_id) {
1110 Effect.Fade(h_id);
1111 }
1112 Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
1113 } catch (e) {
1114 exception_error("appearBlockElement", e);
1115 }
1116
1117}
1118
1119function hideParentElement(e) {
1120 e.parentNode.style.display = "none";
1121}
1122
1123function dropboxSelect(e, v) {
1124 for (i = 0; i < e.length; i++) {
1125 if (e[i].value == v) {
1126 e.selectedIndex = i;
1127 break;
1128 }
1129 }
1130}
1131
1132// originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1133// bugfixed just a little bit :-)
1134function getURLParam(strParamName){
1135 var strReturn = "";
1136 var strHref = window.location.href;
1137
1138 if (strHref.indexOf("#") == strHref.length-1) {
1139 strHref = strHref.substring(0, strHref.length-1);
1140 }
1141
1142 if ( strHref.indexOf("?") > -1 ){
1143 var strQueryString = strHref.substr(strHref.indexOf("?"));
1144 var aQueryString = strQueryString.split("&");
1145 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1146 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1147 var aParam = aQueryString[iParam].split("=");
1148 strReturn = aParam[1];
1149 break;
1150 }
1151 }
1152 }
1153 return strReturn;
1154}
1155
1156function leading_zero(p) {
1157 var s = String(p);
1158 if (s.length == 1) s = "0" + s;
1159 return s;
1160}
1161
1162function closeInfoBox(cleanup) {
1163
1164 if (!is_msie() && !getInitParam("infobox_disable_overlay")) {
1165 var overlay = document.getElementById("dialog_overlay");
1166 if (overlay) {
1167 overlay.style.display = "none";
1168 }
1169 }
1170
1171 var box = document.getElementById('infoBox');
1172 var shadow = document.getElementById('infoBoxShadow');
1173
1174 if (shadow) {
1175 shadow.style.display = "none";
1176 } else if (box) {
1177 box.style.display = "none";
1178 }
1179
1180 if (cleanup) box.innerHTML = "&nbsp;";
1181
1182 enableHotkeys();
1183
1184 return false;
1185}
1186
1187
1188function displayDlg(id, param) {
1189
1190 notify_progress("Loading, please wait...", true);
1191
1192 disableHotkeys();
1193
1194 var query = "backend.php?op=dlg&id=" +
1195 param_escape(id) + "&param=" + param_escape(param);
1196
1197 new Ajax.Request(query, {
1198 onComplete: function (transport) {
1199 infobox_callback2(transport);
1200 } });
1201
1202 return false;
1203}
1204
1205function infobox_submit_callback2(transport) {
1206 closeInfoBox();
1207
1208 try {
1209 // called from prefs, reload tab
1210 if (typeof active_tab != 'undefined' && active_tab) {
1211 selectTab(active_tab, false);
1212 }
1213 } catch (e) { }
1214
1215 if (transport.responseText) {
1216 notify_info(transport.responseText);
1217 }
1218}
1219
1220function infobox_callback2(transport) {
1221 try {
1222
1223 debug("infobox_callback2");
1224
1225 if (!is_msie() && !getInitParam("infobox_disable_overlay")) {
1226 var overlay = document.getElementById("dialog_overlay");
1227 if (overlay) {
1228 overlay.style.display = "block";
1229 }
1230 }
1231
1232 var box = document.getElementById('infoBox');
1233 var shadow = document.getElementById('infoBoxShadow');
1234 if (box) {
1235
1236/* if (!is_safari()) {
1237 new Draggable(shadow);
1238 } */
1239
1240 box.innerHTML=transport.responseText;
1241 if (shadow) {
1242 shadow.style.display = "block";
1243 } else {
1244 box.style.display = "block";
1245 }
1246 }
1247
1248 /* FIXME this needs to be moved out somewhere */
1249
1250 if (document.getElementById("tags_choices")) {
1251 new Ajax.Autocompleter('tags_str', 'tags_choices',
1252 "backend.php?op=rpc&subop=completeTags",
1253 { tokens: ',', paramName: "search" });
1254 }
1255
1256 disableHotkeys();
1257
1258 notify("");
1259 } catch (e) {
1260 exception_error("infobox_callback2", e);
1261 }
1262}
1263
1264function createFilter() {
1265
1266 try {
1267
1268 var form = document.forms['filter_add_form'];
1269 var reg_exp = form.reg_exp.value;
1270
1271 if (reg_exp == "") {
1272 alert(__("Can't add filter: nothing to match on."));
1273 return false;
1274 }
1275
1276 var query = Form.serialize("filter_add_form");
1277
1278 // we can be called from some other tab in Prefs
1279 if (typeof active_tab != 'undefined' && active_tab) {
1280 active_tab = "filterConfig";
1281 }
1282
1283 new Ajax.Request("backend.php?" + query, {
1284 onComplete: function (transport) {
1285 infobox_submit_callback2(transport);
1286 } });
1287
1288 return true;
1289
1290 } catch (e) {
1291 exception_error("createFilter", e);
1292 }
1293}
1294
1295function toggleSubmitNotEmpty(e, submit_id) {
1296 try {
1297 document.getElementById(submit_id).disabled = (e.value == "")
1298 } catch (e) {
1299 exception_error("toggleSubmitNotEmpty", e);
1300 }
1301}
1302
1303function isValidURL(s) {
1304 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
1305}
1306
1307function subscribeToFeed() {
1308
1309 var form = document.forms['feed_add_form'];
1310 var feed_url = form.feed_url.value;
1311
1312 if (feed_url == "") {
1313 alert(__("Can't subscribe: no feed URL given."));
1314 return false;
1315 }
1316
1317 notify_progress(__("Subscribing to feed..."), true);
1318
1319 closeInfoBox();
1320
1321 var feeds_doc = document;
1322
1323// feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1324
1325 var query = Form.serialize("feed_add_form");
1326
1327 debug("subscribe q: " + query);
1328
1329 new Ajax.Request("backend.php", {
1330 parameters: query,
1331 onComplete: function(transport) {
1332 dlg_frefresh_callback(transport);
1333 } });
1334
1335 return false;
1336}
1337
1338function filterCR(e, f)
1339{
1340 var key;
1341
1342 if(window.event)
1343 key = window.event.keyCode; //IE
1344 else
1345 key = e.which; //firefox
1346
1347 if (key == 13) {
1348 if (typeof f != 'undefined') {
1349 f();
1350 return false;
1351 } else {
1352 return false;
1353 }
1354 } else {
1355 return true;
1356 }
1357}
1358
1359function getMainContext() {
1360 return this.window;
1361}
1362
1363function getFeedsContext() {
1364 return this.window;
1365}
1366
1367function getContentContext() {
1368 return this.window;
1369}
1370
1371function getHeadlinesContext() {
1372 return this.window;
1373}
1374
1375var debug_last_class = "even";
1376
1377function debug(msg) {
1378
1379 if (debug_last_class == "even") {
1380 debug_last_class = "odd";
1381 } else {
1382 debug_last_class = "even";
1383 }
1384
1385 var c = document.getElementById('debug_output');
1386 if (c && Element.visible(c)) {
1387 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
1388 c.removeChild(c.lastChild);
1389 }
1390
1391 var d = new Date();
1392 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1393 ":" + leading_zero(d.getSeconds());
1394 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " +
1395 msg + "</li>" + c.innerHTML;
1396 }
1397}
1398
1399function getInitParam(key) {
1400 return init_params[key];
1401}
1402
1403function storeInitParam(key, value) {
1404 debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
1405 init_params[key] = value;
1406}
1407
1408function fatalError(code, message) {
1409 try {
1410
1411 if (code == 6) {
1412 window.location.href = "tt-rss.php";
1413 } else if (code == 5) {
1414 window.location.href = "update.php";
1415 } else {
1416 var fe = document.getElementById("fatal_error");
1417 var fc = document.getElementById("fatal_error_msg");
1418
1419 if (message == "") message = "Unknown error";
1420
1421 fc.innerHTML = "<img src='images/sign_excl.gif'> " + message + " (Code " + code + ")";
1422
1423 fe.style.display = "block";
1424 }
1425
1426 } catch (e) {
1427 exception_error("fatalError", e);
1428 }
1429}
1430
1431function getFeedName(id, is_cat) {
1432 var d = getFeedsContext().document;
1433
1434 var e;
1435
1436 if (is_cat) {
1437 e = d.getElementById("FCATN-" + id);
1438 } else {
1439 e = d.getElementById("FEEDN-" + id);
1440 }
1441 if (e) {
1442 return e.innerHTML.stripTags();
1443 } else {
1444 return null;
1445 }
1446}
1447
1448function viewContentUrl(url) {
1449 getContentContext().location = url;
1450}
1451
1452function filterDlgCheckAction(sender) {
1453
1454 try {
1455
1456 var action = sender[sender.selectedIndex].value;
1457
1458 var form = document.forms["filter_add_form"];
1459
1460 if (!form) {
1461 form = document.forms["filter_edit_form"];
1462 }
1463
1464 if (!form) {
1465 debug("filterDlgCheckAction: can't find form!");
1466 return;
1467 }
1468
1469 var action_param = form.action_param;
1470
1471 if (!action_param) {
1472 debug("filterDlgCheckAction: can't find action param!");
1473 return;
1474 }
1475
1476 // if selected action supports parameters, enable params field
1477 if (action == 4 || action == 6) {
1478 action_param.disabled = false;
1479 } else {
1480 action_param.disabled = true;
1481 }
1482
1483 } catch (e) {
1484 exception_error("filterDlgCheckAction", e);
1485 }
1486
1487}
1488
1489function explainError(code) {
1490 return displayDlg("explainError", code);
1491}
1492
1493// this only searches loaded headlines list, not in CDM
1494function getRelativePostIds(id, limit) {
1495
1496 if (!limit) limit = 3;
1497
1498 debug("getRelativePostIds: " + id + " limit=" + limit);
1499
1500 var ids = new Array();
1501 var container = document.getElementById("headlinesList");
1502
1503 if (container) {
1504 var rows = container.rows;
1505
1506 for (var i = 0; i < rows.length; i++) {
1507 var r_id = rows[i].id.replace("RROW-", "");
1508
1509 if (r_id == id) {
1510/* if (i > 0) ids.push(rows[i-1].id.replace("RROW-", ""));
1511 if (i > 1) ids.push(rows[i-2].id.replace("RROW-", ""));
1512 if (i > 2) ids.push(rows[i-3].id.replace("RROW-", ""));
1513
1514 if (i < rows.length-1) ids.push(rows[i+1].id.replace("RROW-", ""));
1515 if (i < rows.length-2) ids.push(rows[i+2].id.replace("RROW-", ""));
1516 if (i < rows.length-3) ids.push(rows[i+3].id.replace("RROW-", "")); */
1517
1518 for (var k = 1; k <= limit; k++) {
1519 var nid = false;
1520
1521 if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
1522 if (nid) ids.push(nid);
1523
1524 if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
1525 if (nid) ids.push(nid);
1526 }
1527
1528 return ids;
1529 }
1530 }
1531 }
1532
1533 return false;
1534}
1535
1536function openArticleInNewWindow(id) {
1537 try {
1538 debug("openArticleInNewWindow: " + id);
1539
1540 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
1541 var wname = "ttrss_article_" + id;
1542
1543 debug(query + " " + wname);
1544
1545 var w = window.open("", wname);
1546
1547 if (!w) notify_error("Failed to open window for the article");
1548
1549 new Ajax.Request(query, {
1550 onComplete: function(transport) {
1551 open_article_callback(transport);
1552 } });
1553
1554
1555 } catch (e) {
1556 exception_error("openArticleInNewWindow", e);
1557 }
1558}
1559
1560/* http://textsnippets.com/posts/show/835 */
1561
1562Position.GetWindowSize = function(w) {
1563 w = w ? w : window;
1564 var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
1565 var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
1566 return [width, height]
1567}
1568
1569/* http://textsnippets.com/posts/show/836 */
1570
1571Position.Center = function(element, parent) {
1572 var w, h, pw, ph;
1573 var d = Element.getDimensions(element);
1574 w = d.width;
1575 h = d.height;
1576 Position.prepare();
1577 if (!parent) {
1578 var ws = Position.GetWindowSize();
1579 pw = ws[0];
1580 ph = ws[1];
1581 } else {
1582 pw = parent.offsetWidth;
1583 ph = parent.offsetHeight;
1584 }
1585 element.style.top = (ph/2) - (h/2) - Position.deltaY + "px";
1586 element.style.left = (pw/2) - (w/2) - Position.deltaX + "px";
1587}
1588
1589
1590function labeltest_callback(transport) {
1591 try {
1592 var container = document.getElementById('label_test_result');
1593
1594 container.innerHTML = transport.responseText;
1595 if (!Element.visible(container)) {
1596 Effect.SlideDown(container, { duration : 0.5 });
1597 }
1598
1599 notify("");
1600 } catch (e) {
1601 exception_error("labeltest_callback", e);
1602 }
1603}
1604
1605function labelTest() {
1606
1607 try {
1608 var container = document.getElementById('label_test_result');
1609
1610 var form = document.forms['label_edit_form'];
1611
1612 var sql_exp = form.sql_exp.value;
1613 var description = form.description.value;
1614
1615 notify_progress("Loading, please wait...");
1616
1617 var query = "backend.php?op=pref-labels&subop=test&expr=" +
1618 param_escape(sql_exp) + "&descr=" + param_escape(description);
1619
1620 new Ajax.Request(query, {
1621 onComplete: function (transport) {
1622 labeltest_callback(transport);
1623 } });
1624
1625 return false;
1626
1627 } catch (e) {
1628 exception_error("labelTest", e);
1629 }
1630}
1631
1632function isCdmMode() {
1633 return !document.getElementById("headlinesList");
1634}
1635
1636function getSelectedArticleIds2() {
1637 var rows = new Array();
1638 var cdm_mode = isCdmMode();
1639
1640 if (cdm_mode) {
1641 rows = cdmGetSelectedArticles();
1642 } else {
1643 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1644 }
1645
1646 var ids = new Array();
1647
1648 for (var i = 0; i < rows.length; i++) {
1649 var chk = document.getElementById("RCHK-" + rows[i]);
1650 if (chk && chk.checked) {
1651 ids.push(rows[i]);
1652 }
1653 }
1654
1655 return ids;
1656}
1657
1658function displayHelpInfobox(topic_id) {
1659
1660 var url = "backend.php?op=help&tid=" + param_escape(topic_id);
1661
1662 var w = window.open(url, "ttrss_help",
1663 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
1664
1665 return false;
1666}
1667
1668function focus_element(id) {
1669 try {
1670 var e = document.getElementById(id);
1671 if (e) e.focus();
1672 } catch (e) {
1673 exception_error("focus_element", e);
1674 }
1675 return false;
1676}
1677
1678function loading_set_progress(p) {
1679 try {
1680 if (p < last_progress_point || !Element.visible("overlay")) return;
1681
1682 debug("<b>loading_set_progress : " + p + " (" + last_progress_point + ")</b>");
1683
1684 var o = document.getElementById("l_progress_i");
1685
1686// o.style.width = (p * 2) + "px";
1687
1688 new Effect.Scale(o, p, {
1689 scaleY : false,
1690 scaleFrom : last_progress_point,
1691 scaleMode: { originalWidth : 200 },
1692 queue: { position: 'end', scope: 'LSP-Q', limit: 3 } });
1693
1694 last_progress_point = p;
1695
1696 } catch (e) {
1697 exception_error("loading_set_progress", e);
1698 }
1699}