miniflux-legacy/assets/js/app.js

408 lines
9.3 KiB
JavaScript
Raw Normal View History

2013-02-24 20:04:56 +01:00
(function() {
var feeds = [];
var queue = [];
var queue_length = 5;
2013-04-03 04:49:14 +02:00
function switch_status(item_id)
{
var request = new XMLHttpRequest();
2013-04-03 04:49:14 +02:00
request.onreadystatechange = function() {
if (request.readyState === 4 && is_listing()) {
2013-04-03 04:49:14 +02:00
var response = JSON.parse(request.responseText);
2013-04-03 04:49:14 +02:00
if (response.status == "read" || response.status == "unread") {
find_next_item();
remove_item(response.item_id);
}
}
2013-04-03 04:49:14 +02:00
}
request.open("POST", "?action=change-item-status&id=" + item_id, true);
2013-04-03 04:49:14 +02:00
request.send();
}
function mark_as_read(item_id)
{
var request = new XMLHttpRequest();
request.onload = function() {
remove_item(item_id);
};
request.open("POST", "?action=mark-item-read&id=" + item_id, true);
request.send();
}
2013-04-03 04:49:14 +02:00
function mark_as_unread(item_id)
{
var request = new XMLHttpRequest();
request.open("POST", "?action=mark-item-unread&id=" + item_id, true);
2013-04-03 04:49:14 +02:00
request.send();
}
2013-06-15 05:12:08 +02:00
function bookmark_item()
{
var item = document.getElementById("current-item");
if (item) {
var item_id = item.getAttribute("data-item-id");
var redirect = item.getAttribute("data-item-page");
window.location = "?action=bookmark&value=1&id=" + item_id + "&redirect=" + redirect;
}
}
2013-02-24 20:04:56 +01:00
function show_refresh_icon(feed_id)
{
var container = document.getElementById("loading-feed-" + feed_id);
if (container) {
var img = document.createElement("img");
img.src = "./assets/img/refresh.gif";
container.appendChild(img);
}
}
function hide_refresh_icon(feed_id)
{
var container = document.getElementById("loading-feed-" + feed_id);
if (container) {
container.innerHTML = "";
}
}
function refresh_feed(feed_id, callback)
{
if (! feed_id) {
return false;
}
show_refresh_icon(feed_id);
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
2013-02-24 20:04:56 +01:00
if (request.readyState === 4) {
2013-02-24 20:04:56 +01:00
hide_refresh_icon(feed_id);
2013-02-24 20:04:56 +01:00
try {
2013-02-24 20:04:56 +01:00
var response = JSON.parse(this.responseText);
2013-02-24 20:04:56 +01:00
if (callback) {
callback(response);
}
2013-02-24 20:04:56 +01:00
if (! response.result) {
2013-02-24 20:04:56 +01:00
//window.alert('Unable to refresh this feed: ' + feed_id);
}
2013-02-24 20:04:56 +01:00
}
catch (e) {}
2013-02-24 20:04:56 +01:00
}
};
request.open("POST", "?action=refresh-feed&feed_id=" + feed_id, true);
2013-02-24 20:04:56 +01:00
request.send();
return true;
}
function get_feeds()
{
var links = document.getElementsByTagName("a");
for (var i = 0, ilen = links.length; i < ilen; i++) {
var feed_id = links[i].getAttribute('data-feed-id');
if (feed_id) {
feeds.push(parseInt(feed_id));
}
}
}
function refresh_all()
{
get_feeds();
var interval = setInterval(function() {
while (feeds.length > 0 && queue.length < queue_length) {
var feed_id = feeds.shift();
queue.push(feed_id);
refresh_feed(feed_id, function(response) {
var index = queue.indexOf(response.feed_id);
if (index >= 0) {
queue.splice(index, 1);
}
if (feeds.length == 0 && queue.length == 0) {
clearInterval(interval);
window.location.href = "?action=unread";
}
});
}
}, 100);
}
2013-04-03 04:49:14 +02:00
function remove_item(item_id)
{
var item = document.getElementById("item-" + item_id);
if (item) item.parentNode.removeChild(item);
var container = document.getElementById("page-counter");
if (container) {
counter = parseInt(container.textContent.trim(), 10) - 1;
if (counter == 0) {
window.location = "?action=feeds&nothing_to_read=1";
}
else {
container.textContent = counter + " ";
document.title = "miniflux (" + counter + ")";
document.getElementById("nav-counter").textContent = "(" + counter + ")";
}
}
2013-04-03 04:49:14 +02:00
}
function open_original_item()
{
var link = document.getElementById("original-item");
if (link) {
if (link.getAttribute("data-action") == "mark-read") {
mark_as_read(link.getAttribute("data-item-id"));
find_next_item();
}
2013-04-03 04:49:14 +02:00
link.click();
}
}
function open_item()
{
var link = document.getElementById("open-item");
if (link) link.click();
}
function open_next_item()
{
var link = document.getElementById("next-item");
if (link) {
link.click();
}
else if (is_listing()) {
find_next_item();
}
}
function open_previous_item()
{
var link = document.getElementById("previous-item");
if (link) {
link.click();
}
else if (is_listing()) {
find_previous_item();
}
}
function change_item_status()
{
2013-06-15 05:12:08 +02:00
if (is_listing() && ! document.getElementById("current-item")) {
document.querySelector("article").id = "current-item";
}
2013-04-03 04:49:14 +02:00
var item = document.getElementById("current-item");
if (item) switch_status(item.getAttribute("data-item-id"));
}
function set_links_item(item_id)
{
var link = document.getElementById("original-item");
if (link) link.id = "original-" + link.getAttribute("data-item-id");
var link = document.getElementById("open-item");
if (link) link.id = "open-" + link.getAttribute("data-item-id");
var link = document.getElementById("original-" + item_id);
if (link) link.id = "original-item";
var link = document.getElementById("open-" + item_id);
if (link) link.id = "open-item";
}
function find_next_item()
{
var items = document.getElementsByTagName("article");
if (! document.getElementById("current-item")) {
items[0].id = "current-item";
set_links_item(items[0].getAttribute("data-item-id"));
}
else {
for (var i = 0, ilen = items.length; i < ilen; i++) {
if (items[i].id == "current-item") {
items[i].id = "item-" + items[i].getAttribute("data-item-id");
if (i + 1 < ilen) {
items[i + 1].id = "current-item";
set_links_item(items[i + 1].getAttribute("data-item-id"));
}
break;
}
}
}
}
function find_previous_item()
{
var items = document.getElementsByTagName("article");
if (! document.getElementById("current-item")) {
items[items.length - 1].id = "current-item";
set_links_item(items[items.length - 1].getAttribute("data-item-id"));
}
else {
for (var i = items.length - 1; i >= 0; i--) {
if (items[i].id == "current-item") {
items[i].id = "item-" + items[i].getAttribute("data-item-id");
if (i - 1 >= 0) {
items[i - 1].id = "current-item";
set_links_item(items[i - 1].getAttribute("data-item-id"));
}
break;
}
}
}
}
function is_listing()
{
if (document.getElementById("listing")) {
return true;
}
return false;
}
2013-02-24 20:04:56 +01:00
document.onclick = function(e) {
var action = e.target.getAttribute("data-action");
if (action) {
switch (action) {
case 'refresh-all':
e.preventDefault();
2013-02-24 20:04:56 +01:00
refresh_all();
break;
case 'refresh-feed':
e.preventDefault();
2013-02-24 20:04:56 +01:00
var feed_id = e.target.getAttribute("data-feed-id");
refresh_feed(feed_id);
break;
case 'mark-read':
var item_id = e.target.getAttribute("data-item-id");
mark_as_read(item_id);
break;
2013-02-24 20:04:56 +01:00
}
}
};
2013-04-03 04:49:14 +02:00
document.onkeypress = function(e) {
switch (e.keyCode || e.which) {
case 112:
open_previous_item();
break;
case 110:
open_next_item();
break;
case 118:
open_original_item();
break;
case 111:
open_item();
break;
case 109:
change_item_status();
break;
2013-06-15 05:12:08 +02:00
case 102:
bookmark_item();
break;
2013-04-03 04:49:14 +02:00
}
};
2013-02-24 20:04:56 +01:00
})();