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