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