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