]> git.wh0rd.org - tt-rss.git/blob - functions.js
remove modalbox; use dijit.Dialog; further dojo-related updates
[tt-rss.git] / functions.js
1 var hotkeys_enabled = true;
2 var notify_silent = false;
3 var last_progress_point = 0;
4 var sanity_check_done = false;
5 var dialogs = [];
6
7 /* add method to remove element from array */
8
9 Array.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
15 /* create console.log if it doesn't exist */
16
17 if (!window.console) console = {};
18 console.log = console.log || function(msg) { };
19 console.warn = console.warn || function(msg) { };
20 console.error = console.error || function(msg) { };
21
22 function exception_error(location, e, ext_info) {
23 var msg = format_exception_error(location, e);
24
25 if (!ext_info) ext_info = false;
26
27 try {
28
29 if (ext_info) {
30 if (ext_info.responseText) {
31 ext_info = ext_info.responseText;
32 }
33 }
34
35 var content = "<div class=\"fatalError\">" +
36 "<pre>" + msg + "</pre>";
37
38 if (ext_info) {
39 content += "<div><b>Additional information:</b></div>" +
40 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
41 }
42
43 content += "<div><b>Stack trace:</b></div>" +
44 "<textarea readonly=\"1\">" + e.stack + "</textarea>";
45
46 // content += "<div style='text-align : center'>" +
47 // "<button onclick=\"closeInfoBox()\">" +
48 // "Close this window" + "</button></div>";
49
50 content += "</div>";
51
52 // TODO: add code to automatically report errors to tt-rss.org
53
54 var dialog = new dijit.Dialog({
55 title: "Unhandled exception",
56 style: "width: 600px",
57 content: content});
58
59 dialog.show();
60
61 } catch (e) {
62 alert(msg);
63 }
64
65 }
66
67 function format_exception_error(location, e) {
68 var msg;
69
70 if (e.fileName) {
71 var base_fname = e.fileName.substring(e.fileName.lastIndexOf("/") + 1);
72
73 msg = "Exception: " + e.name + ", " + e.message +
74 "\nFunction: " + location + "()" +
75 "\nLocation: " + base_fname + ":" + e.lineNumber;
76
77 } else if (e.description) {
78 msg = "Exception: " + e.description + "\nFunction: " + location + "()";
79 } else {
80 msg = "Exception: " + e + "\nFunction: " + location + "()";
81 }
82
83 console.error("EXCEPTION: " + msg);
84
85 return msg;
86 }
87
88
89 function disableHotkeys() {
90 hotkeys_enabled = false;
91 }
92
93 function enableHotkeys() {
94 hotkeys_enabled = true;
95 }
96
97 function param_escape(arg) {
98 if (typeof encodeURIComponent != 'undefined')
99 return encodeURIComponent(arg);
100 else
101 return escape(arg);
102 }
103
104 function param_unescape(arg) {
105 if (typeof decodeURIComponent != 'undefined')
106 return decodeURIComponent(arg);
107 else
108 return unescape(arg);
109 }
110
111 var notify_hide_timerid = false;
112
113 function hide_notify() {
114 var n = $("notify");
115 if (n) {
116 n.style.display = "none";
117 }
118 }
119
120 function notify_silent_next() {
121 notify_silent = true;
122 }
123
124 function notify_real(msg, no_hide, n_type) {
125
126 if (notify_silent) {
127 notify_silent = false;
128 return;
129 }
130
131 var n = $("notify");
132 var nb = $("notify_body");
133
134 if (!n || !nb) return;
135
136 if (notify_hide_timerid) {
137 window.clearTimeout(notify_hide_timerid);
138 }
139
140 if (msg == "") {
141 if (n.style.display == "block") {
142 notify_hide_timerid = window.setTimeout("hide_notify()", 0);
143 }
144 return;
145 } else {
146 n.style.display = "block";
147 }
148
149 /* types:
150
151 1 - generic
152 2 - progress
153 3 - error
154 4 - info
155
156 */
157
158 if (typeof __ != 'undefined') {
159 msg = __(msg);
160 }
161
162 if (n_type == 1) {
163 n.className = "notify";
164 } else if (n_type == 2) {
165 n.className = "notifyProgress";
166 msg = "<img src='"+getInitParam("sign_progress")+"'> " + msg;
167 } else if (n_type == 3) {
168 n.className = "notifyError";
169 msg = "<img src='"+getInitParam("sign_excl")+"'> " + msg;
170 } else if (n_type == 4) {
171 n.className = "notifyInfo";
172 msg = "<img src='"+getInitParam("sign_info")+"'> " + msg;
173 }
174
175 // msg = "<img src='images/live_com_loading.gif'> " + msg;
176
177 nb.innerHTML = msg;
178
179 if (!no_hide) {
180 notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
181 }
182 }
183
184 function notify(msg, no_hide) {
185 notify_real(msg, no_hide, 1);
186 }
187
188 function notify_progress(msg, no_hide) {
189 notify_real(msg, no_hide, 2);
190 }
191
192 function notify_error(msg, no_hide) {
193 notify_real(msg, no_hide, 3);
194
195 }
196
197 function notify_info(msg, no_hide) {
198 notify_real(msg, no_hide, 4);
199 }
200
201 function setCookie(name, value, lifetime, path, domain, secure) {
202
203 var d = false;
204
205 if (lifetime) {
206 d = new Date();
207 d.setTime(d.getTime() + (lifetime * 1000));
208 }
209
210 console.log("setCookie: " + name + " => " + value + ": " + d);
211
212 int_setCookie(name, value, d, path, domain, secure);
213
214 }
215
216 function int_setCookie(name, value, expires, path, domain, secure) {
217 document.cookie= name + "=" + escape(value) +
218 ((expires) ? "; expires=" + expires.toGMTString() : "") +
219 ((path) ? "; path=" + path : "") +
220 ((domain) ? "; domain=" + domain : "") +
221 ((secure) ? "; secure" : "");
222 }
223
224 function delCookie(name, path, domain) {
225 if (getCookie(name)) {
226 document.cookie = name + "=" +
227 ((path) ? ";path=" + path : "") +
228 ((domain) ? ";domain=" + domain : "" ) +
229 ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
230 }
231 }
232
233
234 function getCookie(name) {
235
236 var dc = document.cookie;
237 var prefix = name + "=";
238 var begin = dc.indexOf("; " + prefix);
239 if (begin == -1) {
240 begin = dc.indexOf(prefix);
241 if (begin != 0) return null;
242 }
243 else {
244 begin += 2;
245 }
246 var end = document.cookie.indexOf(";", begin);
247 if (end == -1) {
248 end = dc.length;
249 }
250 return unescape(dc.substring(begin + prefix.length, end));
251 }
252
253 function gotoPreferences() {
254 document.location.href = "prefs.php";
255 }
256
257 function gotoMain() {
258 document.location.href = "tt-rss.php";
259 }
260
261 function gotoExportOpml() {
262 document.location.href = "opml.php?op=Export";
263 }
264
265
266 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
267 * * @author Sundar Dorai-Raj
268 * * Email: sdoraira@vt.edu
269 * * This program is free software; you can redistribute it and/or
270 * * modify it under the terms of the GNU General Public License
271 * * as published by the Free Software Foundation; either version 2
272 * * of the License, or (at your option) any later version,
273 * * provided that any use properly credits the author.
274 * * This program is distributed in the hope that it will be useful,
275 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
276 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
277 * * GNU General Public License for more details at http://www.gnu.org * * */
278
279 var numbers=".0123456789";
280 function isNumeric(x) {
281 // is x a String or a character?
282 if(x.length>1) {
283 // remove negative sign
284 x=Math.abs(x)+"";
285 for(j=0;j<x.length;j++) {
286 // call isNumeric recursively for each character
287 number=isNumeric(x.substring(j,j+1));
288 if(!number) return number;
289 }
290 return number;
291 }
292 else {
293 // if x is number return true
294 if(numbers.indexOf(x)>=0) return true;
295 return false;
296 }
297 }
298
299
300 function toggleSelectRowById(sender, id) {
301 var row = $(id);
302 return toggleSelectRow(sender, row);
303 }
304
305 function toggleSelectListRow(sender) {
306 var row = sender.parentNode;
307 return toggleSelectRow(sender, row);
308 }
309
310 function tSR(sender, row) {
311 return toggleSelectRow(sender, row);
312 }
313
314 function toggleSelectRow(sender, row) {
315
316 if (!row) row = sender.parentNode.parentNode;
317
318 if (sender.checked && !row.hasClassName('Selected'))
319 row.addClassName('Selected');
320 else
321 row.removeClassName('Selected');
322 }
323
324 function checkboxToggleElement(elem, id) {
325 if (elem.checked) {
326 Effect.Appear(id, {duration : 0.5});
327 } else {
328 Effect.Fade(id, {duration : 0.5});
329 }
330 }
331
332 function dropboxSelect(e, v) {
333 for (i = 0; i < e.length; i++) {
334 if (e[i].value == v) {
335 e.selectedIndex = i;
336 break;
337 }
338 }
339 }
340
341 // originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
342 // bugfixed just a little bit :-)
343 function getURLParam(strParamName){
344 var strReturn = "";
345 var strHref = window.location.href;
346
347 if (strHref.indexOf("#") == strHref.length-1) {
348 strHref = strHref.substring(0, strHref.length-1);
349 }
350
351 if ( strHref.indexOf("?") > -1 ){
352 var strQueryString = strHref.substr(strHref.indexOf("?"));
353 var aQueryString = strQueryString.split("&");
354 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
355 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
356 var aParam = aQueryString[iParam].split("=");
357 strReturn = aParam[1];
358 break;
359 }
360 }
361 }
362 return strReturn;
363 }
364
365 function leading_zero(p) {
366 var s = String(p);
367 if (s.length == 1) s = "0" + s;
368 return s;
369 }
370
371 function make_timestamp() {
372 var d = new Date();
373
374 return leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
375 ":" + leading_zero(d.getSeconds());
376 }
377
378
379 function closeErrorBox() {
380
381 if (Element.visible("errorBoxShadow")) {
382 Element.hide("dialog_overlay");
383 Element.hide("errorBoxShadow");
384
385 enableHotkeys();
386 }
387
388 return false;
389 }
390
391 function closeInfoBox(cleanup) {
392 try {
393 enableHotkeys();
394
395 var dialog = dialogs.pop();
396
397 if (dialog)
398 dialog.hide();
399
400 } catch (e) {
401 //exception_error("closeInfoBox", e);
402 }
403 return false;
404 }
405
406
407 function displayDlg(id, param, callback) {
408
409 notify_progress("Loading, please wait...", true);
410
411 var query = "?op=dlg&id=" +
412 param_escape(id) + "&param=" + param_escape(param);
413
414 new Ajax.Request("backend.php", {
415 parameters: query,
416 onComplete: function (transport) {
417 infobox_callback2(transport);
418 if (callback) callback(transport);
419 } });
420
421 return false;
422 }
423
424 function infobox_submit_callback2(transport) {
425 closeInfoBox();
426
427 try {
428 // called from prefs, reload tab
429 if (typeof active_tab != 'undefined' && active_tab) {
430 selectTab(active_tab, false);
431 }
432 } catch (e) { }
433
434 if (transport.responseText) {
435 notify_info(transport.responseText);
436 }
437 }
438
439 function infobox_callback2(transport) {
440 try {
441
442 //console.log("infobox_callback2");
443 notify('');
444
445 var content;
446 var dtitle = "Dialog";
447
448 if (transport.responseXML) {
449 var dlg = transport.responseXML.getElementsByTagName("dlg")[0];
450
451 var title = transport.responseXML.getElementsByTagName("title")[0];
452 if (title)
453 title = title.firstChild.nodeValue;
454
455 var content = transport.responseXML.getElementsByTagName("content")[0];
456
457 content = content.firstChild.nodeValue;
458
459 } else {
460 content = transport.responseText;
461 }
462
463 var dialog = new dijit.Dialog({
464 title: title,
465 style: "width: 600px",
466 onCancel: function() {
467 dialogs.remove(this);
468 return true;
469 },
470 onExecute: function() {
471 dialogs.remove(this);
472 return true;
473 },
474 onClose: function() {
475 dialogs.remove(this);
476 return true;
477 },
478 content: content});
479
480 dialog.show();
481
482 dialogs.push(dialog);
483
484 notify("");
485 } catch (e) {
486 exception_error("infobox_callback2", e);
487 }
488 }
489
490 function createFilter() {
491
492 try {
493
494 var form = document.forms['filter_add_form'];
495 var reg_exp = form.reg_exp.value;
496
497 if (reg_exp == "") {
498 alert(__("Can't add filter: nothing to match on."));
499 return false;
500 }
501
502 var query = "?op=rpc&subop=verifyRegexp&reg_exp=" + param_escape(reg_exp);
503
504 notify_progress("Verifying regular expression...");
505
506 new Ajax.Request("backend.php", {
507 parameters: query,
508 onComplete: function(transport) {
509 handle_rpc_reply(transport);
510
511 var response = transport.responseXML;
512
513 if (response) {
514 var s = response.getElementsByTagName("status")[0].firstChild.nodeValue;
515
516 notify('');
517
518 if (s == "INVALID") {
519 alert("Match regular expression seems to be invalid.");
520 return;
521 } else {
522
523 var query = Form.serialize("filter_add_form");
524
525 // we can be called from some other tab in Prefs
526 if (typeof active_tab != 'undefined' && active_tab) {
527 active_tab = "filterConfig";
528 }
529
530 new Ajax.Request("backend.php?" + query, {
531 onComplete: function (transport) {
532 infobox_submit_callback2(transport);
533 } });
534
535 return true;
536 }
537 }
538
539 } });
540
541 } catch (e) {
542 exception_error("createFilter", e);
543 }
544 }
545
546 function isValidURL(s) {
547 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
548 }
549
550 function subscribeToFeed() {
551
552 try {
553
554 var form = document.forms['feed_add_form'];
555 var feed_url = form.feed_url.value;
556
557 if (feed_url == "") {
558 alert(__("Can't subscribe: no feed URL given."));
559 return false;
560 }
561
562 notify_progress(__("Subscribing to feed..."), true);
563
564 var query = Form.serialize("feed_add_form");
565
566 console.log("subscribe q: " + query);
567
568 Form.disable("feed_add_form");
569
570 new Ajax.Request("backend.php", {
571 parameters: query,
572 onComplete: function(transport) {
573 //dlg_frefresh_callback(transport);
574
575 try {
576
577 if (!transport.responseXML) {
578 console.log(transport.responseText);
579 alert(__("Server error while trying to subscribe to specified feed."));
580 return;
581 }
582
583 var result = transport.responseXML.getElementsByTagName('result')[0];
584 var rc = parseInt(result.getAttribute('code'));
585
586 Form.enable("feed_add_form");
587
588 notify('');
589
590 switch (rc) {
591 case 1:
592 closeInfoBox();
593 notify_info(__("Subscribed to %s").replace("%s", feed_url));
594
595 if (inPreferences()) {
596 updateFeedList();
597 } else {
598 setTimeout('updateFeedList(false, false)', 50);
599 }
600 break;
601 case 2:
602 alert(__("Specified URL seems to be invalid."));
603 break;
604 case 3:
605 alert(__("Specified URL doesn't seem to contain any feeds."));
606 break;
607 case 4:
608 new Ajax.Request("backend.php", {
609 parameters: 'op=rpc&subop=extractfeedurls&url=' + encodeURIComponent(feed_url),
610 onComplete: function(transport) {
611 var result = transport.responseXML.getElementsByTagName('urls')[0];
612 var feeds = JSON.parse(result.firstChild.nodeValue);
613 var select = document.getElementById("faad_feeds_container_select");
614
615 while (select.hasChildNodes()) {
616 select.removeChild(elem.firstChild);
617 }
618 var count = 0;
619 for (var feedUrl in feeds) {
620 select.insert(new Option(feeds[feedUrl], feedUrl, false));
621 count++;
622 }
623 if (count > 5) count = 5;
624 select.size = count;
625
626 Effect.Appear('fadd_feeds_container', {duration : 0.5});
627 }
628 });
629 break;
630 case 5:
631 alert(__("Couldn't download the specified URL."));
632 break;
633 case 0:
634 alert(__("You are already subscribed to this feed."));
635 break;
636 }
637
638 } catch (e) {
639 exception_error("subscribeToFeed", e);
640 }
641
642 } });
643
644 } catch (e) {
645 exception_error("subscribeToFeed", e);
646 }
647
648 return false;
649 }
650
651 function filterCR(e, f)
652 {
653 var key;
654
655 if(window.event)
656 key = window.event.keyCode; //IE
657 else
658 key = e.which; //firefox
659
660 if (key == 13) {
661 if (typeof f != 'undefined') {
662 f();
663 return false;
664 } else {
665 return false;
666 }
667 } else {
668 return true;
669 }
670 }
671
672 function getInitParam(key) {
673 return init_params[key];
674 }
675
676 function setInitParam(key, value) {
677 init_params[key] = value;
678 }
679
680 function fatalError(code, msg, ext_info) {
681 try {
682
683 if (!ext_info) ext_info = "N/A";
684
685 if (code == 6) {
686 window.location.href = "tt-rss.php";
687 } else if (code == 5) {
688 window.location.href = "db-updater.php";
689 } else {
690
691 if (msg == "") msg = "Unknown error";
692
693 var ebc = $("xebContent");
694
695 if (ebc) {
696
697 Element.show("dialog_overlay");
698 Element.show("errorBoxShadow");
699 Element.hide("xebBtn");
700
701 if (ext_info) {
702 if (ext_info.responseText) {
703 ext_info = ext_info.responseText;
704 }
705 }
706
707 ebc.innerHTML =
708 "<div><b>Error message:</b></div>" +
709 "<pre>" + msg + "</pre>" +
710 "<div><b>Additional information:</b></div>" +
711 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
712 }
713 }
714
715 } catch (e) {
716 exception_error("fatalError", e);
717 }
718 }
719
720 function filterDlgCheckType(sender) {
721
722 try {
723
724 var ftype = sender[sender.selectedIndex].value;
725
726 var form = document.forms["filter_add_form"];
727
728 if (!form) {
729 form = document.forms["filter_edit_form"];
730 }
731
732 if (!form) {
733 console.log("filterDlgCheckType: can't find form!");
734 return;
735 }
736
737 // if selected filter type is 5 (Date) enable the modifier dropbox
738 if (ftype == 5) {
739 Element.show("filter_dlg_date_mod_box");
740 Element.show("filter_dlg_date_chk_box");
741 } else {
742 Element.hide("filter_dlg_date_mod_box");
743 Element.hide("filter_dlg_date_chk_box");
744
745 }
746
747 } catch (e) {
748 exception_error("filterDlgCheckType", e);
749 }
750
751 }
752
753 function filterDlgCheckAction(sender) {
754
755 try {
756
757 var action = sender[sender.selectedIndex].value;
758
759 var form = document.forms["filter_add_form"];
760
761 if (!form) {
762 form = document.forms["filter_edit_form"];
763 }
764
765 if (!form) {
766 console.log("filterDlgCheckAction: can't find form!");
767 return;
768 }
769
770 var action_param = $("filter_dlg_param_box");
771
772 if (!action_param) {
773 console.log("filterDlgCheckAction: can't find action param box!");
774 return;
775 }
776
777 // if selected action supports parameters, enable params field
778 if (action == 4 || action == 6 || action == 7) {
779 Element.show(action_param);
780 if (action != 7) {
781 Element.show(form.action_param);
782 Element.hide(form.action_param_label);
783 } else {
784 Element.show(form.action_param_label);
785 Element.hide(form.action_param);
786 }
787 } else {
788 Element.hide(action_param);
789 }
790
791 } catch (e) {
792 exception_error("filterDlgCheckAction", e);
793 }
794
795 }
796
797 function filterDlgCheckDate() {
798 try {
799 var form = document.forms["filter_add_form"];
800
801 if (!form) {
802 form = document.forms["filter_edit_form"];
803 }
804
805 if (!form) {
806 console.log("filterDlgCheckAction: can't find form!");
807 return;
808 }
809
810 var reg_exp = form.reg_exp.value;
811
812 var query = "?op=rpc&subop=checkDate&date=" + reg_exp;
813
814 new Ajax.Request("backend.php", {
815 parameters: query,
816 onComplete: function(transport) {
817
818 var form = document.forms["filter_add_form"];
819
820 if (!form) {
821 form = document.forms["filter_edit_form"];
822 }
823
824 if (transport.responseXML) {
825 var result = transport.responseXML.getElementsByTagName("result")[0];
826
827 if (result && result.firstChild) {
828 if (result.firstChild.nodeValue == "1") {
829
830 new Effect.Highlight(form.reg_exp, {startcolor : '#00ff00'});
831
832 return;
833 }
834 }
835 }
836
837 new Effect.Highlight(form.reg_exp, {startcolor : '#ff0000'});
838
839 } });
840
841
842 } catch (e) {
843 exception_error("filterDlgCheckDate", e);
844 }
845 }
846
847 function explainError(code) {
848 return displayDlg("explainError", code);
849 }
850
851 function displayHelpInfobox(topic_id) {
852
853 var url = "backend.php?op=help&tid=" + param_escape(topic_id);
854
855 var w = window.open(url, "ttrss_help",
856 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
857
858 }
859
860 function loading_set_progress(p) {
861 try {
862 if (p < last_progress_point || !Element.visible("overlay")) return;
863
864 console.log("loading_set_progress : " + p + " (" + last_progress_point + ")");
865
866 var o = $("l_progress_i");
867
868 // o.style.width = (p * 2) + "px";
869
870 new Effect.Scale(o, p, {
871 scaleY : false,
872 scaleFrom : last_progress_point,
873 scaleMode: { originalWidth : 200 },
874 queue: { position: 'end', scope: 'LSP-Q', limit: 3 } });
875
876 last_progress_point = p;
877
878 } catch (e) {
879 exception_error("loading_set_progress", e);
880 }
881 }
882
883 function remove_splash() {
884 if (Element.visible("overlay")) {
885 console.log("about to remove splash, OMG!");
886 Element.hide("overlay");
887 console.log("removed splash!");
888 }
889 }
890
891 function getSelectedFeedsFromBrowser() {
892
893 var list = $$("#browseFeedList li[id*=FBROW]");
894
895 var selected = new Array();
896
897 list.each(function(child) {
898 var id = child.id.replace("FBROW-", "");
899 var cb = $("FBCHK-" + id);
900
901 if (cb.checked) {
902 selected.push(id);
903 }
904 });
905
906 return selected;
907 }
908
909 function updateFeedBrowser() {
910 try {
911
912 var query = Form.serialize("feed_browser");
913
914 Element.show('feed_browser_spinner');
915
916 new Ajax.Request("backend.php", {
917 parameters: query,
918 onComplete: function(transport) {
919 notify('');
920
921 Element.hide('feed_browser_spinner');
922
923 var c = $("browseFeedList");
924 var r = transport.responseXML.getElementsByTagName("content")[0];
925 var nr = transport.responseXML.getElementsByTagName("num-results")[0];
926 var mode = transport.responseXML.getElementsByTagName("mode")[0];
927
928 if (c && r) {
929 c.innerHTML = r.firstChild.nodeValue;
930 }
931
932 if (parseInt(mode.getAttribute("value")) == 2) {
933 Element.show('feed_archive_remove');
934 } else {
935 Element.hide('feed_archive_remove');
936 }
937
938 } });
939
940 } catch (e) {
941 exception_error("updateFeedBrowser", e);
942 }
943
944 }
945
946 function transport_error_check(transport) {
947 try {
948 if (transport.responseXML) {
949 var error = transport.responseXML.getElementsByTagName("error")[0];
950
951 if (error) {
952 var code = error.getAttribute("error-code");
953 var msg = error.getAttribute("error-msg");
954 if (code != 0) {
955 fatalError(code, msg);
956 return false;
957 }
958 }
959 }
960 } catch (e) {
961 exception_error("check_for_error_xml", e);
962 }
963 return true;
964 }
965
966 function strip_tags(s) {
967 return s.replace(/<\/?[^>]+(>|$)/g, "");
968 }
969
970 function truncate_string(s, length) {
971 if (!length) length = 30;
972 var tmp = s.substring(0, length);
973 if (s.length > length) tmp += "&hellip;";
974 return tmp;
975 }
976
977 function hotkey_prefix_timeout() {
978 try {
979
980 var date = new Date();
981 var ts = Math.round(date.getTime() / 1000);
982
983 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
984 console.log("hotkey_prefix seems to be stuck, aborting");
985 hotkey_prefix_pressed = false;
986 hotkey_prefix = false;
987 Element.hide('cmdline');
988 }
989
990 setTimeout("hotkey_prefix_timeout()", 1000);
991
992 } catch (e) {
993 exception_error("hotkey_prefix_timeout", e);
994 }
995 }
996
997 function hideAuxDlg() {
998 try {
999 Element.hide('auxDlg');
1000 } catch (e) {
1001 exception_error("hideAuxDlg", e);
1002 }
1003 }
1004
1005 function feedBrowserSubscribe() {
1006 try {
1007
1008 var selected = getSelectedFeedsFromBrowser();
1009
1010 var mode = document.forms['feed_browser'].mode;
1011
1012 mode = mode[mode.selectedIndex].value;
1013
1014 if (selected.length > 0) {
1015 closeInfoBox();
1016
1017 notify_progress("Loading, please wait...", true);
1018
1019 var query = "?op=rpc&subop=massSubscribe&ids="+
1020 param_escape(selected.toString()) + "&mode=" + param_escape(mode);
1021
1022 new Ajax.Request("backend.php", {
1023 parameters: query,
1024 onComplete: function(transport) {
1025
1026 var nf = transport.responseXML.getElementsByTagName('num-feeds')[0];
1027 var nf_value = nf.getAttribute("value");
1028
1029 notify_info(__("Subscribed to %d feed(s).").replace("%d", nf_value));
1030
1031 if (inPreferences()) {
1032 updateFeedList();
1033 } else {
1034 setTimeout('updateFeedList(false, false)', 50);
1035 }
1036 } });
1037
1038 } else {
1039 alert(__("No feeds are selected."));
1040 }
1041
1042 } catch (e) {
1043 exception_error("feedBrowserSubscribe", e);
1044 }
1045 }
1046
1047 function feedArchiveRemove() {
1048 try {
1049
1050 var selected = getSelectedFeedsFromBrowser();
1051
1052 if (selected.length > 0) {
1053
1054 var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1055
1056 if (confirm(pr)) {
1057 Element.show('feed_browser_spinner');
1058
1059 var query = "?op=rpc&subop=remarchived&ids=" +
1060 param_escape(selected.toString());;
1061
1062 new Ajax.Request("backend.php", {
1063 parameters: query,
1064 onComplete: function(transport) {
1065 updateFeedBrowser();
1066 } });
1067 }
1068
1069 } else {
1070 alert(__("No feeds are selected."));
1071 }
1072
1073 } catch (e) {
1074 exception_error("feedArchiveRemove", e);
1075 }
1076 }
1077
1078 function uploadIconHandler(rc) {
1079 try {
1080 switch (rc) {
1081 case 0:
1082 notify_info("Upload complete.");
1083 if (inPreferences()) {
1084 updateFeedList();
1085 } else {
1086 setTimeout('updateFeedList(false, false)', 50);
1087 }
1088 break;
1089 case 1:
1090 notify_error("Upload failed: icon is too big.");
1091 break;
1092 case 2:
1093 notify_error("Upload failed.");
1094 break;
1095 }
1096
1097 } catch (e) {
1098 exception_error("uploadIconHandler", e);
1099 }
1100 }
1101
1102 function removeFeedIcon(id) {
1103
1104 try {
1105
1106 if (confirm(__("Remove stored feed icon?"))) {
1107 var query = "backend.php?op=pref-feeds&subop=removeicon&feed_id=" + param_escape(id);
1108
1109 console.log(query);
1110
1111 notify_progress("Removing feed icon...", true);
1112
1113 new Ajax.Request("backend.php", {
1114 parameters: query,
1115 onComplete: function(transport) {
1116 notify_info("Feed icon removed.");
1117 if (inPreferences()) {
1118 updateFeedList();
1119 } else {
1120 setTimeout('updateFeedList(false, false)', 50);
1121 }
1122 } });
1123 }
1124
1125 return false;
1126 } catch (e) {
1127 exception_error("uploadFeedIcon", e);
1128 }
1129 }
1130
1131 function uploadFeedIcon() {
1132
1133 try {
1134
1135 var file = $("icon_file");
1136
1137 if (file.value.length == 0) {
1138 alert(__("Please select an image file to upload."));
1139 } else {
1140 if (confirm(__("Upload new icon for this feed?"))) {
1141 notify_progress("Uploading, please wait...", true);
1142 return true;
1143 }
1144 }
1145
1146 return false;
1147
1148 } catch (e) {
1149 exception_error("uploadFeedIcon", e);
1150 }
1151 }
1152
1153 function addLabel(select, callback) {
1154
1155 try {
1156
1157 var caption = prompt(__("Please enter label caption:"), "");
1158
1159 if (caption != undefined) {
1160
1161 if (caption == "") {
1162 alert(__("Can't create label: missing caption."));
1163 return false;
1164 }
1165
1166 var query = "?op=pref-labels&subop=add&caption=" +
1167 param_escape(caption);
1168
1169 if (select)
1170 query += "&output=select";
1171
1172 notify_progress("Loading, please wait...", true);
1173
1174 if (inPreferences() && !select) active_tab = "labelConfig";
1175
1176 new Ajax.Request("backend.php", {
1177 parameters: query,
1178 onComplete: function(transport) {
1179 if (callback) {
1180 callback(transport);
1181 } else if (inPreferences()) {
1182 infobox_submit_callback2(transport);
1183 } else {
1184 updateFeedList();
1185 }
1186 } });
1187
1188 }
1189
1190 } catch (e) {
1191 exception_error("addLabel", e);
1192 }
1193 }
1194
1195 function quickAddFeed() {
1196 displayDlg('quickAddFeed', '',
1197 function () {$('feed_url').focus();});
1198 }
1199
1200 function quickAddFilter() {
1201 displayDlg('quickAddFilter', '',
1202 function () {document.forms['filter_add_form'].reg_exp.focus();});
1203 }
1204
1205 function unsubscribeFeed(feed_id, title) {
1206
1207 var msg = __("Unsubscribe from %s?").replace("%s", title);
1208
1209 if (title == undefined || confirm(msg)) {
1210 notify_progress("Removing feed...");
1211
1212 var query = "?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id;
1213
1214 new Ajax.Request("backend.php", {
1215 parameters: query,
1216 onComplete: function(transport) {
1217
1218 closeInfoBox();
1219
1220 if (inPreferences()) {
1221 updateFeedList();
1222 } else {
1223 dlg_frefresh_callback(transport, feed_id);
1224 }
1225
1226 } });
1227 }
1228
1229 return false;
1230 }
1231
1232
1233 function backend_sanity_check_callback(transport) {
1234
1235 try {
1236
1237 if (sanity_check_done) {
1238 fatalError(11, "Sanity check request received twice. This can indicate "+
1239 "presence of Firebug or some other disrupting extension. "+
1240 "Please disable it and try again.");
1241 return;
1242 }
1243
1244 if (!transport.responseXML) {
1245 if (!store) {
1246 fatalError(3, "Sanity check: Received reply is not XML",
1247 transport.responseText);
1248 return;
1249 }
1250 }
1251
1252 var reply = transport.responseXML.getElementsByTagName("error")[0];
1253
1254 if (!reply) {
1255 fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1256 return;
1257 }
1258
1259 var error_code = reply.getAttribute("error-code");
1260
1261 if (error_code && error_code != 0) {
1262 return fatalError(error_code, reply.getAttribute("error-msg"));
1263 }
1264
1265 console.log("sanity check ok");
1266
1267 var params = transport.responseXML.getElementsByTagName("init-params")[0];
1268
1269 if (params) {
1270 console.log('reading init-params...');
1271
1272 params = JSON.parse(params.firstChild.nodeValue);
1273
1274 if (params) {
1275 for (k in params) {
1276 var v = params[k];
1277 console.log("IP: " + k + " => " + v);
1278 }
1279 }
1280
1281 init_params = params;
1282 }
1283
1284 sanity_check_done = true;
1285
1286 init_second_stage();
1287
1288 } catch (e) {
1289 exception_error("backend_sanity_check_callback", e, transport);
1290 }
1291 }
1292
1293 function has_local_storage() {
1294 return false;
1295 /* try {
1296 return 'localStorage' in window && window['localStorage'] != null;
1297 } catch (e) {
1298 return false;
1299 } */
1300 }
1301
1302 function catSelectOnChange(elem) {
1303 try {
1304 var value = elem[elem.selectedIndex].value;
1305 var def = elem.getAttribute('default');
1306
1307 if (value == "ADD_CAT") {
1308
1309 if (def)
1310 dropboxSelect(elem, def);
1311 else
1312 elem.selectedIndex = 0;
1313
1314 quickAddCat(elem);
1315 }
1316
1317 } catch (e) {
1318 exception_error("catSelectOnChange", e);
1319 }
1320 }
1321
1322 function quickAddCat(elem) {
1323 try {
1324 var cat = prompt(__("Please enter category title:"));
1325
1326 if (cat) {
1327
1328 var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
1329
1330 notify_progress("Loading, please wait...", true);
1331
1332 new Ajax.Request("backend.php", {
1333 parameters: query,
1334 onComplete: function (transport) {
1335 var response = transport.responseXML;
1336 var select = response.getElementsByTagName("select")[0];
1337 var options = select.getElementsByTagName("option");
1338
1339 dropbox_replace_options(elem, options);
1340
1341 notify('');
1342
1343 } });
1344
1345 }
1346
1347 } catch (e) {
1348 exception_error("quickAddCat", e);
1349 }
1350 }
1351
1352 function genUrlChangeKey(feed, is_cat) {
1353
1354 try {
1355 var ok = confirm(__("Generate new syndication address for this feed?"));
1356
1357 if (ok) {
1358
1359 notify_progress("Trying to change address...", true);
1360
1361 var query = "?op=rpc&subop=regenFeedKey&id=" + param_escape(feed) +
1362 "&is_cat=" + param_escape(is_cat);
1363
1364 new Ajax.Request("backend.php", {
1365 parameters: query,
1366 onComplete: function(transport) {
1367 var new_link = transport.responseXML.getElementsByTagName("link")[0];
1368
1369 var e = $('gen_feed_url');
1370
1371 if (new_link) {
1372
1373 new_link = new_link.firstChild.nodeValue;
1374
1375 e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1376 "&amp;key=" + new_link);
1377
1378 e.href = e.href.replace(/\&amp;key=.*$/,
1379 "&amp;key=" + new_link);
1380
1381 new Effect.Highlight(e);
1382
1383 notify('');
1384
1385 } else {
1386 notify_error("Could not change feed URL.");
1387 }
1388 } });
1389 }
1390 } catch (e) {
1391 exception_error("genUrlChangeKey", e);
1392 }
1393 return false;
1394 }
1395
1396 function labelSelectOnChange(elem) {
1397 try {
1398 var value = elem[elem.selectedIndex].value;
1399 var def = elem.getAttribute('default');
1400
1401 if (value == "ADD_LABEL") {
1402
1403 if (def)
1404 dropboxSelect(elem, def);
1405 else
1406 elem.selectedIndex = 0;
1407
1408 addLabel(elem, function(transport) {
1409
1410 try {
1411
1412 var response = transport.responseXML;
1413 var select = response.getElementsByTagName("select")[0];
1414 var options = select.getElementsByTagName("option");
1415
1416 dropbox_replace_options(elem, options);
1417
1418 notify('');
1419 } catch (e) {
1420 exception_error("addLabel", e);
1421 }
1422 });
1423 }
1424
1425 } catch (e) {
1426 exception_error("labelSelectOnChange", e);
1427 }
1428 }
1429
1430 function dropbox_replace_options(elem, options) {
1431
1432 try {
1433 while (elem.hasChildNodes())
1434 elem.removeChild(elem.firstChild);
1435
1436 var sel_idx = -1;
1437
1438 for (var i = 0; i < options.length; i++) {
1439 var text = options[i].firstChild.nodeValue;
1440 var value = options[i].getAttribute("value");
1441
1442 if (value == undefined) value = text;
1443
1444 var issel = options[i].getAttribute("selected") == "1";
1445
1446 var option = new Option(text, value, issel);
1447
1448 if (options[i].getAttribute("disabled"))
1449 option.setAttribute("disabled", true);
1450
1451 elem.insert(option);
1452
1453 if (issel) sel_idx = i;
1454 }
1455
1456 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1457 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1458
1459 } catch (e) {
1460 exception_error("dropbox_replace_options", e);
1461 }
1462 }
1463
1464 // mode = all, none, invert
1465 function selectTableRows(id, mode) {
1466 try {
1467 var rows = $(id).rows;
1468
1469 for (var i = 0; i < rows.length; i++) {
1470 var row = rows[i];
1471 var cb = false;
1472
1473 if (row.id && row.className) {
1474 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1475 var inputs = rows[i].getElementsByTagName("input");
1476
1477 for (var j = 0; j < inputs.length; j++) {
1478 var input = inputs[j];
1479
1480 if (input.getAttribute("type") == "checkbox" &&
1481 input.id.match(bare_id)) {
1482
1483 cb = input;
1484 break;
1485 }
1486 }
1487
1488 if (cb) {
1489 var issel = row.hasClassName("Selected");
1490
1491 if (mode == "all" && !issel) {
1492 row.addClassName("Selected");
1493 cb.checked = true;
1494 } else if (mode == "none" && issel) {
1495 row.removeClassName("Selected");
1496 cb.checked = false;
1497 } else if (mode == "invert") {
1498
1499 if (issel) {
1500 row.removeClassName("Selected");
1501 cb.checked = false;
1502 } else {
1503 row.addClassName("Selected");
1504 cb.checked = true;
1505 }
1506 }
1507 }
1508 }
1509 }
1510
1511 } catch (e) {
1512 exception_error("selectTableRows", e);
1513
1514 }
1515 }
1516
1517 function getSelectedTableRowIds(id) {
1518 var rows = [];
1519
1520 try {
1521 var elem_rows = $(id).rows;
1522
1523 for (i = 0; i < elem_rows.length; i++) {
1524 if (elem_rows[i].hasClassName("Selected")) {
1525 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1526 rows.push(bare_id);
1527 }
1528 }
1529
1530 } catch (e) {
1531 exception_error("getSelectedTableRowIds", e);
1532 }
1533
1534 return rows;
1535 }
1536