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