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