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