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