]> git.wh0rd.org - tt-rss.git/blame - functions.js
prefs: some async work
[tt-rss.git] / functions.js
CommitLineData
760966c1 1var hotkeys_enabled = true;
a7565293 2var xmlhttp_rpc = Ajax.getTransport();
730dbf19 3var notify_silent = false;
99509451 4var last_progress_point = 0;
6cc1fab3 5var async_counters_work = false;
61992f57 6
b07b61da
AD
7/* add method to remove element from array */
8
9Array.prototype.remove = function(s) {
10 for (var i=0; i < this.length; i++) {
11 if (s == this[i]) this.splice(i, 1);
12 }
13}
14
7c620da8 15function is_opera() {
ee8768db 16 return window.opera;
583c58b8
AD
17}
18
273d1e29 19function exception_error(location, e, ext_info) {
e8a429e3 20 var msg = format_exception_error(location, e);
e8a429e3
AD
21
22 disableHotkeys();
23
273d1e29 24 try {
e8a429e3 25
273d1e29
AD
26 var ebc = document.getElementById("xebContent");
27
28 if (ebc) {
29
30 Element.show("dialog_overlay");
31 Element.show("errorBoxShadow");
32
33 if (ext_info) {
34 if (ext_info.responseText) {
35 ext_info = ext_info.responseText;
36 }
e8a429e3 37 }
273d1e29
AD
38
39 ebc.innerHTML =
40 "<div><b>Error message:</b></div>" +
41 "<pre>" + msg + "</pre>" +
42 "<div><b>Additional information:</b></div>" +
43 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
44
45 } else {
46 alert(msg);
e8a429e3
AD
47 }
48
273d1e29 49 } catch (e) {
e8a429e3 50 alert(msg);
e8a429e3 51
e8a429e3 52 }
273d1e29 53
e8a429e3
AD
54}
55
56function format_exception_error(location, e) {
83f043bb
AD
57 var msg;
58
59 if (e.fileName) {
60 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
61
62 msg = "Exception: " + e.name + ", " + e.message +
63 "\nFunction: " + location + "()" +
64 "\nLocation: " + base_fname + ":" + e.lineNumber;
f74cfded
AD
65
66 } else if (e.description) {
67 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
83f043bb
AD
68 } else {
69 msg = "Exception: " + e + "\nFunction: " + location + "()";
70 }
71
ee1f45f4
AD
72 debug("<b>EXCEPTION: " + msg + "</b>");
73
e8a429e3 74 return msg;
7719618b
AD
75}
76
e8a429e3 77
760966c1
AD
78function disableHotkeys() {
79 hotkeys_enabled = false;
80}
81
82function enableHotkeys() {
83 hotkeys_enabled = true;
84}
85
c0e5a40e
AD
86function xmlhttp_ready(obj) {
87 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
88}
89
d395a942
AD
90function open_article_callback(transport) {
91 try {
298f3f78 92
d395a942 93 if (transport.responseXML) {
06925d9e 94
d395a942
AD
95 var link = transport.responseXML.getElementsByTagName("link")[0];
96 var id = transport.responseXML.getElementsByTagName("id")[0];
298f3f78 97
06925d9e
AD
98 debug("open_article_callback, received link: " + link);
99
04e91733 100 if (link && id) {
06925d9e 101
04e91733
AD
102 var wname = "ttrss_article_" + id.firstChild.nodeValue;
103
104 debug("link url: " + link.firstChild.nodeValue + ", wname " + wname);
105
106 var w = window.open(link.firstChild.nodeValue, wname);
4298481d
AD
107
108 if (!w) { notify_error("Failed to load article in new window"); }
e2ccbfab 109
d395a942
AD
110 if (id) {
111 id = id.firstChild.nodeValue;
112 if (!document.getElementById("headlinesList")) {
113 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
e2ccbfab 114 }
298f3f78 115 }
06925d9e
AD
116 } else {
117 notify_error("Can't open article: received invalid article link");
298f3f78 118 }
06925d9e
AD
119 } else {
120 notify_error("Can't open article: received invalid XML");
01a87dff 121 }
01a87dff 122
d395a942
AD
123 } catch (e) {
124 exception_error("open_article_callback", e);
9cfc649a
AD
125 }
126}
127
7726fa02
AD
128function param_escape(arg) {
129 if (typeof encodeURIComponent != 'undefined')
130 return encodeURIComponent(arg);
131 else
132 return escape(arg);
133}
134
135function param_unescape(arg) {
136 if (typeof decodeURIComponent != 'undefined')
137 return decodeURIComponent(arg);
138 else
139 return unescape(arg);
140}
141
508a81e1
AD
142function delay(gap) {
143 var then,now;
144 then=new Date().getTime();
145 now=then;
146 while((now-then)<gap) {
147 now=new Date().getTime();
148 }
149}
7726fa02 150
ce3bf408 151var notify_hide_timerid = false;
59a543f0 152
ce3bf408 153function hide_notify() {
42c32916
AD
154 var n = document.getElementById("notify");
155 if (n) {
d05514a4 156 n.style.display = "none";
8dcfffd0 157 }
d05514a4 158}
c05608c2 159
730dbf19
AD
160function notify_silent_next() {
161 notify_silent = true;
162}
163
42c32916 164function notify_real(msg, no_hide, n_type) {
7726fa02 165
730dbf19
AD
166 if (notify_silent) {
167 notify_silent = false;
168 return;
169 }
170
42c32916
AD
171 var n = document.getElementById("notify");
172 var nb = document.getElementById("notify_body");
7726fa02 173
c05608c2 174 if (!n || !nb) return;
f0601b87 175
0655a1d5
AD
176 if (notify_hide_timerid) {
177 window.clearTimeout(notify_hide_timerid);
178 }
179
8dcfffd0 180 if (msg == "") {
0655a1d5
AD
181 if (n.style.display == "block") {
182 notify_hide_timerid = window.setTimeout("hide_notify()", 0);
183 }
184 return;
7726fa02 185 } else {
0ceded7a 186 n.style.display = "block";
8dcfffd0 187 }
7726fa02 188
42c32916
AD
189 /* types:
190
191 1 - generic
192 2 - progress
193 3 - error
194 4 - info
195
196 */
197
f3977cf5
AD
198 if (typeof __ != 'undefined') {
199 msg = __(msg);
200 }
201
42c32916
AD
202 if (n_type == 1) {
203 n.className = "notify";
204 } else if (n_type == 2) {
205 n.className = "notifyProgress";
206 msg = "<img src='images/indicator_white.gif'> " + msg;
207 } else if (n_type == 3) {
f407c086 208 n.className = "notifyError";
e780d1d2 209 msg = "<img src='images/sign_excl.gif'> " + msg;
42c32916
AD
210 } else if (n_type == 4) {
211 n.className = "notifyInfo";
e780d1d2 212 msg = "<img src='images/sign_info.gif'> " + msg;
0530ddd8
AD
213 }
214
106689b0
AD
215// msg = "<img src='images/live_com_loading.gif'> " + msg;
216
0ceded7a
AD
217 nb.innerHTML = msg;
218
4d4200a8 219 if (!no_hide) {
292a8a12 220 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
4d4200a8 221 }
ce3bf408
AD
222}
223
42c32916
AD
224function notify(msg, no_hide) {
225 notify_real(msg, no_hide, 1);
ce3bf408
AD
226}
227
42c32916
AD
228function notify_progress(msg, no_hide) {
229 notify_real(msg, no_hide, 2);
230}
231
232function notify_error(msg, no_hide) {
233 notify_real(msg, no_hide, 3);
234
235}
236
237function notify_info(msg, no_hide) {
238 notify_real(msg, no_hide, 4);
7726fa02
AD
239}
240
508a81e1 241function printLockingError() {
42c32916
AD
242 notify_info("Please wait until operation finishes.");
243}
508a81e1 244
e828e31e
AD
245function cleanSelected(element) {
246 var content = document.getElementById(element);
f0601b87
AD
247
248 for (i = 0; i < content.rows.length; i++) {
249 content.rows[i].className = content.rows[i].className.replace("Selected", "");
250 }
f0601b87
AD
251}
252
253function getVisibleUnreadHeadlines() {
254 var content = document.getElementById("headlinesList");
255
256 var rows = new Array();
257
3a40e8a2
AD
258 if (!content) return rows;
259
f0601b87
AD
260 for (i = 0; i < content.rows.length; i++) {
261 var row_id = content.rows[i].id.replace("RROW-", "");
262 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
263 rows.push(row_id);
264 }
265 }
266 return rows;
267}
268
269function getVisibleHeadlineIds() {
270
271 var content = document.getElementById("headlinesList");
272
273 var rows = new Array();
274
3a40e8a2
AD
275 if (!content) return rows;
276
f0601b87
AD
277 for (i = 0; i < content.rows.length; i++) {
278 var row_id = content.rows[i].id.replace("RROW-", "");
279 if (row_id.length > 0) {
280 rows.push(row_id);
281 }
282 }
283 return rows;
284}
285
286function getFirstVisibleHeadlineId() {
5ad9d132
AD
287 if (isCdmMode()) {
288 var rows = cdmGetVisibleArticles();
289 return rows[0];
290 } else {
291 var rows = getVisibleHeadlineIds();
292 return rows[0];
293 }
f0601b87
AD
294}
295
296function getLastVisibleHeadlineId() {
5ad9d132
AD
297 if (isCdmMode()) {
298 var rows = cdmGetVisibleArticles();
299 return rows[rows.length-1];
300 } else {
301 var rows = getVisibleHeadlineIds();
302 return rows[rows.length-1];
303 }
f0601b87
AD
304}
305
306function markHeadline(id) {
307 var row = document.getElementById("RROW-" + id);
308 if (row) {
35f3c923
AD
309 var is_active = false;
310
311 if (row.className.match("Active")) {
312 is_active = true;
313 }
314 row.className = row.className.replace("Selected", "");
315 row.className = row.className.replace("Active", "");
316 row.className = row.className.replace("Insensitive", "");
317
318 if (is_active) {
319 row.className = row.className = "Active";
320 }
321
72020095
AD
322 var check = document.getElementById("RCHK-" + id);
323
324 if (check) {
325 check.checked = true;
326 }
327
35f3c923
AD
328 row.className = row.className + "Selected";
329
f0601b87
AD
330 }
331}
332
333function getFeedIds() {
334 var content = document.getElementById("feedsList");
335
336 var rows = new Array();
337
338 for (i = 0; i < content.rows.length; i++) {
339 var id = content.rows[i].id.replace("FEEDR-", "");
340 if (id.length > 0) {
341 rows.push(id);
342 }
343 }
344
345 return rows;
346}
13ad9102 347
76b4eae1
AD
348function setCookie(name, value, lifetime, path, domain, secure) {
349
350 var d = false;
351
352 if (lifetime) {
353 d = new Date();
be0801a1 354 d.setTime(d.getTime() + (lifetime * 1000));
76b4eae1 355 }
cdbb6dc6
AD
356
357 debug("setCookie: " + name + " => " + value + ": " + d);
76b4eae1
AD
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 444
ac378ad4 445function parse_counters(reply, scheduled_call) {
1a6a9555 446 try {
ac378ad4 447
9e397d0f
AD
448 var feeds_found = 0;
449
85bd574b 450 var elems = reply.getElementsByTagName("counter");
f54f515f 451
85bd574b 452 for (var l = 0; l < elems.length; l++) {
6043fb7e 453
85bd574b
AD
454 var id = elems[l].getAttribute("id");
455 var t = elems[l].getAttribute("type");
456 var ctr = elems[l].getAttribute("counter");
457 var error = elems[l].getAttribute("error");
458 var has_img = elems[l].getAttribute("hi");
459 var updated = elems[l].getAttribute("updated");
4ffa126e 460 var title = elems[l].getAttribute("title");
bdb7369b
AD
461 var xmsg = elems[l].getAttribute("xmsg");
462
1a6a9555 463 if (id == "global-unread") {
0feab655
AD
464 global_unread = ctr;
465 updateTitle();
1a6a9555
AD
466 continue;
467 }
7bf7e4d3
AD
468
469 if (id == "subscribed-feeds") {
470 feeds_found = ctr;
471 continue;
472 }
1a6a9555
AD
473
474 if (t == "category") {
0feab655 475 var catctr = document.getElementById("FCATCTR-" + id);
1a6a9555 476 if (catctr) {
67dabe1a
AD
477 catctr.innerHTML = "(" + ctr + ")";
478 if (ctr > 0) {
479 catctr.className = "catCtrHasUnread";
480 } else {
481 catctr.className = "catCtrNoUnread";
482 }
1a6a9555
AD
483 }
484 continue;
485 }
486
0feab655
AD
487 var feedctr = document.getElementById("FEEDCTR-" + id);
488 var feedu = document.getElementById("FEEDU-" + id);
489 var feedr = document.getElementById("FEEDR-" + id);
490 var feed_img = document.getElementById("FIMG-" + id);
491 var feedlink = document.getElementById("FEEDL-" + id);
492 var feedupd = document.getElementById("FLUPD-" + id);
fb1fb4ab
AD
493
494 if (updated && feedlink) {
495 if (error) {
496 feedlink.title = "Error: " + error + " (" + updated + ")";
497 } else {
498 feedlink.title = "Updated: " + updated;
499 }
500 }
023fe037 501
bdb7369b
AD
502 if (feedupd) {
503 if (!updated) updated = "";
504
78d5212c 505 if (error) {
bdb7369b
AD
506 if (xmsg) {
507 feedupd.innerHTML = updated + " " + xmsg + " (Error)";
508 } else {
509 feedupd.innerHTML = updated + " (Error)";
510 }
78d5212c 511 } else {
bdb7369b
AD
512 if (xmsg) {
513 feedupd.innerHTML = updated + " " + xmsg;
514 } else {
515 feedupd.innerHTML = updated;
516 }
78d5212c
AD
517 }
518 }
519
f74cfded 520 if (has_img && feed_img) {
f780548a
AD
521 if (!feed_img.src.match(id + ".ico")) {
522 feed_img.src = getInitParam("icons_location") + "/" + id + ".ico";
523 }
527c3bf0
AD
524 }
525
4ffa126e
AD
526 if (feedlink && title) {
527 feedlink.innerHTML = title;
528 }
529
1a6a9555 530 if (feedctr && feedu && feedr) {
e8ef3b97
AD
531
532 if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
5b064721 533 viewCurrentFeed();
e8ef3b97 534 }
8e9141ed
AD
535
536 var row_needs_hl = (ctr > 0 && ctr > parseInt(feedu.innerHTML));
537
1a6a9555 538 feedu.innerHTML = ctr;
023fe037 539
426d3c57
AD
540 if (error) {
541 feedr.className = feedr.className.replace("feed", "error");
542 } else if (id > 0) {
543 feedr.className = feedr.className.replace("error", "feed");
023fe037 544 }
1a6a9555
AD
545
546 if (ctr > 0) {
d7e83df7 547 feedctr.className = "feedCtrHasUnread";
1a6a9555
AD
548 if (!feedr.className.match("Unread")) {
549 var is_selected = feedr.className.match("Selected");
550
551 feedr.className = feedr.className.replace("Selected", "");
552 feedr.className = feedr.className.replace("Unread", "");
553
554 feedr.className = feedr.className + "Unread";
555
556 if (is_selected) {
557 feedr.className = feedr.className + "Selected";
8e9141ed
AD
558 }
559
560 }
561
562 if (row_needs_hl) {
1341ea0d
AD
563 new Effect.Highlight(feedr, {duration: 1, startcolor: "#fff7d5",
564 queue: { position:'end', scope: 'EFQ-' + id, limit: 1 } } );
1a6a9555
AD
565 }
566 } else {
d7e83df7 567 feedctr.className = "feedCtrNoUnread";
1a6a9555
AD
568 feedr.className = feedr.className.replace("Unread", "");
569 }
570 }
571 }
9e397d0f 572
60ea2377 573 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
7553dd8b 574
0feab655 575 var feeds_stored = number_of_feeds;
9e397d0f
AD
576
577 debug("Feed counters, C: " + feeds_found + ", S:" + feeds_stored);
578
579 if (feeds_stored != feeds_found) {
0feab655 580 number_of_feeds = feeds_found;
7bf7e4d3 581
2fe69e22 582 if (feeds_stored != 0 && feeds_found != 0) {
9e397d0f 583 debug("Subscribed feed number changed, refreshing feedlist");
0e9dd1ba 584 setTimeout('updateFeedList(false, false)', 50);
9e397d0f
AD
585 }
586 }
587
1a6a9555 588 } catch (e) {
83f043bb 589 exception_error("parse_counters", e);
1a6a9555
AD
590 }
591}
592
288487e4 593function parse_counters_reply(transport, scheduled_call) {
5854573a 594
288487e4 595 if (!transport.responseXML) {
42c32916 596 notify_error("Backend did not return valid XML", true);
5854573a
AD
597 return;
598 }
599
288487e4 600 var reply = transport.responseXML.firstChild;
5854573a
AD
601
602 if (!reply) {
42c32916 603 notify_error("Backend did not return expected XML object", true);
5854573a
AD
604 updateTitle("");
605 return;
606 }
607
608 var error_code = false;
609 var error_msg = false;
610
611 if (reply.firstChild) {
612 error_code = reply.firstChild.getAttribute("error-code");
613 error_msg = reply.firstChild.getAttribute("error-msg");
614 }
615
616 if (!error_code) {
617 error_code = reply.getAttribute("error-code");
618 error_msg = reply.getAttribute("error-msg");
619 }
620
621 if (error_code && error_code != 0) {
622 debug("refetch_callback: got error code " + error_code);
623 return fatalError(error_code, error_msg);
624 }
625
36e05046 626 var counters = reply.getElementsByTagName("counters")[0];
5854573a 627
3ac2f3c7 628 parse_counters(counters, scheduled_call);
5854573a 629
36e05046 630 var runtime_info = reply.getElementsByTagName("runtime-info")[0];
5854573a
AD
631
632 parse_runtime_info(runtime_info);
633
61a20560
AD
634 if (feedsSortByUnread()) {
635 resort_feedlist();
636 }
5854573a 637
60ea2377 638 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
5854573a
AD
639
640}
641
6cc1fab3 642function all_counters_callback2(transport, async_call) {
1341ea0d 643 try {
6cc1fab3
AD
644 if (async_call) async_counters_work = true;
645
1341ea0d
AD
646 debug("<b>all_counters_callback2 IN: " + transport + "</b>");
647 parse_counters_reply(transport);
648 debug("<b>all_counters_callback2 OUT: " + transport + "</b>");
649
650 } catch (e) {
273d1e29 651 exception_error("all_counters_callback2", e, transport);
1341ea0d
AD
652 }
653}
654
8e9dd206
AD
655function get_feed_unread(id) {
656 try {
657 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);
658 } catch (e) {
8e9dd206
AD
659 return -1;
660 }
661}
662
ec37f4f1
AD
663function get_cat_unread(id) {
664 try {
665 var ctr = document.getElementById("FCATCTR-" + id).innerHTML;
666 ctr = ctr.replace("(", "");
667 ctr = ctr.replace(")", "");
668 return parseInt(ctr);
669 } catch (e) {
ec37f4f1
AD
670 return -1;
671 }
672}
673
60ea2377 674function get_feed_entry_unread(elem) {
c9268ed5
AD
675
676 var id = elem.id.replace("FEEDR-", "");
677
678 if (id <= 0) {
679 return -1;
680 }
681
682 try {
60ea2377 683 return parseInt(document.getElementById("FEEDU-" + id).innerHTML);
c9268ed5
AD
684 } catch (e) {
685 return -1;
686 }
687}
688
61a20560
AD
689function get_feed_entry_name(elem) {
690 var id = elem.id.replace("FEEDR-", "");
691 return getFeedName(id);
692}
693
694
60ea2377 695function resort_category(node) {
c9268ed5 696
d6586c1d 697 try {
61a20560 698
d6586c1d
AD
699 debug("resort_category: " + node);
700
701 var by_unread = feedsSortByUnread();
702
703 var list = node.getElementsByTagName("LI");
704
705 for (i = 0; i < list.length; i++) {
706
707 for (j = i+1; j < list.length; j++) {
708
709 var tmp_val = get_feed_entry_unread(list[i]);
710 var cur_val = get_feed_entry_unread(list[j]);
711
712 var tmp_name = get_feed_entry_name(list[i]);
713 var cur_name = get_feed_entry_name(list[j]);
714
61a20560 715 if ((by_unread && (cur_val > tmp_val)) || (!by_unread && (cur_name < tmp_name))) {
d6586c1d
AD
716 tempnode_i = list[i].cloneNode(true);
717 tempnode_j = list[j].cloneNode(true);
718 node.replaceChild(tempnode_i, list[j]);
719 node.replaceChild(tempnode_j, list[i]);
c9268ed5
AD
720 }
721 }
c9268ed5 722 }
d6586c1d
AD
723
724 } catch (e) {
725 exception_error("resort_category", e);
c9268ed5
AD
726 }
727
728}
729
730function resort_feedlist() {
731 debug("resort_feedlist");
732
60ea2377 733 if (document.getElementById("FCATLIST--1")) {
c9268ed5 734
60ea2377 735 var lists = document.getElementsByTagName("UL");
c9268ed5 736
60ea2377
AD
737 for (var i = 0; i < lists.length; i++) {
738 if (lists[i].id && lists[i].id.match("FCATLIST-")) {
739 resort_category(lists[i]);
c9268ed5 740 }
c9268ed5
AD
741 }
742
743 } else {
60ea2377 744 resort_category(document.getElementById("feedList"));
c9268ed5
AD
745 }
746}
747
b6644d29
AD
748/** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
749 * * @author Sundar Dorai-Raj
750 * * Email: sdoraira@vt.edu
751 * * This program is free software; you can redistribute it and/or
752 * * modify it under the terms of the GNU General Public License
753 * * as published by the Free Software Foundation; either version 2
754 * * of the License, or (at your option) any later version,
755 * * provided that any use properly credits the author.
756 * * This program is distributed in the hope that it will be useful,
757 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
758 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
759 * * GNU General Public License for more details at http://www.gnu.org * * */
760
761 var numbers=".0123456789";
762 function isNumeric(x) {
763 // is x a String or a character?
764 if(x.length>1) {
765 // remove negative sign
766 x=Math.abs(x)+"";
767 for(j=0;j<x.length;j++) {
768 // call isNumeric recursively for each character
769 number=isNumeric(x.substring(j,j+1));
770 if(!number) return number;
771 }
772 return number;
773 }
774 else {
775 // if x is number return true
776 if(numbers.indexOf(x)>=0) return true;
777 return false;
778 }
779 }
780
3745788e 781
60ea2377 782function hideOrShowFeeds(hide) {
3745788e 783
60ea2377 784 try {
293fa942 785
60ea2377 786 debug("hideOrShowFeeds: " + hide);
293fa942 787
60ea2377 788 if (document.getElementById("FCATLIST--1")) {
293fa942 789
60ea2377 790 var lists = document.getElementsByTagName("UL");
293fa942 791
60ea2377
AD
792 for (var i = 0; i < lists.length; i++) {
793 if (lists[i].id && lists[i].id.match("FCATLIST-")) {
293fa942 794
60ea2377
AD
795 var id = lists[i].id.replace("FCATLIST-", "");
796 hideOrShowFeedsCategory(id, hide);
293fa942 797 }
293fa942
AD
798 }
799
800 } else {
60ea2377 801 hideOrShowFeedsCategory(null, hide);
293fa942 802 }
293fa942 803
60ea2377
AD
804 } catch (e) {
805 exception_error("hideOrShowFeeds", e);
4724a093 806 }
60ea2377 807}
4724a093 808
60ea2377 809function hideOrShowFeedsCategory(id, hide) {
2c2b019b 810
60ea2377
AD
811 try {
812
813 var node = null;
814 var cat_node = null;
bbf4d0b2 815
60ea2377
AD
816 if (id) {
817 node = document.getElementById("FCATLIST-" + id);
818 cat_node = document.getElementById("FCAT-" + id);
819 } else {
820 node = document.getElementById("feedList"); // no categories
821 }
293fa942 822
60ea2377 823 // debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
b31c2311 824
60ea2377 825 var cat_unread = 0;
b31c2311 826
60ea2377
AD
827 if (!node) {
828 debug("hideOrShowFeeds: passed node is null, aborting");
829 return;
830 }
b31c2311 831
60ea2377
AD
832 // debug("cat: " + node.id);
833
834 if (node.hasChildNodes() && node.firstChild.nextSibling != false) {
835 for (i = 0; i < node.childNodes.length; i++) {
836 if (node.childNodes[i].nodeName != "LI") { continue; }
b31c2311 837
60ea2377
AD
838 if (node.childNodes[i].style != undefined) {
839
840 var has_unread = (node.childNodes[i].className != "feed" &&
841 node.childNodes[i].className != "label" &&
842 !(!getInitParam("hide_read_shows_special") &&
843 node.childNodes[i].className == "virt") &&
844 node.childNodes[i].className != "error" &&
845 node.childNodes[i].className != "tag");
846
847 // debug(node.childNodes[i].id + " --> " + has_unread);
848
849 if (hide && !has_unread) {
850 //node.childNodes[i].style.display = "none";
851 var id = node.childNodes[i].id;
852 Effect.Fade(node.childNodes[i], {duration : 0.3,
853 queue: { position: 'end', scope: 'FFADE-' + id, limit: 1 }});
854 }
855
856 if (!hide) {
857 node.childNodes[i].style.display = "list-item";
858 //Effect.Appear(node.childNodes[i], {duration : 0.3});
859 }
860
861 if (has_unread) {
862 node.childNodes[i].style.display = "list-item";
863 cat_unread++;
864 //Effect.Appear(node.childNodes[i], {duration : 0.3});
865 //Effect.Highlight(node.childNodes[i]);
866 }
b31c2311 867 }
293fa942 868 }
60ea2377
AD
869 }
870
871 // debug("end cat: " + node.id + " unread " + cat_unread);
872
873 if (cat_unread == 0) {
874 if (cat_node.style == undefined) {
875 debug("ERROR: supplied cat_node " + cat_node +
876 " has no styles. WTF?");
877 return;
878 }
879 if (hide) {
880 //cat_node.style.display = "none";
881 Effect.Fade(cat_node, {duration : 0.3,
882 queue: { position: 'end', scope: 'CFADE-' + node.id, limit: 1 }});
883 } else {
884 cat_node.style.display = "list-item";
885 }
293fa942 886 } else {
60ea2377
AD
887 try {
888 cat_node.style.display = "list-item";
889 } catch (e) {
890 debug(e);
891 }
0ac2faea 892 }
3745788e 893
293fa942 894// debug("unread for category: " + cat_unread);
60ea2377
AD
895
896 } catch (e) {
897 exception_error("hideOrShowFeedsCategory", e);
898 }
3745788e
AD
899}
900
35f3c923
AD
901function selectTableRow(r, do_select) {
902 r.className = r.className.replace("Selected", "");
903
904 if (do_select) {
905 r.className = r.className + "Selected";
906 }
907}
908
6c12c809
AD
909function selectTableRowById(elem_id, check_id, do_select) {
910
911 try {
912
913 var row = document.getElementById(elem_id);
914
915 if (row) {
916 selectTableRow(row, do_select);
917 }
918
919 var check = document.getElementById(check_id);
920
921 if (check) {
922 check.checked = do_select;
923 }
924 } catch (e) {
925 exception_error("selectTableRowById", e);
926 }
927}
928
1572afe5 929function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
649e0af9 930 classcheck, reset_others) {
3745788e 931
35f3c923
AD
932 var content = document.getElementById(content_id);
933
934 if (!content) {
935 alert("[selectTableRows] Element " + content_id + " not found.");
936 return;
937 }
938
939 for (i = 0; i < content.rows.length; i++) {
7a822893
AD
940 if (Element.visible(content.rows[i])) {
941 if (!classcheck || content.rows[i].className.match(classcheck)) {
942
943 if (content.rows[i].id.match(prefix)) {
944 selectTableRow(content.rows[i], do_select);
945
946 var row_id = content.rows[i].id.replace(prefix, "");
947 var check = document.getElementById(check_prefix + row_id);
948
949 if (check) {
950 check.checked = do_select;
951 }
952 } else if (reset_others) {
953 selectTableRow(content.rows[i], false);
954
955 var row_id = content.rows[i].id.replace(prefix, "");
956 var check = document.getElementById(check_prefix + row_id);
957
958 if (check) {
959 check.checked = false;
960 }
1572afe5 961
649e0af9
AD
962 }
963 } else if (reset_others) {
964 selectTableRow(content.rows[i], false);
7a822893 965
7d8d10c6
AD
966 var row_id = content.rows[i].id.replace(prefix, "");
967 var check = document.getElementById(check_prefix + row_id);
7a822893 968
7d8d10c6
AD
969 if (check) {
970 check.checked = false;
971 }
7a822893 972
7d8d10c6 973 }
3055bc41 974 }
35f3c923 975 }
295f9b42 976}
91ff844a
AD
977
978function getSelectedTableRowIds(content_id, prefix) {
979
980 var content = document.getElementById(content_id);
981
982 if (!content) {
983 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
984 return;
985 }
986
987 var sel_rows = new Array();
988
989 for (i = 0; i < content.rows.length; i++) {
1572afe5
AD
990 if (content.rows[i].id.match(prefix) &&
991 content.rows[i].className.match("Selected")) {
992
91ff844a
AD
993 var row_id = content.rows[i].id.replace(prefix + "-", "");
994 sel_rows.push(row_id);
995 }
996 }
997
998 return sel_rows;
999
1000}
1001
386cbf27
AD
1002function toggleSelectRowById(sender, id) {
1003 var row = document.getElementById(id);
1004
1005 if (sender.checked) {
1006 if (!row.className.match("Selected")) {
1007 row.className = row.className + "Selected";
1008 }
1009 } else {
1010 if (row.className.match("Selected")) {
1011 row.className = row.className.replace("Selected", "");
1012 }
1013 }
1014}
1015
b92e6209
AD
1016function toggleSelectListRow(sender) {
1017 var parent_row = sender.parentNode;
1018
1019 if (sender.checked) {
1020 if (!parent_row.className.match("Selected")) {
1021 parent_row.className = parent_row.className + "Selected";
1022 }
1023 } else {
1024 if (parent_row.className.match("Selected")) {
1025 parent_row.className = parent_row.className.replace("Selected", "");
1026 }
1027 }
1028}
1029
67343d9f
AD
1030function tSR(sender) {
1031 return toggleSelectRow(sender);
1032}
386cbf27 1033
1572afe5
AD
1034function toggleSelectRow(sender) {
1035 var parent_row = sender.parentNode.parentNode;
1036
1037 if (sender.checked) {
1038 if (!parent_row.className.match("Selected")) {
1039 parent_row.className = parent_row.className + "Selected";
1040 }
1041 } else {
1042 if (parent_row.className.match("Selected")) {
1043 parent_row.className = parent_row.className.replace("Selected", "");
1044 }
1045 }
1046}
1047
f46192bb
AD
1048function getNextUnreadCat(id) {
1049 try {
1050 var rows = document.getElementById("feedList").getElementsByTagName("LI");
1051 var feeds = new Array();
1052
1053 var unread_only = true;
1054 var is_cat = true;
1055
1056 for (var i = 0; i < rows.length; i++) {
1057 if (rows[i].id.match("FCAT-")) {
1058 if (rows[i].id == "FCAT-" + id && is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1059
1060 var cat_id = parseInt(rows[i].id.replace("FCAT-", ""));
1061
1062 if (cat_id >= 0) {
1063 if (!unread_only || get_cat_unread(cat_id) > 0) {
1064 feeds.push(cat_id);
1065 }
1066 }
1067 }
1068 }
1069 }
1070
1071 var idx = feeds.indexOf(id);
1072 if (idx != -1 && idx < feeds.length) {
1073 return feeds[idx+1];
1074 } else {
1075 return feeds.shift();
1076 }
1077
1078 } catch (e) {
1079 exception_error("getNextUnreadCat", e);
1080 }
1081}
ec37f4f1
AD
1082
1083function getRelativeFeedId2(id, is_cat, direction, unread_only) {
1084 try {
1085
1086// alert(id + " IC: " + is_cat + " D: " + direction + " U: " + unread_only);
1087
1088 var rows = document.getElementById("feedList").getElementsByTagName("LI");
1089 var feeds = new Array();
1090
1091 for (var i = 0; i < rows.length; i++) {
1092 if (rows[i].id.match("FEEDR-")) {
1093
1094 if (rows[i].id == "FEEDR-" + id && !is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1095
1096 if (!unread_only ||
1097 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1098 feeds.push(rows[i].id.replace("FEEDR-", ""));
1099 }
1100 }
1101 }
1102
1103 if (rows[i].id.match("FCAT-")) {
1104 if (rows[i].id == "FCAT-" + id && is_cat || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
1105
1106 var cat_id = parseInt(rows[i].id.replace("FCAT-", ""));
1107
1108 if (cat_id >= 0) {
1109 if (!unread_only || get_cat_unread(cat_id) > 0) {
1110 feeds.push("CAT:"+cat_id);
1111 }
1112 }
1113 }
1114 }
1115 }
1116
1117// alert(feeds.toString());
1118
1119 if (!id) {
1120 if (direction == "next") {
1121 return feeds.shift();
1122 } else {
1123 return feeds.pop();
1124 }
1125 } else {
1126 if (direction == "next") {
1127 if (is_cat) id = "CAT:" + id;
1128 var idx = feeds.indexOf(id);
1129 if (idx != -1 && idx < feeds.length) {
1130 return feeds[idx+1];
1131 } else {
1132 return getRelativeFeedId2(false, is_cat, direction, unread_only);
1133 }
1134 } else {
1135 if (is_cat) id = "CAT:" + id;
1136 var idx = feeds.indexOf(id);
1137 if (idx > 0) {
1138 return feeds[idx-1];
1139 } else {
1140 return getRelativeFeedId2(false, is_cat, direction, unread_only);
1141 }
1142 }
1143
1144 }
1145
1146 } catch (e) {
1147 exception_error("getRelativeFeedId2", e);
1148 }
1149}
1150
1151
1da76274 1152function getRelativeFeedId(list, id, direction, unread_only) {
731b05f4
AD
1153 var rows = list.getElementsByTagName("LI");
1154 var feeds = new Array();
1155
1156 for (var i = 0; i < rows.length; i++) {
1157 if (rows[i].id.match("FEEDR-")) {
1158
8809f484 1159 if (rows[i].id == "FEEDR-" + id || (Element.visible(rows[i]) && Element.visible(rows[i].parentNode))) {
731b05f4
AD
1160
1161 if (!unread_only ||
1162 (rows[i].className.match("Unread") || rows[i].id == "FEEDR-" + id)) {
1163 feeds.push(rows[i].id.replace("FEEDR-", ""));
1164 }
1165 }
1166 }
1167 }
1168
7b433d8c 1169 if (!id) {
731b05f4
AD
1170 if (direction == "next") {
1171 return feeds.shift();
1172 } else {
1173 return feeds.pop();
1174 }
1175 } else {
1176 if (direction == "next") {
1177 var idx = feeds.indexOf(id);
1178 if (idx != -1 && idx < feeds.length) {
1179 return feeds[idx+1];
07a67863
AD
1180 } else {
1181 return getRelativeFeedId(list, false, direction, unread_only);
731b05f4
AD
1182 }
1183 } else {
1184 var idx = feeds.indexOf(id);
1185 if (idx > 0) {
1186 return feeds[idx-1];
07a67863
AD
1187 } else {
1188 return getRelativeFeedId(list, false, direction, unread_only);
731b05f4
AD
1189 }
1190 }
1191
1192 }
7b433d8c 1193}
36aab70f 1194
f27de515 1195function showBlockElement(id, h_id) {
36aab70f
AD
1196 var elem = document.getElementById(id);
1197
1198 if (elem) {
1199 elem.style.display = "block";
f27de515
AD
1200
1201 if (h_id) {
1202 elem = document.getElementById(h_id);
1203 if (elem) {
1204 elem.style.display = "none";
1205 }
1206 }
36aab70f
AD
1207 } else {
1208 alert("[showBlockElement] can't find element with id " + id);
1209 }
1210}
1211
b9073cd9
AD
1212function appearBlockElement_afh(effect) {
1213
1214}
1215
1216function checkboxToggleElement(elem, id) {
1217 if (elem.checked) {
ecace165 1218 Effect.Appear(id, {duration : 0.5});
b9073cd9 1219 } else {
ecace165 1220 Effect.Fade(id, {duration : 0.5});
b9073cd9
AD
1221 }
1222}
1223
1224function appearBlockElement(id, h_id) {
1225
1226 try {
a04c8e8d
AD
1227 if (h_id) {
1228 Effect.Fade(h_id);
1229 }
b9073cd9
AD
1230 Effect.SlideDown(id, {duration : 1.0, afterFinish: appearBlockElement_afh});
1231 } catch (e) {
1232 exception_error("appearBlockElement", e);
1233 }
1234
1235}
1236
a9b0bfd5
AD
1237function hideParentElement(e) {
1238 e.parentNode.style.display = "none";
1239}
1b0809ae
AD
1240
1241function dropboxSelect(e, v) {
1242 for (i = 0; i < e.length; i++) {
1243 if (e[i].value == v) {
1244 e.selectedIndex = i;
1245 break;
1246 }
1247 }
1248}
0ee1d1a0
AD
1249
1250// originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
1251// bugfixed just a little bit :-)
1252function getURLParam(strParamName){
1253 var strReturn = "";
1254 var strHref = window.location.href;
1255
1256 if (strHref.indexOf("#") == strHref.length-1) {
1257 strHref = strHref.substring(0, strHref.length-1);
1258 }
1259
1260 if ( strHref.indexOf("?") > -1 ){
1261 var strQueryString = strHref.substr(strHref.indexOf("?"));
1262 var aQueryString = strQueryString.split("&");
1263 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
1264 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
1265 var aParam = aQueryString[iParam].split("=");
1266 strReturn = aParam[1];
1267 break;
1268 }
1269 }
1270 }
1271 return strReturn;
1272}
1273
cf1bc085
AD
1274function leading_zero(p) {
1275 var s = String(p);
1276 if (s.length == 1) s = "0" + s;
1277 return s;
1278}
c38c2b69 1279
e8a429e3 1280function closeErrorBox() {
5ede560f 1281
273d1e29 1282 if (Element.visible("errorBoxShadow")) {
e8a429e3 1283 Element.hide("dialog_overlay");
273d1e29 1284 Element.hide("errorBoxShadow");
e5d758e3 1285
e8a429e3 1286 enableHotkeys();
e5d758e3
AD
1287 }
1288
e8a429e3
AD
1289 return false;
1290}
86b682ce 1291
e8a429e3
AD
1292function closeInfoBox(cleanup) {
1293
1294 if (Element.visible("infoBoxShadow")) {
1295 Element.hide("dialog_overlay");
1296
1297 var shadow = document.getElementById('infoBoxShadow');
1298 var box = document.getElementById('infoBoxShadow');
1299
1300 Element.hide(shadow);
1301
1302 if (cleanup) box.innerHTML = "&nbsp;";
1303
1304 enableHotkeys();
1305 }
c14b5566 1306
90ac84df 1307 return false;
e5d758e3
AD
1308}
1309
1310
7b5c6012
AD
1311function displayDlg(id, param) {
1312
35a03bdd 1313 notify_progress("Loading, please wait...", true);
7b5c6012 1314
7b5c6012 1315 disableHotkeys();
2371c520 1316
288487e4
AD
1317 var query = "backend.php?op=dlg&id=" +
1318 param_escape(id) + "&param=" + param_escape(param);
1319
1320 new Ajax.Request(query, {
1321 onComplete: function (transport) {
1322 infobox_callback2(transport);
1323 } });
1324
90ac84df 1325 return false;
7b5c6012
AD
1326}
1327
288487e4
AD
1328function infobox_submit_callback2(transport) {
1329 closeInfoBox();
79f3553b 1330
288487e4
AD
1331 try {
1332 // called from prefs, reload tab
438f2ce9 1333 if (typeof active_tab != 'undefined' && active_tab) {
288487e4 1334 selectTab(active_tab, false);
5e6f933a 1335 }
288487e4 1336 } catch (e) { }
79f3553b 1337
288487e4
AD
1338 if (transport.responseText) {
1339 notify_info(transport.responseText);
1340 }
7b5c6012
AD
1341}
1342
d395a942
AD
1343function infobox_callback2(transport) {
1344 try {
5ede560f 1345
d395a942 1346 debug("infobox_callback2");
5ede560f 1347
569720c5
AD
1348 if (!getInitParam("infobox_disable_overlay")) {
1349 Element.show("dialog_overlay");
d395a942
AD
1350 }
1351
1352 var box = document.getElementById('infoBox');
1353 var shadow = document.getElementById('infoBoxShadow');
1354 if (box) {
1dc47c41 1355
d395a942
AD
1356 box.innerHTML=transport.responseText;
1357 if (shadow) {
1358 shadow.style.display = "block";
1359 } else {
1360 box.style.display = "block";
aa8716da 1361 }
d395a942 1362 }
05fcdf52 1363
d395a942 1364 /* FIXME this needs to be moved out somewhere */
05fcdf52 1365
d395a942
AD
1366 if (document.getElementById("tags_choices")) {
1367 new Ajax.Autocompleter('tags_str', 'tags_choices',
1368 "backend.php?op=rpc&subop=completeTags",
1369 { tokens: ',', paramName: "search" });
442d77f1 1370 }
d395a942 1371
6068d33b
AD
1372 disableHotkeys();
1373
d395a942
AD
1374 notify("");
1375 } catch (e) {
1376 exception_error("infobox_callback2", e);
442d77f1
AD
1377 }
1378}
1379
18ab3d7a 1380function createFilter() {
7b5c6012 1381
438f2ce9 1382 try {
6068d33b 1383
438f2ce9
AD
1384 var form = document.forms['filter_add_form'];
1385 var reg_exp = form.reg_exp.value;
1386
1387 if (reg_exp == "") {
1388 alert(__("Can't add filter: nothing to match on."));
1389 return false;
1390 }
288487e4 1391
438f2ce9
AD
1392 var query = Form.serialize("filter_add_form");
1393
1394 // we can be called from some other tab in Prefs
1395 if (typeof active_tab != 'undefined' && active_tab) {
1396 active_tab = "filterConfig";
1397 }
1398
1399 new Ajax.Request("backend.php?" + query, {
1400 onComplete: function (transport) {
1401 infobox_submit_callback2(transport);
1402 } });
1403
1404 return true;
1405
1406 } catch (e) {
1407 exception_error("createFilter", e);
1408 }
7b5c6012
AD
1409}
1410
2371c520
AD
1411function toggleSubmitNotEmpty(e, submit_id) {
1412 try {
1413 document.getElementById(submit_id).disabled = (e.value == "")
1414 } catch (e) {
1415 exception_error("toggleSubmitNotEmpty", e);
1416 }
1417}
1d7bf5a0 1418
605f7d46 1419function isValidURL(s) {
0d32b41e 1420 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
605f7d46 1421}
07eb9178 1422
18ab3d7a 1423function subscribeToFeed() {
07eb9178 1424
c91c2249
AD
1425 var form = document.forms['feed_add_form'];
1426 var feed_url = form.feed_url.value;
1427
1428 if (feed_url == "") {
89cb787e 1429 alert(__("Can't subscribe: no feed URL given."));
c91c2249
AD
1430 return false;
1431 }
1432
1ba6daf7 1433 notify_progress(__("Subscribing to feed..."), true);
07eb9178
AD
1434
1435 closeInfoBox();
1436
0feab655 1437 var feeds_doc = document;
07eb9178 1438
80e4dc34 1439// feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
07eb9178
AD
1440
1441 var query = Form.serialize("feed_add_form");
1442
f27de515
AD
1443 debug("subscribe q: " + query);
1444
288487e4
AD
1445 new Ajax.Request("backend.php", {
1446 parameters: query,
1447 onComplete: function(transport) {
1448 dlg_frefresh_callback(transport);
1449 } });
7bc4f251
AD
1450
1451 return false;
07eb9178
AD
1452}
1453
6e6504bc 1454function filterCR(e, f)
86b682ce
AD
1455{
1456 var key;
1457
1458 if(window.event)
1459 key = window.event.keyCode; //IE
1460 else
1461 key = e.which; //firefox
1462
6e6504bc
AD
1463 if (key == 13) {
1464 if (typeof f != 'undefined') {
1465 f();
1466 return false;
1467 } else {
1468 return false;
1469 }
1470 } else {
1471 return true;
1472 }
86b682ce
AD
1473}
1474
e8614131
AD
1475var debug_last_class = "even";
1476
ac378ad4 1477function debug(msg) {
ac378ad4 1478
0feab655
AD
1479 if (debug_last_class == "even") {
1480 debug_last_class = "odd";
e8614131 1481 } else {
0feab655 1482 debug_last_class = "even";
e8614131
AD
1483 }
1484
0feab655 1485 var c = document.getElementById('debug_output');
8836613c 1486 if (c && Element.visible(c)) {
c9268ed5 1487 while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
ac378ad4
AD
1488 c.removeChild(c.lastChild);
1489 }
1490
1491 var d = new Date();
1492 var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
1493 ":" + leading_zero(d.getSeconds());
0feab655 1494 c.innerHTML = "<li class=\"" + debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " +
e8614131 1495 msg + "</li>" + c.innerHTML;
ac378ad4
AD
1496 }
1497}
1498
33d13e72 1499function getInitParam(key) {
40496720 1500 return init_params[key];
33d13e72 1501}
3ac2b520 1502
be0801a1 1503function storeInitParam(key, value) {
40496720 1504 debug("<b>storeInitParam is OBSOLETE: " + key + " => " + value + "</b>");
5158ced9 1505 init_params[key] = value;
be0801a1
AD
1506}
1507
273d1e29 1508function fatalError(code, msg, ext_info) {
a7565293 1509 try {
a7565293 1510
b4c27af7 1511 if (code == 6) {
8e849206 1512 window.location.href = "tt-rss.php";
b4c27af7
AD
1513 } else if (code == 5) {
1514 window.location.href = "update.php";
1515 } else {
4724a093 1516
273d1e29 1517 if (msg == "") msg = "Unknown error";
42c32916 1518
273d1e29 1519 var ebc = document.getElementById("xebContent");
4724a093 1520
273d1e29
AD
1521 if (ebc) {
1522
1523 Element.show("dialog_overlay");
1524 Element.show("errorBoxShadow");
1525 Element.hide("xebBtn");
1526
1527 if (ext_info) {
1528 if (ext_info.responseText) {
1529 ext_info = ext_info.responseText;
1530 }
1531 }
1532
1533 ebc.innerHTML =
1534 "<div><b>Error message:</b></div>" +
1535 "<pre>" + msg + "</pre>" +
1536 "<div><b>Additional information:</b></div>" +
1537 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
1538 }
4724a093 1539 }
a7565293
AD
1540
1541 } catch (e) {
1542 exception_error("fatalError", e);
1543 }
1544}
1545
234e467c 1546function getFeedName(id, is_cat) {
234e467c
AD
1547 var e;
1548
1549 if (is_cat) {
5fc13f63 1550 e = document.getElementById("FCATN-" + id);
234e467c 1551 } else {
5fc13f63 1552 e = document.getElementById("FEEDN-" + id);
234e467c 1553 }
64a2875d
AD
1554 if (e) {
1555 return e.innerHTML.stripTags();
1556 } else {
1557 return null;
1558 }
1559}
0bd411db 1560
d0da85c2
AD
1561function filterDlgCheckType(sender) {
1562
1563 try {
1564
1565 var ftype = sender[sender.selectedIndex].value;
1566
1567 var form = document.forms["filter_add_form"];
1568
1569 if (!form) {
1570 form = document.forms["filter_edit_form"];
1571 }
1572
1573 if (!form) {
1574 debug("filterDlgCheckType: can't find form!");
1575 return;
1576 }
1577
1578 // if selected filter type is 5 (Date) enable the modifier dropbox
1579 if (ftype == 5) {
1580 Element.show("filter_dlg_date_mod_box");
1581 Element.show("filter_dlg_date_chk_box");
1582 } else {
1583 Element.hide("filter_dlg_date_mod_box");
1584 Element.hide("filter_dlg_date_chk_box");
1585
1586 }
1587
1588 } catch (e) {
1589 exception_error("filterDlgCheckType", e);
1590 }
1591
1592}
1593
350f0ad1
AD
1594function filterDlgCheckAction(sender) {
1595
1596 try {
1597
1598 var action = sender[sender.selectedIndex].value;
1599
1600 var form = document.forms["filter_add_form"];
1601
1602 if (!form) {
1603 form = document.forms["filter_edit_form"];
1604 }
1605
1606 if (!form) {
1607 debug("filterDlgCheckAction: can't find form!");
1608 return;
1609 }
1610
143a4973 1611 var action_param = document.getElementById("filter_dlg_param_box");
350f0ad1
AD
1612
1613 if (!action_param) {
143a4973 1614 debug("filterDlgCheckAction: can't find action param box!");
350f0ad1
AD
1615 return;
1616 }
1617
1618 // if selected action supports parameters, enable params field
ceb30ba4 1619 if (action == 4 || action == 6 || action == 7) {
143a4973 1620 Element.show(action_param);
ceb30ba4
AD
1621 if (action != 7) {
1622 Element.show(form.action_param);
1623 Element.hide(form.action_param_label);
1624 } else {
1625 Element.show(form.action_param_label);
1626 Element.hide(form.action_param);
1627 }
350f0ad1 1628 } else {
143a4973 1629 Element.hide(action_param);
350f0ad1
AD
1630 }
1631
1632 } catch (e) {
438f2ce9 1633 exception_error("filterDlgCheckAction", e);
350f0ad1
AD
1634 }
1635
1636}
ef16ae37 1637
d0da85c2
AD
1638function filterDlgCheckDate() {
1639 try {
1640 var form = document.forms["filter_add_form"];
1641
1642 if (!form) {
1643 form = document.forms["filter_edit_form"];
1644 }
1645
1646 if (!form) {
1647 debug("filterDlgCheckAction: can't find form!");
1648 return;
1649 }
1650
1651 var reg_exp = form.reg_exp.value;
1652
1653 var query = "backend.php?op=rpc&subop=checkDate&date=" + reg_exp;
1654
1655 new Ajax.Request(query, {
1656 onComplete: function(transport) {
1657
1658 var form = document.forms["filter_add_form"];
1659
1660 if (!form) {
1661 form = document.forms["filter_edit_form"];
1662 }
1663
1664 if (transport.responseXML) {
1665 var result = transport.responseXML.getElementsByTagName("result")[0];
1666
1667 if (result && result.firstChild) {
1668 if (result.firstChild.nodeValue == "1") {
1669
1670 new Effect.Highlight(form.reg_exp, {startcolor : '#00ff00'});
1671
1672 return;
1673 }
1674 }
1675 }
1676
1677 new Effect.Highlight(form.reg_exp, {startcolor : '#ff0000'});
1678
1679 } });
1680
1681
1682 } catch (e) {
1683 exception_error("filterDlgCheckDate", e);
1684 }
1685}
1686
ef16ae37
AD
1687function explainError(code) {
1688 return displayDlg("explainError", code);
1689}
01a87dff 1690
e097e8be 1691// this only searches loaded headlines list, not in CDM
aa0fa9df 1692function getRelativePostIds(id, limit) {
e097e8be 1693
aa0fa9df
AD
1694 if (!limit) limit = 3;
1695
1696 debug("getRelativePostIds: " + id + " limit=" + limit);
e097e8be
AD
1697
1698 var ids = new Array();
1699 var container = document.getElementById("headlinesList");
1700
1701 if (container) {
1702 var rows = container.rows;
1703
1704 for (var i = 0; i < rows.length; i++) {
1705 var r_id = rows[i].id.replace("RROW-", "");
1706
1707 if (r_id == id) {
aa0fa9df
AD
1708 for (var k = 1; k <= limit; k++) {
1709 var nid = false;
1710
1711 if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
1712 if (nid) ids.push(nid);
1713
1714 if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
1715 if (nid) ids.push(nid);
1716 }
e097e8be
AD
1717
1718 return ids;
1719 }
1720 }
1721 }
1722
1723 return false;
1724}
298f3f78
AD
1725
1726function openArticleInNewWindow(id) {
1727 try {
298f3f78
AD
1728 debug("openArticleInNewWindow: " + id);
1729
1730 var query = "backend.php?op=rpc&subop=getArticleLink&id=" + id;
04e91733
AD
1731 var wname = "ttrss_article_" + id;
1732
1733 debug(query + " " + wname);
1734
1735 var w = window.open("", wname);
298f3f78 1736
04e91733 1737 if (!w) notify_error("Failed to open window for the article");
298f3f78 1738
d395a942
AD
1739 new Ajax.Request(query, {
1740 onComplete: function(transport) {
1741 open_article_callback(transport);
1742 } });
1743
298f3f78
AD
1744
1745 } catch (e) {
1746 exception_error("openArticleInNewWindow", e);
1747 }
1748}
e4914b62 1749
537625c6
AD
1750/* http://textsnippets.com/posts/show/835 */
1751
1752Position.GetWindowSize = function(w) {
1753 w = w ? w : window;
1754 var width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
1755 var height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
1756 return [width, height]
1757}
1758
1759/* http://textsnippets.com/posts/show/836 */
1760
1761Position.Center = function(element, parent) {
1762 var w, h, pw, ph;
1763 var d = Element.getDimensions(element);
1764 w = d.width;
1765 h = d.height;
1766 Position.prepare();
1767 if (!parent) {
1768 var ws = Position.GetWindowSize();
1769 pw = ws[0];
1770 ph = ws[1];
1771 } else {
1772 pw = parent.offsetWidth;
1773 ph = parent.offsetHeight;
1774 }
1775 element.style.top = (ph/2) - (h/2) - Position.deltaY + "px";
1776 element.style.left = (pw/2) - (w/2) - Position.deltaX + "px";
1777}
1778
768858f1 1779
288487e4
AD
1780function labeltest_callback(transport) {
1781 try {
1782 var container = document.getElementById('label_test_result');
1783
1784 container.innerHTML = transport.responseText;
768858f1
AD
1785 if (!Element.visible(container)) {
1786 Effect.SlideDown(container, { duration : 0.5 });
1787 }
288487e4 1788
6f151277 1789 notify("");
288487e4
AD
1790 } catch (e) {
1791 exception_error("labeltest_callback", e);
6f151277
AD
1792 }
1793}
1794
1795function labelTest() {
1796
288487e4
AD
1797 try {
1798 var container = document.getElementById('label_test_result');
1799
1800 var form = document.forms['label_edit_form'];
1801
1802 var sql_exp = form.sql_exp.value;
1803 var description = form.description.value;
1804
1805 notify_progress("Loading, please wait...");
1806
1807 var query = "backend.php?op=pref-labels&subop=test&expr=" +
1808 param_escape(sql_exp) + "&descr=" + param_escape(description);
1809
1810 new Ajax.Request(query, {
1811 onComplete: function (transport) {
1812 labeltest_callback(transport);
1813 } });
1814
1815 return false;
6f151277 1816
288487e4
AD
1817 } catch (e) {
1818 exception_error("labelTest", e);
1819 }
6f151277
AD
1820}
1821
c32cd48a
AD
1822function isCdmMode() {
1823 return !document.getElementById("headlinesList");
1824}
1825
1826function getSelectedArticleIds2() {
1827 var rows = new Array();
1828 var cdm_mode = isCdmMode();
1829
1830 if (cdm_mode) {
1831 rows = cdmGetSelectedArticles();
1832 } else {
1833 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1834 }
1835
1836 var ids = new Array();
1837
1838 for (var i = 0; i < rows.length; i++) {
1839 var chk = document.getElementById("RCHK-" + rows[i]);
1840 if (chk && chk.checked) {
1841 ids.push(rows[i]);
1842 }
1843 }
1844
1845 return ids;
1846}
c4a36709 1847
f6d40ed2 1848function displayHelpInfobox(topic_id) {
c4a36709 1849
f6d40ed2
AD
1850 var url = "backend.php?op=help&tid=" + param_escape(topic_id);
1851
1852 var w = window.open(url, "ttrss_help",
1853 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
1854
1855 return false;
1856}
e635d56a 1857
477402d8 1858function focus_element(id) {
61992f57 1859 try {
477402d8
AD
1860 var e = document.getElementById(id);
1861 if (e) e.focus();
61992f57 1862 } catch (e) {
438f2ce9 1863 exception_error("focus_element", e);
61992f57 1864 }
477402d8 1865 return false;
61992f57 1866}
e635d56a 1867
99509451 1868function loading_set_progress(p) {
730dbf19 1869 try {
fca95d5f
AD
1870 if (p < last_progress_point || !Element.visible("overlay")) return;
1871
1872 debug("<b>loading_set_progress : " + p + " (" + last_progress_point + ")</b>");
99509451 1873
730dbf19 1874 var o = document.getElementById("l_progress_i");
99509451 1875
673c9946
AD
1876// o.style.width = (p * 2) + "px";
1877
99509451
AD
1878 new Effect.Scale(o, p, {
1879 scaleY : false,
1880 scaleFrom : last_progress_point,
1881 scaleMode: { originalWidth : 200 },
673c9946 1882 queue: { position: 'end', scope: 'LSP-Q', limit: 3 } });
99509451
AD
1883
1884 last_progress_point = p;
730dbf19
AD
1885
1886 } catch (e) {
1887 exception_error("loading_set_progress", e);
1888 }
1889}
08827aaf
AD
1890
1891function remove_splash() {
1892 if (Element.visible("overlay")) {
1893 debug("about to remove splash, OMG!");
1894 Element.hide("overlay");
1895 debug("removed splash!");
1896 }
1897}
071ec48f
AD
1898
1899function addLabelExample() {
1900 try {
1901 var form = document.forms["label_edit_form"];
1902
1903 var text = form.sql_exp;
1904 var op = form.label_fields[form.label_fields.selectedIndex];
1905 var p = form.label_fields_param;
1906
1907 if (op) {
1908 op = op.value;
1909
1910 var tmp = "";
1911
1912 if (text.value != "") {
1913 if (text.value.substring(text.value.length-3, 3).toUpperCase() != "AND") {
1914 tmp = " AND ";
1915 } else {
1916 tmp = " ";
1917 }
1918 }
1919
1920 if (op == "unread") {
1921 tmp = tmp + "unread = true";
1922 }
1923
1924 if (op == "updated") {
1925 tmp = tmp + "last_read is null and unread = false";
1926 }
1927
1928 if (op == "kw_title") {
8c96d4b1
AD
1929 if (p.value == "") {
1930 alert("This action requires a parameter.");
1931 return false;
1932 }
071ec48f
AD
1933 tmp = tmp + "ttrss_entries.title like '%"+p.value+"%'";
1934 }
8c96d4b1 1935
071ec48f 1936 if (op == "kw_content") {
8c96d4b1
AD
1937 if (p.value == "") {
1938 alert("This action requires a parameter.");
1939 return false;
1940 }
1941
071ec48f
AD
1942 tmp = tmp + "ttrss_entries.content like '%"+p.value+"%'";
1943 }
1944
1945 if (op == "scoreE") {
8c96d4b1
AD
1946 if (isNaN(parseInt(p.value))) {
1947 alert("This action expects numeric parameter.");
1948 return false;
1949 }
071ec48f
AD
1950 tmp = tmp + "score = " + p.value;
1951 }
1952
1953 if (op == "scoreG") {
8c96d4b1
AD
1954 if (isNaN(parseInt(p.value))) {
1955 alert("This action expects numeric parameter.");
1956 return false;
1957 }
071ec48f
AD
1958 tmp = tmp + "score > " + p.value;
1959 }
1960
1961 if (op == "scoreL") {
8c96d4b1
AD
1962 if (isNaN(parseInt(p.value))) {
1963 alert("This action expects numeric parameter.");
1964 return false;
1965 }
071ec48f
AD
1966 tmp = tmp + "score < " + p.value;
1967 }
1968
1969 if (op == "newerD") {
e176b01a 1970 if (isNaN(parseInt(p.value))) {
071ec48f
AD
1971 alert("This action expects numeric parameter.");
1972 return false;
1973 }
1974 tmp = tmp + "updated > NOW() - INTERVAL '"+parseInt(p.value)+" days'";
1975 }
1976
1977 if (op == "newerH") {
e176b01a 1978 if (isNaN(parseInt(p.value))) {
071ec48f
AD
1979 alert("This action expects numeric parameter.");
1980 return false;
1981 }
1982
1983 tmp = tmp + "updated > NOW() - INTERVAL '"+parseInt(p.value)+" hours'";
1984 }
1985
1986 text.value = text.value + tmp;
1987
1988 p.value = "";
1989
1990 }
1991
1992 } catch (e) {
1993 exception_error("addLabelExample", e);
1994 }
1995
1996 return false;
1997}
1998
1999function labelFieldsCheck(elem) {
2000 try {
2001 var op = elem[elem.selectedIndex].value;
2002
2003 var p = document.forms["label_edit_form"].label_fields_param;
2004
2005 if (op == "kw_title" || op == "kw_content" || op == "scoreL" ||
2006 op == "scoreG" || op == "scoreE" || op == "newerD" ||
2007 op == "newerH" ) {
2008 Element.show(p);
2009 } else {
2010 Element.hide(p);
2011 }
2012
2013 } catch (e) {
2014 exception_error("labelFieldsCheck", e);
2015
2016 }
2017}
c2913898
AD
2018
2019function getSelectedFeedsFromBrowser() {
2020
2021 var list = document.getElementById("browseFeedList");
2022 if (!list) list = document.getElementById("browseBigFeedList");
2023
2024 var selected = new Array();
2025
2026 for (i = 0; i < list.childNodes.length; i++) {
2027 var child = list.childNodes[i];
2028 if (child.id && child.id.match("FBROW-")) {
2029 var id = child.id.replace("FBROW-", "");
2030
2031 var cb = document.getElementById("FBCHK-" + id);
2032
2033 if (cb.checked) {
2034 selected.push(id);
2035 }
2036 }
2037 }
2038
2039 return selected;
2040}
2041
2bab8824
AD
2042function updateFeedBrowser() {
2043 try {
2044
2045 var query = "backend.php?op=rpc&subop=feedBrowser";
2046
2047 var search = document.getElementById("feed_browser_search");
2048 var limit = document.getElementById("feed_browser_limit");
2049
2050 if (limit) {
2051 query = query + "&limit=" + limit[limit.selectedIndex].value;
2052 }
2053
2054 if (search) {
2055 query = query + "&search=" + param_escape(search.value);
2056 }
2057
2058 notify_progress("Loading, please wait...", true);
2059
2060 new Ajax.Request(query, {
2061 onComplete: function(transport) {
2062 notify('');
2063
2064 var c = document.getElementById("browseFeedList");
2065 var r = transport.responseXML.getElementsByTagName("content")[0];
2066 var nr = transport.responseXML.getElementsByTagName("num-results")[0];
2067 var sb = document.getElementById("feed_browser_subscribe");
2068
2069 if (c && r) {
2070 c.innerHTML = r.firstChild.nodeValue;
2071 }
2072
2073 if (nr && sb) {
2074 if (nr.getAttribute("value") > 0) {
2075 sb.disabled = false;
2076 } else {
2077 sb.disabled = true;
2078 }
2079 }
2080
2081 } });
2082
2083
2084 } catch (e) {
2085 exception_error("updateFeedBrowser", e);
2086 }
2087}
2088
350468d0
AD
2089function browseFeeds(limit) {
2090
2091 try {
2092
2093 var query = "backend.php?op=pref-feeds&subop=browse";
2094
2095 notify_progress("Loading, please wait...", true);
2096
2097 new Ajax.Request(query, {
2098 onComplete: function(transport) {
2099 infobox_callback2(transport);
2100 } });
2101
2102 return false;
2103 } catch (e) {
2104 exception_error("browseFeeds", e);
2105 }
2106}
2107
2bab8824 2108