]> git.wh0rd.org - tt-rss.git/blame - functions.js
fix adding feed w/o cat_id (closes #14)
[tt-rss.git] / functions.js
CommitLineData
760966c1
AD
1var hotkeys_enabled = true;
2
7719618b 3function exception_error(location, e) {
83f043bb
AD
4 var msg;
5
6 if (e.fileName) {
7 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
8
9 msg = "Exception: " + e.name + ", " + e.message +
10 "\nFunction: " + location + "()" +
11 "\nLocation: " + base_fname + ":" + e.lineNumber;
12 } else {
13 msg = "Exception: " + e + "\nFunction: " + location + "()";
14 }
15
16 alert(msg);
7719618b
AD
17}
18
760966c1
AD
19function disableHotkeys() {
20 hotkeys_enabled = false;
21}
22
23function enableHotkeys() {
24 hotkeys_enabled = true;
25}
26
c0e5a40e
AD
27function xmlhttp_ready(obj) {
28 return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
29}
30
31
9cfc649a
AD
32function notify_callback() {
33 var container = document.getElementById('notify');
34 if (xmlhttp.readyState == 4) {
35 container.innerHTML=xmlhttp.responseText;
36 }
37}
38
39function rpc_notify_callback() {
40 var container = document.getElementById('notify');
41 if (xmlhttp_rpc.readyState == 4) {
42 container.innerHTML=xmlhttp_rpc.responseText;
43 }
44}
45
1572afe5
AD
46function rpc_pnotify_callback() {
47 var container = parent.document.getElementById('notify');
48 if (xmlhttp_rpc.readyState == 4) {
49 container.innerHTML=xmlhttp_rpc.responseText;
50 }
51}
52
7726fa02
AD
53function param_escape(arg) {
54 if (typeof encodeURIComponent != 'undefined')
55 return encodeURIComponent(arg);
56 else
57 return escape(arg);
58}
59
60function param_unescape(arg) {
61 if (typeof decodeURIComponent != 'undefined')
62 return decodeURIComponent(arg);
63 else
64 return unescape(arg);
65}
66
508a81e1
AD
67function delay(gap) {
68 var then,now;
69 then=new Date().getTime();
70 now=then;
71 while((now-then)<gap) {
72 now=new Date().getTime();
73 }
74}
7726fa02 75
c05608c2
AD
76function p_notify(msg) {
77
78 var n = parent.document.getElementById("notify");
79 var nb = parent.document.getElementById("notify_body");
80
81 if (!n || !nb) return;
82
8dcfffd0
AD
83 if (msg == "") {
84 nb.innerHTML = "&nbsp;";
e93919dd 85// n.style.background = "#ffffff";
c05608c2 86 } else {
8dcfffd0 87 nb.innerHTML = msg;
e93919dd 88// n.style.background = "#fffff0";
8dcfffd0 89 }
c05608c2
AD
90}
91
7726fa02
AD
92function notify(msg) {
93
94 var n = document.getElementById("notify");
c05608c2 95 var nb = document.getElementById("notify_body");
7726fa02 96
c05608c2 97 if (!n || !nb) return;
f0601b87 98
8dcfffd0
AD
99 if (msg == "") {
100 nb.innerHTML = "&nbsp;";
e93919dd 101// n.style.background = "#ffffff";
7726fa02 102 } else {
8dcfffd0 103 nb.innerHTML = msg;
e93919dd 104// n.style.background = "#fffff0";
8dcfffd0 105 }
7726fa02
AD
106
107}
108
508a81e1 109function printLockingError() {
f0601b87 110 notify("Please wait until operation finishes");}
508a81e1 111
13ad9102
AD
112var seq = "";
113
114function hotkey_handler(e) {
760966c1 115
13ad9102
AD
116 var keycode;
117
760966c1
AD
118 if (!hotkeys_enabled) return;
119
13ad9102
AD
120 if (window.event) {
121 keycode = window.event.keyCode;
122 } else if (e) {
123 keycode = e.which;
124 }
125
126 if (keycode == 13 || keycode == 27) {
127 seq = "";
128 } else {
129 seq = seq + "" + keycode;
130 }
131
132 var piggie = document.getElementById("piggie");
133
bb7cface 134 if (piggie) {
13ad9102 135
bb7cface
AD
136 if (seq.match("807371717369")) {
137 localPiggieFunction(true);
138 } else {
139 localPiggieFunction(false);
140 }
141 }
142
9cfc649a 143 if (typeof localHotkeyHandler != 'undefined') {
7b433d8c
AD
144 try {
145 localHotkeyHandler(keycode);
146 } catch (e) {
83f043bb 147 exception_error("hotkey_handler", e);
7b433d8c 148 }
9cfc649a
AD
149 }
150
13ad9102
AD
151}
152
e828e31e 153function cleanSelectedList(element) {
f0601b87
AD
154 var content = document.getElementById(element);
155
703b632e
AD
156 if (!document.getElementById("feedCatHolder")) {
157 for (i = 0; i < content.childNodes.length; i++) {
158 var child = content.childNodes[i];
159 child.className = child.className.replace("Selected", "");
160 }
161 } else {
162 for (i = 0; i < content.childNodes.length; i++) {
163 var child = content.childNodes[i];
164
165 if (child.id == "feedCatHolder") {
c3f348c2 166 var fcat = child.lastChild;
703b632e
AD
167 for (j = 0; j < fcat.childNodes.length; j++) {
168 var feed = fcat.childNodes[j];
169 feed.className = feed.className.replace("Selected", "");
170 }
171 }
172 }
e828e31e 173
703b632e 174 }
e828e31e
AD
175}
176
177
178function cleanSelected(element) {
179 var content = document.getElementById(element);
f0601b87
AD
180
181 for (i = 0; i < content.rows.length; i++) {
182 content.rows[i].className = content.rows[i].className.replace("Selected", "");
183 }
184
185}
186
187function getVisibleUnreadHeadlines() {
188 var content = document.getElementById("headlinesList");
189
190 var rows = new Array();
191
192 for (i = 0; i < content.rows.length; i++) {
193 var row_id = content.rows[i].id.replace("RROW-", "");
194 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
195 rows.push(row_id);
196 }
197 }
198 return rows;
199}
200
201function getVisibleHeadlineIds() {
202
203 var content = document.getElementById("headlinesList");
204
205 var rows = new Array();
206
207 for (i = 0; i < content.rows.length; i++) {
208 var row_id = content.rows[i].id.replace("RROW-", "");
209 if (row_id.length > 0) {
210 rows.push(row_id);
211 }
212 }
213 return rows;
214}
215
216function getFirstVisibleHeadlineId() {
217 var rows = getVisibleHeadlineIds();
218 return rows[0];
219}
220
221function getLastVisibleHeadlineId() {
222 var rows = getVisibleHeadlineIds();
223 return rows[rows.length-1];
224}
225
226function markHeadline(id) {
227 var row = document.getElementById("RROW-" + id);
228 if (row) {
35f3c923
AD
229 var is_active = false;
230
231 if (row.className.match("Active")) {
232 is_active = true;
233 }
234 row.className = row.className.replace("Selected", "");
235 row.className = row.className.replace("Active", "");
236 row.className = row.className.replace("Insensitive", "");
237
238 if (is_active) {
239 row.className = row.className = "Active";
240 }
241
242 row.className = row.className + "Selected";
243
f0601b87
AD
244 }
245}
246
247function getFeedIds() {
248 var content = document.getElementById("feedsList");
249
250 var rows = new Array();
251
252 for (i = 0; i < content.rows.length; i++) {
253 var id = content.rows[i].id.replace("FEEDR-", "");
254 if (id.length > 0) {
255 rows.push(id);
256 }
257 }
258
259 return rows;
260}
13ad9102 261
ac43eba1
AD
262function setCookie(name, value, expires, path, domain, secure) {
263 document.cookie= name + "=" + escape(value) +
264 ((expires) ? "; expires=" + expires.toGMTString() : "") +
265 ((path) ? "; path=" + path : "") +
266 ((domain) ? "; domain=" + domain : "") +
267 ((secure) ? "; secure" : "");
268}
269
270function getCookie(name) {
271
272 var dc = document.cookie;
273 var prefix = name + "=";
274 var begin = dc.indexOf("; " + prefix);
275 if (begin == -1) {
276 begin = dc.indexOf(prefix);
277 if (begin != 0) return null;
278 }
279 else {
280 begin += 2;
281 }
282 var end = document.cookie.indexOf(";", begin);
283 if (end == -1) {
284 end = dc.length;
285 }
286 return unescape(dc.substring(begin + prefix.length, end));
287}
288
1a66d16e
AD
289function disableContainerChildren(id, disable, doc) {
290
291 if (!doc) doc = document;
292
293 var container = doc.getElementById(id);
ac43eba1
AD
294
295 for (var i = 0; i < container.childNodes.length; i++) {
296 var child = container.childNodes[i];
297
d5224f0d
AD
298 try {
299 child.disabled = disable;
300 } catch (E) {
301
302 }
ac43eba1
AD
303
304 if (disable) {
305 if (child.className && child.className.match("button")) {
306 child.className = "disabledButton";
307 }
308 } else {
309 if (child.className && child.className.match("disabledButton")) {
310 child.className = "button";
311 }
d5224f0d 312 }
ac43eba1
AD
313 }
314
315}
7726fa02 316
e828e31e
AD
317function gotoPreferences() {
318 document.location.href = "prefs.php";
319}
320
321function gotoMain() {
322 document.location.href = "tt-rss.php";
323}
324
8158c57a
AD
325function gotoExportOpml() {
326 document.location.href = "opml.php?op=Export";
327}
86741347
AD
328
329function getActiveFeedId() {
330 return getCookie("ttrss_vf_actfeed");
331}
332
333function setActiveFeedId(id) {
334 return setCookie("ttrss_vf_actfeed", id);
335}
090e250b
AD
336
337var xmlhttp_rpc = false;
338
339/*@cc_on @*/
340/*@if (@_jscript_version >= 5)
341// JScript gives us Conditional compilation, we can cope with old IE versions.
342// and security blocked creation of the objects.
343try {
344 xmlhttp_rpc = new ActiveXObject("Msxml2.XMLHTTP");
345} catch (e) {
346 try {
347 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
348 } catch (E) {
349 xmlhttp_rpc = false;
350 }
351}
352@end @*/
353
354if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
355 xmlhttp_rpc = new XMLHttpRequest();
356}
357
3bdb368b 358function parse_counters(reply, f_document, title_obj) {
1a6a9555
AD
359 try {
360 for (var l = 0; l < reply.childNodes.length; l++) {
6043fb7e
AD
361 if (!reply.childNodes[l] || !reply.childNodes[l].getAttribute) {
362 // where did this come from?
363 continue;
364 }
365
1a6a9555
AD
366 var id = reply.childNodes[l].getAttribute("id");
367 var t = reply.childNodes[l].getAttribute("type");
368 var ctr = reply.childNodes[l].getAttribute("counter");
023fe037
AD
369 var error = reply.childNodes[l].getAttribute("error");
370 var has_img = reply.childNodes[l].getAttribute("hi");
1a6a9555
AD
371
372 if (id == "global-unread") {
3bdb368b
AD
373 title_obj.global_unread = ctr;
374 title_obj.updateTitle();
1a6a9555
AD
375 continue;
376 }
377
378 if (t == "category") {
379 var catctr = f_document.getElementById("FCATCTR-" + id);
380 if (catctr) {
381 catctr.innerHTML = "(" + ctr + " unread)";
382 }
383 continue;
384 }
385
386 var feedctr = f_document.getElementById("FEEDCTR-" + id);
387 var feedu = f_document.getElementById("FEEDU-" + id);
388 var feedr = f_document.getElementById("FEEDR-" + id);
023fe037
AD
389 var feed_img = f_document.getElementById("FIMG-" + id);
390
1a6a9555
AD
391 if (feedctr && feedu && feedr) {
392
393 feedu.innerHTML = ctr;
023fe037 394
426d3c57
AD
395 if (error) {
396 feedr.className = feedr.className.replace("feed", "error");
397 } else if (id > 0) {
398 feedr.className = feedr.className.replace("error", "feed");
023fe037 399 }
1a6a9555
AD
400
401 if (ctr > 0) {
402 feedctr.className = "odd";
403 if (!feedr.className.match("Unread")) {
404 var is_selected = feedr.className.match("Selected");
405
406 feedr.className = feedr.className.replace("Selected", "");
407 feedr.className = feedr.className.replace("Unread", "");
408
409 feedr.className = feedr.className + "Unread";
410
411 if (is_selected) {
412 feedr.className = feedr.className + "Selected";
413 }
414
415 }
416 } else {
417 feedctr.className = "invisible";
418 feedr.className = feedr.className.replace("Unread", "");
419 }
420 }
421 }
422 } catch (e) {
83f043bb 423 exception_error("parse_counters", e);
1a6a9555
AD
424 }
425}
426
3bdb368b
AD
427// this one is called from feedlist context
428// thus title_obj passed to parse_counters is parent (e.g. main ttrss window)
429
4f3a84f4 430function all_counters_callback() {
090e250b 431 if (xmlhttp_rpc.readyState == 4) {
7719618b 432 try {
3866b046 433 if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
7719618b
AD
434 notify("[all_counters_callback] backend did not return valid XML");
435 return;
436 }
437
438 var reply = xmlhttp_rpc.responseXML.firstChild;
7719618b 439 var f_document = parent.frames["feeds-frame"].document;
280ee9a3 440
3bdb368b 441 parse_counters(reply, f_document, parent);
7719618b 442
7719618b
AD
443 } catch (e) {
444 exception_error("all_counters_callback", e);
090e250b
AD
445 }
446 }
447}
448
4f3a84f4 449function update_all_counters(feed) {
090e250b 450 if (xmlhttp_ready(xmlhttp_rpc)) {
8143ae1f 451 var query = "backend.php?op=rpc&subop=getAllCounters";
e47cfe37
AD
452
453 if (feed > 0) {
454 query = query + "&aid=" + feed;
455 }
456
090e250b 457 xmlhttp_rpc.open("GET", query, true);
4f3a84f4 458 xmlhttp_rpc.onreadystatechange=all_counters_callback;
090e250b
AD
459 xmlhttp_rpc.send(null);
460 }
461}
7dc66a61
AD
462
463function popupHelp(tid) {
464 var w = window.open("backend.php?op=help&tid=" + tid,
465 "Popup Help",
466 "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no");
467}
b6644d29
AD
468
469/** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
470 * * @author Sundar Dorai-Raj
471 * * Email: sdoraira@vt.edu
472 * * This program is free software; you can redistribute it and/or
473 * * modify it under the terms of the GNU General Public License
474 * * as published by the Free Software Foundation; either version 2
475 * * of the License, or (at your option) any later version,
476 * * provided that any use properly credits the author.
477 * * This program is distributed in the hope that it will be useful,
478 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
479 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
480 * * GNU General Public License for more details at http://www.gnu.org * * */
481
482 var numbers=".0123456789";
483 function isNumeric(x) {
484 // is x a String or a character?
485 if(x.length>1) {
486 // remove negative sign
487 x=Math.abs(x)+"";
488 for(j=0;j<x.length;j++) {
489 // call isNumeric recursively for each character
490 number=isNumeric(x.substring(j,j+1));
491 if(!number) return number;
492 }
493 return number;
494 }
495 else {
496 // if x is number return true
497 if(numbers.indexOf(x)>=0) return true;
498 return false;
499 }
500 }
501
3745788e
AD
502
503function hideOrShowFeeds(doc, hide) {
504
3ed751d4
AD
505 if (!doc.styleSheets) return;
506
3745788e
AD
507 var css_rules = doc.styleSheets[0].cssRules;
508
bbf4d0b2
AD
509 if (!css_rules || !css_rules.length) return;
510
3745788e
AD
511 for (i = 0; i < css_rules.length; i++) {
512 var rule = css_rules[i];
513
514 if (rule.selectorText == "ul.feedList li.feed") {
515 if (!hide) {
516 rule.style.display = "block";
517 } else {
518 rule.style.display = "none";
519 }
520 }
521
522 }
523
524}
525
295f9b42
AD
526function fatalError(code) {
527 window.location = "error.php?c=" + param_escape(code);
35f3c923
AD
528}
529
530function selectTableRow(r, do_select) {
531 r.className = r.className.replace("Selected", "");
532
533 if (do_select) {
534 r.className = r.className + "Selected";
535 }
536}
537
1572afe5
AD
538function selectTableRowsByIdPrefix(content_id, prefix, check_prefix, do_select,
539 classcheck) {
3745788e 540
35f3c923
AD
541 var content = document.getElementById(content_id);
542
543 if (!content) {
544 alert("[selectTableRows] Element " + content_id + " not found.");
545 return;
546 }
547
548 for (i = 0; i < content.rows.length; i++) {
1572afe5
AD
549 if (!classcheck || content.rows[i].className.match(classcheck)) {
550
551 if (content.rows[i].id.match(prefix)) {
552 selectTableRow(content.rows[i], do_select);
553 }
3055bc41 554
1572afe5
AD
555 var row_id = content.rows[i].id.replace(prefix, "");
556 var check = document.getElementById(check_prefix + row_id);
3055bc41 557
1572afe5
AD
558 if (check) {
559 check.checked = do_select;
560 }
3055bc41 561 }
35f3c923 562 }
295f9b42 563}
91ff844a
AD
564
565function getSelectedTableRowIds(content_id, prefix) {
566
567 var content = document.getElementById(content_id);
568
569 if (!content) {
570 alert("[getSelectedTableRowIds] Element " + content_id + " not found.");
571 return;
572 }
573
574 var sel_rows = new Array();
575
576 for (i = 0; i < content.rows.length; i++) {
1572afe5
AD
577 if (content.rows[i].id.match(prefix) &&
578 content.rows[i].className.match("Selected")) {
579
91ff844a
AD
580 var row_id = content.rows[i].id.replace(prefix + "-", "");
581 sel_rows.push(row_id);
582 }
583 }
584
585 return sel_rows;
586
587}
588
386cbf27
AD
589function toggleSelectRowById(sender, id) {
590 var row = document.getElementById(id);
591
592 if (sender.checked) {
593 if (!row.className.match("Selected")) {
594 row.className = row.className + "Selected";
595 }
596 } else {
597 if (row.className.match("Selected")) {
598 row.className = row.className.replace("Selected", "");
599 }
600 }
601}
602
603
1572afe5
AD
604function toggleSelectRow(sender) {
605 var parent_row = sender.parentNode.parentNode;
606
607 if (sender.checked) {
608 if (!parent_row.className.match("Selected")) {
609 parent_row.className = parent_row.className + "Selected";
610 }
611 } else {
612 if (parent_row.className.match("Selected")) {
613 parent_row.className = parent_row.className.replace("Selected", "");
614 }
615 }
616}
617
3866b046
AD
618function openExternalUrl(url) {
619 var w = window.open(url);
620}
1572afe5 621
7b433d8c
AD
622
623function getRelativeFeedId(list, id, direction) {
624 if (!id) {
625 if (direction == "next") {
626 for (i = 0; i < list.childNodes.length; i++) {
627 var child = list.childNodes[i];
628 if (child.id == "feedCatHolder") {
c3f348c2 629 if (child.lastChild) {
7b433d8c
AD
630 var cr = getRelativeFeedId(child.firstChild, id, direction);
631 if (cr) return cr;
632 }
633 } else if (child.id.match("FEEDR-")) {
634 return child.id.replace('FEEDR-', '');
635 }
636 }
637 }
638
639 // FIXME select last feed doesn't work when only unread feeds are visible
640
641 if (direction == "prev") {
642 for (i = list.childNodes.length-1; i >= 0; i--) {
643 var child = list.childNodes[i];
644 if (child.id == "feedCatHolder") {
645 if (child.firstChild) {
646 var cr = getRelativeFeedId(child.firstChild, id, direction);
647 if (cr) return cr;
648 }
649 } else if (child.id.match("FEEDR-")) {
650
651 if (getCookie("ttrss_vf_hreadf") == 1) {
652 if (child.className != "feed") {
653 alert(child.className);
654 return child.id.replace('FEEDR-', '');
655 }
656 } else {
657 return child.id.replace('FEEDR-', '');
658 }
659 }
660 }
661 }
662 } else {
663
664 var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
665
666 if (direction == "next") {
667
668 if (feed.nextSibling) {
669
670 var next_feed = feed.nextSibling;
671
672 while (!next_feed.id && next_feed.nextSibling) {
673 next_feed = next_feed.nextSibling;
674 }
675
676 if (getCookie("ttrss_vf_hreadf") == 1) {
677 while (next_feed && next_feed.className == "feed") {
678 next_feed = next_feed.nextSibling;
679 }
680 }
681
682 if (next_feed && next_feed.id.match("FEEDR-")) {
683 return next_feed.id.replace("FEEDR-", "");
684 }
685 }
686
687 var this_cat = feed.parentNode.parentNode;
688
689 if (this_cat && this_cat.nextSibling) {
690 while (this_cat = this_cat.nextSibling) {
691 if (this_cat.firstChild && this_cat.firstChild.firstChild) {
692 var next_feed = this_cat.firstChild.firstChild;
693 if (getCookie("ttrss_vf_hreadf") == 1) {
694 while (next_feed && next_feed.className == "feed") {
695 next_feed = next_feed.nextSibling;
696 }
697 }
698 if (next_feed && next_feed.id.match("FEEDR-")) {
699 return next_feed.id.replace("FEEDR-", "");
700 }
701 }
702 }
703 }
704 } else if (direction == "prev") {
705
706 if (feed.previousSibling) {
707
708 var prev_feed = feed.previousSibling;
709
710 if (getCookie("ttrss_vf_hreadf") == 1) {
711 while (prev_feed && prev_feed.className == "feed") {
712 prev_feed = prev_feed.previousSibling;
713 }
714 }
715
716 while (!prev_feed.id && prev_feed.previousSibling) {
717 prev_feed = prev_feed.previousSibling;
718 }
719
720 if (prev_feed && prev_feed.id.match("FEEDR-")) {
721 return prev_feed.id.replace("FEEDR-", "");
722 }
723 }
724
725 var this_cat = feed.parentNode.parentNode;
726
727 if (this_cat && this_cat.previousSibling) {
728 while (this_cat = this_cat.previousSibling) {
729 if (this_cat.lastChild && this_cat.firstChild.lastChild) {
730 var prev_feed = this_cat.firstChild.lastChild;
731 if (getCookie("ttrss_vf_hreadf") == 1) {
732 while (prev_feed && prev_feed.className == "feed") {
733 prev_feed = prev_feed.previousSibling;
734 }
735 }
736 if (prev_feed && prev_feed.id.match("FEEDR-")) {
737 return prev_feed.id.replace("FEEDR-", "");
738 }
739 }
740 }
741 }
742 }
743 }
744}
36aab70f
AD
745
746function showBlockElement(id) {
747 var elem = document.getElementById(id);
748
749 if (elem) {
750 elem.style.display = "block";
751 } else {
752 alert("[showBlockElement] can't find element with id " + id);
753 }
754}
755
a9b0bfd5
AD
756function hideParentElement(e) {
757 e.parentNode.style.display = "none";
758}
1b0809ae
AD
759
760function dropboxSelect(e, v) {
761 for (i = 0; i < e.length; i++) {
762 if (e[i].value == v) {
763 e.selectedIndex = i;
764 break;
765 }
766 }
767}