]> git.wh0rd.org - tt-rss.git/blame_incremental - functions.js
better feed url checking
[tt-rss.git] / functions.js
... / ...
CommitLineData
1var hotkeys_enabled = true;
2var notify_silent = false;
3var loading_progress = 0;
4var sanity_check_done = false;
5
6/* add method to remove element from array */
7
8Array.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
16if (!window.console) console = {};
17console.log = console.log || function(msg) { };
18console.warn = console.warn || function(msg) { };
19console.error = console.error || function(msg) { };
20
21function 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
66function 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
88function disableHotkeys() {
89 hotkeys_enabled = false;
90}
91
92function enableHotkeys() {
93 hotkeys_enabled = true;
94}
95
96function param_escape(arg) {
97 if (typeof encodeURIComponent != 'undefined')
98 return encodeURIComponent(arg);
99 else
100 return escape(arg);
101}
102
103function param_unescape(arg) {
104 if (typeof decodeURIComponent != 'undefined')
105 return decodeURIComponent(arg);
106 else
107 return unescape(arg);
108}
109
110var notify_hide_timerid = false;
111
112function hide_notify() {
113 var n = $("notify");
114 if (n) {
115 n.style.display = "none";
116 }
117}
118
119function notify_silent_next() {
120 notify_silent = true;
121}
122
123function 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
183function notify(msg, no_hide) {
184 notify_real(msg, no_hide, 1);
185}
186
187function notify_progress(msg, no_hide) {
188 notify_real(msg, no_hide, 2);
189}
190
191function notify_error(msg, no_hide) {
192 notify_real(msg, no_hide, 3);
193
194}
195
196function notify_info(msg, no_hide) {
197 notify_real(msg, no_hide, 4);
198}
199
200function 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
215function 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
223function 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
233function 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
252function gotoPreferences() {
253 document.location.href = "prefs.php";
254}
255
256function gotoMain() {
257 document.location.href = "tt-rss.php";
258}
259
260function 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
299function toggleSelectRowById(sender, id) {
300 var row = $(id);
301 return toggleSelectRow(sender, row);
302}
303
304function toggleSelectListRow(sender) {
305 var row = sender.parentNode;
306 return toggleSelectRow(sender, row);
307}
308
309/* this is for dijit Checkbox */
310function toggleSelectListRow2(sender) {
311 var row = sender.domNode.parentNode;
312 return toggleSelectRow(sender, row);
313}
314
315function tSR(sender, row) {
316 return toggleSelectRow(sender, row);
317}
318
319/* this is for dijit Checkbox */
320function toggleSelectRow2(sender, row) {
321
322 if (!row) row = sender.domNode.parentNode.parentNode;
323
324 if (sender.checked && !row.hasClassName('Selected'))
325 row.addClassName('Selected');
326 else
327 row.removeClassName('Selected');
328}
329
330
331function toggleSelectRow(sender, row) {
332
333 if (!row) row = sender.parentNode.parentNode;
334
335 if (sender.checked && !row.hasClassName('Selected'))
336 row.addClassName('Selected');
337 else
338 row.removeClassName('Selected');
339}
340
341function checkboxToggleElement(elem, id) {
342 if (elem.checked) {
343 Effect.Appear(id, {duration : 0.5});
344 } else {
345 Effect.Fade(id, {duration : 0.5});
346 }
347}
348
349function dropboxSelect(e, v) {
350 for (i = 0; i < e.length; i++) {
351 if (e[i].value == v) {
352 e.selectedIndex = i;
353 break;
354 }
355 }
356}
357
358// originally stolen from http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z
359// bugfixed just a little bit :-)
360function getURLParam(strParamName){
361 var strReturn = "";
362 var strHref = window.location.href;
363
364 if (strHref.indexOf("#") == strHref.length-1) {
365 strHref = strHref.substring(0, strHref.length-1);
366 }
367
368 if ( strHref.indexOf("?") > -1 ){
369 var strQueryString = strHref.substr(strHref.indexOf("?"));
370 var aQueryString = strQueryString.split("&");
371 for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
372 if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
373 var aParam = aQueryString[iParam].split("=");
374 strReturn = aParam[1];
375 break;
376 }
377 }
378 }
379 return strReturn;
380}
381
382function leading_zero(p) {
383 var s = String(p);
384 if (s.length == 1) s = "0" + s;
385 return s;
386}
387
388function make_timestamp() {
389 var d = new Date();
390
391 return leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
392 ":" + leading_zero(d.getSeconds());
393}
394
395
396function closeErrorBox() {
397
398 if (Element.visible("errorBoxShadow")) {
399 Element.hide("dialog_overlay");
400 Element.hide("errorBoxShadow");
401
402 enableHotkeys();
403 }
404
405 return false;
406}
407
408function closeInfoBox(cleanup) {
409 try {
410 enableHotkeys();
411
412 dialog = dijit.byId("infoBox");
413
414 if (dialog) dialog.hide();
415
416 } catch (e) {
417 //exception_error("closeInfoBox", e);
418 }
419 return false;
420}
421
422
423function displayDlg(id, param, callback) {
424
425 notify_progress("Loading, please wait...", true);
426
427 var query = "?op=dlg&id=" +
428 param_escape(id) + "&param=" + param_escape(param);
429
430 new Ajax.Request("backend.php", {
431 parameters: query,
432 onComplete: function (transport) {
433 infobox_callback2(transport);
434 if (callback) callback(transport);
435 } });
436
437 return false;
438}
439
440function infobox_submit_callback2(transport) {
441 closeInfoBox();
442
443 try {
444 // called from prefs, reload tab
445 if (typeof active_tab != 'undefined' && active_tab) {
446 selectTab(active_tab, false);
447 }
448 } catch (e) { }
449
450 if (transport.responseText) {
451 notify_info(transport.responseText);
452 }
453}
454
455function infobox_callback2(transport) {
456 try {
457 var dialog = false;
458
459 if (dijit.byId("infoBox")) {
460 dialog = dijit.byId("infoBox");
461 }
462
463 //console.log("infobox_callback2");
464 notify('');
465
466 var content;
467 var dtitle = "Dialog";
468
469 var dlg = transport.responseXML.getElementsByTagName("dlg")[0];
470
471 var title = transport.responseXML.getElementsByTagName("title")[0];
472 if (title)
473 title = title.firstChild.nodeValue;
474
475 var content = transport.responseXML.getElementsByTagName("content")[0];
476
477 content = content.firstChild.nodeValue;
478
479 if (!dialog) {
480 dialog = new dijit.Dialog({
481 title: title,
482 id: 'infoBox',
483 style: "width: 600px",
484 onCancel: function() {
485 return true;
486 },
487 onExecute: function() {
488 return true;
489 },
490 onClose: function() {
491 return true;
492 },
493 content: content});
494 } else {
495 dialog.attr('title', title);
496 dialog.attr('content', content);
497 }
498
499 dialog.show();
500
501 notify("");
502 } catch (e) {
503 exception_error("infobox_callback2", e);
504 }
505}
506
507function createFilter() {
508
509 try {
510
511 var form = document.forms['filter_add_form'];
512 var reg_exp = form.reg_exp.value;
513
514 if (reg_exp == "") {
515 alert(__("Can't add filter: nothing to match on."));
516 return false;
517 }
518
519 var query = "?op=rpc&subop=verifyRegexp&reg_exp=" + param_escape(reg_exp);
520
521 notify_progress("Verifying regular expression...");
522
523 new Ajax.Request("backend.php", {
524 parameters: query,
525 onComplete: function(transport) {
526 handle_rpc_reply(transport);
527
528 var response = transport.responseXML;
529
530 if (response) {
531 var s = response.getElementsByTagName("status")[0].firstChild.nodeValue;
532
533 notify('');
534
535 if (s == "INVALID") {
536 alert("Match regular expression seems to be invalid.");
537 return;
538 } else {
539
540 var query = Form.serialize("filter_add_form");
541
542 // we can be called from some other tab in Prefs
543 if (typeof active_tab != 'undefined' && active_tab) {
544 active_tab = "filterConfig";
545 }
546
547 new Ajax.Request("backend.php?" + query, {
548 onComplete: function (transport) {
549 infobox_submit_callback2(transport);
550 } });
551
552 return true;
553 }
554 }
555
556 } });
557
558 } catch (e) {
559 exception_error("createFilter", e);
560 }
561}
562
563function isValidURL(s) {
564 return s.match("http://") != null || s.match("https://") != null || s.match("feed://") != null;
565}
566
567function subscribeToFeed() {
568
569 try {
570
571 var form = document.forms['feed_add_form'];
572 var feed_url = form.feed_url.value;
573
574 if (feed_url == "") {
575 alert(__("Can't subscribe: no feed URL given."));
576 return false;
577 }
578
579 notify_progress(__("Subscribing to feed..."), true);
580
581 var query = Form.serialize("feed_add_form");
582
583 console.log("subscribe q: " + query);
584
585 Form.disable("feed_add_form");
586
587 new Ajax.Request("backend.php", {
588 parameters: query,
589 onComplete: function(transport) {
590 try {
591
592 if (!transport.responseXML) {
593 console.log(transport.responseText);
594 alert(__("Server error while trying to subscribe to specified feed."));
595 return;
596 }
597
598 var result = transport.responseXML.getElementsByTagName('result')[0];
599 var rc = parseInt(result.getAttribute('code'));
600
601 Form.enable("feed_add_form");
602
603 notify('');
604
605 switch (rc) {
606 case 1:
607 closeInfoBox();
608 notify_info(__("Subscribed to %s").replace("%s", feed_url));
609
610 if (inPreferences()) {
611 updateFeedList();
612 } else {
613 setTimeout('updateFeedList(false, false)', 50);
614 }
615 break;
616 case 2:
617 alert(__("Specified URL seems to be invalid."));
618 break;
619 case 3:
620 alert(__("Specified URL doesn't seem to contain any feeds."));
621 break;
622 case 4:
623 new Ajax.Request("backend.php", {
624 parameters: 'op=rpc&subop=extractfeedurls&url=' + encodeURIComponent(feed_url),
625 onComplete: function(transport) {
626 var result = transport.responseXML.getElementsByTagName('urls')[0];
627 var feeds = JSON.parse(result.firstChild.nodeValue);
628 var select = document.getElementById("faad_feeds_container_select");
629
630 while (select.hasChildNodes()) {
631 select.removeChild(elem.firstChild);
632 }
633 var count = 0;
634 for (var feedUrl in feeds) {
635 select.insert(new Option(feeds[feedUrl], feedUrl, false));
636 count++;
637 }
638 if (count > 5) count = 5;
639 select.size = count;
640
641 Effect.Appear('fadd_feeds_container', {duration : 0.5});
642 }
643 });
644 break;
645 case 5:
646 alert(__("Couldn't download the specified URL."));
647 break;
648 case 0:
649 alert(__("You are already subscribed to this feed."));
650 break;
651 }
652
653 } catch (e) {
654 exception_error("subscribeToFeed", e);
655 }
656
657 } });
658
659 } catch (e) {
660 exception_error("subscribeToFeed", e);
661 }
662
663 return false;
664}
665
666function filterCR(e, f)
667{
668 var key;
669
670 if(window.event)
671 key = window.event.keyCode; //IE
672 else
673 key = e.which; //firefox
674
675 if (key == 13) {
676 if (typeof f != 'undefined') {
677 f();
678 return false;
679 } else {
680 return false;
681 }
682 } else {
683 return true;
684 }
685}
686
687function getInitParam(key) {
688 return init_params[key];
689}
690
691function setInitParam(key, value) {
692 init_params[key] = value;
693}
694
695function fatalError(code, msg, ext_info) {
696 try {
697
698 if (!ext_info) ext_info = "N/A";
699
700 if (code == 6) {
701 window.location.href = "tt-rss.php";
702 } else if (code == 5) {
703 window.location.href = "db-updater.php";
704 } else {
705
706 if (msg == "") msg = "Unknown error";
707
708 var ebc = $("xebContent");
709
710 if (ebc) {
711
712 Element.show("dialog_overlay");
713 Element.show("errorBoxShadow");
714 Element.hide("xebBtn");
715
716 if (ext_info) {
717 if (ext_info.responseText) {
718 ext_info = ext_info.responseText;
719 }
720 }
721
722 ebc.innerHTML =
723 "<div><b>Error message:</b></div>" +
724 "<pre>" + msg + "</pre>" +
725 "<div><b>Additional information:</b></div>" +
726 "<textarea readonly=\"1\">" + ext_info + "</textarea>";
727 }
728 }
729
730 } catch (e) {
731 exception_error("fatalError", e);
732 }
733}
734
735function filterDlgCheckType(sender) {
736
737 try {
738
739 var ftype = sender.value;
740
741 // if selected filter type is 5 (Date) enable the modifier dropbox
742 if (ftype == 5) {
743 Element.show("filterDlg_dateModBox");
744 Element.show("filterDlg_dateChkBox");
745 } else {
746 Element.hide("filterDlg_dateModBox");
747 Element.hide("filterDlg_dateChkBox");
748
749 }
750
751 } catch (e) {
752 exception_error("filterDlgCheckType", e);
753 }
754
755}
756
757function filterDlgCheckAction(sender) {
758
759 try {
760
761 var action = sender.value;
762
763 var action_param = $("filterDlg_paramBox");
764
765 if (!action_param) {
766 console.log("filterDlgCheckAction: can't find action param box!");
767 return;
768 }
769
770 // if selected action supports parameters, enable params field
771 if (action == 4 || action == 6 || action == 7) {
772 new Effect.Appear(action_param, {duration : 0.5});
773 if (action != 7) {
774 Element.show(dijit.byId("filterDlg_actionParam").domNode);
775 Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
776 } else {
777 Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
778 Element.hide(dijit.byId("filterDlg_actionParam").domNode);
779 }
780 } else {
781 Element.hide(action_param);
782 }
783
784 } catch (e) {
785 exception_error("filterDlgCheckAction", e);
786 }
787
788}
789
790function filterDlgCheckDate() {
791 try {
792 var dialog = dijit.byId("filterEditDlg");
793
794 var reg_exp = dialog.attr('value').reg_exp;
795
796 var query = "?op=rpc&subop=checkDate&date=" + reg_exp;
797
798 new Ajax.Request("backend.php", {
799 parameters: query,
800 onComplete: function(transport) {
801
802 if (transport.responseXML) {
803 var result = transport.responseXML.getElementsByTagName("result")[0];
804
805 if (result && result.firstChild) {
806 if (result.firstChild.nodeValue == "1") {
807 alert(__("Date syntax appears to be correct."));
808 return;
809 }
810 }
811 }
812
813 alert(__("Date syntax is incorrect."));
814
815 } });
816
817
818 } catch (e) {
819 exception_error("filterDlgCheckDate", e);
820 }
821}
822
823function explainError(code) {
824 return displayDlg("explainError", code);
825}
826
827function displayHelpInfobox(topic_id) {
828
829 var url = "backend.php?op=help&tid=" + param_escape(topic_id);
830
831 var w = window.open(url, "ttrss_help",
832 "status=0,toolbar=0,location=0,width=450,height=500,scrollbars=1,menubar=0");
833
834}
835
836function loading_set_progress(p) {
837 try {
838 loading_progress += p;
839
840 if (dijit.byId("loading_bar"))
841 dijit.byId("loading_bar").update({progress: loading_progress});
842
843 if (loading_progress >= 90)
844 remove_splash();
845
846 } catch (e) {
847 exception_error("loading_set_progress", e);
848 }
849}
850
851function remove_splash() {
852
853 if (Element.visible("overlay")) {
854 console.log("about to remove splash, OMG!");
855 Element.hide("overlay");
856 console.log("removed splash!");
857 }
858}
859
860function getSelectedFeedsFromBrowser() {
861
862 var list = $$("#browseFeedList li[id*=FBROW]");
863
864 var selected = new Array();
865
866 list.each(function(child) {
867 var id = child.id.replace("FBROW-", "");
868 var cb = $("FBCHK-" + id);
869
870 if (cb.checked) {
871 selected.push(id);
872 }
873 });
874
875 return selected;
876}
877
878function updateFeedBrowser() {
879 try {
880
881 var query = Form.serialize("feed_browser");
882
883 Element.show('feed_browser_spinner');
884
885 new Ajax.Request("backend.php", {
886 parameters: query,
887 onComplete: function(transport) {
888 notify('');
889
890 Element.hide('feed_browser_spinner');
891
892 var c = $("browseFeedList");
893 var r = transport.responseXML.getElementsByTagName("content")[0];
894 var nr = transport.responseXML.getElementsByTagName("num-results")[0];
895 var mode = transport.responseXML.getElementsByTagName("mode")[0];
896
897 if (c && r) {
898 c.innerHTML = r.firstChild.nodeValue;
899 }
900
901 if (parseInt(mode.getAttribute("value")) == 2) {
902 Element.show('feed_archive_remove');
903 } else {
904 Element.hide('feed_archive_remove');
905 }
906
907 } });
908
909 } catch (e) {
910 exception_error("updateFeedBrowser", e);
911 }
912
913}
914
915function transport_error_check(transport) {
916 try {
917 if (transport.responseXML) {
918 var error = transport.responseXML.getElementsByTagName("error")[0];
919
920 if (error) {
921 var code = error.getAttribute("error-code");
922 var msg = error.getAttribute("error-msg");
923 if (code != 0) {
924 fatalError(code, msg);
925 return false;
926 }
927 }
928 }
929 } catch (e) {
930 exception_error("check_for_error_xml", e);
931 }
932 return true;
933}
934
935function strip_tags(s) {
936 return s.replace(/<\/?[^>]+(>|$)/g, "");
937}
938
939function truncate_string(s, length) {
940 if (!length) length = 30;
941 var tmp = s.substring(0, length);
942 if (s.length > length) tmp += "&hellip;";
943 return tmp;
944}
945
946function hotkey_prefix_timeout() {
947 try {
948
949 var date = new Date();
950 var ts = Math.round(date.getTime() / 1000);
951
952 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
953 console.log("hotkey_prefix seems to be stuck, aborting");
954 hotkey_prefix_pressed = false;
955 hotkey_prefix = false;
956 Element.hide('cmdline');
957 }
958
959 setTimeout("hotkey_prefix_timeout()", 1000);
960
961 } catch (e) {
962 exception_error("hotkey_prefix_timeout", e);
963 }
964}
965
966function hideAuxDlg() {
967 try {
968 Element.hide('auxDlg');
969 } catch (e) {
970 exception_error("hideAuxDlg", e);
971 }
972}
973
974function feedBrowserSubscribe() {
975 try {
976
977 var selected = getSelectedFeedsFromBrowser();
978
979 var mode = document.forms['feed_browser'].mode;
980
981 mode = mode[mode.selectedIndex].value;
982
983 if (selected.length > 0) {
984 closeInfoBox();
985
986 notify_progress("Loading, please wait...", true);
987
988 var query = "?op=rpc&subop=massSubscribe&ids="+
989 param_escape(selected.toString()) + "&mode=" + param_escape(mode);
990
991 new Ajax.Request("backend.php", {
992 parameters: query,
993 onComplete: function(transport) {
994
995 var nf = transport.responseXML.getElementsByTagName('num-feeds')[0];
996 var nf_value = nf.getAttribute("value");
997
998 notify_info(__("Subscribed to %d feed(s).").replace("%d", nf_value));
999
1000 if (inPreferences()) {
1001 updateFeedList();
1002 } else {
1003 setTimeout('updateFeedList(false, false)', 50);
1004 }
1005 } });
1006
1007 } else {
1008 alert(__("No feeds are selected."));
1009 }
1010
1011 } catch (e) {
1012 exception_error("feedBrowserSubscribe", e);
1013 }
1014}
1015
1016function feedArchiveRemove() {
1017 try {
1018
1019 var selected = getSelectedFeedsFromBrowser();
1020
1021 if (selected.length > 0) {
1022
1023 var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1024
1025 if (confirm(pr)) {
1026 Element.show('feed_browser_spinner');
1027
1028 var query = "?op=rpc&subop=remarchived&ids=" +
1029 param_escape(selected.toString());;
1030
1031 new Ajax.Request("backend.php", {
1032 parameters: query,
1033 onComplete: function(transport) {
1034 updateFeedBrowser();
1035 } });
1036 }
1037
1038 } else {
1039 alert(__("No feeds are selected."));
1040 }
1041
1042 } catch (e) {
1043 exception_error("feedArchiveRemove", e);
1044 }
1045}
1046
1047function uploadIconHandler(rc) {
1048 try {
1049 switch (rc) {
1050 case 0:
1051 notify_info("Upload complete.");
1052 if (inPreferences()) {
1053 updateFeedList();
1054 } else {
1055 setTimeout('updateFeedList(false, false)', 50);
1056 }
1057 break;
1058 case 1:
1059 notify_error("Upload failed: icon is too big.");
1060 break;
1061 case 2:
1062 notify_error("Upload failed.");
1063 break;
1064 }
1065
1066 } catch (e) {
1067 exception_error("uploadIconHandler", e);
1068 }
1069}
1070
1071function removeFeedIcon(id) {
1072
1073 try {
1074
1075 if (confirm(__("Remove stored feed icon?"))) {
1076 var query = "backend.php?op=pref-feeds&subop=removeicon&feed_id=" + param_escape(id);
1077
1078 console.log(query);
1079
1080 notify_progress("Removing feed icon...", true);
1081
1082 new Ajax.Request("backend.php", {
1083 parameters: query,
1084 onComplete: function(transport) {
1085 notify_info("Feed icon removed.");
1086 if (inPreferences()) {
1087 updateFeedList();
1088 } else {
1089 setTimeout('updateFeedList(false, false)', 50);
1090 }
1091 } });
1092 }
1093
1094 return false;
1095 } catch (e) {
1096 exception_error("uploadFeedIcon", e);
1097 }
1098}
1099
1100function uploadFeedIcon() {
1101
1102 try {
1103
1104 var file = $("icon_file");
1105
1106 if (file.value.length == 0) {
1107 alert(__("Please select an image file to upload."));
1108 } else {
1109 if (confirm(__("Upload new icon for this feed?"))) {
1110 notify_progress("Uploading, please wait...", true);
1111 return true;
1112 }
1113 }
1114
1115 return false;
1116
1117 } catch (e) {
1118 exception_error("uploadFeedIcon", e);
1119 }
1120}
1121
1122function addLabel(select, callback) {
1123
1124 try {
1125
1126 var caption = prompt(__("Please enter label caption:"), "");
1127
1128 if (caption != undefined) {
1129
1130 if (caption == "") {
1131 alert(__("Can't create label: missing caption."));
1132 return false;
1133 }
1134
1135 var query = "?op=pref-labels&subop=add&caption=" +
1136 param_escape(caption);
1137
1138 if (select)
1139 query += "&output=select";
1140
1141 notify_progress("Loading, please wait...", true);
1142
1143 if (inPreferences() && !select) active_tab = "labelConfig";
1144
1145 new Ajax.Request("backend.php", {
1146 parameters: query,
1147 onComplete: function(transport) {
1148 if (callback) {
1149 callback(transport);
1150 } else if (inPreferences()) {
1151 infobox_submit_callback2(transport);
1152 } else {
1153 updateFeedList();
1154 }
1155 } });
1156
1157 }
1158
1159 } catch (e) {
1160 exception_error("addLabel", e);
1161 }
1162}
1163
1164function quickAddFeed() {
1165 displayDlg('quickAddFeed', '',
1166 function () {$('feed_url').focus();});
1167}
1168
1169function quickAddFilter() {
1170 try {
1171 var query = "backend.php?op=dlg&id=quickAddFilter";
1172
1173 if (dijit.byId("filterEditDlg"))
1174 dijit.byId("filterEditDlg").destroyRecursive();
1175
1176 dialog = new dijit.Dialog({
1177 id: "filterEditDlg",
1178 title: __("Create Filter"),
1179 style: "width: 600px",
1180 execute: function() {
1181 if (this.validate()) {
1182
1183 var query = "?op=rpc&subop=verifyRegexp&reg_exp=" +
1184 param_escape(dialog.attr('value').reg_exp);
1185
1186 notify_progress("Verifying regular expression...");
1187
1188 new Ajax.Request("backend.php", {
1189 parameters: query,
1190 onComplete: function(transport) {
1191 handle_rpc_reply(transport);
1192 var response = transport.responseXML;
1193
1194 if (response) {
1195 var s = response.getElementsByTagName("status")[0].firstChild.nodeValue;
1196
1197 notify('');
1198
1199 if (s == "INVALID") {
1200 alert("Match regular expression seems to be invalid.");
1201 return;
1202 } else {
1203 notify_progress("Saving data...", true);
1204
1205 console.log(dojo.objectToQuery(dialog.attr('value')));
1206
1207 new Ajax.Request("backend.php", {
1208 parameters: dojo.objectToQuery(dialog.attr('value')),
1209 onComplete: function(transport) {
1210 dialog.hide();
1211 notify_info(transport.responseText);
1212 if (inPreferences()) {
1213 updateFilterList();
1214 }
1215 }})
1216 }
1217 }
1218 }});
1219 }
1220 },
1221 href: query});
1222
1223 dialog.show();
1224 } catch (e) {
1225 exception_error("quickAddFilter", e);
1226 }
1227}
1228
1229function unsubscribeFeed(feed_id, title) {
1230
1231 var msg = __("Unsubscribe from %s?").replace("%s", title);
1232
1233 if (title == undefined || confirm(msg)) {
1234 notify_progress("Removing feed...");
1235
1236 var query = "?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id;
1237
1238 new Ajax.Request("backend.php", {
1239 parameters: query,
1240 onComplete: function(transport) {
1241
1242 closeInfoBox();
1243
1244 if (inPreferences()) {
1245 updateFeedList();
1246 } else {
1247 dlg_frefresh_callback(transport, feed_id);
1248 }
1249
1250 } });
1251 }
1252
1253 return false;
1254}
1255
1256
1257function backend_sanity_check_callback(transport) {
1258
1259 try {
1260
1261 if (sanity_check_done) {
1262 fatalError(11, "Sanity check request received twice. This can indicate "+
1263 "presence of Firebug or some other disrupting extension. "+
1264 "Please disable it and try again.");
1265 return;
1266 }
1267
1268 if (!transport.responseXML) {
1269 if (!store) {
1270 fatalError(3, "Sanity check: Received reply is not XML",
1271 transport.responseText);
1272 return;
1273 }
1274 }
1275
1276 var reply = transport.responseXML.getElementsByTagName("error")[0];
1277
1278 if (!reply) {
1279 fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1280 return;
1281 }
1282
1283 var error_code = reply.getAttribute("error-code");
1284
1285 if (error_code && error_code != 0) {
1286 return fatalError(error_code, reply.getAttribute("error-msg"));
1287 }
1288
1289 console.log("sanity check ok");
1290
1291 var params = transport.responseXML.getElementsByTagName("init-params")[0];
1292
1293 if (params) {
1294 console.log('reading init-params...');
1295
1296 params = JSON.parse(params.firstChild.nodeValue);
1297
1298 if (params) {
1299 for (k in params) {
1300 var v = params[k];
1301 console.log("IP: " + k + " => " + v);
1302 }
1303 }
1304
1305 init_params = params;
1306 }
1307
1308 sanity_check_done = true;
1309
1310 init_second_stage();
1311
1312 } catch (e) {
1313 exception_error("backend_sanity_check_callback", e, transport);
1314 }
1315}
1316
1317function has_local_storage() {
1318 return false;
1319/* try {
1320 return 'localStorage' in window && window['localStorage'] != null;
1321 } catch (e) {
1322 return false;
1323 } */
1324}
1325
1326function catSelectOnChange(elem) {
1327 try {
1328/* var value = elem[elem.selectedIndex].value;
1329 var def = elem.getAttribute('default');
1330
1331 if (value == "ADD_CAT") {
1332
1333 if (def)
1334 dropboxSelect(elem, def);
1335 else
1336 elem.selectedIndex = 0;
1337
1338 quickAddCat(elem);
1339 } */
1340
1341 } catch (e) {
1342 exception_error("catSelectOnChange", e);
1343 }
1344}
1345
1346function quickAddCat(elem) {
1347 try {
1348 var cat = prompt(__("Please enter category title:"));
1349
1350 if (cat) {
1351
1352 var query = "?op=rpc&subop=quickAddCat&cat=" + param_escape(cat);
1353
1354 notify_progress("Loading, please wait...", true);
1355
1356 new Ajax.Request("backend.php", {
1357 parameters: query,
1358 onComplete: function (transport) {
1359 var response = transport.responseXML;
1360 var select = response.getElementsByTagName("select")[0];
1361 var options = select.getElementsByTagName("option");
1362
1363 dropbox_replace_options(elem, options);
1364
1365 notify('');
1366
1367 } });
1368
1369 }
1370
1371 } catch (e) {
1372 exception_error("quickAddCat", e);
1373 }
1374}
1375
1376function genUrlChangeKey(feed, is_cat) {
1377
1378 try {
1379 var ok = confirm(__("Generate new syndication address for this feed?"));
1380
1381 if (ok) {
1382
1383 notify_progress("Trying to change address...", true);
1384
1385 var query = "?op=rpc&subop=regenFeedKey&id=" + param_escape(feed) +
1386 "&is_cat=" + param_escape(is_cat);
1387
1388 new Ajax.Request("backend.php", {
1389 parameters: query,
1390 onComplete: function(transport) {
1391 var new_link = transport.responseXML.getElementsByTagName("link")[0];
1392
1393 var e = $('gen_feed_url');
1394
1395 if (new_link) {
1396
1397 new_link = new_link.firstChild.nodeValue;
1398
1399 e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1400 "&amp;key=" + new_link);
1401
1402 e.href = e.href.replace(/\&amp;key=.*$/,
1403 "&amp;key=" + new_link);
1404
1405 new Effect.Highlight(e);
1406
1407 notify('');
1408
1409 } else {
1410 notify_error("Could not change feed URL.");
1411 }
1412 } });
1413 }
1414 } catch (e) {
1415 exception_error("genUrlChangeKey", e);
1416 }
1417 return false;
1418}
1419
1420function labelSelectOnChange(elem) {
1421 try {
1422/* var value = elem[elem.selectedIndex].value;
1423 var def = elem.getAttribute('default');
1424
1425 if (value == "ADD_LABEL") {
1426
1427 if (def)
1428 dropboxSelect(elem, def);
1429 else
1430 elem.selectedIndex = 0;
1431
1432 addLabel(elem, function(transport) {
1433
1434 try {
1435
1436 var response = transport.responseXML;
1437 var select = response.getElementsByTagName("select")[0];
1438 var options = select.getElementsByTagName("option");
1439
1440 dropbox_replace_options(elem, options);
1441
1442 notify('');
1443 } catch (e) {
1444 exception_error("addLabel", e);
1445 }
1446 });
1447 } */
1448
1449 } catch (e) {
1450 exception_error("labelSelectOnChange", e);
1451 }
1452}
1453
1454function dropbox_replace_options(elem, options) {
1455
1456 try {
1457 while (elem.hasChildNodes())
1458 elem.removeChild(elem.firstChild);
1459
1460 var sel_idx = -1;
1461
1462 for (var i = 0; i < options.length; i++) {
1463 var text = options[i].firstChild.nodeValue;
1464 var value = options[i].getAttribute("value");
1465
1466 if (value == undefined) value = text;
1467
1468 var issel = options[i].getAttribute("selected") == "1";
1469
1470 var option = new Option(text, value, issel);
1471
1472 if (options[i].getAttribute("disabled"))
1473 option.setAttribute("disabled", true);
1474
1475 elem.insert(option);
1476
1477 if (issel) sel_idx = i;
1478 }
1479
1480 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1481 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1482
1483 } catch (e) {
1484 exception_error("dropbox_replace_options", e);
1485 }
1486}
1487
1488// mode = all, none, invert
1489function selectTableRows(id, mode) {
1490 try {
1491 var rows = $(id).rows;
1492
1493 for (var i = 0; i < rows.length; i++) {
1494 var row = rows[i];
1495 var cb = false;
1496
1497 if (row.id && row.className) {
1498 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1499 var inputs = rows[i].getElementsByTagName("input");
1500
1501 for (var j = 0; j < inputs.length; j++) {
1502 var input = inputs[j];
1503
1504 if (input.getAttribute("type") == "checkbox" &&
1505 input.id.match(bare_id)) {
1506
1507 cb = input;
1508 break;
1509 }
1510 }
1511
1512 if (cb) {
1513 var issel = row.hasClassName("Selected");
1514
1515 if (mode == "all" && !issel) {
1516 row.addClassName("Selected");
1517 cb.checked = true;
1518 } else if (mode == "none" && issel) {
1519 row.removeClassName("Selected");
1520 cb.checked = false;
1521 } else if (mode == "invert") {
1522
1523 if (issel) {
1524 row.removeClassName("Selected");
1525 cb.checked = false;
1526 } else {
1527 row.addClassName("Selected");
1528 cb.checked = true;
1529 }
1530 }
1531 }
1532 }
1533 }
1534
1535 } catch (e) {
1536 exception_error("selectTableRows", e);
1537
1538 }
1539}
1540
1541function getSelectedTableRowIds(id) {
1542 var rows = [];
1543
1544 try {
1545 var elem_rows = $(id).rows;
1546
1547 for (i = 0; i < elem_rows.length; i++) {
1548 if (elem_rows[i].hasClassName("Selected")) {
1549 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1550 rows.push(bare_id);
1551 }
1552 }
1553
1554 } catch (e) {
1555 exception_error("getSelectedTableRowIds", e);
1556 }
1557
1558 return rows;
1559}
1560
1561function editFeed(feed, event) {
1562 try {
1563 if (feed <= 0)
1564 return alert(__("You can't edit this kind of feed."));
1565
1566 var query = "backend.php?op=pref-feeds&subop=editfeed&id=" +
1567 param_escape(feed);
1568
1569 console.log(query);
1570
1571 if (dijit.byId("feedEditDlg"))
1572 dijit.byId("feedEditDlg").destroyRecursive();
1573
1574 dialog = new dijit.Dialog({
1575 id: "feedEditDlg",
1576 title: __("Edit Feed"),
1577 style: "width: 600px",
1578 execute: function() {
1579 if (this.validate()) {
1580 console.log(dojo.objectToQuery(this.attr('value')));
1581
1582 notify_progress("Saving data...", true);
1583
1584 new Ajax.Request("backend.php", {
1585 parameters: dojo.objectToQuery(dialog.attr('value')),
1586 onComplete: function(transport) {
1587 dialog.hide();
1588 if (inPreferences()) {
1589 updateFeedList();
1590 } else {
1591 notify('');
1592 dlg_frefresh_callback(transport);
1593 }
1594 }})
1595 }
1596 },
1597 href: query});
1598
1599 dialog.show();
1600
1601 } catch (e) {
1602 exception_error("editFeed", e);
1603 }
1604}
1605
1606