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