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