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