1 var global_unread = -1;
2 var hotkey_prefix = false;
3 var hotkey_prefix_pressed = false;
4 var _widescreen_mode = false;
6 var _active_feed_id = 0;
7 var _active_feed_is_cat = false;
18 function activeFeedIsCat() {
19 return _active_feed_is_cat;
22 function getActiveFeedId() {
24 return _active_feed_id;
26 exception_error("getActiveFeedId", e);
30 function setActiveFeedId(id, is_cat) {
33 hash_set('c', is_cat ? 1 : 0);
36 _active_feed_is_cat = is_cat;
38 selectFeed(id, is_cat);
40 exception_error("setActiveFeedId", e);
45 function updateFeedList() {
48 // $("feeds-holder").innerHTML = "<div id=\"feedlistLoading\">" +
49 // __("Loading, please wait...") + "</div>";
51 Element.show("feedlistLoading");
53 if (dijit.byId("feedTree")) {
54 dijit.byId("feedTree").destroyRecursive();
57 var store = new dojo.data.ItemFileWriteStore({
58 url: "backend.php?op=pref_feeds&method=getfeedtree&mode=2"});
60 var treeModel = new fox.FeedStoreModel({
63 "type": getInitParam('enable_feed_cats') == 1 ? "category" : "feed"
67 childrenAttrs: ["items"]
70 var tree = new fox.FeedTree({
72 onClick: function (item, node) {
73 var id = String(item.id);
74 var is_cat = id.match("^CAT:");
75 var feed = id.substr(id.indexOf(":")+1);
76 viewfeed(feed, '', is_cat);
84 /* var menu = new dijit.Menu({id: 'feedMenu'});
86 menu.addChild(new dijit.MenuItem({
87 label: "Simple menu item"
90 // menu.bindDomNode(tree.domNode); */
92 var tmph = dojo.connect(dijit.byId('feedMenu'), '_openMyself', function (event) {
93 console.log(dijit.getEnclosingWidget(event.target));
94 dojo.disconnect(tmph);
97 $("feeds-holder").appendChild(tree.domNode);
99 var tmph = dojo.connect(tree, 'onLoad', function() {
100 dojo.disconnect(tmph);
101 Element.hide("feedlistLoading");
105 // var node = dijit.byId("feedTree")._itemNodesMap['FEED:-2'][0].domNode
106 // menu.bindDomNode(node);
108 loading_set_progress(25);
114 exception_error("updateFeedList", e);
118 function catchupAllFeeds() {
120 var str = __("Mark all articles as read?");
122 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
124 var query_str = "backend.php?op=feeds&method=catchupAll";
126 notify_progress("Marking all feeds as read...");
128 //console.log("catchupAllFeeds Q=" + query_str);
130 new Ajax.Request("backend.php", {
131 parameters: query_str,
132 onComplete: function(transport) {
133 feedlist_callback2(transport);
141 function viewCurrentFeed(method) {
142 console.log("viewCurrentFeed");
144 if (getActiveFeedId() != undefined) {
145 viewfeed(getActiveFeedId(), method, activeFeedIsCat());
147 return false; // block unneeded form submits
151 if (getInitParam("bw_limit") != "1") {
153 setTimeout("timeout()", 60*1000);
158 var query = "backend.php?op=dlg&method=search¶m=" +
159 param_escape(getActiveFeedId() + ":" + activeFeedIsCat());
161 if (dijit.byId("searchDlg"))
162 dijit.byId("searchDlg").destroyRecursive();
164 dialog = new dijit.Dialog({
167 style: "width: 600px",
168 execute: function() {
169 if (this.validate()) {
170 _search_query = dojo.objectToQuery(this.attr('value'));
180 function updateTitle() {
181 var tmp = document.title;
183 if (tmp.indexOf(")") > 0) {
184 tmp = tmp.substr(tmp.indexOf(")") + 1);
187 if (global_unread > 0) {
188 tmp = "(" + global_unread + ") " + tmp;
192 if (global_unread > 0) {
193 window.fluid.dockBadge = global_unread;
195 window.fluid.dockBadge = "";
199 document.title = tmp;
202 function genericSanityCheck() {
203 setCookie("ttrss_test", "TEST");
205 if (getCookie("ttrss_test") != "TEST") {
206 return fatalError(2);
214 //dojo.registerModulePath("fox", "../../js/");
216 dojo.require("fox.FeedTree");
218 dojo.require("dijit.ColorPalette");
219 dojo.require("dijit.Dialog");
220 dojo.require("dijit.form.Button");
221 dojo.require("dijit.form.CheckBox");
222 dojo.require("dijit.form.DropDownButton");
223 dojo.require("dijit.form.FilteringSelect");
224 dojo.require("dijit.form.Form");
225 dojo.require("dijit.form.RadioButton");
226 dojo.require("dijit.form.Select");
227 dojo.require("dijit.form.SimpleTextarea");
228 dojo.require("dijit.form.TextBox");
229 dojo.require("dijit.form.ValidationTextBox");
230 dojo.require("dijit.InlineEditBox");
231 dojo.require("dijit.layout.AccordionContainer");
232 dojo.require("dijit.layout.BorderContainer");
233 dojo.require("dijit.layout.ContentPane");
234 dojo.require("dijit.layout.TabContainer");
235 dojo.require("dijit.Menu");
236 dojo.require("dijit.ProgressBar");
237 dojo.require("dijit.ProgressBar");
238 dojo.require("dijit.Toolbar");
239 dojo.require("dijit.Tree");
240 dojo.require("dijit.tree.dndSource");
241 dojo.require("dojo.data.ItemFileWriteStore");
245 if (!genericSanityCheck())
248 loading_set_progress(20);
250 var hasAudio = !!((myAudioTag = document.createElement('audio')).canPlayType);
251 var hasSandbox = "sandbox" in document.createElement("iframe");
253 new Ajax.Request("backend.php", {
254 parameters: {op: "rpc", method: "sanityCheck", hasAudio: hasAudio,
255 hasSandbox: hasSandbox},
256 onComplete: function(transport) {
257 backend_sanity_check_callback(transport);
261 exception_error("init", e);
265 function init_second_stage() {
268 dojo.addOnLoad(function() {
272 _widescreen_mode = getInitParam("widescreen");
274 if (_widescreen_mode) {
275 switchPanelMode(_widescreen_mode);
280 delCookie("ttrss_test");
282 var toolbar = document.forms["main_toolbar_form"];
284 dijit.getEnclosingWidget(toolbar.view_mode).attr('value',
285 getInitParam("default_view_mode"));
287 dijit.getEnclosingWidget(toolbar.order_by).attr('value',
288 getInitParam("default_view_order_by"));
290 feeds_sort_by_unread = getInitParam("feeds_sort_by_unread") == 1;
292 var hash_feed_id = hash_get('f');
293 var hash_feed_is_cat = hash_get('c') == "1";
295 if (hash_feed_id != undefined) {
296 setActiveFeedId(hash_feed_id, hash_feed_is_cat);
299 loading_set_progress(30);
301 // can't use cache_clear() here because viewfeed might not have initialized yet
302 if ('sessionStorage' in window && window['sessionStorage'] !== null)
303 sessionStorage.clear();
305 var hotkeys = getInitParam("hotkeys");
308 for (sequence in hotkeys[1]) {
309 filtered = sequence.replace(/\|.*$/, "");
310 tmp[filtered] = hotkeys[1][sequence];
314 setInitParam("hotkeys", hotkeys);
316 console.log("second stage ok");
318 if (getInitParam("simple_update")) {
319 console.log("scheduling simple feed updater...");
320 window.setTimeout("update_random_feed()", 30*1000);
324 exception_error("init_second_stage", e);
328 function quickMenuGo(opid) {
338 displayDlg("printTagCloud");
341 displayDlg("printTagSelect");
350 window.location.href = "backend.php?op=digest";
353 if (activeFeedIsCat())
354 alert(__("You can't edit this kind of feed."));
356 editFeed(getActiveFeedId());
358 case "qmcRemoveFeed":
359 var actid = getActiveFeedId();
361 if (activeFeedIsCat()) {
362 alert(__("You can't unsubscribe from the category."));
367 alert(__("Please select some feed first."));
371 var fn = getFeedName(actid);
373 var pr = __("Unsubscribe from %s?").replace("%s", fn);
376 unsubscribeFeed(actid);
379 case "qmcCatchupAll":
382 case "qmcShowOnlyUnread":
391 case "qmcRescoreFeed":
392 rescoreCurrentFeed();
394 case "qmcToggleWidescreen":
396 _widescreen_mode = !_widescreen_mode;
398 switchPanelMode(_widescreen_mode);
405 console.log("quickMenuGo: unknown action: " + opid);
409 exception_error("quickMenuGo", e);
413 function toggleDispRead() {
416 var hide = !(getInitParam("hide_read_feeds") == "1");
418 hideOrShowFeeds(hide);
420 var query = "?op=rpc&method=setpref&key=HIDE_READ_FEEDS&value=" +
423 setInitParam("hide_read_feeds", hide);
425 new Ajax.Request("backend.php", {
427 onComplete: function(transport) {
431 exception_error("toggleDispRead", e);
435 function parse_runtime_info(data) {
437 //console.log("parsing runtime info...");
442 // console.log("RI: " + k + " => " + v);
444 if (k == "new_version_available") {
446 Element.show(dijit.byId("newVersionIcon").domNode);
448 Element.hide(dijit.byId("newVersionIcon").domNode);
453 if (k == "dep_ts" && parseInt(getInitParam("dep_ts")) > 0) {
454 if (parseInt(getInitParam("dep_ts")) < parseInt(v)) {
455 window.location.reload();
459 if (k == "daemon_is_running" && v != 1) {
460 notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
464 if (k == "daemon_stamp_ok" && v != 1) {
465 notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
469 if (k == "max_feed_id" || k == "num_feeds") {
470 if (init_params[k] != v) {
471 console.log("feed count changed, need to reload feedlist.");
481 function collapse_feedlist() {
484 if (!Element.visible('feeds-holder')) {
485 Element.show('feeds-holder');
486 Element.show('feeds-holder_splitter');
487 $("collapse_feeds_btn").innerHTML = "<<";
489 Element.hide('feeds-holder');
490 Element.hide('feeds-holder_splitter');
491 $("collapse_feeds_btn").innerHTML = ">>";
494 dijit.byId("main").resize();
496 query = "?op=rpc&method=setpref&key=_COLLAPSED_FEEDLIST&value=true";
497 new Ajax.Request("backend.php", { parameters: query });
500 exception_error("collapse_feedlist", e);
504 function viewModeChanged() {
506 return viewCurrentFeed('');
509 function viewLimitChanged() {
510 return viewCurrentFeed('');
513 function rescoreCurrentFeed() {
515 var actid = getActiveFeedId();
517 if (activeFeedIsCat() || actid < 0) {
518 alert(__("You can't rescore this kind of feed."));
523 alert(__("Please select some feed first."));
527 var fn = getFeedName(actid);
528 var pr = __("Rescore articles in %s?").replace("%s", fn);
531 notify_progress("Rescoring articles...");
533 var query = "?op=pref-feeds&method=rescore&quiet=1&ids=" + actid;
535 new Ajax.Request("backend.php", {
537 onComplete: function(transport) {
543 function hotkey_handler(e) {
546 if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") return;
549 var shift_key = false;
551 var cmdline = $('cmdline');
553 shift_key = e.shiftKey;
554 ctrl_key = e.ctrlKey;
557 keycode = window.event.keyCode;
562 var keychar = String.fromCharCode(keycode);
564 if (keycode == 27) { // escape
565 hotkey_prefix = false;
568 if (keycode == 16) return; // ignore lone shift
569 if (keycode == 17) return; // ignore lone ctrl
571 keychar = keychar.toLowerCase();
573 var hotkeys = getInitParam("hotkeys");
575 if (!hotkey_prefix && hotkeys[0].indexOf(keychar) != -1) {
577 var date = new Date();
578 var ts = Math.round(date.getTime() / 1000);
580 hotkey_prefix = keychar;
581 hotkey_prefix_pressed = ts;
583 cmdline.innerHTML = keychar;
584 Element.show(cmdline);
589 Element.hide(cmdline);
591 var hotkey = keychar.search(/[a-zA-Z0-9]/) != -1 ? keychar : "(" + keycode + ")";
593 // ensure ^*char notation
594 if (shift_key) hotkey = "*" + hotkey;
595 if (ctrl_key) hotkey = "^" + hotkey;
597 hotkey = hotkey_prefix ? hotkey_prefix + " " + hotkey : hotkey;
598 hotkey_prefix = false;
600 var hotkey_action = false;
601 var hotkeys = getInitParam("hotkeys");
603 for (sequence in hotkeys[1]) {
604 if (sequence == hotkey) {
605 hotkey_action = hotkeys[1][sequence];
610 switch (hotkey_action) {
612 var rv = dijit.byId("feedTree").getNextFeed(
613 getActiveFeedId(), activeFeedIsCat());
615 if (rv) viewfeed(rv[0], '', rv[1]);
618 var rv = dijit.byId("feedTree").getPreviousFeed(
619 getActiveFeedId(), activeFeedIsCat());
621 if (rv) viewfeed(rv[0], '', rv[1]);
629 case "next_article_noscroll":
630 moveToPost('next', true);
632 case "prev_article_noscroll":
633 moveToPost('prev', true);
635 case "search_dialog":
639 selectionToggleMarked(undefined, false, true);
642 selectionTogglePublished(undefined, false, true);
644 case "toggle_unread":
645 selectionToggleUnread(undefined, false, true);
648 var id = getActiveArticleId();
650 editArticleTags(id, getActiveFeedId(), isCdmMode());
654 case "dismiss_selected":
655 dismissSelectedArticles();
659 case "open_in_new_window":
660 if (getActiveArticleId()) {
661 openArticleInNewWindow(getActiveArticleId());
665 case "catchup_below":
666 catchupRelativeToArticle(1);
668 case "catchup_above":
669 catchupRelativeToArticle(0);
671 case "article_scroll_down":
672 var ctr = $("content_insert") ? $("content_insert") : $("headlines-frame");
674 scrollArticle(ctr.offsetHeight/3);
676 case "article_scroll_up":
677 var ctr = $("content_insert") ? $("content_insert") : $("headlines-frame");
679 scrollArticle(-ctr.offsetHeight/3);
681 case "close_article":
683 if (!getInitParam("cdm_expanded")) {
684 cdmCollapseArticle(false, getActiveArticleId());
686 dismissArticle(getActiveArticleId());
692 case "email_article":
693 if (typeof emailArticle != "undefined") {
695 } else if (typeof mailtoArticle != "undefined") {
698 alert(__("Please enable mail plugin first."));
702 selectArticles('all');
704 case "select_unread":
705 selectArticles('unread');
707 case "select_marked":
708 selectArticles('marked');
710 case "select_published":
711 selectArticles('published');
713 case "select_invert":
714 selectArticles('invert');
717 selectArticles('none');
720 if (getActiveFeedId() != undefined) {
721 viewfeed(getActiveFeedId(), '', activeFeedIsCat());
725 case "feed_unhide_read":
728 case "feed_subscribe":
731 case "feed_debug_update":
732 window.open("backend.php?op=feeds&method=view&feed=" + getActiveFeedId() +
733 "&view_mode=adaptive&order_by=default&update=&m=ForceUpdate&cat=" +
734 activeFeedIsCat() + "&DevForceUpdate=1&debug=1&xdebug=1&csrf_token=" +
735 getInitParam("csrf_token"));
738 if (activeFeedIsCat())
739 alert(__("You can't edit this kind of feed."));
741 editFeed(getActiveFeedId());
744 if (getActiveFeedId() != undefined) {
745 catchupCurrentFeed();
750 reverseHeadlineOrder();
755 case "cat_toggle_collapse":
756 if (activeFeedIsCat()) {
757 dijit.byId("feedTree").collapseCat(getActiveFeedId());
770 case "goto_published":
773 case "goto_tagcloud":
774 displayDlg("printTagCloud");
779 case "select_article_cursor":
780 var id = getArticleUnderPointer();
782 var cb = dijit.byId("RCHK-" + id);
784 cb.attr("checked", !cb.attr("checked"));
785 toggleSelectRowById(cb, "RROW-" + id);
793 case "create_filter":
796 case "collapse_sidebar":
799 case "toggle_embed_original":
800 if (typeof embedOriginalArticle != "undefined") {
801 if (getActiveArticleId())
802 embedOriginalArticle(getActiveArticleId());
804 alert(__("Please enable embed_original plugin first."));
807 case "toggle_widescreen":
809 _widescreen_mode = !_widescreen_mode;
811 switchPanelMode(_widescreen_mode);
817 case "toggle_combined_mode":
818 notify_progress("Loading, please wait...");
820 var value = isCdmMode() ? "false" : "true";
821 var query = "?op=rpc&method=setpref&key=COMBINED_DISPLAY_MODE&value=" + value;
823 new Ajax.Request("backend.php", {
825 onComplete: function(transport) {
826 window.location.reload();
831 console.log("unhandled action: " + hotkey_action + "; hotkey: " + hotkey);
835 exception_error("hotkey_handler", e);
839 function inPreferences() {
843 function reverseHeadlineOrder() {
846 var query_str = "?op=rpc&method=togglepref&key=REVERSE_HEADLINES";
848 new Ajax.Request("backend.php", {
849 parameters: query_str,
850 onComplete: function(transport) {
855 exception_error("reverseHeadlineOrder", e);
859 function newVersionDlg() {
861 var query = "backend.php?op=dlg&method=newVersion";
863 if (dijit.byId("newVersionDlg"))
864 dijit.byId("newVersionDlg").destroyRecursive();
866 dialog = new dijit.Dialog({
868 title: __("New version available!"),
869 style: "width: 600px",
876 exception_error("newVersionDlg", e);
880 function handle_rpc_json(transport, scheduled_call) {
882 var reply = JSON.parse(transport.responseText);
886 var error = reply['error'];
889 var code = error['code'];
890 var msg = error['msg'];
892 console.warn("[handle_rpc_json] received fatal error " + code + "/" + msg);
895 fatalError(code, msg);
900 var seq = reply['seq'];
903 if (get_seq() != seq) {
904 console.log("[handle_rpc_json] sequence mismatch: " + seq +
905 " (want: " + get_seq() + ")");
910 var message = reply['message'];
913 if (message == "UPDATE_COUNTERS") {
914 console.log("need to refresh counters...");
915 setInitParam("last_article_id", -1);
916 request_counters(true);
920 var counters = reply['counters'];
923 parse_counters(counters, scheduled_call);
925 var runtime_info = reply['runtime-info'];;
928 parse_runtime_info(runtime_info);
930 Element.hide(dijit.byId("net-alert").domNode);
933 //notify_error("Error communicating with server.");
934 Element.show(dijit.byId("net-alert").domNode);
938 Element.show(dijit.byId("net-alert").domNode);
939 //notify_error("Error communicating with server.");
941 //exception_error("handle_rpc_json", e, transport);
947 function switchPanelMode(wide) {
949 if (isCdmMode()) return;
951 article_id = getActiveArticleId();
954 dijit.byId("headlines-wrap-inner").attr("design", 'sidebar');
955 dijit.byId("content-insert").attr("region", "trailing");
957 dijit.byId("content-insert").domNode.setStyle({width: '50%',
959 borderLeftWidth: '1px',
960 borderLeftColor: '#c0c0c0',
961 borderTopWidth: '0px' });
963 $("headlines-toolbar").setStyle({ borderBottomWidth: '0px' });
967 dijit.byId("content-insert").attr("region", "bottom");
969 dijit.byId("content-insert").domNode.setStyle({width: 'auto',
971 borderLeftWidth: '0px',
972 borderTopWidth: '1px'});
974 $("headlines-toolbar").setStyle({ borderBottomWidth: '1px' });
979 if (article_id) view(article_id);
981 new Ajax.Request("backend.php", {
982 parameters: "op=rpc&method=setpanelmode&wide=" + (wide ? 1 : 0),
983 onComplete: function(transport) {
984 console.log(transport.responseText);
989 exception_error("switchPanelMode", e);
993 function update_random_feed() {
995 console.log("in update_random_feed");
997 new Ajax.Request("backend.php", {
998 parameters: "op=rpc&method=updateRandomFeed",
999 onComplete: function(transport) {
1000 handle_rpc_json(transport, true);
1001 window.setTimeout("update_random_feed()", 30*1000);
1005 exception_error("update_random_feed", e);
1009 function hash_get(key) {
1011 kv = window.location.hash.substring(1).toQueryParams();
1014 exception_error("hash_set", e);
1017 function hash_set(key, value) {
1019 kv = window.location.hash.substring(1).toQueryParams();
1021 window.location.hash = $H(kv).toQueryString();
1023 exception_error("hash_set", e);