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