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