]> git.wh0rd.org - tt-rss.git/blob - functions.js
tag display now works
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2
3 function exception_error(location, e) {
4 var msg;
5
6 if (e.fileName) {
7 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
8
9 msg = "Exception: " + e.name + ", " + e.message +
10 "\nFunction: " + location + "()" +
11 "\nLocation: " + base_fname + ":" + e.lineNumber;
12 } else {
13 msg = "Exception: " + e + "\nFunction: " + location + "()";
14 }
15
16 alert(msg);
17 }
18
19 function disableHotkeys() {
20 hotkeys_enabled = false;
21 }
22
23 function enableHotkeys() {
24 hotkeys_enabled = true;
25 }
26
27 function xmlhttp_ready(obj) {
28 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
29 }
30
31
32 function notify_callback() {
33 var container = document.getElementById('notify');
34 if (xmlhttp.readyState == 4) {
35 container.innerHTML=xmlhttp.responseText;
36 }
37 }
38
39 function rpc_notify_callback() {
40 var container = document.getElementById('notify');
41 if (xmlhttp_rpc.readyState == 4) {
42 container.innerHTML=xmlhttp_rpc.responseText;
43 }
44 }
45
46 function rpc_pnotify_callback() {
47 var container = parent.document.getElementById('notify');
48 if (xmlhttp_rpc.readyState == 4) {
49 container.innerHTML=xmlhttp_rpc.responseText;
50 }
51 }
52
53 function param_escape(arg) {
54 if (typeof encodeURIComponent != 'undefined')
55 return encodeURIComponent(arg);
56 else
57 return escape(arg);
58 }
59
60 function param_unescape(arg) {
61 if (typeof decodeURIComponent != 'undefined')
62 return decodeURIComponent(arg);
63 else
64 return unescape(arg);
65 }
66
67 function delay(gap) {
68 var then,now;
69 then=new Date().getTime();
70 now=then;
71 while((now-then)<gap) {
72 now=new Date().getTime();
73 }
74 }
75
76 function p_notify(msg) {
77
78 var n = parent.document.getElementById("notify");
79 var nb = parent.document.getElementById("notify_body");
80
81 if (!n || !nb) return;
82
83 if (msg == "") {
84 nb.innerHTML = "&nbsp;";
85 // n.style.background = "#ffffff";
86 } else {
87 nb.innerHTML = msg;
88 // n.style.background = "#fffff0";
89 }
90 }
91
92 function notify(msg) {
93
94 var n = document.getElementById("notify");
95 var nb = document.getElementById("notify_body");
96
97 if (!n || !nb) return;
98
99 if (msg == "") {
100 nb.innerHTML = "&nbsp;";
101 // n.style.background = "#ffffff";
102 } else {
103 nb.innerHTML = msg;
104 // n.style.background = "#fffff0";
105 }
106
107 }
108
109 function printLockingError() {
110 notify("Please wait until operation finishes");}
111
112 var seq = "";
113
114 function hotkey_handler(e) {
115
116 var keycode;
117
118 if (!hotkeys_enabled) return;
119
120 if (window.event) {
121 keycode = window.event.keyCode;
122 } else if (e) {
123 keycode = e.which;
124 }
125
126 if (keycode == 13 || keycode == 27) {
127 seq = "";
128 } else {
129 seq = seq + "" + keycode;
130 }
131
132 if (document.getElementById("piggie")) {
133
134 if (seq.match("807371717369")) {
135 seq = "";
136 localPiggieFunction(true);
137 } else {
138 localPiggieFunction(false);
139 }
140 }
141
142 if (typeof localHotkeyHandler != 'undefined') {
143 try {
144 localHotkeyHandler(keycode);
145 } catch (e) {
146 exception_error("hotkey_handler", e);
147 }
148 }
149
150 }
151
152 function cleanSelectedList(element) {
153 var content = document.getElementById(element);
154
155 if (!document.getElementById("feedCatHolder")) {
156 for (i = 0; i < content.childNodes.length; i++) {
157 var child = content.childNodes[i];
158 try {
159 child.className = child.className.replace("Selected", "");
160 } catch (e) {
161 //
162 }
163 }
164 } else {
165 for (i = 0; i < content.childNodes.length; i++) {
166 var child = content.childNodes[i];
167
168 if (child.id == "feedCatHolder") {
169 var fcat = child.lastChild;
170 for (j = 0; j < fcat.childNodes.length; j++) {
171 var feed = fcat.childNodes[j];
172 feed.className = feed.className.replace("Selected", "");
173 }
174 }
175 }
176
177 }
178 }
179
180
181 function cleanSelected(element) {
182 var content = document.getElementById(element);
183
184 for (i = 0; i < content.rows.length; i++) {
185 content.rows[i].className = content.rows[i].className.replace("Selected", "");
186 }
187
188 }
189
190 function getVisibleUnreadHeadlines() {
191 var content = document.getElementById("headlinesList");
192
193 var rows = new Array();
194
195 for (i = 0; i < content.rows.length; i++) {
196 var row_id = content.rows[i].id.replace("RROW-", "");
197 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
198 rows.push(row_id);
199 }
200 }
201 return rows;
202 }
203
204 function getVisibleHeadlineIds() {
205
206 var content = document.getElementById("headlinesList");
207
208 var rows = new Array();
209
210 for (i = 0; i < content.rows.length; i++) {
211 var row_id = content.rows[i].id.replace("RROW-", "");
212 if (row_id.length > 0) {
213 rows.push(row_id);
214 }
215 }
216 return rows;
217 }
218
219 function getFirstVisibleHeadlineId() {
220 var rows = getVisibleHeadlineIds();
221 return rows[0];
222 }
223
224 function getLastVisibleHeadlineId() {
225 var rows = getVisibleHeadlineIds();
226 return rows[rows.length-1];
227 }
228
229 function markHeadline(id) {
230 var row = document.getElementById("RROW-" + id);
231 if (row) {
232 var is_active = false;
233
234 if (row.className.match("Active")) {
235 is_active = true;
236 }
237 row.className = row.className.replace("Selected", "");
238 row.className = row.className.replace("Active", "");
239 row.className = row.className.replace("Insensitive", "");
240
241 if (is_active) {
242 row.className = row.className = "Active";
243 }
244
245 row.className = row.className + "Selected";
246
247 }
248 }
249
250 function getFeedIds() {
251 var content = document.getElementById("feedsList");
252
253 var rows = new Array();
254
255 for (i = 0; i < content.rows.length; i++) {
256 var id = content.rows[i].id.replace("FEEDR-", "");
257 if (id.length > 0) {
258 rows.push(id);
259 }
260 }
261
262 return rows;
263 }
264
265 function setCookie(name, value, expires, path, domain, secure) {
266 document.cookie= name + "=" + escape(value) +
267 ((expires) ? "; expires=" + expires.toGMTString() : "") +
268 ((path) ? "; path=" + path : "") +
269 ((domain) ? "; domain=" + domain : "") +
270 ((secure) ? "; secure" : "");
271 }
272
273 function getCookie(name) {
274
275 var dc = document.cookie;
276 var prefix = name + "=";
277 var begin = dc.indexOf("; " + prefix);
278 if (begin == -1) {
279 begin = dc.indexOf(prefix);
280 if (begin != 0) return null;
281 }
282 else {
283 begin += 2;
284 }
285 var end = document.cookie.indexOf(";", begin);
286 if (end == -1) {
287 end = dc.length;
288 }
289 return unescape(dc.substring(begin + prefix.length, end));
290 }
291
292 function disableContainerChildren(id, disable, doc) {
293
294 if (!doc) doc = document;
295
296 var container = doc.getElementById(id);
297
298 for (var i = 0; i < container.childNodes.length; i++) {
299 var child = container.childNodes[i];
300
301 try {
302 child.disabled = disable;
303 } catch (E) {
304
305 }
306
307 if (disable) {
308 if (child.className && child.className.match("button")) {
309 child.className = "disabledButton";
310 }
311 } else {
312 if (child.className && child.className.match("disabledButton")) {
313 child.className = "button";
314 }
315 }
316 }
317
318 }
319
320 function gotoPreferences() {
321 document.location.href = "prefs.php";
322 }
323
324 function gotoMain() {
325 document.location.href = "tt-rss.php";
326 }
327
328 function gotoExportOpml() {
329 document.location.href = "opml.php?op=Export";
330 }
331
332 function getActiveFeedId() {
333 return getCookie("ttrss_vf_actfeed");
334 }
335
336 function setActiveFeedId(id) {
337 return setCookie("ttrss_vf_actfeed", id);
338 }
339
340 var xmlhttp_rpc = false;
341
342 /*@cc_on @*/
343 /*@if (@_jscript_version >= 5)
344 // JScript gives us Conditional compilation, we can cope with old IE versions.
345 // and security blocked creation of the objects.
346 try {
347 xmlhttp_rpc = new ActiveXObject("Msxml2.XMLHTTP");
348 } catch (e) {
349 try {
350 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
351 } catch (E) {
352 xmlhttp_rpc = false;
353 }
354 }
355 @end @*/
356
357 if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
358 xmlhttp_rpc = new XMLHttpRequest();
359 }
360
361 function parse_counters(reply, f_document, title_obj) {
362 try {
363 for (var l = 0; l < reply.childNodes.length; l++) {
364 if (!reply.childNodes[l] ||
365 typeof(reply.childNodes[l].getAttribute) == "undefined") {
366 // where did this come from?
367 continue;
368 }
369
370 var id = reply.childNodes[l].getAttribute("id");
371 var t = reply.childNodes[l].getAttribute("type");
372 var ctr = reply.childNodes[l].getAttribute("counter");
373 var error = reply.childNodes[l].getAttribute("error");
374 var has_img = reply.childNodes[l].getAttribute("hi");
375
376 if (id == "global-unread") {
377 title_obj.global_unread = ctr;
378 title_obj.updateTitle();
379 continue;
380 }
381
382 if (t == "category") {
383 var catctr = f_document.getElementById("FCATCTR-" + id);
384 if (catctr) {
385 catctr.innerHTML = "(" + ctr + " unread)";
386 }
387 continue;
388 }
389
390 var feedctr = f_document.getElementById("FEEDCTR-" + id);
391 var feedu = f_document.getElementById("FEEDU-" + id);
392 var feedr = f_document.getElementById("FEEDR-" + id);
393 var feed_img = f_document.getElementById("FIMG-" + id);
394
395 if (feedctr && feedu && feedr) {
396
397 feedu.innerHTML = ctr;
398
399 if (error) {
400 feedr.className = feedr.className.replace("feed", "error");
401 } else if (id > 0) {
402 feedr.className = feedr.className.replace("error", "feed");
403 }
404
405 if (ctr > 0) {
406 feedctr.className = "odd";
407 if (!feedr.className.match("Unread")) {
408 var is_selected = feedr.className.match("Selected");
409
410 feedr.className = feedr.className.replace("Selected", "");
411 feedr.className = feedr.className.replace("Unread", "");
412
413 feedr.className = feedr.className + "Unread";
414
415 if (is_selected) {
416 feedr.className = feedr.className + "Selected";
417 }
418
419 }
420 } else {
421 feedctr.className = "invisible";
422 feedr.className = feedr.className.replace("Unread", "");
423 }
424 }
425 }
426 } catch (e) {
427 exception_error("parse_counters", e);
428 }
429 }
430
431 // this one is called from feedlist context
432 // thus title_obj passed to parse_counters is parent (e.g. main ttrss window)
433
434 function all_counters_callback() {
435 if (xmlhttp_rpc.readyState == 4) {
436 try {
437 if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
438 notify("[all_counters_callback] backend did not return valid XML");
439 return;
440 }
441
442 var reply = xmlhttp_rpc.responseXML.firstChild;
443 var f_document = parent.frames["feeds-frame"].document;
444
445 parse_counters(reply, f_document, parent);
446
447 } catch (e) {
448 exception_error("all_counters_callback", e);
449 }
450 }
451 }
452
453 function update_all_counters(feed) {
454 if (xmlhttp_ready(xmlhttp_rpc)) {
455 var query = "backend.php?op=rpc&subop=getAllCounters";
456
457 if (feed > 0) {
458 query = query + "&aid=" + feed;
459 }
460
461 xmlhttp_rpc.open("GET", query, true);
462 xmlhttp_rpc.onreadystatechange=all_counters_callback;
463 xmlhttp_rpc.send(null);
464 }
465 }
466
467 function popupHelp(tid) {
468 var w = window.open("backend.php?op=help&tid=" + tid,
469 "Popup Help",
470 "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
471 }
472
473 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
474 * * @author Sundar Dorai-Raj
475 * * Email: sdoraira@vt.edu
476 * * This program is free software; you can redistribute it and/or
477 * * modify it under the terms of the GNU General Public License
478 * * as published by the Free Software Foundation; either version 2
479 * * of the License, or (at your option) any later version,
480 * * provided that any use properly credits the author.
481 * * This program is distributed in the hope that it will be useful,
482 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
483 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
484 * * GNU General Public License for more details at http://www.gnu.org * * */
485
486 var numbers=".0123456789";
487 function isNumeric(x) {
488 // is x a String or a character?
489 if(x.length>1) {
490 // remove negative sign
491 x=Math.abs(x)+"";
492 for(j=0;j<x.length;j++) {
493 // call isNumeric recursively for each character
494 number=isNumeric(x.substring(j,j+1));
495 if(!number) return number;
496 }
497 return number;
498 }
499 else {
500 // if x is number return true
501 if(numbers.indexOf(x)>=0) return true;
502 return false;
503 }
504 }
505
506
507 function hideOrShowFeeds(doc, hide) {
508
509 if (!doc.styleSheets) return;
510
511 var css_rules = doc.styleSheets[0].cssRules;
512
513 if (!css_rules || !css_rules.length) return;
514
515 for (i = 0; i < css_rules.length; i++) {
516 var rule = css_rules[i];
517
518 if (rule.selectorText == "ul.feedList li.feed") {
519 if (!hide) {
520 rule.style.display = "block";
521 } else {
522 rule.style.display = "none";
523 }
524 }
525
526 }
527
528 }
529
530 function fatalError(code, params) {
531 if (!params) {
532 window.location = "error.php?c=" + param_escape(code);
533 } else {
534 window.location = "error.php?c=" + param_escape(code) +
535 "&p=" + param_escape(params);
536 }
537 }
538
539 function selectTableRow(r, do_select) {
540 r.className = r.className.replace("Selected", "");
541
542 if (do_select) {
543 r.className = r.className + "Selected";
544 }
545 }
546
547 function selectTableRowById(elem_id, check_id, do_select) {
548
549 try {
550
551 var row = document.getElementById(elem_id);
552
553 if (row) {
554 selectTableRow(row, do_select);
555 }
556
557 var check = document.getElementById(check_id);
558
559 if (check) {
560 check.checked = do_select;
561 }
562 } catch (e) {
563 exception_error("selectTableRowById", e);
564 }
565 }
566
567 function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
568 classcheck, reset_others) {
569
570 var content = document.getElementById(content_id);
571
572 if (!content) {
573 alert("[selectTableRows] Element " + content_id + " not found.");
574 return;
575 }
576
577 for (i = 0; i < content.rows.length; i++) {
578 if (!classcheck || content.rows[i].className.match(classcheck)) {
579
580 if (content.rows[i].id.match(prefix)) {
581 selectTableRow(content.rows[i], do_select);
582
583 var row_id = content.rows[i].id.replace(prefix, "");
584 var check = document.getElementById(check_prefix + row_id);
585
586 if (check) {
587 check.checked = do_select;
588 }
589 } else if (reset_others) {
590 selectTableRow(content.rows[i], false);
591
592 var row_id = content.rows[i].id.replace(prefix, "");
593 var check = document.getElementById(check_prefix + row_id);
594
595 if (check) {
596 check.checked = false;
597 }
598
599 }
600 } else if (reset_others) {
601 selectTableRow(content.rows[i], false);
602
603 var row_id = content.rows[i].id.replace(prefix, "");
604 var check = document.getElementById(check_prefix + row_id);
605
606 if (check) {
607 check.checked = false;
608 }
609
610 }
611 }
612 }
613
614 function getSelectedTableRowIds(content_id, prefix) {
615
616 var content = document.getElementById(content_id);
617
618 if (!content) {
619 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
620 return;
621 }
622
623 var sel_rows = new Array();
624
625 for (i = 0; i < content.rows.length; i++) {
626 if (content.rows[i].id.match(prefix) &&
627 content.rows[i].className.match("Selected")) {
628
629 var row_id = content.rows[i].id.replace(prefix + "-", "");
630 sel_rows.push(row_id);
631 }
632 }
633
634 return sel_rows;
635
636 }
637
638 function toggleSelectRowById(sender, id) {
639 var row = document.getElementById(id);
640
641 if (sender.checked) {
642 if (!row.className.match("Selected")) {
643 row.className = row.className + "Selected";
644 }
645 } else {
646 if (row.className.match("Selected")) {
647 row.className = row.className.replace("Selected", "");
648 }
649 }
650 }
651
652 function toggleSelectListRow(sender) {
653 var parent_row = sender.parentNode;
654
655 if (sender.checked) {
656 if (!parent_row.className.match("Selected")) {
657 parent_row.className = parent_row.className + "Selected";
658 }
659 } else {
660 if (parent_row.className.match("Selected")) {
661 parent_row.className = parent_row.className.replace("Selected", "");
662 }
663 }
664 }
665
666
667 function toggleSelectRow(sender) {
668 var parent_row = sender.parentNode.parentNode;
669
670 if (sender.checked) {
671 if (!parent_row.className.match("Selected")) {
672 parent_row.className = parent_row.className + "Selected";
673 }
674 } else {
675 if (parent_row.className.match("Selected")) {
676 parent_row.className = parent_row.className.replace("Selected", "");
677 }
678 }
679 }
680
681 function openExternalUrl(url) {
682 var w = window.open(url);
683 }
684
685
686 function getRelativeFeedId(list, id, direction) {
687 if (!id) {
688 if (direction == "next") {
689 for (i = 0; i < list.childNodes.length; i++) {
690 var child = list.childNodes[i];
691 if (child.id == "feedCatHolder") {
692 if (child.lastChild) {
693 var cr = getRelativeFeedId(child.firstChild, id, direction);
694 if (cr) return cr;
695 }
696 } else if (child.id.match("FEEDR-")) {
697 return child.id.replace('FEEDR-', '');
698 }
699 }
700 }
701
702 // FIXME select last feed doesn't work when only unread feeds are visible
703
704 if (direction == "prev") {
705 for (i = list.childNodes.length-1; i >= 0; i--) {
706 var child = list.childNodes[i];
707 if (child.id == "feedCatHolder") {
708 if (child.firstChild) {
709 var cr = getRelativeFeedId(child.firstChild, id, direction);
710 if (cr) return cr;
711 }
712 } else if (child.id.match("FEEDR-")) {
713
714 if (getCookie("ttrss_vf_hreadf") == 1) {
715 if (child.className != "feed") {
716 alert(child.className);
717 return child.id.replace('FEEDR-', '');
718 }
719 } else {
720 return child.id.replace('FEEDR-', '');
721 }
722 }
723 }
724 }
725 } else {
726
727 var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
728
729 if (direction == "next") {
730
731 if (feed.nextSibling) {
732
733 var next_feed = feed.nextSibling;
734
735 while (!next_feed.id && next_feed.nextSibling) {
736 next_feed = next_feed.nextSibling;
737 }
738
739 if (getCookie("ttrss_vf_hreadf") == 1) {
740 while (next_feed && next_feed.className == "feed") {
741 next_feed = next_feed.nextSibling;
742 }
743 }
744
745 if (next_feed && next_feed.id.match("FEEDR-")) {
746 return next_feed.id.replace("FEEDR-", "");
747 }
748 }
749
750 var this_cat = feed.parentNode.parentNode;
751
752 if (this_cat && this_cat.nextSibling) {
753 while (this_cat = this_cat.nextSibling) {
754 if (this_cat.firstChild && this_cat.firstChild.firstChild) {
755 var next_feed = this_cat.firstChild.firstChild;
756 if (getCookie("ttrss_vf_hreadf") == 1) {
757 while (next_feed && next_feed.className == "feed") {
758 next_feed = next_feed.nextSibling;
759 }
760 }
761 if (next_feed && next_feed.id.match("FEEDR-")) {
762 return next_feed.id.replace("FEEDR-", "");
763 }
764 }
765 }
766 }
767 } else if (direction == "prev") {
768
769 if (feed.previousSibling) {
770
771 var prev_feed = feed.previousSibling;
772
773 if (getCookie("ttrss_vf_hreadf") == 1) {
774 while (prev_feed && prev_feed.className == "feed") {
775 prev_feed = prev_feed.previousSibling;
776 }
777 }
778
779 while (!prev_feed.id && prev_feed.previousSibling) {
780 prev_feed = prev_feed.previousSibling;
781 }
782
783 if (prev_feed && prev_feed.id.match("FEEDR-")) {
784 return prev_feed.id.replace("FEEDR-", "");
785 }
786 }
787
788 var this_cat = feed.parentNode.parentNode;
789
790 if (this_cat && this_cat.previousSibling) {
791 while (this_cat = this_cat.previousSibling) {
792 if (this_cat.lastChild && this_cat.firstChild.lastChild) {
793 var prev_feed = this_cat.firstChild.lastChild;
794 if (getCookie("ttrss_vf_hreadf") == 1) {
795 while (prev_feed && prev_feed.className == "feed") {
796 prev_feed = prev_feed.previousSibling;
797 }
798 }
799 if (prev_feed && prev_feed.id.match("FEEDR-")) {
800 return prev_feed.id.replace("FEEDR-", "");
801 }
802 }
803 }
804 }
805 }
806 }
807 }
808
809 function showBlockElement(id) {
810 var elem = document.getElementById(id);
811
812 if (elem) {
813 elem.style.display = "block";
814 } else {
815 alert("[showBlockElement] can't find element with id " + id);
816 }
817 }
818
819 function hideParentElement(e) {
820 e.parentNode.style.display = "none";
821 }
822
823 function dropboxSelect(e, v) {
824 for (i = 0; i < e.length; i++) {
825 if (e[i].value == v) {
826 e.selectedIndex = i;
827 break;
828 }
829 }
830 }
831
832 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
833 // bugfixed just a little bit :-)
834 function getURLParam(strParamName){
835 var strReturn = "";
836 var strHref = window.location.href;
837
838 if (strHref.indexOf("#") == strHref.length-1) {
839 strHref = strHref.substring(0, strHref.length-1);
840 }
841
842 if ( strHref.indexOf("?") > -1 ){
843 var strQueryString = strHref.substr(strHref.indexOf("?"));
844 var aQueryString = strQueryString.split("&");
845 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
846 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
847 var aParam = aQueryString[iParam].split("=");
848 strReturn = aParam[1];
849 break;
850 }
851 }
852 }
853 return strReturn;
854 }
855
856 function leading_zero(p) {
857 var s = String(p);
858 if (s.length == 1) s = "0" + s;
859 return s;
860 }