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