]> git.wh0rd.org - tt-rss.git/blame - functions.js
rework hideOrShowFeeds(): now hides empty categories should be compatible with brows...
[tt-rss.git] / functions.js
CommitLineData
760966c1
AD
1var hotkeys_enabled = true;
2
a7565293
AD
3var xmlhttp_rpc = Ajax.getTransport();
4
292a8a12 5function browser_has_opacity() {
605f7d46
AD
6 return navigator.userAgent.match("Gecko") != null ||
7 navigator.userAgent.match("Opera") != null;
292a8a12
AD
8}
9
7719618b 10function exception_error(location, e) {
83f043bb
AD
11 var msg;
12
13 if (e.fileName) {
14 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
15
16 msg = "Exception: " + e.name + ", " + e.message +
17 "\nFunction: " + location + "()" +
18 "\nLocation: " + base_fname + ":" + e.lineNumber;
ee1f45f4 19
83f043bb
AD
20 } else {
21 msg = "Exception: " + e + "\nFunction: " + location + "()";
22 }
23
ee1f45f4
AD
24 debug("<b>EXCEPTION: " + msg + "</b>");
25
83f043bb 26 alert(msg);
7719618b
AD
27}
28
760966c1
AD
29function disableHotkeys() {
30 hotkeys_enabled = false;
31}
32
33function enableHotkeys() {
34 hotkeys_enabled = true;
35}
36
c0e5a40e
AD
37function xmlhttp_ready(obj) {
38 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
39}
40
9cfc649a
AD
41function notify_callback() {
42 var container = document.getElementById('notify');
43 if (xmlhttp.readyState == 4) {
44 container.innerHTML=xmlhttp.responseText;
45 }
46}
47
48function rpc_notify_callback() {
49 var container = document.getElementById('notify');
50 if (xmlhttp_rpc.readyState == 4) {
51 container.innerHTML=xmlhttp_rpc.responseText;
52 }
53}
54
7726fa02
AD
55function param_escape(arg) {
56 if (typeof encodeURIComponent != 'undefined')
57 return encodeURIComponent(arg);
58 else
59 return escape(arg);
60}
61
62function param_unescape(arg) {
63 if (typeof decodeURIComponent != 'undefined')
64 return decodeURIComponent(arg);
65 else
66 return unescape(arg);
67}
68
508a81e1
AD
69function delay(gap) {
70 var then,now;
71 then=new Date().getTime();
72 now=then;
73 while((now-then)<gap) {
74 now=new Date().getTime();
75 }
76}
7726fa02 77
ce3bf408
AD
78var notify_hide_timerid = false;
79var notify_last_doc = false;
80
59a543f0
AD
81var notify_effect = false;
82
ce3bf408
AD
83function hide_notify() {
84 if (notify_last_doc) {
85 var n = notify_last_doc.getElementById("notify");
292a8a12 86 if (browser_has_opacity()) {
ce3bf408 87 if (notify_opacity >= 0) {
0655a1d5 88 notify_opacity = notify_opacity - 0.1;
ce3bf408 89 n.style.opacity = notify_opacity;
292a8a12 90 notify_hide_timerid = window.setTimeout("hide_notify()", 20);
ce3bf408
AD
91 } else {
92 n.style.display = "none";
292a8a12 93 n.style.opacity = 1;
ce3bf408
AD
94 }
95 } else {
96 n.style.display = "none";
97 }
8dcfffd0 98 }
c05608c2
AD
99}
100
0530ddd8 101function notify_real(msg, doc, no_hide, is_err) {
7726fa02 102
ce3bf408
AD
103 var n = doc.getElementById("notify");
104 var nb = doc.getElementById("notify_body");
7726fa02 105
c05608c2 106 if (!n || !nb) return;
f0601b87 107
0655a1d5
AD
108 if (notify_hide_timerid) {
109 window.clearTimeout(notify_hide_timerid);
110 }
111
112 notify_last_doc = doc;
113 notify_opacity = 1;
114
8dcfffd0 115 if (msg == "") {
0655a1d5
AD
116 if (n.style.display == "block") {
117 notify_hide_timerid = window.setTimeout("hide_notify()", 0);
118 }
119 return;
7726fa02 120 } else {
0ceded7a 121 n.style.display = "block";
8dcfffd0 122 }
7726fa02 123
0530ddd8 124 if (is_err) {
caa53a7c
AD
125 n.style.backgroundColor = "#ffcccc";
126 n.style.color = "black";
0530ddd8
AD
127 n.style.borderColor = "#ff0000";
128 } else {
129 n.style.backgroundColor = "#fff7d5";
130 n.style.borderColor = "#d7c47a";
131 n.style.color = "black";
132 }
133
0ceded7a
AD
134 nb.innerHTML = msg;
135
4d4200a8 136 if (!no_hide) {
292a8a12 137 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
4d4200a8 138 }
ce3bf408
AD
139}
140
0530ddd8
AD
141function p_notify(msg, no_hide, is_err) {
142 notify_real(msg, parent.document, no_hide, is_err);
ce3bf408
AD
143}
144
0530ddd8
AD
145function notify(msg, no_hide, is_err) {
146 notify_real(msg, document, no_hide, is_err);
7726fa02
AD
147}
148
508a81e1 149function printLockingError() {
f0601b87 150 notify("Please wait until operation finishes");}
508a81e1 151
13ad9102
AD
152var seq = "";
153
154function hotkey_handler(e) {
760966c1 155
ee1f45f4 156 try {
2e02b896 157
ee1f45f4
AD
158 var keycode;
159
160 if (!hotkeys_enabled) return;
161
162 if (window.event) {
163 keycode = window.event.keyCode;
164 } else if (e) {
165 keycode = e.which;
166 }
167
168 if (keycode == 13 || keycode == 27) {
2e02b896 169 seq = "";
2e02b896 170 } else {
ee1f45f4 171 seq = seq + "" + keycode;
2e02b896 172 }
bb7cface 173
ee1f45f4
AD
174 var m_ctx = getMainContext();
175 var f_ctx = getFeedsContext();
176 var h_ctx = getHeadlinesContext();
177
178 if (keycode == 82) { // r
179 return m_ctx.scheduleFeedUpdate(true);
180 }
181
182 if (keycode == 85) { // u
183 if (getActiveFeedId()) {
184 return f_ctx.viewfeed(getActiveFeedId(), 0, "ForceUpdate");
185 }
186 }
187
188 if (keycode == 65) { // a
189 return m_ctx.toggleDispRead();
190 }
191
192 var f_doc = m_ctx.frames["feeds-frame"].document;
193 var feedlist = f_doc.getElementById('feedList');
194
195 if (keycode == 74) { // j
196 var feed = getActiveFeedId();
197 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
198 if (new_feed) viewfeed(new_feed, 0, '');
199 }
200
201 if (keycode == 75) { // k
202 var feed = getActiveFeedId();
203 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
204 if (new_feed) viewfeed(new_feed, 0, '');
7b433d8c 205 }
9cfc649a 206
ee1f45f4
AD
207 if (keycode == 78 || keycode == 40) { // n, down
208 return h_ctx.moveToPost('next');
209 }
210
211 if (keycode == 80 || keycode == 38) { // p, up
212 return h_ctx.moveToPost('prev');
213 }
214
215 if (document.getElementById("piggie")) {
216
217 if (seq.match("807371717369")) {
218 seq = "";
219 localPiggieFunction(true);
220 } else {
221 localPiggieFunction(false);
222 }
223 }
224
225 if (typeof localHotkeyHandler != 'undefined') {
226 try {
227 localHotkeyHandler(keycode);
228 } catch (e) {
229 exception_error("hotkey_handler, local:", e);
230 }
231 }
232 } catch (e) {
233 exception_error("hotkey_handler", e);
234 }
13ad9102
AD
235}
236
e828e31e 237function cleanSelectedList(element) {
f0601b87
AD
238 var content = document.getElementById(element);
239
703b632e
AD
240 if (!document.getElementById("feedCatHolder")) {
241 for (i = 0; i < content.childNodes.length; i++) {
242 var child = content.childNodes[i];
d0bb308e
AD
243 try {
244 child.className = child.className.replace("Selected", "");
245 } catch (e) {
246 //
247 }
703b632e
AD
248 }
249 } else {
250 for (i = 0; i < content.childNodes.length; i++) {
251 var child = content.childNodes[i];
703b632e 252 if (child.id == "feedCatHolder") {
befc807f 253 parent.debug(child.id);
c3f348c2 254 var fcat = child.lastChild;
703b632e 255 for (j = 0; j < fcat.childNodes.length; j++) {
befc807f 256 var feed = fcat.childNodes[j];
703b632e
AD
257 feed.className = feed.className.replace("Selected", "");
258 }
259 }
befc807f 260 }
703b632e 261 }
e828e31e
AD
262}
263
264
265function cleanSelected(element) {
266 var content = document.getElementById(element);
f0601b87
AD
267
268 for (i = 0; i < content.rows.length; i++) {
269 content.rows[i].className = content.rows[i].className.replace("Selected", "");
270 }
f0601b87
AD
271}
272
273function getVisibleUnreadHeadlines() {
274 var content = document.getElementById("headlinesList");
275
276 var rows = new Array();
277
278 for (i = 0; i < content.rows.length; i++) {
279 var row_id = content.rows[i].id.replace("RROW-", "");
280 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
281 rows.push(row_id);
282 }
283 }
284 return rows;
285}
286
287function getVisibleHeadlineIds() {
288
289 var content = document.getElementById("headlinesList");
290
291 var rows = new Array();
292
293 for (i = 0; i < content.rows.length; i++) {
294 var row_id = content.rows[i].id.replace("RROW-", "");
295 if (row_id.length > 0) {
296 rows.push(row_id);
297 }
298 }
299 return rows;
300}
301
302function getFirstVisibleHeadlineId() {
303 var rows = getVisibleHeadlineIds();
304 return rows[0];
305}
306
307function getLastVisibleHeadlineId() {
308 var rows = getVisibleHeadlineIds();
309 return rows[rows.length-1];
310}
311
312function markHeadline(id) {
313 var row = document.getElementById("RROW-" + id);
314 if (row) {
35f3c923
AD
315 var is_active = false;
316
317 if (row.className.match("Active")) {
318 is_active = true;
319 }
320 row.className = row.className.replace("Selected", "");
321 row.className = row.className.replace("Active", "");
322 row.className = row.className.replace("Insensitive", "");
323
324 if (is_active) {
325 row.className = row.className = "Active";
326 }
327
72020095
AD
328 var check = document.getElementById("RCHK-" + id);
329
330 if (check) {
331 check.checked = true;
332 }
333
35f3c923
AD
334 row.className = row.className + "Selected";
335
f0601b87
AD
336 }
337}
338
339function getFeedIds() {
340 var content = document.getElementById("feedsList");
341
342 var rows = new Array();
343
344 for (i = 0; i < content.rows.length; i++) {
345 var id = content.rows[i].id.replace("FEEDR-", "");
346 if (id.length > 0) {
347 rows.push(id);
348 }
349 }
350
351 return rows;
352}
13ad9102 353
76b4eae1
AD
354function setCookie(name, value, lifetime, path, domain, secure) {
355
356 var d = false;
357
358 if (lifetime) {
359 d = new Date();
360 d.setTime(lifetime * 1000);
361 }
362
363 int_setCookie(name, value, d, path, domain, secure);
364
365}
366
367function int_setCookie(name, value, expires, path, domain, secure) {
ac43eba1
AD
368 document.cookie= name + "=" + escape(value) +
369 ((expires) ? "; expires=" + expires.toGMTString() : "") +
370 ((path) ? "; path=" + path : "") +
371 ((domain) ? "; domain=" + domain : "") +
372 ((secure) ? "; secure" : "");
373}
374
76b4eae1
AD
375function delCookie(name, path, domain) {
376 if (getCookie(name)) {
377 document.cookie = name + "=" +
378 ((path) ? ";path=" + path : "") +
379 ((domain) ? ";domain=" + domain : "" ) +
380 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
381 }
382}
383
384
ac43eba1
AD
385function getCookie(name) {
386
387 var dc = document.cookie;
388 var prefix = name + "=";
389 var begin = dc.indexOf("; " + prefix);
390 if (begin == -1) {
391 begin = dc.indexOf(prefix);
392 if (begin != 0) return null;
393 }
394 else {
395 begin += 2;
396 }
397 var end = document.cookie.indexOf(";", begin);
398 if (end == -1) {
399 end = dc.length;
400 }
401 return unescape(dc.substring(begin + prefix.length, end));
402}
403
1a66d16e
AD
404function disableContainerChildren(id, disable, doc) {
405
406 if (!doc) doc = document;
407
408 var container = doc.getElementById(id);
ac43eba1 409
4220b0bd
AD
410 if (!container) {
411 //alert("disableContainerChildren: element " + id + " not found");
412 return;
413 }
414
ac43eba1
AD
415 for (var i = 0; i < container.childNodes.length; i++) {
416 var child = container.childNodes[i];
417
d5224f0d
AD
418 try {
419 child.disabled = disable;
420 } catch (E) {
421
422 }
ac43eba1
AD
423
424 if (disable) {
425 if (child.className && child.className.match("button")) {
426 child.className = "disabledButton";
427 }
428 } else {
429 if (child.className && child.className.match("disabledButton")) {
430 child.className = "button";
431 }
d5224f0d 432 }
ac43eba1
AD
433 }
434
435}
7726fa02 436
e828e31e
AD
437function gotoPreferences() {
438 document.location.href = "prefs.php";
439}
440
441function gotoMain() {
442 document.location.href = "tt-rss.php";
443}
444
8158c57a
AD
445function gotoExportOpml() {
446 document.location.href = "opml.php?op=Export";
447}
86741347
AD
448
449function getActiveFeedId() {
33d13e72
AD
450// return getCookie("ttrss_vf_actfeed");
451 try {
452 debug("gAFID: " + getMainContext().active_feed_id);
453 return getMainContext().active_feed_id;
454 } catch (e) {
455 exception_error("getActiveFeedId", e);
456 }
86741347
AD
457}
458
459function setActiveFeedId(id) {
33d13e72
AD
460// return setCookie("ttrss_vf_actfeed", id);
461 try {
462 getMainContext().active_feed_id = id;
463 } catch (e) {
464 exception_error("setActiveFeedId", e);
465 }
86741347 466}
090e250b 467
ac378ad4 468function parse_counters(reply, scheduled_call) {
1a6a9555 469 try {
33d13e72 470 var f_document = getFeedsContext().document;
ac378ad4
AD
471 var title_obj = getMainContext();
472
f54f515f
AD
473 if (reply.firstChild && reply.firstChild.firstChild) {
474 debug("<b>wrong element passed to parse_counters, adjusting.</b>");
475 reply = reply.firstChild;
476 }
477
ac378ad4
AD
478 debug("F_DOC: " + f_document + ", T_OBJ: " + title_obj);
479
1a6a9555 480 for (var l = 0; l < reply.childNodes.length; l++) {
9cbca41f
AD
481 if (!reply.childNodes[l] ||
482 typeof(reply.childNodes[l].getAttribute) == "undefined") {
6043fb7e
AD
483 // where did this come from?
484 continue;
485 }
486
1a6a9555
AD
487 var id = reply.childNodes[l].getAttribute("id");
488 var t = reply.childNodes[l].getAttribute("type");
489 var ctr = reply.childNodes[l].getAttribute("counter");
023fe037
AD
490 var error = reply.childNodes[l].getAttribute("error");
491 var has_img = reply.childNodes[l].getAttribute("hi");
fb1fb4ab 492 var updated = reply.childNodes[l].getAttribute("updated");
1a6a9555
AD
493
494 if (id == "global-unread") {
3bdb368b
AD
495 title_obj.global_unread = ctr;
496 title_obj.updateTitle();
1a6a9555
AD
497 continue;
498 }
499
500 if (t == "category") {
501 var catctr = f_document.getElementById("FCATCTR-" + id);
502 if (catctr) {
503 catctr.innerHTML = "(" + ctr + " unread)";
504 }
505 continue;
506 }
507
508 var feedctr = f_document.getElementById("FEEDCTR-" + id);
509 var feedu = f_document.getElementById("FEEDU-" + id);
510 var feedr = f_document.getElementById("FEEDR-" + id);
023fe037 511 var feed_img = f_document.getElementById("FIMG-" + id);
fb1fb4ab 512 var feedlink = f_document.getElementById("FEEDL-" + id);
78d5212c 513 var feedupd = f_document.getElementById("FLUPD-" + id);
fb1fb4ab
AD
514
515 if (updated && feedlink) {
516 if (error) {
517 feedlink.title = "Error: " + error + " (" + updated + ")";
518 } else {
519 feedlink.title = "Updated: " + updated;
520 }
521 }
023fe037 522
78d5212c
AD
523 if (updated && feedupd) {
524 if (error) {
525 feedupd.innerHTML = updated + " (Error)";
526 } else {
527 feedupd.innerHTML = updated;
528 }
529 }
530
1a6a9555 531 if (feedctr && feedu && feedr) {
e8ef3b97
AD
532
533 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
534 var hf = title_obj.parent.frames["headlines-frame"];
535 hf.location.reload(true);
536 }
1a6a9555
AD
537
538 feedu.innerHTML = ctr;
023fe037 539
426d3c57
AD
540 if (error) {
541 feedr.className = feedr.className.replace("feed", "error");
542 } else if (id > 0) {
543 feedr.className = feedr.className.replace("error", "feed");
023fe037 544 }
1a6a9555
AD
545
546 if (ctr > 0) {
547 feedctr.className = "odd";
548 if (!feedr.className.match("Unread")) {
549 var is_selected = feedr.className.match("Selected");
550
551 feedr.className = feedr.className.replace("Selected", "");
552 feedr.className = feedr.className.replace("Unread", "");
553
554 feedr.className = feedr.className + "Unread";
555
556 if (is_selected) {
557 feedr.className = feedr.className + "Selected";
558 }
559
560 }
561 } else {
562 feedctr.className = "invisible";
563 feedr.className = feedr.className.replace("Unread", "");
564 }
565 }
566 }
567 } catch (e) {
83f043bb 568 exception_error("parse_counters", e);
1a6a9555
AD
569 }
570}
571
4f3a84f4 572function all_counters_callback() {
090e250b 573 if (xmlhttp_rpc.readyState == 4) {
7719618b 574 try {
3866b046 575 if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
b00ed196 576 debug("[all_counters_callback] backend did not return valid XML");
7719618b
AD
577 return;
578 }
894ebcf5 579
1cb7492d
AD
580 debug("in all_counters_callback");
581
7719618b 582 var reply = xmlhttp_rpc.responseXML.firstChild;
280ee9a3 583
f54f515f
AD
584 var counters = reply.firstChild;
585
586 parse_counters(counters);
1cb7492d
AD
587
588 var runtime = counters.nextSibling;
589
590 if (runtime) {
591 getMainContext().parse_runtime_info(runtime);
592 }
c9268ed5 593
cea51014 594 if (getInitParam("feeds_sort_by_unread") == 1) {
c9268ed5
AD
595 resort_feedlist();
596 }
293fa942
AD
597
598 hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
599
7719618b
AD
600 } catch (e) {
601 exception_error("all_counters_callback", e);
090e250b
AD
602 }
603 }
604}
605
c9268ed5
AD
606function get_feed_entry_unread(doc, elem) {
607
608 var id = elem.id.replace("FEEDR-", "");
609
610 if (id <= 0) {
611 return -1;
612 }
613
614 try {
615 return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);
616 } catch (e) {
617 return -1;
618 }
619}
620
621function resort_category(doc, node) {
622 debug("resort_category: " + node);
623
624 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
625 for (i = 0; i < node.childNodes.length; i++) {
626 if (node.childNodes[i].nodeName != "LI") { continue; }
627
628 if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
629 continue;
630 }
631
632 for (j = i+1; j < node.childNodes.length; j++) {
633 if (node.childNodes[j].nodeName != "LI") { continue; }
634
635 var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
636 var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
637
638 if (cur_val > tmp_val) {
639 tempnode_i = node.childNodes[i].cloneNode(true);
640 tempnode_j = node.childNodes[j].cloneNode(true);
641 node.replaceChild(tempnode_i, node.childNodes[j]);
642 node.replaceChild(tempnode_j, node.childNodes[i]);
643 }
644 }
645
646 }
647 }
648
649}
650
651function resort_feedlist() {
652 debug("resort_feedlist");
653
654 var fd = getFeedsContext().document;
655
656 if (fd.getElementById("feedCatHolder")) {
657
658 var feeds = fd.getElementById("feedList");
659 var child = feeds.firstChild;
660
661 while (child) {
662
663 if (child.id == "feedCatHolder") {
664 resort_category(fd, child.firstChild);
665 }
666
667 child = child.nextSibling;
668 }
669
670 } else {
671 resort_category(fd, fd.getElementById("feedList"));
672 }
673}
674
4f3a84f4 675function update_all_counters(feed) {
090e250b 676 if (xmlhttp_ready(xmlhttp_rpc)) {
8143ae1f 677 var query = "backend.php?op=rpc&subop=getAllCounters";
e47cfe37
AD
678
679 if (feed > 0) {
680 query = query + "&aid=" + feed;
681 }
682
090e250b 683 xmlhttp_rpc.open("GET", query, true);
4f3a84f4 684 xmlhttp_rpc.onreadystatechange=all_counters_callback;
090e250b
AD
685 xmlhttp_rpc.send(null);
686 }
687}
7dc66a61
AD
688
689function popupHelp(tid) {
690 var w = window.open("backend.php?op=help&tid=" + tid,
691 "Popup Help",
692 "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
693}
b6644d29
AD
694
695/** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
696 * * @author Sundar Dorai-Raj
697 * * Email: sdoraira@vt.edu
698 * * This program is free software; you can redistribute it and/or
699 * * modify it under the terms of the GNU General Public License
700 * * as published by the Free Software Foundation; either version 2
701 * * of the License, or (at your option) any later version,
702 * * provided that any use properly credits the author.
703 * * This program is distributed in the hope that it will be useful,
704 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
705 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
706 * * GNU General Public License for more details at http://www.gnu.org * * */
707
708 var numbers=".0123456789";
709 function isNumeric(x) {
710 // is x a String or a character?
711 if(x.length>1) {
712 // remove negative sign
713 x=Math.abs(x)+"";
714 for(j=0;j<x.length;j++) {
715 // call isNumeric recursively for each character
716 number=isNumeric(x.substring(j,j+1));
717 if(!number) return number;
718 }
719 return number;
720 }
721 else {
722 // if x is number return true
723 if(numbers.indexOf(x)>=0) return true;
724 return false;
725 }
726 }
727
3745788e
AD
728
729function hideOrShowFeeds(doc, hide) {
730
293fa942
AD
731 var fd = getFeedsContext().document;
732
733 var list = fd.getElementById("feedList");
734
735 if (fd.getElementById("feedCatHolder")) {
736
737 var feeds = fd.getElementById("feedList");
738 var child = feeds.firstChild;
739
740 while (child) {
741
742 if (child.id == "feedCatHolder") {
743 hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
744 }
745
746 child = child.nextSibling;
747 }
748
749 } else {
750 hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
751 }
752}
753
754function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
755
756// debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
3ed751d4 757
293fa942 758 var cat_unread = 0;
3745788e 759
293fa942
AD
760 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
761 for (i = 0; i < node.childNodes.length; i++) {
762 if (node.childNodes[i].nodeName != "LI") { continue; }
bbf4d0b2 763
293fa942
AD
764 var has_unread = (node.childNodes[i].className != "feed");
765
766// debug(node.childNodes[i].id + " --> " + has_unread);
767
768 if (hide && !has_unread) {
769 node.childNodes[i].style.display = "none";
770 }
3745788e 771
3745788e 772 if (!hide) {
293fa942 773 node.childNodes[i].style.display = "list-item";
3745788e 774 }
293fa942
AD
775
776 if (has_unread) {
777 cat_unread++;
778 }
779
3745788e 780 }
293fa942 781 }
3745788e 782
293fa942
AD
783 if (cat_unread == 0) {
784 if (hide) {
785 cat_node.style.display = "none";
786 } else {
787 cat_node.style.display = "list-item";
788 }
789 }
3745788e 790
293fa942 791// debug("unread for category: " + cat_unread);
3745788e
AD
792}
793
35f3c923
AD
794function selectTableRow(r, do_select) {
795 r.className = r.className.replace("Selected", "");
796
797 if (do_select) {
798 r.className = r.className + "Selected";
799 }
800}
801
6c12c809
AD
802function selectTableRowById(elem_id, check_id, do_select) {
803
804 try {
805
806 var row = document.getElementById(elem_id);
807
808 if (row) {
809 selectTableRow(row, do_select);
810 }
811
812 var check = document.getElementById(check_id);
813
814 if (check) {
815 check.checked = do_select;
816 }
817 } catch (e) {
818 exception_error("selectTableRowById", e);
819 }
820}
821
1572afe5 822function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
649e0af9 823 classcheck, reset_others) {
3745788e 824
35f3c923
AD
825 var content = document.getElementById(content_id);
826
827 if (!content) {
828 alert("[selectTableRows] Element " + content_id + " not found.");
829 return;
830 }
831
832 for (i = 0; i < content.rows.length; i++) {
1572afe5
AD
833 if (!classcheck || content.rows[i].className.match(classcheck)) {
834
835 if (content.rows[i].id.match(prefix)) {
836 selectTableRow(content.rows[i], do_select);
649e0af9
AD
837
838 var row_id = content.rows[i].id.replace(prefix, "");
839 var check = document.getElementById(check_prefix + row_id);
3055bc41 840
649e0af9
AD
841 if (check) {
842 check.checked = do_select;
843 }
844 } else if (reset_others) {
845 selectTableRow(content.rows[i], false);
7d8d10c6
AD
846
847 var row_id = content.rows[i].id.replace(prefix, "");
848 var check = document.getElementById(check_prefix + row_id);
849
850 if (check) {
851 check.checked = false;
852 }
853
1572afe5 854 }
649e0af9
AD
855 } else if (reset_others) {
856 selectTableRow(content.rows[i], false);
7d8d10c6
AD
857
858 var row_id = content.rows[i].id.replace(prefix, "");
859 var check = document.getElementById(check_prefix + row_id);
860
861 if (check) {
862 check.checked = false;
863 }
864
3055bc41 865 }
35f3c923 866 }
295f9b42 867}
91ff844a
AD
868
869function getSelectedTableRowIds(content_id, prefix) {
870
871 var content = document.getElementById(content_id);
872
873 if (!content) {
874 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
875 return;
876 }
877
878 var sel_rows = new Array();
879
880 for (i = 0; i < content.rows.length; i++) {
1572afe5
AD
881 if (content.rows[i].id.match(prefix) &&
882 content.rows[i].className.match("Selected")) {
883
91ff844a
AD
884 var row_id = content.rows[i].id.replace(prefix + "-", "");
885 sel_rows.push(row_id);
886 }
887 }
888
889 return sel_rows;
890
891}
892
386cbf27
AD
893function toggleSelectRowById(sender, id) {
894 var row = document.getElementById(id);
895
896 if (sender.checked) {
897 if (!row.className.match("Selected")) {
898 row.className = row.className + "Selected";
899 }
900 } else {
901 if (row.className.match("Selected")) {
902 row.className = row.className.replace("Selected", "");
903 }
904 }
905}
906
b92e6209
AD
907function toggleSelectListRow(sender) {
908 var parent_row = sender.parentNode;
909
910 if (sender.checked) {
911 if (!parent_row.className.match("Selected")) {
912 parent_row.className = parent_row.className + "Selected";
913 }
914 } else {
915 if (parent_row.className.match("Selected")) {
916 parent_row.className = parent_row.className.replace("Selected", "");
917 }
918 }
919}
920
386cbf27 921
1572afe5
AD
922function toggleSelectRow(sender) {
923 var parent_row = sender.parentNode.parentNode;
924
925 if (sender.checked) {
926 if (!parent_row.className.match("Selected")) {
927 parent_row.className = parent_row.className + "Selected";
928 }
929 } else {
930 if (parent_row.className.match("Selected")) {
931 parent_row.className = parent_row.className.replace("Selected", "");
932 }
933 }
934}
935
3866b046
AD
936function openExternalUrl(url) {
937 var w = window.open(url);
938}
1572afe5 939
1da76274 940function getRelativeFeedId(list, id, direction, unread_only) {
7b433d8c
AD
941 if (!id) {
942 if (direction == "next") {
943 for (i = 0; i < list.childNodes.length; i++) {
944 var child = list.childNodes[i];
1da76274 945 if (child.id && child.id == "feedCatHolder") {
c3f348c2 946 if (child.lastChild) {
7b433d8c
AD
947 var cr = getRelativeFeedId(child.firstChild, id, direction);
948 if (cr) return cr;
949 }
1da76274 950 } else if (child.id && child.id.match("FEEDR-")) {
7b433d8c
AD
951 return child.id.replace('FEEDR-', '');
952 }
953 }
954 }
955
956 // FIXME select last feed doesn't work when only unread feeds are visible
957
958 if (direction == "prev") {
959 for (i = list.childNodes.length-1; i >= 0; i--) {
960 var child = list.childNodes[i];
961 if (child.id == "feedCatHolder") {
962 if (child.firstChild) {
963 var cr = getRelativeFeedId(child.firstChild, id, direction);
964 if (cr) return cr;
965 }
966 } else if (child.id.match("FEEDR-")) {
967
e8bd0da9 968 if (getInitParam("hide_read_feeds") == 1) {
7b433d8c
AD
969 if (child.className != "feed") {
970 alert(child.className);
971 return child.id.replace('FEEDR-', '');
972 }
973 } else {
974 return child.id.replace('FEEDR-', '');
975 }
976 }
977 }
978 }
979 } else {
980
981 var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
982
e8bd0da9 983 if (getInitParam("hide_read_feeds") == 1) {
1da76274
AD
984 unread_only = true;
985 }
986
7b433d8c
AD
987 if (direction == "next") {
988
1da76274 989 var e = feed;
7b433d8c 990
1da76274 991 while (e) {
7b433d8c 992
1da76274
AD
993 if (e.nextSibling) {
994
995 e = e.nextSibling;
996
997 } else if (e.parentNode.parentNode.nextSibling) {
7b433d8c 998
1da76274 999 var this_cat = e.parentNode.parentNode;
7b433d8c 1000
1da76274 1001 e = false;
7b433d8c 1002
1da76274
AD
1003 if (this_cat && this_cat.nextSibling) {
1004 while (!e && this_cat.nextSibling) {
1005 this_cat = this_cat.nextSibling;
1006 if (this_cat.id == "feedCatHolder") {
1007 e = this_cat.firstChild.firstChild;
7b433d8c
AD
1008 }
1009 }
7b433d8c 1010 }
1da76274
AD
1011
1012 } else {
1013 e = false;
1014 }
1015
1016 if (e) {
e686782e
AD
1017 if (!unread_only || (unread_only && e.className != "feed" &&
1018 e.className != "error")) {
1da76274
AD
1019 return e.id.replace("FEEDR-", "");
1020 }
1021 }
7b433d8c 1022 }
1da76274 1023
7b433d8c
AD
1024 } else if (direction == "prev") {
1025
1da76274 1026 var e = feed;
7b433d8c 1027
1da76274 1028 while (e) {
7b433d8c 1029
1da76274
AD
1030 if (e.previousSibling) {
1031
1032 e = e.previousSibling;
1033
1034 } else if (e.parentNode.parentNode.previousSibling) {
7b433d8c 1035
1da76274 1036 var this_cat = e.parentNode.parentNode;
7b433d8c 1037
1da76274
AD
1038 e = false;
1039
1040 if (this_cat && this_cat.previousSibling) {
1041 while (!e && this_cat.previousSibling) {
1042 this_cat = this_cat.previousSibling;
1043 if (this_cat.id == "feedCatHolder") {
1044 e = this_cat.firstChild.lastChild;
7b433d8c
AD
1045 }
1046 }
7b433d8c 1047 }
1da76274
AD
1048
1049 } else {
1050 e = false;
1051 }
1052
1053 if (e) {
e686782e
AD
1054 if (!unread_only || (unread_only && e.className != "feed" &&
1055 e.className != "error")) {
1da76274
AD
1056 return e.id.replace("FEEDR-", "");
1057 }
1058 }
1059 }
7b433d8c
AD
1060 }
1061 }
1062}
36aab70f
AD
1063
1064function showBlockElement(id) {
1065 var elem = document.getElementById(id);
1066
1067 if (elem) {
1068 elem.style.display = "block";
1069 } else {
1070 alert("[showBlockElement] can't find element with id " + id);
1071 }
1072}
1073
a9b0bfd5
AD
1074function hideParentElement(e) {
1075 e.parentNode.style.display = "none";
1076}
1b0809ae
AD
1077
1078function dropboxSelect(e, v) {
1079 for (i = 0; i < e.length; i++) {
1080 if (e[i].value == v) {
1081 e.selectedIndex = i;
1082 break;
1083 }
1084 }
1085}
0ee1d1a0
AD
1086
1087// originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1088// bugfixed just a little bit :-)
1089function getURLParam(strParamName){
1090 var strReturn = "";
1091 var strHref = window.location.href;
1092
1093 if (strHref.indexOf("#") == strHref.length-1) {
1094 strHref = strHref.substring(0, strHref.length-1);
1095 }
1096
1097 if ( strHref.indexOf("?") > -1 ){
1098 var strQueryString = strHref.substr(strHref.indexOf("?"));
1099 var aQueryString = strQueryString.split("&");
1100 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1101 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1102 var aParam = aQueryString[iParam].split("=");
1103 strReturn = aParam[1];
1104 break;
1105 }
1106 }
1107 }
1108 return strReturn;
1109}
1110
cf1bc085
AD
1111function leading_zero(p) {
1112 var s = String(p);
1113 if (s.length == 1) s = "0" + s;
1114 return s;
1115}
c38c2b69 1116
86b682ce 1117function closeInfoBox(cleanup) {
e5d758e3
AD
1118 var box = document.getElementById('infoBox');
1119 var shadow = document.getElementById('infoBoxShadow');
1120
1121 if (shadow) {
1122 shadow.style.display = "none";
1123 } else if (box) {
1124 box.style.display = "none";
1125 }
1126
86b682ce
AD
1127 if (cleanup) box.innerHTML = "&nbsp;";
1128
e5d758e3 1129 enableHotkeys();
c14b5566 1130
e5d758e3
AD
1131}
1132
1133
7b5c6012
AD
1134function displayDlg(id, param) {
1135
1136 if (!xmlhttp_ready(xmlhttp)) {
1137 printLockingError();
1138 return
1139 }
1140
1141 notify("");
1142
1143 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
1144 param_escape(id) + "&param=" + param_escape(param), true);
e5d758e3 1145 xmlhttp.onreadystatechange=infobox_callback;
7b5c6012
AD
1146 xmlhttp.send(null);
1147
1148 disableHotkeys();
2371c520 1149
7b5c6012
AD
1150}
1151
e5d758e3 1152function infobox_submit_callback() {
7b5c6012 1153 if (xmlhttp.readyState == 4) {
e5d758e3 1154 closeInfoBox();
7b5c6012
AD
1155
1156 // called from prefs, reload tab
1157 if (active_tab) {
1158 selectTab(active_tab, false);
1159 }
79f3553b
AD
1160
1161 notify(xmlhttp.responseText);
1162
7b5c6012
AD
1163 }
1164}
1165
e5d758e3 1166function infobox_callback() {
7b5c6012 1167 if (xmlhttp.readyState == 4) {
e5d758e3
AD
1168 var box = document.getElementById('infoBox');
1169 var shadow = document.getElementById('infoBoxShadow');
1170 if (box) {
1171 box.innerHTML=xmlhttp.responseText;
1172 if (shadow) {
1173 shadow.style.display = "block";
1174 } else {
1175 box.style.display = "block";
1176 }
1177 }
1178 }
7b5c6012
AD
1179}
1180
1181function qaddFilter() {
1182
1183 if (!xmlhttp_ready(xmlhttp)) {
1184 printLockingError();
1185 return
1186 }
1187
79f3553b 1188 var query = Form.serialize("filter_add_form");
7b5c6012 1189
79f3553b
AD
1190 xmlhttp.open("GET", "backend.php?" + query, true);
1191 xmlhttp.onreadystatechange=infobox_submit_callback;
1192 xmlhttp.send(null);
7b5c6012 1193
c14b5566 1194 return true;
7b5c6012
AD
1195}
1196
2371c520
AD
1197function toggleSubmitNotEmpty(e, submit_id) {
1198 try {
1199 document.getElementById(submit_id).disabled = (e.value == "")
1200 } catch (e) {
1201 exception_error("toggleSubmitNotEmpty", e);
1202 }
1203}
1d7bf5a0 1204
605f7d46
AD
1205function isValidURL(s) {
1206 return s.match("http://") != null || s.match("https://") != null;
1207}
07eb9178
AD
1208
1209function qafAdd() {
1210
1211 if (!xmlhttp_ready(xmlhttp)) {
1212 printLockingError();
1213 return
1214 }
1215
1216 notify("Adding feed...");
1217
1218 closeInfoBox();
1219
33d13e72 1220 var feeds_doc = getFeedsContext().document;
07eb9178
AD
1221
1222 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
1223
1224 var query = Form.serialize("feed_add_form");
1225
1226 xmlhttp.open("GET", "backend.php?" + query, true);
1227 xmlhttp.onreadystatechange=dlg_frefresh_callback;
1228 xmlhttp.send(null);
07eb9178
AD
1229}
1230
86b682ce
AD
1231function filterCR(e)
1232{
1233 var key;
1234
1235 if(window.event)
1236 key = window.event.keyCode; //IE
1237 else
1238 key = e.which; //firefox
1239
1240 if(key == 13)
1241 return false;
1242 else
1243 return true;
1244}
1245
ac378ad4
AD
1246function getMainContext() {
1247 if (parent.window != window) {
1248 return parent.window;
1249 } else {
1250 return this.window;
1251 }
1252}
1253
33d13e72
AD
1254function getFeedsContext() {
1255 try {
1256 return getMainContext().frames["feeds-frame"];
1257 } catch (e) {
1258 exception_error("getFeedsContext", e);
1259 }
1260}
1261
ee1f45f4
AD
1262
1263function getHeadlinesContext() {
1264 try {
1265 return getMainContext().frames["headlines-frame"];
1266 } catch (e) {
1267 exception_error("getHeadlinesContext", e);
1268 }
1269}
1270
ac378ad4
AD
1271function debug(msg) {
1272 var ctx = getMainContext();
1273
1274 var c = ctx.document.getElementById('debug_output');
1275 if (c && c.style.display == "block") {
c9268ed5 1276 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
ac378ad4
AD
1277 c.removeChild(c.lastChild);
1278 }
1279
1280 var d = new Date();
1281 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1282 ":" + leading_zero(d.getSeconds());
1283 c.innerHTML = "<li>[" + ts + "] " + msg + "</li>" + c.innerHTML;
1284 }
1285}
1286
33d13e72
AD
1287function getInitParam(key) {
1288 return getMainContext().init_params[key];
1289}
3ac2b520 1290
e8bd0da9 1291function storeInitParam(key, value, is_client) {
33d13e72 1292 try {
e8bd0da9 1293 if (!is_client) {
1035fcec
AD
1294 if (getMainContext().init_params[key] != value) {
1295 debug("storeInitParam: " + key + " => " + value);
1296 new Ajax.Request("backend.php?op=rpc&subop=storeParam&key=" +
1297 param_escape(key) + "&value=" + param_escape(value));
1298 }
e8bd0da9 1299 }
1035fcec 1300 getMainContext().init_params[key] = value;
33d13e72
AD
1301 } catch (e) {
1302 exception_error("storeInitParam", e);
1303 }
1304}
a7565293 1305
1035fcec
AD
1306/*
1307function storeInitParams(params, is_client) {
1308 try {
1309 var s = "";
1310
1311 for (k in params) {
1312 if (getMainContext().init_params[k] != params[k]) {
1313 s += k + "=" + params[k] + ";";
1314 getMainContext().init_params[k] = params[k];
1315 }
1316 }
1317
1318 debug("storeInitParams: " + s);
1319
1320 if (!is_client) {
1321 new Ajax.Request("backend.php?op=rpc&subop=storeParams&str=" + s);
1322 }
1323 } catch (e) {
1324 exception_error("storeInitParams", e);
1325 }
1326}*/
1327
a7565293
AD
1328function fatalError(code, message) {
1329 try {
1330 var fe = document.getElementById("fatal_error");
1331 var fc = document.getElementById("fatal_error_msg");
1332
1333 fc.innerHTML = "Code " + code + ": " + message;
1334
1335 fe.style.display = "block";
1336
1337 } catch (e) {
1338 exception_error("fatalError", e);
1339 }
1340}
1341
64a2875d
AD
1342function getFeedName(id) {
1343 var d = getFeedsContext().document;
1344 var e = d.getElementById("FEEDN-" + id);
1345 if (e) {
1346 return e.innerHTML.stripTags();
1347 } else {
1348 return null;
1349 }
1350}