]> git.wh0rd.org Git - tt-rss.git/blob - js/functions.js
db: remove qlog stuff
[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 gotoLogout() {
296         document.location.href = "backend.php?op=logout";
297 }
298
299 function gotoMain() {
300         document.location.href = "index.php";
301 }
302
303 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
304   * * @author Sundar Dorai-Raj
305   * * Email: sdoraira@vt.edu
306   * * This program is free software; you can redistribute it and/or
307   * * modify it under the terms of the GNU General Public License
308   * * as published by the Free Software Foundation; either version 2
309   * * of the License, or (at your option) any later version,
310   * * provided that any use properly credits the author.
311   * * This program is distributed in the hope that it will be useful,
312   * * but WITHOUT ANY WARRANTY; without even the implied warranty of
313   * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
314   * * GNU General Public License for more details at http://www.gnu.org * * */
315
316   var numbers=".0123456789";
317   function isNumeric(x) {
318     // is x a String or a character?
319     if(x.length>1) {
320       // remove negative sign
321       x=Math.abs(x)+"";
322       for(var j=0;j<x.length;j++) {
323         // call isNumeric recursively for each character
324         number=isNumeric(x.substring(j,j+1));
325         if(!number) return number;
326       }
327       return number;
328     }
329     else {
330       // if x is number return true
331       if(numbers.indexOf(x)>=0) return true;
332       return false;
333     }
334   }
335
336
337 function toggleSelectRowById(sender, id) {
338         var row = $(id);
339         return toggleSelectRow(sender, row);
340 }
341
342 function toggleSelectListRow(sender) {
343         var row = sender.parentNode;
344         return toggleSelectRow(sender, row);
345 }
346
347 /* this is for dijit Checkbox */
348 function toggleSelectListRow2(sender) {
349         var row = sender.domNode.parentNode;
350         return toggleSelectRow(sender, row);
351 }
352
353 /* this is for dijit Checkbox */
354 function toggleSelectRow2(sender, row) {
355
356         if (!row) row = sender.domNode.parentNode.parentNode;
357
358         if (sender.checked && !row.hasClassName('Selected'))
359                 row.addClassName('Selected');
360         else
361                 row.removeClassName('Selected');
362 }
363
364
365 function toggleSelectRow(sender, row) {
366
367         if (!row) row = sender.parentNode.parentNode;
368
369         if (sender.checked && !row.hasClassName('Selected'))
370                 row.addClassName('Selected');
371         else
372                 row.removeClassName('Selected');
373 }
374
375 function checkboxToggleElement(elem, id) {
376         if (elem.checked) {
377                 Effect.Appear(id, {duration : 0.5});
378         } else {
379                 Effect.Fade(id, {duration : 0.5});
380         }
381 }
382
383 function dropboxSelect(e, v) {
384         for (var i = 0; i < e.length; i++) {
385                 if (e[i].value == v) {
386                         e.selectedIndex = i;
387                         break;
388                 }
389         }
390 }
391
392 function getURLParam(param){
393         return String(window.location.href).parseQuery()[param];
394 }
395
396 function closeInfoBox(cleanup) {
397         try {
398                 dialog = dijit.byId("infoBox");
399
400                 if (dialog)     dialog.hide();
401
402         } catch (e) {
403                 //exception_error("closeInfoBox", e);
404         }
405         return false;
406 }
407
408
409 function displayDlg(id, param, callback) {
410
411         notify_progress("Loading, please wait...", true);
412
413         var query = "?op=dlg&method=" +
414                 param_escape(id) + "&param=" + param_escape(param);
415
416         new Ajax.Request("backend.php", {
417                 parameters: query,
418                 onComplete: function (transport) {
419                         infobox_callback2(transport);
420                         if (callback) callback(transport);
421                 } });
422
423         return false;
424 }
425
426 function infobox_callback2(transport) {
427         try {
428                 var dialog = false;
429
430                 if (dijit.byId("infoBox")) {
431                         dialog = dijit.byId("infoBox");
432                 }
433
434                 //console.log("infobox_callback2");
435                 notify('');
436
437                 var title = transport.responseXML.getElementsByTagName("title")[0];
438                 if (title)
439                         title = title.firstChild.nodeValue;
440
441                 var content = transport.responseXML.getElementsByTagName("content")[0];
442
443                 content = content.firstChild.nodeValue;
444
445                 if (!dialog) {
446                         dialog = new dijit.Dialog({
447                                 title: title,
448                                 id: 'infoBox',
449                                 style: "width: 600px",
450                                 onCancel: function() {
451                                         return true;
452                                 },
453                                 onExecute: function() {
454                                         return true;
455                                 },
456                                 onClose: function() {
457                                         return true;
458                                         },
459                                 content: content});
460                 } else {
461                         dialog.attr('title', title);
462                         dialog.attr('content', content);
463                 }
464
465                 dialog.show();
466
467                 notify("");
468         } catch (e) {
469                 exception_error("infobox_callback2", e);
470         }
471 }
472
473 function filterCR(e, f)
474 {
475      var key;
476
477      if(window.event)
478           key = window.event.keyCode;     //IE
479      else
480           key = e.which;     //firefox
481
482         if (key == 13) {
483                 if (typeof f != 'undefined') {
484                         f();
485                         return false;
486                 } else {
487                         return false;
488                 }
489         } else {
490                 return true;
491         }
492 }
493
494 function getInitParam(key) {
495         return init_params[key];
496 }
497
498 function setInitParam(key, value) {
499         init_params[key] = value;
500 }
501
502 function fatalError(code, msg, ext_info) {
503         try {
504
505                 if (code == 6) {
506                         window.location.href = "index.php";
507                 } else if (code == 5) {
508                         window.location.href = "db-updater.php";
509                 } else {
510
511                         if (msg == "") msg = "Unknown error";
512
513                         if (ext_info) {
514                                 if (ext_info.responseText) {
515                                         ext_info = ext_info.responseText;
516                                 }
517                         }
518
519                         if (ERRORS && ERRORS[code] && !msg) {
520                                 msg = ERRORS[code];
521                         }
522
523                         var content = "<div><b>Error code:</b> " + code + "</div>" +
524                                 "<p>" + msg + "</p>";
525
526                         if (ext_info) {
527                                 content = content + "<div><b>Additional information:</b></div>" +
528                                         "<textarea style='width: 100%' readonly=\"1\">" +
529                                         ext_info + "</textarea>";
530                         }
531
532                         var dialog = new dijit.Dialog({
533                                 title: "Fatal error",
534                                 style: "width: 600px",
535                                 content: content});
536
537                         dialog.show();
538
539                 }
540
541                 return false;
542
543         } catch (e) {
544                 exception_error("fatalError", e);
545         }
546 }
547
548 /* function filterDlgCheckType(sender) {
549
550         try {
551
552                 var ftype = sender.value;
553
554                 // if selected filter type is 5 (Date) enable the modifier dropbox
555                 if (ftype == 5) {
556                         Element.show("filterDlg_dateModBox");
557                         Element.show("filterDlg_dateChkBox");
558                 } else {
559                         Element.hide("filterDlg_dateModBox");
560                         Element.hide("filterDlg_dateChkBox");
561
562                 }
563
564         } catch (e) {
565                 exception_error("filterDlgCheckType", e);
566         }
567
568 } */
569
570 function filterDlgCheckAction(sender) {
571
572         try {
573
574                 var action = sender.value;
575
576                 var action_param = $("filterDlg_paramBox");
577
578                 if (!action_param) {
579                         console.log("filterDlgCheckAction: can't find action param box!");
580                         return;
581                 }
582
583                 // if selected action supports parameters, enable params field
584                 if (action == 4 || action == 6 || action == 7) {
585                         new Effect.Appear(action_param, {duration : 0.5});
586                         if (action != 7) {
587                                 Element.show(dijit.byId("filterDlg_actionParam").domNode);
588                                 Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
589                         } else {
590                                 Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
591                                 Element.hide(dijit.byId("filterDlg_actionParam").domNode);
592                         }
593                 } else {
594                         Element.hide(action_param);
595                 }
596
597         } catch (e) {
598                 exception_error("filterDlgCheckAction", e);
599         }
600
601 }
602
603 function filterDlgCheckDate() {
604         try {
605                 var dialog = dijit.byId("filterEditDlg");
606
607                 var reg_exp = dialog.attr('value').reg_exp;
608
609                 var query = "?op=rpc&method=checkDate&date=" + reg_exp;
610
611                 new Ajax.Request("backend.php", {
612                         parameters: query,
613                         onComplete: function(transport) {
614
615                                 var reply = JSON.parse(transport.responseText);
616
617                                 if (reply['result'] == true) {
618                                         alert(__("Date syntax appears to be correct:") + " " + reply['date']);
619                                         return;
620                                 } else {
621                                         alert(__("Date syntax is incorrect."));
622                                 }
623
624                         } });
625
626
627         } catch (e) {
628                 exception_error("filterDlgCheckDate", e);
629         }
630 }
631
632 function explainError(code) {
633         return displayDlg("explainError", code);
634 }
635
636 function loading_set_progress(p) {
637         try {
638                 loading_progress += p;
639
640                 if (dijit.byId("loading_bar"))
641                         dijit.byId("loading_bar").update({progress: loading_progress});
642
643                 if (loading_progress >= 90)
644                         remove_splash();
645
646         } catch (e) {
647                 exception_error("loading_set_progress", e);
648         }
649 }
650
651 function remove_splash() {
652
653         if (Element.visible("overlay")) {
654                 console.log("about to remove splash, OMG!");
655                 Element.hide("overlay");
656                 console.log("removed splash!");
657         }
658 }
659
660 function transport_error_check(transport) {
661         try {
662                 if (transport.responseXML) {
663                         var error = transport.responseXML.getElementsByTagName("error")[0];
664
665                         if (error) {
666                                 var code = error.getAttribute("error-code");
667                                 var msg = error.getAttribute("error-msg");
668                                 if (code != 0) {
669                                         fatalError(code, msg);
670                                         return false;
671                                 }
672                         }
673                 }
674         } catch (e) {
675                 exception_error("check_for_error_xml", e);
676         }
677         return true;
678 }
679
680 function strip_tags(s) {
681         return s.replace(/<\/?[^>]+(>|$)/g, "");
682 }
683
684 function truncate_string(s, length) {
685         if (!length) length = 30;
686         var tmp = s.substring(0, length);
687         if (s.length > length) tmp += "&hellip;";
688         return tmp;
689 }
690
691 function hotkey_prefix_timeout() {
692         try {
693
694                 var date = new Date();
695                 var ts = Math.round(date.getTime() / 1000);
696
697                 if (hotkey_prefix_pressed && ts - hotkey_prefix_pressed >= 5) {
698                         console.log("hotkey_prefix seems to be stuck, aborting");
699                         hotkey_prefix_pressed = false;
700                         hotkey_prefix = false;
701                         Element.hide('cmdline');
702                 }
703
704                 setTimeout("hotkey_prefix_timeout()", 1000);
705
706         } catch  (e) {
707                 exception_error("hotkey_prefix_timeout", e);
708         }
709 }
710
711 function hideAuxDlg() {
712         try {
713                 Element.hide('auxDlg');
714         } catch (e) {
715                 exception_error("hideAuxDlg", e);
716         }
717 }
718
719
720 function uploadIconHandler(rc) {
721         try {
722                 switch (rc) {
723                         case 0:
724                                 notify_info("Upload complete.");
725                                 if (inPreferences()) {
726                                         updateFeedList();
727                                 } else {
728                                         setTimeout('updateFeedList(false, false)', 50);
729                                 }
730                                 break;
731                         case 1:
732                                 notify_error("Upload failed: icon is too big.");
733                                 break;
734                         case 2:
735                                 notify_error("Upload failed.");
736                                 break;
737                 }
738
739         } catch (e) {
740                 exception_error("uploadIconHandler", e);
741         }
742 }
743
744 function removeFeedIcon(id) {
745
746         try {
747
748                 if (confirm(__("Remove stored feed icon?"))) {
749                         var query = "backend.php?op=pref-feeds&method=removeicon&feed_id=" + param_escape(id);
750
751                         console.log(query);
752
753                         notify_progress("Removing feed icon...", true);
754
755                         new Ajax.Request("backend.php", {
756                                 parameters: query,
757                                 onComplete: function(transport) {
758                                         notify_info("Feed icon removed.");
759                                         if (inPreferences()) {
760                                                 updateFeedList();
761                                         } else {
762                                                 setTimeout('updateFeedList(false, false)', 50);
763                                         }
764                                 } });
765                 }
766
767                 return false;
768         } catch (e) {
769                 exception_error("removeFeedIcon", e);
770         }
771 }
772
773 function uploadFeedIcon() {
774
775         try {
776
777                 var file = $("icon_file");
778
779                 if (file.value.length == 0) {
780                         alert(__("Please select an image file to upload."));
781                 } else {
782                         if (confirm(__("Upload new icon for this feed?"))) {
783                                 notify_progress("Uploading, please wait...", true);
784                                 return true;
785                         }
786                 }
787
788                 return false;
789
790         } catch (e) {
791                 exception_error("uploadFeedIcon", e);
792         }
793 }
794
795 function addLabel(select, callback) {
796
797         try {
798
799                 var caption = prompt(__("Please enter label caption:"), "");
800
801                 if (caption != undefined) {
802
803                         if (caption == "") {
804                                 alert(__("Can't create label: missing caption."));
805                                 return false;
806                         }
807
808                         var query = "?op=pref-labels&method=add&caption=" +
809                                 param_escape(caption);
810
811                         if (select)
812                                 query += "&output=select";
813
814                         notify_progress("Loading, please wait...", true);
815
816                         if (inPreferences() && !select) active_tab = "labelConfig";
817
818                         new Ajax.Request("backend.php", {
819                                 parameters: query,
820                                 onComplete: function(transport) {
821                                         if (callback) {
822                                                 callback(transport);
823                                         } else if (inPreferences()) {
824                                                 updateLabelList();
825                                         } else {
826                                                 updateFeedList();
827                                         }
828                         } });
829
830                 }
831
832         } catch (e) {
833                 exception_error("addLabel", e);
834         }
835 }
836
837 function quickAddFeed() {
838         try {
839                 var query = "backend.php?op=dlg&method=quickAddFeed";
840
841                 // overlapping widgets
842                 if (dijit.byId("batchSubDlg")) dijit.byId("batchSubDlg").destroyRecursive();
843                 if (dijit.byId("feedAddDlg"))   dijit.byId("feedAddDlg").destroyRecursive();
844
845                 var dialog = new dijit.Dialog({
846                         id: "feedAddDlg",
847                         title: __("Subscribe to Feed"),
848                         style: "width: 600px",
849                         execute: function() {
850                                 if (this.validate()) {
851                                         console.log(dojo.objectToQuery(this.attr('value')));
852
853                                         var feed_url = this.attr('value').feed;
854
855                                         Element.show("feed_add_spinner");
856
857                                         new Ajax.Request("backend.php", {
858                                                 parameters: dojo.objectToQuery(this.attr('value')),
859                                                 onComplete: function(transport) {
860                                                         try {
861
862                                                                 var reply = JSON.parse(transport.responseText);
863
864                                                                 var rc = reply['result'];
865
866                                                                 notify('');
867                                                                 Element.hide("feed_add_spinner");
868
869                                                                 console.log("GOT RC: " + rc);
870
871                                                                 switch (parseInt(rc['code'])) {
872                                                                 case 1:
873                                                                         dialog.hide();
874                                                                         notify_info(__("Subscribed to %s").replace("%s", feed_url));
875
876                                                                         updateFeedList();
877                                                                         break;
878                                                                 case 2:
879                                                                         alert(__("Specified URL seems to be invalid."));
880                                                                         break;
881                                                                 case 3:
882                                                                         alert(__("Specified URL doesn't seem to contain any feeds."));
883                                                                         break;
884                                                                 case 4:
885                                                                         /* notify_progress("Searching for feed urls...", true);
886
887                                                                         new Ajax.Request("backend.php", {
888                                                                                 parameters: 'op=rpc&method=extractfeedurls&url=' + param_escape(feed_url),
889                                                                                 onComplete: function(transport, dialog, feed_url) {
890
891                                                                                         notify('');
892
893                                                                                         var reply = JSON.parse(transport.responseText);
894
895                                                                                         var feeds = reply['urls'];
896
897                                                                                         console.log(transport.responseText);
898
899                                                                                         var select = dijit.byId("feedDlg_feedContainerSelect");
900
901                                                                                         while (select.getOptions().length > 0)
902                                                                                                 select.removeOption(0);
903
904                                                                                         var count = 0;
905                                                                                         for (var feedUrl in feeds) {
906                                                                                                 select.addOption({value: feedUrl, label: feeds[feedUrl]});
907                                                                                                 count++;
908                                                                                         }
909
910 //                                                                                      if (count > 5) count = 5;
911 //                                                                                      select.size = count;
912
913                                                                                         Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
914                                                                                 }
915                                                                         });
916                                                                         break; */
917
918                                                                         feeds = rc['feeds'];
919
920                                                                         var select = dijit.byId("feedDlg_feedContainerSelect");
921
922                                                                         while (select.getOptions().length > 0)
923                                                                                 select.removeOption(0);
924
925                                                                         var count = 0;
926                                                                         for (var feedUrl in feeds) {
927                                                                                 select.addOption({value: feedUrl, label: feeds[feedUrl]});
928                                                                                 count++;
929                                                                         }
930
931                                                                         Effect.Appear('feedDlg_feedsContainer', {duration : 0.5});
932
933                                                                         break;
934                                                                 case 5:
935                                                                         alert(__("Couldn't download the specified URL: %s").
936                                                                                         replace("%s", rc['message']));
937                                                                         break;
938                                                                 case 0:
939                                                                         alert(__("You are already subscribed to this feed."));
940                                                                         break;
941                                                                 }
942
943                                                         } catch (e) {
944                                                                 exception_error("subscribeToFeed", e, transport);
945                                                         }
946
947                                                 } });
948
949                                         }
950                         },
951                         href: query});
952
953                 dialog.show();
954         } catch (e) {
955                 exception_error("quickAddFeed", e);
956         }
957 }
958
959 function createNewRuleElement(parentNode, replaceNode) {
960         try {
961                 var form = document.forms["filter_new_rule_form"];
962
963                 var query = "backend.php?op=pref-filters&method=printrulename&rule="+
964                         param_escape(dojo.formToJson(form));
965
966                 console.log(query);
967
968                 new Ajax.Request("backend.php", {
969                         parameters: query,
970                         onComplete: function (transport) {
971                                 try {
972                                         var li = dojo.create("li");
973
974                                         var cb = dojo.create("input", { type: "checkbox" }, li);
975
976                                         new dijit.form.CheckBox({
977                                                 onChange: function() {
978                                                         toggleSelectListRow2(this) },
979                                         }, cb);
980
981                                         dojo.create("input", { type: "hidden",
982                                                 name: "rule[]",
983                                                 value: dojo.formToJson(form) }, li);
984
985                                         dojo.create("span", {
986                                                 onclick: function() {
987                                                         dijit.byId('filterEditDlg').editRule(this);
988                                                 },
989                                                 innerHTML: transport.responseText }, li);
990
991                                         if (replaceNode) {
992                                                 parentNode.replaceChild(li, replaceNode);
993                                         } else {
994                                                 parentNode.appendChild(li);
995                                         }
996                                 } catch (e) {
997                                         exception_error("createNewRuleElement", e);
998                                 }
999                 } });
1000         } catch (e) {
1001                 exception_error("createNewRuleElement", e);
1002         }
1003 }
1004
1005 function createNewActionElement(parentNode, replaceNode) {
1006         try {
1007                 var form = document.forms["filter_new_action_form"];
1008
1009                 if (form.action_id.value == 7) {
1010                         form.action_param.value = form.action_param_label.value;
1011                 }
1012
1013                 var query = "backend.php?op=pref-filters&method=printactionname&action="+
1014                         param_escape(dojo.formToJson(form));
1015
1016                 console.log(query);
1017
1018                 new Ajax.Request("backend.php", {
1019                         parameters: query,
1020                         onComplete: function (transport) {
1021                                 try {
1022                                         var li = dojo.create("li");
1023
1024                                         var cb = dojo.create("input", { type: "checkbox" }, li);
1025
1026                                         new dijit.form.CheckBox({
1027                                                 onChange: function() {
1028                                                         toggleSelectListRow2(this) },
1029                                         }, cb);
1030
1031                                         dojo.create("input", { type: "hidden",
1032                                                 name: "action[]",
1033                                                 value: dojo.formToJson(form) }, li);
1034
1035                                         dojo.create("span", {
1036                                                 onclick: function() {
1037                                                         dijit.byId('filterEditDlg').editAction(this);
1038                                                 },
1039                                                 innerHTML: transport.responseText }, li);
1040
1041                                         if (replaceNode) {
1042                                                 parentNode.replaceChild(li, replaceNode);
1043                                         } else {
1044                                                 parentNode.appendChild(li);
1045                                         }
1046
1047                                 } catch (e) {
1048                                         exception_error("createNewActionElement", e);
1049                                 }
1050                         } });
1051         } catch (e) {
1052                 exception_error("createNewActionElement", e);
1053         }
1054 }
1055
1056
1057 function addFilterRule(replaceNode, ruleStr) {
1058         try {
1059                 if (dijit.byId("filterNewRuleDlg"))
1060                         dijit.byId("filterNewRuleDlg").destroyRecursive();
1061
1062                 var query = "backend.php?op=pref-filters&method=newrule&rule=" +
1063                         param_escape(ruleStr);
1064
1065                 var rule_dlg = new dijit.Dialog({
1066                         id: "filterNewRuleDlg",
1067                         title: ruleStr ? __("Edit rule") : __("Add rule"),
1068                         style: "width: 600px",
1069                         execute: function() {
1070                                 if (this.validate()) {
1071                                         createNewRuleElement($("filterDlg_Matches"), replaceNode);
1072                                         this.hide();
1073                                 }
1074                         },
1075                         href: query});
1076
1077                 rule_dlg.show();
1078         } catch (e) {
1079                 exception_error("addFilterRule", e);
1080         }
1081 }
1082
1083 function addFilterAction(replaceNode, actionStr) {
1084         try {
1085                 if (dijit.byId("filterNewActionDlg"))
1086                         dijit.byId("filterNewActionDlg").destroyRecursive();
1087
1088                 var query = "backend.php?op=pref-filters&method=newaction&action=" +
1089                         param_escape(actionStr);
1090
1091                 var rule_dlg = new dijit.Dialog({
1092                         id: "filterNewActionDlg",
1093                         title: actionStr ? __("Edit action") : __("Add action"),
1094                         style: "width: 600px",
1095                         execute: function() {
1096                                 if (this.validate()) {
1097                                         createNewActionElement($("filterDlg_Actions"), replaceNode);
1098                                         this.hide();
1099                                 }
1100                         },
1101                         href: query});
1102
1103                 rule_dlg.show();
1104         } catch (e) {
1105                 exception_error("addFilterAction", e);
1106         }
1107 }
1108
1109 function quickAddFilter() {
1110         try {
1111                 var query = "";
1112                 if (!inPreferences()) {
1113                         query = "backend.php?op=pref-filters&method=newfilter&feed=" +
1114                                 param_escape(getActiveFeedId()) + "&is_cat=" +
1115                                 param_escape(activeFeedIsCat());
1116                 } else {
1117                         query = "backend.php?op=pref-filters&method=newfilter";
1118                 }
1119
1120                 console.log(query);
1121
1122                 if (dijit.byId("feedEditDlg"))
1123                         dijit.byId("feedEditDlg").destroyRecursive();
1124
1125                 if (dijit.byId("filterEditDlg"))
1126                         dijit.byId("filterEditDlg").destroyRecursive();
1127
1128                 dialog = new dijit.Dialog({
1129                         id: "filterEditDlg",
1130                         title: __("Create Filter"),
1131                         style: "width: 600px",
1132                         test: function() {
1133                                 var query = "backend.php?" + dojo.formToQuery("filter_new_form") + "&savemode=test";
1134
1135                                 if (dijit.byId("filterTestDlg"))
1136                                         dijit.byId("filterTestDlg").destroyRecursive();
1137
1138                                 var test_dlg = new dijit.Dialog({
1139                                         id: "filterTestDlg",
1140                                         title: "Test Filter",
1141                                         style: "width: 600px",
1142                                         href: query});
1143
1144                                 test_dlg.show();
1145                         },
1146                         selectRules: function(select) {
1147                                 $$("#filterDlg_Matches input[type=checkbox]").each(function(e) {
1148                                         e.checked = select;
1149                                         if (select)
1150                                                 e.parentNode.addClassName("Selected");
1151                                         else
1152                                                 e.parentNode.removeClassName("Selected");
1153                                 });
1154                         },
1155                         selectActions: function(select) {
1156                                 $$("#filterDlg_Actions input[type=checkbox]").each(function(e) {
1157                                         e.checked = select;
1158
1159                                         if (select)
1160                                                 e.parentNode.addClassName("Selected");
1161                                         else
1162                                                 e.parentNode.removeClassName("Selected");
1163
1164                                 });
1165                         },
1166                         editRule: function(e) {
1167                                 var li = e.parentNode;
1168                                 var rule = li.getElementsByTagName("INPUT")[1].value;
1169                                 addFilterRule(li, rule);
1170                         },
1171                         editAction: function(e) {
1172                                 var li = e.parentNode;
1173                                 var action = li.getElementsByTagName("INPUT")[1].value;
1174                                 addFilterAction(li, action);
1175                         },
1176                         addAction: function() { addFilterAction(); },
1177                         addRule: function() { addFilterRule(); },
1178                         deleteAction: function() {
1179                                 $$("#filterDlg_Actions li.[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
1180                         },
1181                         deleteRule: function() {
1182                                 $$("#filterDlg_Matches li.[class*=Selected]").each(function(e) { e.parentNode.removeChild(e) });
1183                         },
1184                         execute: function() {
1185                                 if (this.validate()) {
1186
1187                                         var query = dojo.formToQuery("filter_new_form");
1188
1189                                         console.log(query);
1190
1191                                         new Ajax.Request("backend.php", {
1192                                                 parameters: query,
1193                                                 onComplete: function (transport) {
1194                                                         if (inPreferences()) {
1195                                                                 updateFilterList();
1196                                                         }
1197
1198                                                         dialog.hide();
1199                                         } });
1200                                 }
1201                         },
1202                         href: query});
1203
1204                 if (!inPreferences()) {
1205                         var lh = dojo.connect(dialog, "onLoad", function(){
1206                                 dojo.disconnect(lh);
1207
1208                                 var title = $("PTITLE-FULL-" + getActiveArticleId());
1209
1210                                 if (title || getActiveFeedId() || activeFeedIsCat()) {
1211                                         if (title) title = title.innerHTML;
1212
1213                                         console.log(title + " " + getActiveFeedId());
1214
1215                                         var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
1216                                                 getActiveFeedId();
1217
1218                                         var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
1219
1220                                         addFilterRule(null, dojo.toJson(rule));
1221                                 }
1222                         });
1223                 }
1224
1225                 dialog.show();
1226
1227         } catch (e) {
1228                 exception_error("quickAddFilter", e);
1229         }
1230 }
1231
1232 function resetPubSub(feed_id, title) {
1233
1234         var msg = __("Reset subscription? Tiny Tiny RSS will try to subscribe to the notification hub again on next feed update.").replace("%s", title);
1235
1236         if (title == undefined || confirm(msg)) {
1237                 notify_progress("Loading, please wait...");
1238
1239                 var query = "?op=pref-feeds&quiet=1&method=resetPubSub&ids=" + feed_id;
1240
1241                 new Ajax.Request("backend.php", {
1242                         parameters: query,
1243                         onComplete: function(transport) {
1244                                 dijit.byId("pubsubReset_Btn").attr('disabled', true);
1245                                 notify_info("Subscription reset.");
1246                         } });
1247         }
1248
1249         return false;
1250 }
1251
1252
1253 function unsubscribeFeed(feed_id, title) {
1254
1255         var msg = __("Unsubscribe from %s?").replace("%s", title);
1256
1257         if (title == undefined || confirm(msg)) {
1258                 notify_progress("Removing feed...");
1259
1260                 var query = "?op=pref-feeds&quiet=1&method=remove&ids=" + feed_id;
1261
1262                 new Ajax.Request("backend.php", {
1263                         parameters: query,
1264                         onComplete: function(transport) {
1265
1266                                         if (dijit.byId("feedEditDlg")) dijit.byId("feedEditDlg").hide();
1267
1268                                         if (inPreferences()) {
1269                                                 updateFeedList();
1270                                         } else {
1271                                                 if (feed_id == getActiveFeedId())
1272                                                         setTimeout("viewfeed(-5)", 100);
1273                                         }
1274
1275                                 } });
1276         }
1277
1278         return false;
1279 }
1280
1281
1282 function backend_sanity_check_callback(transport) {
1283
1284         try {
1285
1286                 if (sanity_check_done) {
1287                         fatalError(11, "Sanity check request received twice. This can indicate "+
1288                       "presence of Firebug or some other disrupting extension. "+
1289                                 "Please disable it and try again.");
1290                         return;
1291                 }
1292
1293                 var reply = JSON.parse(transport.responseText);
1294
1295                 if (!reply) {
1296                         fatalError(3, "Sanity check: invalid RPC reply", transport.responseText);
1297                         return;
1298                 }
1299
1300                 var error_code = reply['error']['code'];
1301
1302                 if (error_code && error_code != 0) {
1303                         return fatalError(error_code, reply['error']['message']);
1304                 }
1305
1306                 console.log("sanity check ok");
1307
1308                 var params = reply['init-params'];
1309
1310                 if (params) {
1311                         console.log('reading init-params...');
1312
1313                         if (params) {
1314                                 for (k in params) {
1315                                         var v = params[k];
1316                                         console.log("IP: " + k + " => " + v);
1317                                 }
1318                         }
1319
1320                         init_params = params;
1321                 }
1322
1323                 sanity_check_done = true;
1324
1325                 init_second_stage();
1326
1327         } catch (e) {
1328                 exception_error("backend_sanity_check_callback", e, transport);
1329         }
1330 }
1331
1332 /*function has_local_storage() {
1333         try {
1334                 return 'sessionStorage' in window && window['sessionStorage'] != null;
1335         } catch (e) {
1336                 return false;
1337         }
1338 } */
1339
1340 function catSelectOnChange(elem) {
1341         try {
1342 /*              var value = elem[elem.selectedIndex].value;
1343                 var def = elem.getAttribute('default');
1344
1345                 if (value == "ADD_CAT") {
1346
1347                         if (def)
1348                                 dropboxSelect(elem, def);
1349                         else
1350                                 elem.selectedIndex = 0;
1351
1352                         quickAddCat(elem);
1353                 } */
1354
1355         } catch (e) {
1356                 exception_error("catSelectOnChange", e);
1357         }
1358 }
1359
1360 function quickAddCat(elem) {
1361         try {
1362                 var cat = prompt(__("Please enter category title:"));
1363
1364                 if (cat) {
1365
1366                         var query = "?op=rpc&method=quickAddCat&cat=" + param_escape(cat);
1367
1368                         notify_progress("Loading, please wait...", true);
1369
1370                         new Ajax.Request("backend.php", {
1371                                 parameters: query,
1372                                 onComplete: function (transport) {
1373                                         var response = transport.responseXML;
1374                                         var select = response.getElementsByTagName("select")[0];
1375                                         var options = select.getElementsByTagName("option");
1376
1377                                         dropbox_replace_options(elem, options);
1378
1379                                         notify('');
1380
1381                         } });
1382
1383                 }
1384
1385         } catch (e) {
1386                 exception_error("quickAddCat", e);
1387         }
1388 }
1389
1390 function genUrlChangeKey(feed, is_cat) {
1391
1392         try {
1393                 var ok = confirm(__("Generate new syndication address for this feed?"));
1394
1395                 if (ok) {
1396
1397                         notify_progress("Trying to change address...", true);
1398
1399                         var query = "?op=rpc&method=regenFeedKey&id=" + param_escape(feed) +
1400                                 "&is_cat=" + param_escape(is_cat);
1401
1402                         new Ajax.Request("backend.php", {
1403                                 parameters: query,
1404                                 onComplete: function(transport) {
1405                                                 var reply = JSON.parse(transport.responseText);
1406                                                 var new_link = reply.link;
1407
1408                                                 var e = $('gen_feed_url');
1409
1410                                                 if (new_link) {
1411
1412                                                         e.innerHTML = e.innerHTML.replace(/\&amp;key=.*$/,
1413                                                                 "&amp;key=" + new_link);
1414
1415                                                         e.href = e.href.replace(/\&key=.*$/,
1416                                                                 "&key=" + new_link);
1417
1418                                                         new Effect.Highlight(e);
1419
1420                                                         notify('');
1421
1422                                                 } else {
1423                                                         notify_error("Could not change feed URL.");
1424                                                 }
1425                                 } });
1426                 }
1427         } catch (e) {
1428                 exception_error("genUrlChangeKey", e);
1429         }
1430         return false;
1431 }
1432
1433 function labelSelectOnChange(elem) {
1434         try {
1435 /*              var value = elem[elem.selectedIndex].value;
1436                 var def = elem.getAttribute('default');
1437
1438                 if (value == "ADD_LABEL") {
1439
1440                         if (def)
1441                                 dropboxSelect(elem, def);
1442                         else
1443                                 elem.selectedIndex = 0;
1444
1445                         addLabel(elem, function(transport) {
1446
1447                                         try {
1448
1449                                                 var response = transport.responseXML;
1450                                                 var select = response.getElementsByTagName("select")[0];
1451                                                 var options = select.getElementsByTagName("option");
1452
1453                                                 dropbox_replace_options(elem, options);
1454
1455                                                 notify('');
1456                                         } catch (e) {
1457                                                 exception_error("addLabel", e);
1458                                         }
1459                         });
1460                 } */
1461
1462         } catch (e) {
1463                 exception_error("labelSelectOnChange", e);
1464         }
1465 }
1466
1467 function dropbox_replace_options(elem, options) {
1468
1469         try {
1470                 while (elem.hasChildNodes())
1471                         elem.removeChild(elem.firstChild);
1472
1473                 var sel_idx = -1;
1474
1475                 for (var i = 0; i < options.length; i++) {
1476                         var text = options[i].firstChild.nodeValue;
1477                         var value = options[i].getAttribute("value");
1478
1479                         if (value == undefined) value = text;
1480
1481                         var issel = options[i].getAttribute("selected") == "1";
1482
1483                         var option = new Option(text, value, issel);
1484
1485                         if (options[i].getAttribute("disabled"))
1486                                 option.setAttribute("disabled", true);
1487
1488                         elem.insert(option);
1489
1490                         if (issel) sel_idx = i;
1491                 }
1492
1493                 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1494                 if (sel_idx >= 0) elem.selectedIndex = sel_idx;
1495
1496         } catch (e) {
1497                 exception_error("dropbox_replace_options", e);
1498         }
1499 }
1500
1501 // mode = all, none, invert
1502 function selectTableRows(id, mode) {
1503         try {
1504                 var rows = $(id).rows;
1505
1506                 for (var i = 0; i < rows.length; i++) {
1507                         var row = rows[i];
1508                         var cb = false;
1509                         var dcb = false;
1510
1511                         if (row.id && row.className) {
1512                                 var bare_id = row.id.replace(/^[A-Z]*?-/, "");
1513                                 var inputs = rows[i].getElementsByTagName("input");
1514
1515                                 for (var j = 0; j < inputs.length; j++) {
1516                                         var input = inputs[j];
1517
1518                                         if (input.getAttribute("type") == "checkbox" &&
1519                                                         input.id.match(bare_id)) {
1520
1521                                                 cb = input;
1522                                                 dcb = dijit.getEnclosingWidget(cb);
1523                                                 break;
1524                                         }
1525                                 }
1526
1527                                 if (cb || dcb) {
1528                                         var issel = row.hasClassName("Selected");
1529
1530                                         if (mode == "all" && !issel) {
1531                                                 row.addClassName("Selected");
1532                                                 cb.checked = true;
1533                                                 if (dcb) dcb.set("checked", true);
1534                                         } else if (mode == "none" && issel) {
1535                                                 row.removeClassName("Selected");
1536                                                 cb.checked = false;
1537                                                 if (dcb) dcb.set("checked", false);
1538
1539                                         } else if (mode == "invert") {
1540
1541                                                 if (issel) {
1542                                                         row.removeClassName("Selected");
1543                                                         cb.checked = false;
1544                                                         if (dcb) dcb.set("checked", false);
1545                                                 } else {
1546                                                         row.addClassName("Selected");
1547                                                         cb.checked = true;
1548                                                         if (dcb) dcb.set("checked", true);
1549                                                 }
1550                                         }
1551                                 }
1552                         }
1553                 }
1554
1555         } catch (e) {
1556                 exception_error("selectTableRows", e);
1557
1558         }
1559 }
1560
1561 function getSelectedTableRowIds(id) {
1562         var rows = [];
1563
1564         try {
1565                 var elem_rows = $(id).rows;
1566
1567                 for (var i = 0; i < elem_rows.length; i++) {
1568                         if (elem_rows[i].hasClassName("Selected")) {
1569                                 var bare_id = elem_rows[i].id.replace(/^[A-Z]*?-/, "");
1570                                 rows.push(bare_id);
1571                         }
1572                 }
1573
1574         } catch (e) {
1575                 exception_error("getSelectedTableRowIds", e);
1576         }
1577
1578         return rows;
1579 }
1580
1581 function editFeed(feed, event) {
1582         try {
1583                 if (feed <= 0)
1584                         return alert(__("You can't edit this kind of feed."));
1585
1586                 var query = "backend.php?op=pref-feeds&method=editfeed&id=" +
1587                         param_escape(feed);
1588
1589                 console.log(query);
1590
1591                 if (dijit.byId("filterEditDlg"))
1592                         dijit.byId("filterEditDlg").destroyRecursive();
1593
1594                 if (dijit.byId("feedEditDlg"))
1595                         dijit.byId("feedEditDlg").destroyRecursive();
1596
1597                 dialog = new dijit.Dialog({
1598                         id: "feedEditDlg",
1599                         title: __("Edit Feed"),
1600                         style: "width: 600px",
1601                         execute: function() {
1602                                 if (this.validate()) {
1603 //                                      console.log(dojo.objectToQuery(this.attr('value')));
1604
1605                                         notify_progress("Saving data...", true);
1606
1607                                         new Ajax.Request("backend.php", {
1608                                                 parameters: dojo.objectToQuery(dialog.attr('value')),
1609                                                 onComplete: function(transport) {
1610                                                         dialog.hide();
1611                                                         notify('');
1612                                                         updateFeedList();
1613                                         }});
1614                                 }
1615                         },
1616                         href: query});
1617
1618                 dialog.show();
1619
1620         } catch (e) {
1621                 exception_error("editFeed", e);
1622         }
1623 }
1624
1625 function feedBrowser() {
1626         try {
1627                 var query = "backend.php?op=dlg&method=feedBrowser";
1628
1629                 if (dijit.byId("feedAddDlg"))
1630                         dijit.byId("feedAddDlg").hide();
1631
1632                 if (dijit.byId("feedBrowserDlg"))
1633                         dijit.byId("feedBrowserDlg").destroyRecursive();
1634
1635                 var dialog = new dijit.Dialog({
1636                         id: "feedBrowserDlg",
1637                         title: __("More Feeds"),
1638                         style: "width: 600px",
1639                         getSelectedFeedIds: function() {
1640                                 var list = $$("#browseFeedList li[id*=FBROW]");
1641                                 var selected = new Array();
1642
1643                                 list.each(function(child) {
1644                                         var id = child.id.replace("FBROW-", "");
1645
1646                                         if (child.hasClassName('Selected')) {
1647                                                 selected.push(id);
1648                                         }
1649                                 });
1650
1651                                 return selected;
1652                         },
1653                         getSelectedFeeds: function() {
1654                                 var list = $$("#browseFeedList li.Selected");
1655                                 var selected = new Array();
1656
1657                                 list.each(function(child) {
1658                                         var title = child.getElementsBySelector("span.fb_feedTitle")[0].innerHTML;
1659                                         var url = child.getElementsBySelector("a.fb_feedUrl")[0].href;
1660
1661                                         selected.push([title,url]);
1662
1663                                 });
1664
1665                                 return selected;
1666                         },
1667
1668                         subscribe: function() {
1669                                 var mode = this.attr('value').mode;
1670                                 var selected = [];
1671
1672                                 if (mode == "1")
1673                                         selected = this.getSelectedFeeds();
1674                                 else
1675                                         selected = this.getSelectedFeedIds();
1676
1677                                 if (selected.length > 0) {
1678                                         dijit.byId("feedBrowserDlg").hide();
1679
1680                                         notify_progress("Loading, please wait...", true);
1681
1682                                         // we use dojo.toJson instead of JSON.stringify because
1683                                         // it somehow escapes everything TWICE, at least in Chrome 9
1684
1685                                         var query = "?op=rpc&method=massSubscribe&payload="+
1686                                                 param_escape(dojo.toJson(selected)) + "&mode=" + param_escape(mode);
1687
1688                                         console.log(query);
1689
1690                                         new Ajax.Request("backend.php", {
1691                                                 parameters: query,
1692                                                 onComplete: function(transport) {
1693                                                         notify('');
1694                                                         updateFeedList();
1695                                                 } });
1696
1697                                 } else {
1698                                         alert(__("No feeds are selected."));
1699                                 }
1700
1701                         },
1702                         update: function() {
1703                                 var query = dojo.objectToQuery(dialog.attr('value'));
1704
1705                                 Element.show('feed_browser_spinner');
1706
1707                                 new Ajax.Request("backend.php", {
1708                                         parameters: query,
1709                                         onComplete: function(transport) {
1710                                                 notify('');
1711
1712                                                 Element.hide('feed_browser_spinner');
1713
1714                                                 var c = $("browseFeedList");
1715
1716                                                 var reply = JSON.parse(transport.responseText);
1717
1718                                                 var r = reply['content'];
1719                                                 var mode = reply['mode'];
1720
1721                                                 if (c && r) {
1722                                                         c.innerHTML = r;
1723                                                 }
1724
1725                                                 dojo.parser.parse("browseFeedList");
1726
1727                                                 if (mode == 2) {
1728                                                         Element.show(dijit.byId('feed_archive_remove').domNode);
1729                                                 } else {
1730                                                         Element.hide(dijit.byId('feed_archive_remove').domNode);
1731                                                 }
1732
1733                                         } });
1734                         },
1735                         removeFromArchive: function() {
1736                                 var selected = this.getSelectedFeeds();
1737
1738                                 if (selected.length > 0) {
1739
1740                                         var pr = __("Remove selected feeds from the archive? Feeds with stored articles will not be removed.");
1741
1742                                         if (confirm(pr)) {
1743                                                 Element.show('feed_browser_spinner');
1744
1745                                                 var query = "?op=rpc&method=remarchived&ids=" +
1746                                                         param_escape(selected.toString());;
1747
1748                                                 new Ajax.Request("backend.php", {
1749                                                         parameters: query,
1750                                                         onComplete: function(transport) {
1751                                                                 dialog.update();
1752                                                         } });
1753                                         }
1754                                 }
1755                         },
1756                         execute: function() {
1757                                 if (this.validate()) {
1758                                         this.subscribe();
1759                                 }
1760                         },
1761                         href: query});
1762
1763                 dialog.show();
1764
1765         } catch (e) {
1766                 exception_error("editFeed", e);
1767         }
1768 }
1769
1770 function showFeedsWithErrors() {
1771         try {
1772                 var query = "backend.php?op=pref-feeds&method=feedsWithErrors";
1773
1774                 if (dijit.byId("errorFeedsDlg"))
1775                         dijit.byId("errorFeedsDlg").destroyRecursive();
1776
1777                 dialog = new dijit.Dialog({
1778                         id: "errorFeedsDlg",
1779                         title: __("Feeds with update errors"),
1780                         style: "width: 600px",
1781                         getSelectedFeeds: function() {
1782                                 return getSelectedTableRowIds("prefErrorFeedList");
1783                         },
1784                         removeSelected: function() {
1785                                 var sel_rows = this.getSelectedFeeds();
1786
1787                                 console.log(sel_rows);
1788
1789                                 if (sel_rows.length > 0) {
1790                                         var ok = confirm(__("Remove selected feeds?"));
1791
1792                                         if (ok) {
1793                                                 notify_progress("Removing selected feeds...", true);
1794
1795                                                 var query = "?op=pref-feeds&method=remove&ids="+
1796                                                         param_escape(sel_rows.toString());
1797
1798                                                 new Ajax.Request("backend.php", {
1799                                                         parameters: query,
1800                                                         onComplete: function(transport) {
1801                                                                 notify('');
1802                                                                 dialog.hide();
1803                                                                 updateFeedList();
1804                                                         } });
1805                                         }
1806
1807                                 } else {
1808                                         alert(__("No feeds are selected."));
1809                                 }
1810                         },
1811                         execute: function() {
1812                                 if (this.validate()) {
1813                                 }
1814                         },
1815                         href: query});
1816
1817                 dialog.show();
1818
1819         } catch (e) {
1820                 exception_error("showFeedsWithErrors", e);
1821         }
1822
1823 }
1824
1825 /* new support functions for SelectByTag */
1826
1827 function get_all_tags(selObj){
1828         try {
1829                 if( !selObj ) return "";
1830
1831                 var result = "";
1832                 var len = selObj.options.length;
1833
1834                 for (var i=0; i < len; i++){
1835                         if (selObj.options[i].selected) {
1836                                 result += selObj[i].value + "%2C";   // is really a comma
1837                         }
1838                 }
1839
1840                 if (result.length > 0){
1841                         result = result.substr(0, result.length-3);  // remove trailing %2C
1842                 }
1843
1844                 return(result);
1845
1846         } catch (e) {
1847                 exception_error("get_all_tags", e);
1848         }
1849 }
1850
1851 function get_radio_checked(radioObj) {
1852         try {
1853                 if (!radioObj) return "";
1854
1855                 var len = radioObj.length;
1856
1857                 if (len == undefined){
1858                         if(radioObj.checked){
1859                                 return(radioObj.value);
1860                         } else {
1861                                 return("");
1862                         }
1863                 }
1864
1865                 for( var i=0; i < len; i++ ){
1866                         if( radioObj[i].checked ){
1867                                 return( radioObj[i].value);
1868                         }
1869                 }
1870
1871         } catch (e) {
1872                 exception_error("get_radio_checked", e);
1873         }
1874         return("");
1875 }
1876
1877 function get_timestamp() {
1878         var date = new Date();
1879         return Math.round(date.getTime() / 1000);
1880 }
1881
1882 function helpDialog(topic) {
1883         try {
1884                 var query = "backend.php?op=backend&method=help&topic=" + param_escape(topic);
1885
1886                 if (dijit.byId("helpDlg"))
1887                         dijit.byId("helpDlg").destroyRecursive();
1888
1889                 dialog = new dijit.Dialog({
1890                         id: "helpDlg",
1891                         title: __("Help"),
1892                         style: "width: 600px",
1893                         href: query,
1894                 });
1895
1896                 dialog.show();
1897
1898         } catch (e) {
1899                 exception_error("helpDialog", e);
1900         }
1901 }
1902