]> git.wh0rd.org Git - tt-rss.git/blob - js/tt-rss.js
Merge pull request #68 from gvmelle/master
[tt-rss.git] / js / tt-rss.js
1 var global_unread = -1;
2 var hotkey_prefix = false;
3 var hotkey_prefix_pressed = false;
4 var _widescreen_mode = false;
5 var _rpc_seq = 0;
6 var _active_feed_id = 0;
7 var _active_feed_is_cat = false;
8
9 function next_seq() {
10         _rpc_seq += 1;
11         return _rpc_seq;
12 }
13
14 function get_seq() {
15         return _rpc_seq;
16 }
17
18 function activeFeedIsCat() {
19         return _active_feed_is_cat;
20 }
21
22 function getActiveFeedId() {
23         try {
24                 return _active_feed_id;
25         } catch (e) {
26                 exception_error("getActiveFeedId", e);
27         }
28 }
29
30 function setActiveFeedId(id, is_cat) {
31         try {
32                 hash_set('f', id);
33                 hash_set('c', is_cat ? 1 : 0);
34
35                 _active_feed_id = id;
36                 _active_feed_is_cat = is_cat;
37
38                 selectFeed(id, is_cat);
39         } catch (e) {
40                 exception_error("setActiveFeedId", e);
41         }
42 }
43
44
45 function updateFeedList() {
46         try {
47
48 //              $("feeds-holder").innerHTML = "<div id=\"feedlistLoading\">" +
49 //                      __("Loading, please wait...") + "</div>";
50
51                 Element.show("feedlistLoading");
52
53                 if (dijit.byId("feedTree")) {
54                         dijit.byId("feedTree").destroyRecursive();
55                 }
56
57                 var store = new dojo.data.ItemFileWriteStore({
58          url: "backend.php?op=pref_feeds&method=getfeedtree&mode=2"});
59
60                 var treeModel = new fox.FeedStoreModel({
61                         store: store,
62                         query: {
63                                 "type": getInitParam('enable_feed_cats') == 1 ? "category" : "feed"
64                         },
65                         rootId: "root",
66                         rootLabel: "Feeds",
67                         childrenAttrs: ["items"]
68                 });
69
70                 var tree = new fox.FeedTree({
71                 model: treeModel,
72                 onClick: function (item, node) {
73                         var id = String(item.id);
74                         var is_cat = id.match("^CAT:");
75                         var feed = id.substr(id.indexOf(":")+1);
76                         viewfeed(feed, '', is_cat);
77                         return false;
78                 },
79                 openOnClick: false,
80                 showRoot: false,
81                 id: "feedTree",
82                 }, "feedTree");
83
84 /*              var menu = new dijit.Menu({id: 'feedMenu'});
85
86                 menu.addChild(new dijit.MenuItem({
87           label: "Simple menu item"
88                 }));
89
90 //              menu.bindDomNode(tree.domNode); */
91
92                 var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
93                         console.log(dijit.getEnclosingWidget(event.target));
94                         dojo.disconnect(tmph);
95                 });
96
97                 $("feeds-holder").appendChild(tree.domNode);
98
99                 var tmph = dojo.connect(tree, 'onLoad', function() {
100                 dojo.disconnect(tmph);
101                         Element.hide("feedlistLoading");
102
103                         feedlist_init();
104
105 //                      var node = dijit.byId("feedTree")._itemNodesMap['FEED:-2'][0].domNode
106 //                      menu.bindDomNode(node);
107
108                         loading_set_progress(25);
109                 });
110
111                 tree.startup();
112
113         } catch (e) {
114                 exception_error("updateFeedList", e);
115         }
116 }
117
118 function catchupAllFeeds() {
119
120         var str = __("Mark all articles as read?");
121
122         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
123
124                 var query_str = "backend.php?op=feeds&method=catchupAll";
125
126                 notify_progress("Marking all feeds as read...");
127
128                 //console.log("catchupAllFeeds Q=" + query_str);
129
130                 new Ajax.Request("backend.php", {
131                         parameters: query_str,
132                         onComplete: function(transport) {
133                                 feedlist_callback2(transport);
134                         } });
135
136                 global_unread = 0;
137                 updateTitle("");
138         }
139 }
140
141 function viewCurrentFeed(method) {
142         console.log("viewCurrentFeed");
143
144         if (getActiveFeedId() != undefined) {
145                 viewfeed(getActiveFeedId(), method, activeFeedIsCat());
146         }
147         return false; // block unneeded form submits
148 }
149
150 function timeout() {
151         if (getInitParam("bw_limit") != "1") {
152                 request_counters();
153                 setTimeout("timeout()", 60*1000);
154         }
155 }
156
157 function search() {
158         var query = "backend.php?op=dlg&method=search&param=" +
159                 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
160
161         if (dijit.byId("searchDlg"))
162                 dijit.byId("searchDlg").destroyRecursive();
163
164         dialog = new dijit.Dialog({
165                 id: "searchDlg",
166                 title: __("Search"),
167                 style: "width: 600px",
168                 execute: function() {
169                         if (this.validate()) {
170                                 _search_query = dojo.objectToQuery(this.attr('value'));
171                                 this.hide();
172                                 viewCurrentFeed();
173                         }
174                 },
175                 href: query});
176
177         dialog.show();
178 }
179
180 function updateTitle() {
181         var tmp = document.title;
182
183         if (tmp.indexOf(")") > 0) {
184                 tmp = tmp.substr(tmp.indexOf(")") + 1);
185         }
186
187         if (global_unread > 0) {
188                 tmp = "(" + global_unread + ") " + tmp;
189         }
190
191         if (window.fluid) {
192                 if (global_unread > 0) {
193                         window.fluid.dockBadge = global_unread;
194                 } else {
195                         window.fluid.dockBadge = "";
196                 }
197         }
198
199         document.title = tmp;
200 }
201
202 function genericSanityCheck() {
203         setCookie("ttrss_test", "TEST");
204
205         if (getCookie("ttrss_test") != "TEST") {
206                 return fatalError(2);
207         }
208
209         return true;
210 }
211
212 function init() {
213         try {
214                 //dojo.registerModulePath("fox", "../../js/");
215
216                 dojo.require("fox.FeedTree");
217
218                 dojo.require("dijit.ColorPalette");
219                 dojo.require("dijit.Dialog");
220                 dojo.require("dijit.form.Button");
221                 dojo.require("dijit.form.CheckBox");
222                 dojo.require("dijit.form.DropDownButton");
223                 dojo.require("dijit.form.FilteringSelect");
224                 dojo.require("dijit.form.Form");
225                 dojo.require("dijit.form.RadioButton");
226                 dojo.require("dijit.form.Select");
227                 dojo.require("dijit.form.SimpleTextarea");
228                 dojo.require("dijit.form.TextBox");
229                 dojo.require("dijit.form.ValidationTextBox");
230                 dojo.require("dijit.InlineEditBox");
231                 dojo.require("dijit.layout.AccordionContainer");
232                 dojo.require("dijit.layout.BorderContainer");
233                 dojo.require("dijit.layout.ContentPane");
234                 dojo.require("dijit.layout.TabContainer");
235                 dojo.require("dijit.Menu");
236                 dojo.require("dijit.ProgressBar");
237                 dojo.require("dijit.ProgressBar");
238                 dojo.require("dijit.Toolbar");
239                 dojo.require("dijit.Tree");
240                 dojo.require("dijit.tree.dndSource");
241                 dojo.require("dojo.data.ItemFileWriteStore");
242
243                 dojo.parser.parse();
244
245                 if (!genericSanityCheck())
246                         return false;
247
248                 loading_set_progress(20);
249
250                 var hasAudio = !!((myAudioTag = document.createElement('audio')).canPlayType);
251                 var hasSandbox = "sandbox" in document.createElement("iframe");
252
253                 new Ajax.Request("backend.php", {
254                         parameters: {op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
255                                 hasSandbox: hasSandbox},
256                         onComplete: function(transport) {
257                                         backend_sanity_check_callback(transport);
258                                 } });
259
260         } catch (e) {
261                 exception_error("init", e);
262         }
263 }
264
265 function init_second_stage() {
266
267         try {
268                 dojo.addOnLoad(function() {
269                         updateFeedList();
270                         closeArticlePanel();
271
272                         _widescreen_mode = getInitParam("widescreen");
273
274                         if (_widescreen_mode) {
275                                 switchPanelMode(_widescreen_mode);
276                         }
277
278                 });
279
280                 delCookie("ttrss_test");
281
282                 var toolbar = document.forms["main_toolbar_form"];
283
284                 dijit.getEnclosingWidget(toolbar.view_mode).attr('value',
285                         getInitParam("default_view_mode"));
286
287                 dijit.getEnclosingWidget(toolbar.order_by).attr('value',
288                         getInitParam("default_view_order_by"));
289
290                 feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
291
292                 var hash_feed_id = hash_get('f');
293                 var hash_feed_is_cat = hash_get('c') == "1";
294
295                 if (hash_feed_id != undefined) {
296                         setActiveFeedId(hash_feed_id, hash_feed_is_cat);
297                 }
298
299                 loading_set_progress(30);
300
301                 // can't use cache_clear() here because viewfeed might not have initialized yet
302                 if ('sessionStorage' in window && window['sessionStorage'] !== null)
303                         sessionStorage.clear();
304
305                 var hotkeys = getInitParam("hotkeys");
306                 var tmp = [];
307
308                 for (sequence in hotkeys[1]) {
309                         filtered = sequence.replace(/\|.*$/, "");
310                         tmp[filtered] = hotkeys[1][sequence];
311                 }
312
313                 hotkeys[1] = tmp;
314                 setInitParam("hotkeys", hotkeys);
315
316                 console.log("second stage ok");
317
318                 if (getInitParam("simple_update")) {
319                         console.log("scheduling simple feed updater...");
320                         window.setTimeout("update_random_feed()", 30*1000);
321                 }
322
323         } catch (e) {
324                 exception_error("init_second_stage", e);
325         }
326 }
327
328 function quickMenuGo(opid) {
329         try {
330                 switch (opid) {
331                 case "qmcPrefs":
332                         gotoPreferences();
333                         break;
334                 case "qmcLogout":
335                         gotoLogout();
336                         break;
337                 case "qmcTagCloud":
338                         displayDlg("printTagCloud");
339                         break;
340                 case "qmcTagSelect":
341                         displayDlg("printTagSelect");
342                         break;
343                 case "qmcSearch":
344                         search();
345                         break;
346                 case "qmcAddFeed":
347                         quickAddFeed();
348                         break;
349                 case "qmcDigest":
350                         window.location.href = "backend.php?op=digest";
351                         break;
352                 case "qmcEditFeed":
353                         if (activeFeedIsCat())
354                                 alert(__("You can't edit this kind of feed."));
355                         else
356                                 editFeed(getActiveFeedId());
357                         break;
358                 case "qmcRemoveFeed":
359                         var actid = getActiveFeedId();
360
361                         if (activeFeedIsCat()) {
362                                 alert(__("You can't unsubscribe from the category."));
363                                 return;
364                         }
365
366                         if (!actid) {
367                                 alert(__("Please select some feed first."));
368                                 return;
369                         }
370
371                         var fn = getFeedName(actid);
372
373                         var pr = __("Unsubscribe from %s?").replace("%s", fn);
374
375                         if (confirm(pr)) {
376                                 unsubscribeFeed(actid);
377                         }
378                         break;
379                 case "qmcCatchupAll":
380                         catchupAllFeeds();
381                         break;
382                 case "qmcShowOnlyUnread":
383                         toggleDispRead();
384                         break;
385                 case "qmcAddFilter":
386                         quickAddFilter();
387                         break;
388                 case "qmcAddLabel":
389                         addLabel();
390                         break;
391                 case "qmcRescoreFeed":
392                         rescoreCurrentFeed();
393                         break;
394                 case "qmcToggleWidescreen":
395                         if (!isCdmMode()) {
396                                 _widescreen_mode = !_widescreen_mode;
397
398                                 switchPanelMode(_widescreen_mode);
399                         }
400                         break;
401                 case "qmcHKhelp":
402                         helpDialog("main");
403                         break;
404                 default:
405                         console.log("quickMenuGo: unknown action: " + opid);
406                 }
407
408         } catch (e) {
409                 exception_error("quickMenuGo", e);
410         }
411 }
412
413 function toggleDispRead() {
414         try {
415
416                 var hide = !(getInitParam("hide_read_feeds") == "1");
417
418                 hideOrShowFeeds(hide);
419
420                 var query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" +
421                         param_escape(hide);
422
423                 setInitParam("hide_read_feeds", hide);
424
425                 new Ajax.Request("backend.php", {
426                         parameters: query,
427                         onComplete: function(transport) {
428                         } });
429
430         } catch (e) {
431                 exception_error("toggleDispRead", e);
432         }
433 }
434
435 function parse_runtime_info(data) {
436
437         //console.log("parsing runtime info...");
438
439         for (k in data) {
440                 var v = data[k];
441
442 //              console.log("RI: " + k + " => " + v);
443
444                 if (k == "new_version_available") {
445                         if (v == "1") {
446                                 Element.show(dijit.byId("newVersionIcon").domNode);
447                         } else {
448                                 Element.hide(dijit.byId("newVersionIcon").domNode);
449                         }
450                         return;
451                 }
452
453                 if (k == "dep_ts" && parseInt(getInitParam("dep_ts")) > 0) {
454                         if (parseInt(getInitParam("dep_ts")) < parseInt(v)) {
455                                 window.location.reload();
456                         }
457                 }
458
459                 if (k == "daemon_is_running" && v != 1) {
460                         notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
461                         return;
462                 }
463
464                 if (k == "daemon_stamp_ok" && v != 1) {
465                         notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
466                         return;
467                 }
468
469                 if (k == "max_feed_id" || k == "num_feeds") {
470                         if (init_params[k] != v) {
471                                 console.log("feed count changed, need to reload feedlist.");
472                                 updateFeedList();
473                         }
474                 }
475
476                 init_params[k] = v;
477                 notify('');
478         }
479 }
480
481 function collapse_feedlist() {
482         try {
483
484                 if (!Element.visible('feeds-holder')) {
485                         Element.show('feeds-holder');
486                         Element.show('feeds-holder_splitter');
487                         $("collapse_feeds_btn").innerHTML = "&lt;&lt;";
488                 } else {
489                         Element.hide('feeds-holder');
490                         Element.hide('feeds-holder_splitter');
491                         $("collapse_feeds_btn").innerHTML = "&gt;&gt;";
492                 }
493
494                 dijit.byId("main").resize();
495
496                 query = "?op=rpc&method=setpref&key=_COLLAPSED_FEEDLIST&value=true";
497                 new Ajax.Request("backend.php", { parameters: query });
498
499         } catch (e) {
500                 exception_error("collapse_feedlist", e);
501         }
502 }
503
504 function viewModeChanged() {
505         cache_clear();
506         return viewCurrentFeed('');
507 }
508
509 function viewLimitChanged() {
510         return viewCurrentFeed('');
511 }
512
513 function rescoreCurrentFeed() {
514
515         var actid = getActiveFeedId();
516
517         if (activeFeedIsCat() || actid < 0) {
518                 alert(__("You can't rescore this kind of feed."));
519                 return;
520         }
521
522         if (!actid) {
523                 alert(__("Please select some feed first."));
524                 return;
525         }
526
527         var fn = getFeedName(actid);
528         var pr = __("Rescore articles in %s?").replace("%s", fn);
529
530         if (confirm(pr)) {
531                 notify_progress("Rescoring articles...");
532
533                 var query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid;
534
535                 new Ajax.Request("backend.php", {
536                         parameters: query,
537                         onComplete: function(transport) {
538                                 viewCurrentFeed();
539                         } });
540         }
541 }
542
543 function hotkey_handler(e) {
544         try {
545
546                 if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
547
548                 var keycode = false;
549                 var shift_key = false;
550
551                 var cmdline = $('cmdline');
552
553                 shift_key = e.shiftKey;
554                 ctrl_key = e.ctrlKey;
555
556                 if (window.event) {
557                         keycode = window.event.keyCode;
558                 } else if (e) {
559                         keycode = e.which;
560                 }
561
562                 var keychar = String.fromCharCode(keycode);
563
564                 if (keycode == 27) { // escape
565                         hotkey_prefix = false;
566                 }
567
568                 if (keycode == 16) return; // ignore lone shift
569                 if (keycode == 17) return; // ignore lone ctrl
570
571                 keychar = keychar.toLowerCase();
572
573                 var hotkeys = getInitParam("hotkeys");
574
575                 if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
576
577                         var date = new Date();
578                         var ts = Math.round(date.getTime() / 1000);
579
580                         hotkey_prefix = keychar;
581                         hotkey_prefix_pressed = ts;
582
583                         cmdline.innerHTML = keychar;
584                         Element.show(cmdline);
585
586                         return true;
587                 }
588
589                 Element.hide(cmdline);
590
591                 var hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
592
593                 // ensure ^*char notation
594                 if (shift_key) hotkey = "*" + hotkey;
595                 if (ctrl_key) hotkey = "^" + hotkey;
596
597                 hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
598                 hotkey_prefix = false;
599
600                 var hotkey_action = false;
601                 var hotkeys = getInitParam("hotkeys");
602
603                 for (sequence in hotkeys[1]) {
604                         if (sequence == hotkey) {
605                                 hotkey_action = hotkeys[1][sequence];
606                                 break;
607                         }
608                 }
609
610                 switch (hotkey_action) {
611                 case "next_feed":
612                         var rv = dijit.byId("feedTree").getNextFeed(
613                                         getActiveFeedId(), activeFeedIsCat());
614
615                         if (rv) viewfeed(rv[0], '', rv[1]);
616                         return false;
617                 case "prev_feed":
618                         var rv = dijit.byId("feedTree").getPreviousFeed(
619                                         getActiveFeedId(), activeFeedIsCat());
620
621                         if (rv) viewfeed(rv[0], '', rv[1]);
622                         return false;
623                 case "next_article":
624                         moveToPost('next');
625                         return false;
626                 case "prev_article":
627                         moveToPost('prev');
628                         return false;
629                 case "next_article_noscroll":
630                         moveToPost('next', true);
631                         return false;
632                 case "prev_article_noscroll":
633                         moveToPost('prev', true);
634                         return false;
635                 case "search_dialog":
636                         search();
637                         return ;
638                 case "toggle_mark":
639                         selectionToggleMarked(undefined, false, true);
640                         return false;
641                 case "toggle_publ":
642                         selectionTogglePublished(undefined, false, true);
643                         return false;
644                 case "toggle_unread":
645                         selectionToggleUnread(undefined, false, true);
646                         return false;
647                 case "edit_tags":
648                         var id = getActiveArticleId();
649                         if (id) {
650                                 editArticleTags(id, getActiveFeedId(), isCdmMode());
651                                 return;
652                         }
653                         return false;
654                 case "dismiss_selected":
655                         dismissSelectedArticles();
656                         return false;
657                 case "dismiss_read":
658                         return false;
659                 case "open_in_new_window":
660                         if (getActiveArticleId()) {
661                                 openArticleInNewWindow(getActiveArticleId());
662                                 return;
663                         }
664                         return false;
665                 case "catchup_below":
666                         catchupRelativeToArticle(1);
667                         return false;
668                 case "catchup_above":
669                         catchupRelativeToArticle(0);
670                         return false;
671                 case "article_scroll_down":
672                         var ctr = $("content_insert") ? $("content_insert") : $("headlines-frame");
673
674                         scrollArticle(ctr.offsetHeight/3);
675                         return false;
676                 case "article_scroll_up":
677                         var ctr = $("content_insert") ? $("content_insert") : $("headlines-frame");
678
679                         scrollArticle(-ctr.offsetHeight/3);
680                         return false;
681                 case "close_article":
682                         if (isCdmMode()) {
683                                 if (!getInitParam("cdm_expanded")) {
684                                         cdmCollapseArticle(false, getActiveArticleId());
685                                 } else {
686                                         dismissArticle(getActiveArticleId());
687                                 }
688                         } else {
689                                 closeArticlePanel();
690                         }
691                         return false;
692                 case "email_article":
693                         if (typeof emailArticle != "undefined") {
694                                 emailArticle();
695                         } else if (typeof mailtoArticle != "undefined") {
696                                 mailtoArticle();
697                         } else {
698                                 alert(__("Please enable mail plugin first."));
699                         }
700                         return false;
701                 case "select_all":
702                         selectArticles('all');
703                         return false;
704                 case "select_unread":
705                         selectArticles('unread');
706                         return false;
707                 case "select_marked":
708                         selectArticles('marked');
709                         return false;
710                 case "select_published":
711                         selectArticles('published');
712                         return false;
713                 case "select_invert":
714                         selectArticles('invert');
715                         return false;
716                 case "select_none":
717                         selectArticles('none');
718                         return false;
719                 case "feed_refresh":
720                         if (getActiveFeedId() != undefined) {
721                                 viewfeed(getActiveFeedId(), '', activeFeedIsCat());
722                                 return;
723                         }
724                         return false;
725                 case "feed_unhide_read":
726                         toggleDispRead();
727                         return false;
728                 case "feed_subscribe":
729                         quickAddFeed();
730                         return false;
731                 case "feed_debug_update":
732                         window.open("backend.php?op=feeds&method=view&feed=" + getActiveFeedId() +
733                                 "&view_mode=adaptive&order_by=default&update=&m=ForceUpdate&cat=" +
734                                 activeFeedIsCat() + "&DevForceUpdate=1&debug=1&xdebug=1&csrf_token=" +
735                                 getInitParam("csrf_token"));
736                         return false;
737                 case "feed_edit":
738                         if (activeFeedIsCat())
739                                 alert(__("You can't edit this kind of feed."));
740                         else
741                                 editFeed(getActiveFeedId());
742                         return false;
743                 case "feed_catchup":
744                         if (getActiveFeedId() != undefined) {
745                                 catchupCurrentFeed();
746                                 return;
747                         }
748                         return false;
749                 case "feed_reverse":
750                         reverseHeadlineOrder();
751                         return false;
752                 case "catchup_all":
753                         catchupAllFeeds();
754                         return false;
755                 case "cat_toggle_collapse":
756                         if (activeFeedIsCat()) {
757                                 dijit.byId("feedTree").collapseCat(getActiveFeedId());
758                                 return;
759                         }
760                         return false;
761                 case "goto_all":
762                         viewfeed(-4);
763                         return false;
764                 case "goto_fresh":
765                         viewfeed(-3);
766                         return false;
767                 case "goto_marked":
768                         viewfeed(-1);
769                         return false;
770                 case "goto_published":
771                         viewfeed(-2);
772                         return false;
773                 case "goto_tagcloud":
774                         displayDlg("printTagCloud");
775                         return false;
776                 case "goto_prefs":
777                         gotoPreferences();
778                         return false;
779                 case "select_article_cursor":
780                         var id = getArticleUnderPointer();
781                         if (id) {
782                                 var cb = dijit.byId("RCHK-" + id);
783                                 if (cb) {
784                                         cb.attr("checked", !cb.attr("checked"));
785                                         toggleSelectRowById(cb, "RROW-" + id);
786                                         return false;
787                                 }
788                         }
789                         return false;
790                 case "create_label":
791                         addLabel();
792                         return false;
793                 case "create_filter":
794                         quickAddFilter();
795                         return false;
796                 case "collapse_sidebar":
797                         collapse_feedlist();
798                         return false;
799                 case "toggle_embed_original":
800                         if (typeof embedOriginalArticle != "undefined") {
801                                 if (getActiveArticleId())
802                                         embedOriginalArticle(getActiveArticleId());
803                         } else {
804                                 alert(__("Please enable embed_original plugin first."));
805                         }
806                         return false;
807                 case "toggle_widescreen":
808                         if (!isCdmMode()) {
809                                 _widescreen_mode = !_widescreen_mode;
810
811                                 switchPanelMode(_widescreen_mode);
812                         }
813                         return false;
814                 case "help_dialog":
815                         helpDialog("main");
816                         return false;
817                 case "toggle_combined_mode":
818                         notify_progress("Loading, please wait...");
819
820                         var value = isCdmMode() ? "false" : "true";
821                         var query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value;
822
823                         new Ajax.Request("backend.php", {
824                                 parameters: query,
825                                 onComplete: function(transport) {
826                                         window.location.reload();
827                                 } });
828
829                         return false;
830                 default:
831                         console.log("unhandled action: " + hotkey_action + "; hotkey: " + hotkey);
832                 }
833
834         } catch (e) {
835                 exception_error("hotkey_handler", e);
836         }
837 }
838
839 function inPreferences() {
840         return false;
841 }
842
843 function reverseHeadlineOrder() {
844         try {
845
846                 var query_str = "?op=rpc&method=togglepref&key=REVERSE_HEADLINES";
847
848                 new Ajax.Request("backend.php", {
849                         parameters: query_str,
850                         onComplete: function(transport) {
851                                         viewCurrentFeed();
852                                 } });
853
854         } catch (e) {
855                 exception_error("reverseHeadlineOrder", e);
856         }
857 }
858
859 function newVersionDlg() {
860         try {
861                 var query = "backend.php?op=dlg&method=newVersion";
862
863                 if (dijit.byId("newVersionDlg"))
864                         dijit.byId("newVersionDlg").destroyRecursive();
865
866                 dialog = new dijit.Dialog({
867                         id: "newVersionDlg",
868                         title: __("New version available!"),
869                         style: "width: 600px",
870                         href: query,
871                 });
872
873                 dialog.show();
874
875         } catch (e) {
876                 exception_error("newVersionDlg", e);
877         }
878 }
879
880 function handle_rpc_json(transport, scheduled_call) {
881         try {
882                 var reply = JSON.parse(transport.responseText);
883
884                 if (reply) {
885
886                         var error = reply['error'];
887
888                         if (error) {
889                                 var code = error['code'];
890                                 var msg = error['msg'];
891
892                                 console.warn("[handle_rpc_json] received fatal error " + code + "/" + msg);
893
894                                 if (code != 0) {
895                                         fatalError(code, msg);
896                                         return false;
897                                 }
898                         }
899
900                         var seq = reply['seq'];
901
902                         if (seq) {
903                                 if (get_seq() != seq) {
904                                         console.log("[handle_rpc_json] sequence mismatch: " + seq +
905                                                 " (want: " + get_seq() + ")");
906                                         return true;
907                                 }
908                         }
909
910                         var message = reply['message'];
911
912                         if (message) {
913                                 if (message == "UPDATE_COUNTERS") {
914                                         console.log("need to refresh counters...");
915                                         setInitParam("last_article_id", -1);
916                                         request_counters(true);
917                                 }
918                         }
919
920                         var counters = reply['counters'];
921
922                         if (counters)
923                                 parse_counters(counters, scheduled_call);
924
925                         var runtime_info = reply['runtime-info'];;
926
927                         if (runtime_info)
928                                 parse_runtime_info(runtime_info);
929
930                         Element.hide(dijit.byId("net-alert").domNode);
931
932                 } else {
933                         //notify_error("Error communicating with server.");
934                         Element.show(dijit.byId("net-alert").domNode);
935                 }
936
937         } catch (e) {
938                 Element.show(dijit.byId("net-alert").domNode);
939                 //notify_error("Error communicating with server.");
940                 console.log(e);
941                 //exception_error("handle_rpc_json", e, transport);
942         }
943
944         return true;
945 }
946
947 function switchPanelMode(wide) {
948         try {
949                 if (isCdmMode()) return;
950
951                 article_id = getActiveArticleId();
952
953                 if (wide) {
954                         dijit.byId("headlines-wrap-inner").attr("design", 'sidebar');
955                         dijit.byId("content-insert").attr("region", "trailing");
956
957                         dijit.byId("content-insert").domNode.setStyle({width: '50%',
958                                 height: 'auto',
959                                 borderLeftWidth: '1px',
960                                 borderLeftColor: '#c0c0c0',
961                                 borderTopWidth: '0px' });
962
963                         $("headlines-toolbar").setStyle({ borderBottomWidth: '0px' });
964
965                 } else {
966
967                         dijit.byId("content-insert").attr("region", "bottom");
968
969                         dijit.byId("content-insert").domNode.setStyle({width: 'auto',
970                                 height: '50%',
971                                 borderLeftWidth: '0px',
972                                 borderTopWidth: '1px'});
973
974                         $("headlines-toolbar").setStyle({ borderBottomWidth: '1px' });
975                 }
976
977                 closeArticlePanel();
978
979                 if (article_id) view(article_id);
980
981                 new Ajax.Request("backend.php", {
982                         parameters: "op=rpc&method=setpanelmode&wide=" + (wide ? 1 : 0),
983                         onComplete: function(transport) {
984                                 console.log(transport.responseText);
985                         } });
986
987
988         } catch (e) {
989                 exception_error("switchPanelMode", e);
990         }
991 }
992
993 function update_random_feed() {
994         try {
995                 console.log("in update_random_feed");
996
997                 new Ajax.Request("backend.php", {
998                         parameters: "op=rpc&method=updateRandomFeed",
999                         onComplete: function(transport) {
1000                                 handle_rpc_json(transport, true);
1001                                 window.setTimeout("update_random_feed()", 30*1000);
1002                         } });
1003
1004         } catch (e) {
1005                 exception_error("update_random_feed", e);
1006         }
1007 }
1008
1009 function hash_get(key) {
1010         try {
1011                 kv = window.location.hash.substring(1).toQueryParams();
1012                 return kv[key];
1013         } catch (e) {
1014                 exception_error("hash_set", e);
1015         }
1016 }
1017 function hash_set(key, value) {
1018         try {
1019                 kv = window.location.hash.substring(1).toQueryParams();
1020                 kv[key] = value;
1021                 window.location.hash = $H(kv).toQueryString();
1022         } catch (e) {
1023                 exception_error("hash_set", e);
1024         }
1025 }