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