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