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