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