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