miniflux-legacy/assets/js/event.js

220 lines
7.9 KiB
JavaScript
Raw Normal View History

2013-10-15 04:38:07 +02:00
Miniflux.Event = (function() {
var queue = [];
function isEventIgnored(e)
{
if (e.keyCode !== 63 && e.which !== 63 && (e.ctrlKey || e.shiftKey || e.altKey || e.metaKey)) {
return true;
}
// Do not handle events when there is a focus in form fields
var target = e.target || e.srcElement;
if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') {
return true;
}
return false;
}
2013-10-15 04:38:07 +02:00
return {
lastEventType: "",
2013-10-15 04:38:07 +02:00
ListenMouseEvents: function() {
document.onclick = function(e) {
if (e.target.hasAttribute("data-action") && e.target.className !== 'original') {
e.preventDefault();
}
};
document.onmouseup = function(e) {
// ignore right mouse button (context menu)
if (e.button === 2) {
return;
}
2013-10-15 04:38:07 +02:00
// Auto-select input content
if (e.target.nodeName === "INPUT" && e.target.className === "auto-select") {
e.target.select();
return;
}
// Application actions
2013-10-15 04:38:07 +02:00
var action = e.target.getAttribute("data-action");
if (action) {
Miniflux.Event.lastEventType = "mouse";
var currentItem = function () {
element = e.target;
while (element && element.parentNode) {
element = element.parentNode;
if (element.tagName && element.tagName.toLowerCase() === 'article') {
return element;
}
}
return;
}();
2013-10-15 04:38:07 +02:00
switch (action) {
case 'refresh-all':
Miniflux.Feed.UpdateAll();
break;
case 'refresh-feed':
currentItem && Miniflux.Feed.Update(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 'mark-read':
currentItem && Miniflux.Item.MarkAsRead(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 'mark-unread':
currentItem && Miniflux.Item.MarkAsUnread(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 'mark-removed':
currentItem && Miniflux.Item.MarkAsRemoved(currentItem);
break;
2013-10-15 04:38:07 +02:00
case 'bookmark':
currentItem && Miniflux.Item.SwitchBookmark(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 'download-item':
currentItem && Miniflux.Item.DownloadContent(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 'mark-all-read':
Miniflux.Item.MarkListingAsRead("?action=unread");
break;
case 'mark-feed-read':
Miniflux.Item.MarkFeedAsRead(e.target.getAttribute("data-feed-id"));
2013-10-15 04:38:07 +02:00
break;
}
}
};
},
ListenKeyboardEvents: function() {
document.onkeypress = function(e) {
if (isEventIgnored(e)) {
return;
}
Miniflux.Event.lastEventType = "keyboard";
2013-10-15 04:38:07 +02:00
queue.push(e.keyCode || e.which);
if (queue[0] === 103) { // g
2013-10-15 04:38:07 +02:00
switch (queue[1]) {
case undefined:
break;
case 117: // u
window.location.href = "?action=unread";
queue = [];
break;
case 98: // b
window.location.href = "?action=bookmarks";
queue = [];
break;
case 104: // h
window.location.href = "?action=history";
queue = [];
break;
case 115: // s
window.location.href = "?action=feeds";
queue = [];
break;
case 112: // p
window.location.href = "?action=config";
queue = [];
break;
default:
queue = [];
break;
}
}
else {
queue = [];
var currentItem = function () {
return document.getElementById("current-item");
}();
2013-10-15 04:38:07 +02:00
switch (e.keyCode || e.which) {
case 100: // d
currentItem && Miniflux.Item.DownloadContent(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 112: // p
case 107: // k
Miniflux.Nav.SelectPreviousItem();
break;
case 110: // n
case 106: // j
Miniflux.Nav.SelectNextItem();
break;
case 118: // v
currentItem && Miniflux.Item.OpenOriginal(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 111: // o
currentItem && Miniflux.Item.Show(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 109: // m
currentItem && Miniflux.Item.SwitchStatus(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 102: // f
currentItem && Miniflux.Item.SwitchBookmark(currentItem);
2013-10-15 04:38:07 +02:00
break;
case 104: // h
Miniflux.Nav.OpenPreviousPage();
break
case 108: // l
Miniflux.Nav.OpenNextPage();
break;
case 114: // r
Miniflux.Feed.UpdateAll();
break;
2013-10-15 04:38:07 +02:00
case 63: // ?
Miniflux.Nav.ShowHelp();
break;
case 122: // z
Miniflux.Item.ToggleRTLMode();
break;
2013-10-15 04:38:07 +02:00
}
}
};
document.onkeydown = function(e) {
if (isEventIgnored(e)) {
return;
}
Miniflux.Event.lastEventType = "keyboard";
switch (e.keyCode || e.which) {
case 37: // left arrow
Miniflux.Nav.SelectPreviousItem();
break;
case 39: // right arrow
Miniflux.Nav.SelectNextItem();
break;
}
implement frontend autoupdate Only the unread counter is updated right know. The AutoUpdate Feature is designed on the premise of don't wasting resources. A distinction is made between updates when Miniflux is visible or hidden. To determine the visibility status, the Page Visibility API is used. The API is available starting with Chrome 33, Firefox 18 and IE10. [https://developer.mozilla.org/en-US/docs/Web/Guide/User_experience/Using_the_Page_Visibility_API] As IE9 returns an undefined, it doesn't break the compatibility at least. If Miniflux is visible, the unread counter on the web page is updated as soon as a mismatch between the counter and the number of unread articles in the database is found. If Miniflux is hidden, the timestamp of the most recent article from each feed is compared with the value from the last run. We have an update If the timestamp of the latest article is greater than the stored one and the latest article is unread. The web page title is updated with a ? symbol to notify the user and the update check pauses till Miniflux gets visible again. If Miniflux gets visible again, the number of unread articles is queried from the database, the unread counter on the web page is updated and finally the ? symbol is removed from the web page title. This way I can use my fever API client to read new articles (or at least the latest article) while Miniflux is hidden and as I've seen the new articles already a new articles notification is prevented. It's intentionally that the page does not reload automatically as long as articles are visible. If I'm in hurry, I only scroll through the articles to spot something interesting. Most of the time I don't reach the last article. If the page is reloaded while I'm away, I would have to scan from the top again. If we're on a nothing_to_read page and have unread articles in the database, a redirect to the unread page will be done. The default update check interval is 10 minutes and can be changed on the settings page. A zero value disables the update check entirely. fixes #213
2014-11-27 22:36:04 +01:00
};
},
ListenVisibilityEvents: function() {
document.addEventListener('visibilitychange', function() {
Miniflux.App.Log('document.visibilityState: ' + document.visibilityState);
if (!document.hidden && Miniflux.Item.hasNewUnread()) {
Miniflux.App.Log('Need to update the unread counter with fresh values from the database');
Miniflux.Item.CheckForUpdates();
}
});
2013-10-15 04:38:07 +02:00
}
};
})();