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