miniflux-legacy/assets/js/app.js

483 lines
12 KiB
JavaScript
Raw Normal View History

2013-02-24 20:04:56 +01:00
(function() {
var feeds = [];
var queue = [];
var queue_length = 5;
function switch_status(item_id, hide)
{
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();
if (hide) remove_item(response.item_id);
2013-04-03 04:49:14 +02:00
}
}
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_all_as_read()
{
var $articles = document.getElementsByTagName("article");
var idlist = [];
for (var i=0;i<$articles.length;i++)
{
idlist.push($articles[i].getAttribute("data-item-id"));
}
var request = new XMLHttpRequest();
request.onload = function() {
window.location.href = "?action=unread";
};
request.open("POST", "?action=mark-all-read", true);
request.send(JSON.stringify(idlist));
}
2013-04-03 04:49:14 +02:00
function mark_as_read(item_id)
{
var request = new XMLHttpRequest();
request.onload = function() {
//find_next_item();
2013-04-03 04:49:14 +02:00
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.onload = function() {
//find_next_item();
remove_item(item_id);
};
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 link = document.getElementById("bookmark-" + item_id);
2013-06-15 05:12:08 +02:00
if (link) link.click();
2013-06-15 05:12:08 +02:00
}
}
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 = "";
2013-02-24 20:04:56 +01:00
}
function refresh_feed(feed_id, callback)
{
if (! feed_id) return false;
2013-02-24 20:04:56 +01:00
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
}
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-07-06 04:37:19 +02:00
function open_next_page()
{
var link = document.getElementById("next-page");
if (link) link.click();
}
function open_previous_page()
{
var link = document.getElementById("previous-page");
if (link) link.click();
}
2013-04-03 04:49:14 +02:00
function remove_item(item_id)
{
var item = document.getElementById("item-" + item_id);
if (! item) {
item = document.getElementById("current-item");
if (item.getAttribute("data-item-id") != item_id) item = false;
}
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 (is_listing() && link.getAttribute("data-hide")) {
mark_as_read(link.getAttribute("data-item-id"));
}
link.removeAttribute("data-action");
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")) {
find_next_item();
}
2013-04-03 04:49:14 +02:00
var item = document.getElementById("current-item");
if (item) {
switch_status(item.getAttribute("data-item-id"), item.getAttribute("data-hide"));
}
2013-04-03 04:49:14 +02:00
}
2013-07-11 04:33:23 +02:00
function scroll_page_to(item)
{
var clientHeight = pageYOffset + document.documentElement.clientHeight;
var itemPosition = item.offsetTop + item.offsetHeight;
2013-07-11 04:33:23 +02:00
if (clientHeight - itemPosition < 0 || clientHeight - item.offsetTop > document.documentElement.clientHeight) {
2013-07-11 04:33:23 +02:00
window.scrollTo(0, item.offsetTop - 10);
}
}
2013-04-03 04:49:14 +02:00
2013-07-11 04:33:23 +02:00
2013-04-03 04:49:14 +02:00
function set_links_item(item_id)
{
var link = document.getElementById("current-item");
if (link) scroll_page_to(link);
2013-04-03 04:49:14 +02:00
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;
2013-04-03 04:49:14 +02:00
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':
e.preventDefault();
var item_id = e.target.getAttribute("data-item-id");
mark_as_read(item_id);
break;
case 'mark-unread':
e.preventDefault();
var item_id = e.target.getAttribute("data-item-id");
mark_as_unread(item_id);
break;
case 'mark-all-read':
e.preventDefault();
mark_all_as_read();
break;
case 'original-link':
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) {
2013-07-03 03:50:53 +02:00
case 112: // p
case 107: // k
2013-04-03 04:49:14 +02:00
open_previous_item();
break;
2013-07-03 03:50:53 +02:00
case 110: // n
case 106: // j
2013-04-03 04:49:14 +02:00
open_next_item();
break;
2013-07-03 03:50:53 +02:00
case 118: // v
2013-04-03 04:49:14 +02:00
open_original_item();
break;
2013-07-03 03:50:53 +02:00
case 111: // o
2013-04-03 04:49:14 +02:00
open_item();
break;
2013-07-03 03:50:53 +02:00
case 109: // m
2013-04-03 04:49:14 +02:00
change_item_status();
break;
2013-07-03 03:50:53 +02:00
case 102: // f
2013-06-15 05:12:08 +02:00
bookmark_item();
break;
2013-07-06 04:37:19 +02:00
case 104: // h
open_previous_page();
break
case 108: // l
open_next_page();
break;
2013-04-03 04:49:14 +02:00
}
};
})();
2013-07-10 16:47:54 +02:00