]> git.wh0rd.org - tt-rss.git/blob - functions.js
update label editor
[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 try {
578
579 if (!transport.responseXML) {
580 console.log(transport.responseText);
581 alert(__("Server error while trying to subscribe to specified feed."));
582 return;
583 }
584
585 var result = transport.responseXML.getElementsByTagName('result')[0];
586 var rc = parseInt(result.getAttribute('code'));
587
588 Form.enable("feed_add_form");
589
590 notify('');
591
592 switch (rc) {
593 case 1:
594 closeInfoBox();
595 notify_info(__("Subscribed to %s").replace("%s", feed_url));
596
597 if (inPreferences()) {
598 updateFeedList();
599 } else {
600 setTimeout('updateFeedList(false, false)', 50);
601 }
602 break;
603 case 2:
604 alert(__("Specified URL seems to be invalid."));
605 break;
606 case 3:
607 alert(__("Specified URL doesn't seem to contain any feeds."));
608 break;
609 case 4:
610 new Ajax.Request("backend.php", {
611 parameters: 'op=rpc&subop=extractfeedurls&url=' + encodeURIComponent(feed_url),
612 onComplete: function(transport) {
613 var result = transport.responseXML.getElementsByTagName('urls')[0];
614 var feeds = JSON.parse(result.firstChild.nodeValue);
615 var select = document.getElementById("faad_feeds_container_select");
616
617 while (select.hasChildNodes()) {
618 select.removeChild(elem.firstChild);
619 }
620 var count = 0;
621 for (var feedUrl in feeds) {
622 select.insert(new Option(feeds[feedUrl], feedUrl, false));
623 count++;
624 }
625 if (count > 5) count = 5;
626 select.size = count;
627
628 Effect.Appear('fadd_feeds_container', {duration : 0.5});
629 }
630 });
631 break;
632 case 5:
633 alert(__("Couldn't download the specified URL."));
634 break;
635 case 0:
636 alert(__("You are already subscribed to this feed."));
637 break;
638 }
639
640 } catch (e) {
641 exception_error("subscribeToFeed", e);
642 }
643
644 } });
645
646 } catch (e) {
647 exception_error("subscribeToFeed", e);
648 }
649
650 return false;
651 }
652
653 function filterCR(e, f)
654 {
655 var key;
656
657 if(window.event)
658 key = window.event.keyCode; //IE
659 else
660 key = e.which; //firefox
661
662 if (key == 13) {
663 if (typeof f != 'undefined') {
664 f();
665 return false;
666 } else {
667 return false;
668 }
669 } else {
670 return true;
671 }
672 }
673
674 function getInitParam(key) {
675 return init_params[key];
676 }
677
678 function setInitParam(key, value) {
679 init_params[key] = value;
680 }
681
682 function fatalError(code, msg, ext_info) {
683 try {
684
685 if (!ext_info) ext_info = "N/A";
686
687 if (code == 6) {
688 window.location.href = "tt-rss.php";
689 } else if (code == 5) {
690 window.location.href = "db-updater.php";
691 } else {
692
693 if (msg == "") msg = "Unknown error";
694
695 var ebc = $("xebContent");
696
697 if (ebc) {
698
699 Element.show("dialog_overlay");
700 Element.show("errorBoxShadow");
701 Element.hide("xebBtn");
702
703 if (ext_info) {
704 if (ext_info.responseText) {
705 ext_info = ext_info.responseText;
706 }
707 }
708
709 ebc.innerHTML =
710 "<div><b>Error message:</b></div>" +
711 "<pre>" + msg + "</pre>" +
712 "<div><b>Additional information:</b></div>" +
713 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
714 }
715 }
716
717 } catch (e) {
718 exception_error("fatalError", e);
719 }
720 }
721
722 function filterDlgCheckType(sender) {
723
724 try {
725
726 var ftype = sender[sender.selectedIndex].value;
727
728 var form = document.forms["filter_add_form"];
729
730 if (!form) {
731 form = document.forms["filter_edit_form"];
732 }
733
734 if (!form) {
735 console.log("filterDlgCheckType: can't find form!");
736 return;
737 }
738
739 // if selected filter type is 5 (Date) enable the modifier dropbox
740 if (ftype == 5) {
741 Element.show("filter_dlg_date_mod_box");
742 Element.show("filter_dlg_date_chk_box");
743 } else {
744 Element.hide("filter_dlg_date_mod_box");
745 Element.hide("filter_dlg_date_chk_box");
746
747 }
748
749 } catch (e) {
750 exception_error("filterDlgCheckType", e);
751 }
752
753 }
754
755 function filterDlgCheckAction(sender) {
756
757 try {
758
759 var action = sender[sender.selectedIndex].value;
760
761 var form = document.forms["filter_add_form"];
762
763 if (!form) {
764 form = document.forms["filter_edit_form"];
765 }
766
767 if (!form) {
768 console.log("filterDlgCheckAction: can't find form!");
769 return;
770 }
771
772 var action_param = $("filter_dlg_param_box");
773
774 if (!action_param) {
775 console.log("filterDlgCheckAction: can't find action param box!");
776 return;
777 }
778
779 // if selected action supports parameters, enable params field
780 if (action == 4 || action == 6 || action == 7) {
781 Element.show(action_param);
782 if (action != 7) {
783 Element.show(form.action_param);
784 Element.hide(form.action_param_label);
785 } else {
786 Element.show(form.action_param_label);
787 Element.hide(form.action_param);
788 }
789 } else {
790 Element.hide(action_param);
791 }
792
793 } catch (e) {
794 exception_error("filterDlgCheckAction", e);
795 }
796
797 }
798
799 function filterDlgCheckDate() {
800 try {
801 var form = document.forms["filter_add_form"];
802
803 if (!form) {
804 form = document.forms["filter_edit_form"];
805 }
806
807 if (!form) {
808 console.log("filterDlgCheckAction: can't find form!");
809 return;
810 }
811
812 var reg_exp = form.reg_exp.value;
813
814 var query = "?op=rpc&subop=checkDate&date=" + reg_exp;
815
816 new Ajax.Request("backend.php", {
817 parameters: query,
818 onComplete: function(transport) {
819
820 var form = document.forms["filter_add_form"];
821
822 if (!form) {
823 form = document.forms["filter_edit_form"];
824 }
825
826 if (transport.responseXML) {
827 var result = transport.responseXML.getElementsByTagName("result")[0];
828
829 if (result && result.firstChild) {
830 if (result.firstChild.nodeValue == "1") {
831
832 new Effect.Highlight(form.reg_exp, {startcolor : '#00ff00'});
833
834 return;
835 }
836 }
837 }
838
839 new Effect.Highlight(form.reg_exp, {startcolor : '#ff0000'});
840
841 } });
842
843
844 } catch (e) {
845 exception_error("filterDlgCheckDate", e);
846 }
847 }
848
849 function explainError(code) {
850 return displayDlg("explainError", code);
851 }
852
853 function displayHelpInfobox(topic_id) {
854
855 var url = "backend.php?op=help&tid=" + param_escape(topic_id);
856
857 var w = window.open(url, "ttrss_help",
858 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
859
860 }
861
862 function loading_set_progress(p) {
863 try {
864 if (p < last_progress_point || !Element.visible("overlay")) return;
865
866 console.log("loading_set_progress : " + p + " (" + last_progress_point + ")");
867
868 var o = $("l_progress_i");
869
870 // o.style.width = (p * 2) + "px";
871
872 new Effect.Scale(o, p, {
873 scaleY : false,
874 scaleFrom : last_progress_point,
875 scaleMode: { originalWidth : 200 },
876 queue: { position: 'end', scope: 'LSP-Q', limit: 3 } });
877
878 last_progress_point = p;
879
880 } catch (e) {
881 exception_error("loading_set_progress", e);
882 }
883 }
884
885 function remove_splash() {
886 if (Element.visible("overlay")) {
887 console.log("about to remove splash, OMG!");
888 Element.hide("overlay");
889 console.log("removed splash!");
890 }
891 }
892
893 function getSelectedFeedsFromBrowser() {
894
895 var list = $$("#browseFeedList li[id*=FBROW]");
896
897 var selected = new Array();
898
899 list.each(function(child) {
900 var id = child.id.replace("FBROW-", "");
901 var cb = $("FBCHK-" + id);
902
903 if (cb.checked) {
904 selected.push(id);
905 }
906 });
907
908 return selected;
909 }
910
911 function updateFeedBrowser() {
912 try {
913
914 var query = Form.serialize("feed_browser");
915
916 Element.show('feed_browser_spinner');
917
918 new Ajax.Request("backend.php", {
919 parameters: query,
920 onComplete: function(transport) {
921 notify('');
922
923 Element.hide('feed_browser_spinner');
924
925 var c = $("browseFeedList");
926 var r = transport.responseXML.getElementsByTagName("content")[0];
927 var nr = transport.responseXML.getElementsByTagName("num-results")[0];
928 var mode = transport.responseXML.getElementsByTagName("mode")[0];
929
930 if (c && r) {
931 c.innerHTML = r.firstChild.nodeValue;
932 }
933
934 if (parseInt(mode.getAttribute("value")) == 2) {
935 Element.show('feed_archive_remove');
936 } else {
937 Element.hide('feed_archive_remove');
938 }
939
940 } });
941
942 } catch (e) {
943 exception_error("updateFeedBrowser", e);
944 }
945
946 }
947
948 function transport_error_check(transport) {
949 try {
950 if (transport.responseXML) {
951 var error = transport.responseXML.getElementsByTagName("error")[0];
952
953 if (error) {
954 var code = error.getAttribute("error-code");
955 var msg = error.getAttribute("error-msg");
956 if (code != 0) {
957 fatalError(code, msg);
958 return false;
959 }
960 }
961 }
962 } catch (e) {
963 exception_error("check_for_error_xml", e);
964 }
965 return true;
966 }
967
968 function strip_tags(s) {
969 return s.replace(/<\/?[^>]+(>|$)/g, "");
970 }
971
972 function truncate_string(s, length) {
973 if (!length) length = 30;
974 var tmp = s.substring(0, length);
975 if (s.length > length) tmp += "&hellip;";
976 return tmp;
977 }
978
979 function hotkey_prefix_timeout() {
980 try {
981
982 var date = new Date();
983 var ts = Math.round(date.getTime() / 1000);
984
985 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
986 console.log("hotkey_prefix seems to be stuck, aborting");
987 hotkey_prefix_pressed = false;
988 hotkey_prefix = false;
989 Element.hide('cmdline');
990 }
991
992 setTimeout("hotkey_prefix_timeout()", 1000);
993
994 } catch (e) {
995 exception_error("hotkey_prefix_timeout", e);
996 }
997 }
998
999 function hideAuxDlg() {
1000 try {
1001 Element.hide('auxDlg');
1002 } catch (e) {
1003 exception_error("hideAuxDlg", e);
1004 }
1005 }
1006
1007 function feedBrowserSubscribe() {
1008 try {
1009
1010 var selected = getSelectedFeedsFromBrowser();
1011
1012 var mode = document.forms['feed_browser'].mode;
1013
1014 mode = mode[mode.selectedIndex].value;
1015
1016 if (selected.length > 0) {
1017 closeInfoBox();
1018
1019 notify_progress("Loading, please wait...", true);
1020
1021 var query = "?op=rpc&subop=massSubscribe&ids="+
1022 param_escape(selected.toString()) + "&mode=" + param_escape(mode);
1023
1024 new Ajax.Request("backend.php", {
1025 parameters: query,
1026 onComplete: function(transport) {
1027
1028 var nf = transport.responseXML.getElementsByTagName('num-feeds')[0];
1029 var nf_value = nf.getAttribute("value");
1030
1031 notify_info(__("Subscribed to %d feed(s).").replace("%d", nf_value));
1032
1033 if (inPreferences()) {
1034 updateFeedList();
1035 } else {
1036 setTimeout('updateFeedList(false, false)', 50);
1037 }
1038 } });
1039
1040 } else {
1041 alert(__("No feeds are selected."));
1042 }
1043
1044 } catch (e) {
1045 exception_error("feedBrowserSubscribe", e);
1046 }
1047 }
1048
1049 function feedArchiveRemove() {
1050 try {
1051
1052 var selected = getSelectedFeedsFromBrowser();
1053
1054 if (selected.length > 0) {
1055
1056 var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1057
1058 if (confirm(pr)) {
1059 Element.show('feed_browser_spinner');
1060
1061 var query = "?op=rpc&subop=remarchived&ids=" +
1062 param_escape(selected.toString());;
1063
1064 new Ajax.Request("backend.php", {
1065 parameters: query,
1066 onComplete: function(transport) {
1067 updateFeedBrowser();
1068 } });
1069 }
1070
1071 } else {
1072 alert(__("No feeds are selected."));
1073 }
1074
1075 } catch (e) {
1076 exception_error("feedArchiveRemove", e);
1077 }
1078 }
1079
1080 function uploadIconHandler(rc) {
1081 try {
1082 switch (rc) {
1083 case 0:
1084 notify_info("Upload complete.");
1085 if (inPreferences()) {
1086 updateFeedList();
1087 } else {
1088 setTimeout('updateFeedList(false, false)', 50);
1089 }
1090 break;
1091 case 1:
1092 notify_error("Upload failed: icon is too big.");
1093 break;
1094 case 2:
1095 notify_error("Upload failed.");
1096 break;
1097 }
1098
1099 } catch (e) {
1100 exception_error("uploadIconHandler", e);
1101 }
1102 }
1103
1104 function removeFeedIcon(id) {
1105
1106 try {
1107
1108 if (confirm(__("Remove stored feed icon?"))) {
1109 var query = "backend.php?op=pref-feeds&subop=removeicon&feed_id=" + param_escape(id);
1110
1111 console.log(query);
1112
1113 notify_progress("Removing feed icon...", true);
1114
1115 new Ajax.Request("backend.php", {
1116 parameters: query,
1117 onComplete: function(transport) {
1118 notify_info("Feed icon removed.");
1119 if (inPreferences()) {
1120 updateFeedList();
1121 } else {
1122 setTimeout('updateFeedList(false, false)', 50);
1123 }
1124 } });
1125 }
1126
1127 return false;
1128 } catch (e) {
1129 exception_error("uploadFeedIcon", e);
1130 }
1131 }
1132
1133 function uploadFeedIcon() {
1134
1135 try {
1136
1137 var file = $("icon_file");
1138
1139 if (file.value.length == 0) {
1140 alert(__("Please select an image file to upload."));
1141 } else {
1142 if (confirm(__("Upload new icon for this feed?"))) {
1143 notify_progress("Uploading, please wait...", true);
1144 return true;
1145 }
1146 }
1147
1148 return false;
1149
1150 } catch (e) {
1151 exception_error("uploadFeedIcon", e);
1152 }
1153 }
1154
1155 function addLabel(select, callback) {
1156
1157 try {
1158
1159 var caption = prompt(__("Please enter label caption:"), "");
1160
1161 if (caption != undefined) {
1162
1163 if (caption == "") {
1164 alert(__("Can't create label: missing caption."));
1165 return false;
1166 }
1167
1168 var query = "?op=pref-labels&subop=add&caption=" +
1169 param_escape(caption);
1170
1171 if (select)
1172 query += "&output=select";
1173
1174 notify_progress("Loading, please wait...", true);
1175
1176 if (inPreferences() && !select) active_tab = "labelConfig";
1177
1178 new Ajax.Request("backend.php", {
1179 parameters: query,
1180 onComplete: function(transport) {
1181 if (callback) {
1182 callback(transport);
1183 } else if (inPreferences()) {
1184 infobox_submit_callback2(transport);
1185 } else {
1186 updateFeedList();
1187 }
1188 } });
1189
1190 }
1191
1192 } catch (e) {
1193 exception_error("addLabel", e);
1194 }
1195 }
1196
1197 function quickAddFeed() {
1198 displayDlg('quickAddFeed', '',
1199 function () {$('feed_url').focus();});
1200 }
1201
1202 function quickAddFilter() {
1203 displayDlg('quickAddFilter', '',
1204 function () {document.forms['filter_add_form'].reg_exp.focus();});
1205 }
1206
1207 function unsubscribeFeed(feed_id, title) {
1208
1209 var msg = __("Unsubscribe from %s?").replace("%s", title);
1210
1211 if (title == undefined || confirm(msg)) {
1212 notify_progress("Removing feed...");
1213
1214 var query = "?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id;
1215
1216 new Ajax.Request("backend.php", {
1217 parameters: query,
1218 onComplete: function(transport) {
1219
1220 closeInfoBox();
1221
1222 if (inPreferences()) {
1223 updateFeedList();
1224 } else {
1225 dlg_frefresh_callback(transport, feed_id);
1226 }
1227
1228 } });
1229 }
1230
1231 return false;
1232 }
1233
1234
1235 function backend_sanity_check_callback(transport) {
1236
1237 try {
1238
1239 if (sanity_check_done) {
1240 fatalError(11, "Sanity check request received twice. This can indicate "+
1241 "presence of Firebug or some other disrupting extension. "+
1242 "Please disable it and try again.");
1243 return;
1244 }
1245
1246 if (!transport.responseXML) {
1247 if (!store) {
1248 fatalError(3, "Sanity check: Received reply is not XML",
1249 transport.responseText);
1250 return;
1251 }
1252 }
1253
1254 var reply = transport.responseXML.getElementsByTagName("error")[0];
1255
1256 if (!reply) {
1257 fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1258 return;
1259 }
1260
1261 var error_code = reply.getAttribute("error-code");
1262
1263 if (error_code && error_code != 0) {
1264 return fatalError(error_code, reply.getAttribute("error-msg"));
1265 }
1266
1267 console.log("sanity check ok");
1268
1269 var params = transport.responseXML.getElementsByTagName("init-params")[0];
1270
1271 if (params) {
1272 console.log('reading init-params...');
1273
1274 params = JSON.parse(params.firstChild.nodeValue);
1275
1276 if (params) {
1277 for (k in params) {
1278 var v = params[k];
1279 console.log("IP: " + k + " => " + v);
1280 }
1281 }
1282
1283 init_params = params;
1284 }
1285
1286 sanity_check_done = true;
1287
1288 init_second_stage();
1289
1290 } catch (e) {
1291 exception_error("backend_sanity_check_callback", e, transport);
1292 }
1293 }
1294
1295 function has_local_storage() {
1296 return false;
1297 /* try {
1298 return 'localStorage' in window && window['localStorage'] != null;
1299 } catch (e) {
1300 return false;
1301 } */
1302 }
1303
1304 function catSelectOnChange(elem) {
1305 try {
1306 var value = elem[elem.selectedIndex].value;
1307 var def = elem.getAttribute('default');
1308
1309 if (value == "ADD_CAT") {
1310
1311 if (def)
1312 dropboxSelect(elem, def);
1313 else
1314 elem.selectedIndex = 0;
1315
1316 quickAddCat(elem);
1317 }
1318
1319 } catch (e) {
1320 exception_error("catSelectOnChange", e);
1321 }
1322 }
1323
1324 function quickAddCat(elem) {
1325 try {
1326 var cat = prompt(__("Please enter category title:"));
1327
1328 if (cat) {
1329
1330 var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
1331
1332 notify_progress("Loading, please wait...", true);
1333
1334 new Ajax.Request("backend.php", {
1335 parameters: query,
1336 onComplete: function (transport) {
1337 var response = transport.responseXML;
1338 var select = response.getElementsByTagName("select")[0];
1339 var options = select.getElementsByTagName("option");
1340
1341 dropbox_replace_options(elem, options);
1342
1343 notify('');
1344
1345 } });
1346
1347 }
1348
1349 } catch (e) {
1350 exception_error("quickAddCat", e);
1351 }
1352 }
1353
1354 function genUrlChangeKey(feed, is_cat) {
1355
1356 try {
1357 var ok = confirm(__("Generate new syndication address for this feed?"));
1358
1359 if (ok) {
1360
1361 notify_progress("Trying to change address...", true);
1362
1363 var query = "?op=rpc&subop=regenFeedKey&id=" + param_escape(feed) +
1364 "&is_cat=" + param_escape(is_cat);
1365
1366 new Ajax.Request("backend.php", {
1367 parameters: query,
1368 onComplete: function(transport) {
1369 var new_link = transport.responseXML.getElementsByTagName("link")[0];
1370
1371 var e = $('gen_feed_url');
1372
1373 if (new_link) {
1374
1375 new_link = new_link.firstChild.nodeValue;
1376
1377 e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1378 "&amp;key=" + new_link);
1379
1380 e.href = e.href.replace(/\&amp;key=.*$/,
1381 "&amp;key=" + new_link);
1382
1383 new Effect.Highlight(e);
1384
1385 notify('');
1386
1387 } else {
1388 notify_error("Could not change feed URL.");
1389 }
1390 } });
1391 }
1392 } catch (e) {
1393 exception_error("genUrlChangeKey", e);
1394 }
1395 return false;
1396 }
1397
1398 function labelSelectOnChange(elem) {
1399 try {
1400 var value = elem[elem.selectedIndex].value;
1401 var def = elem.getAttribute('default');
1402
1403 if (value == "ADD_LABEL") {
1404
1405 if (def)
1406 dropboxSelect(elem, def);
1407 else
1408 elem.selectedIndex = 0;
1409
1410 addLabel(elem, function(transport) {
1411
1412 try {
1413
1414 var response = transport.responseXML;
1415 var select = response.getElementsByTagName("select")[0];
1416 var options = select.getElementsByTagName("option");
1417
1418 dropbox_replace_options(elem, options);
1419
1420 notify('');
1421 } catch (e) {
1422 exception_error("addLabel", e);
1423 }
1424 });
1425 }
1426
1427 } catch (e) {
1428 exception_error("labelSelectOnChange", e);
1429 }
1430 }
1431
1432 function dropbox_replace_options(elem, options) {
1433
1434 try {
1435 while (elem.hasChildNodes())
1436 elem.removeChild(elem.firstChild);
1437
1438 var sel_idx = -1;
1439
1440 for (var i = 0; i < options.length; i++) {
1441 var text = options[i].firstChild.nodeValue;
1442 var value = options[i].getAttribute("value");
1443
1444 if (value == undefined) value = text;
1445
1446 var issel = options[i].getAttribute("selected") == "1";
1447
1448 var option = new Option(text, value, issel);
1449
1450 if (options[i].getAttribute("disabled"))
1451 option.setAttribute("disabled", true);
1452
1453 elem.insert(option);
1454
1455 if (issel) sel_idx = i;
1456 }
1457
1458 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1459 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1460
1461 } catch (e) {
1462 exception_error("dropbox_replace_options", e);
1463 }
1464 }
1465
1466 // mode = all, none, invert
1467 function selectTableRows(id, mode) {
1468 try {
1469 var rows = $(id).rows;
1470
1471 for (var i = 0; i < rows.length; i++) {
1472 var row = rows[i];
1473 var cb = false;
1474
1475 if (row.id && row.className) {
1476 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1477 var inputs = rows[i].getElementsByTagName("input");
1478
1479 for (var j = 0; j < inputs.length; j++) {
1480 var input = inputs[j];
1481
1482 if (input.getAttribute("type") == "checkbox" &&
1483 input.id.match(bare_id)) {
1484
1485 cb = input;
1486 break;
1487 }
1488 }
1489
1490 if (cb) {
1491 var issel = row.hasClassName("Selected");
1492
1493 if (mode == "all" && !issel) {
1494 row.addClassName("Selected");
1495 cb.checked = true;
1496 } else if (mode == "none" && issel) {
1497 row.removeClassName("Selected");
1498 cb.checked = false;
1499 } else if (mode == "invert") {
1500
1501 if (issel) {
1502 row.removeClassName("Selected");
1503 cb.checked = false;
1504 } else {
1505 row.addClassName("Selected");
1506 cb.checked = true;
1507 }
1508 }
1509 }
1510 }
1511 }
1512
1513 } catch (e) {
1514 exception_error("selectTableRows", e);
1515
1516 }
1517 }
1518
1519 function getSelectedTableRowIds(id) {
1520 var rows = [];
1521
1522 try {
1523 var elem_rows = $(id).rows;
1524
1525 for (i = 0; i < elem_rows.length; i++) {
1526 if (elem_rows[i].hasClassName("Selected")) {
1527 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1528 rows.push(bare_id);
1529 }
1530 }
1531
1532 } catch (e) {
1533 exception_error("getSelectedTableRowIds", e);
1534 }
1535
1536 return rows;
1537 }
1538