miniflux-legacy/assets/js/item.js
Mathias Kresin a4d8abb631 Move update icon and update icon blinking to css
Use CSS3 Animation for the "loading icon" blinking and move the definition of
the loading icon to CSS as well.

CSS3 Animations are supported by IE10, Firefox and using the -webkit prefix by
Chrome and Safari.

I've dropped the usage of element.classList in favour of supporting refreshing/
article downloading in IE9 (again). The classList stuff isn't necessarily
needed, as the elements in question only get one or no class assigned.

I've dropped no longer supported css options like:

the appearance option isn't supported by any browser, the -webkit-appearance
doesn't change anything visible.

-webkit-font-smoothing support was dropped in chrome 22 and font-smoothing
isn't specified anywhere.
2014-12-26 23:01:05 +01:00

250 lines
7.9 KiB
JavaScript

Miniflux.Item = (function() {
function getItemID(item)
{
item_id = item.getAttribute("data-item-id");
return item_id;
}
function changeLabel(link)
{
if (link && link.hasAttribute("data-reverse-label")) {
var content = link.innerHTML;
link.innerHTML = link.getAttribute("data-reverse-label");
link.setAttribute("data-reverse-label", content);
}
}
function changeBookmarkLabel(item)
{
var link = item.querySelector("a.bookmark");
changeLabel(link);
}
function changeStatusLabel(item)
{
var link = item.querySelector("a.mark");
changeLabel(link);
}
function showItemAsRead(item)
{
if (item.getAttribute("data-hide")) {
hideItem(item);
}
else {
item.setAttribute("data-item-status", "read");
changeStatusLabel(item);
// Change action
var link = item.querySelector("a.mark");
if (link) link.setAttribute("data-action", "mark-unread");
}
}
function showItemAsUnread(item)
{
if (item.getAttribute("data-hide")) {
hideItem(item);
}
else {
item.setAttribute("data-item-status", "unread");
changeStatusLabel(item);
// Change action
var link = item.querySelector("a.mark");
if (link) link.setAttribute("data-action", "mark-read");
}
}
function hideItem(item)
{
if (Miniflux.Event.lastEventType !== "mouse") {
Miniflux.Nav.SelectNextItem();
}
item.parentNode.removeChild(item);
var pageCounter = document.getElementById("page-counter");
if (pageCounter) {
var source = item.getAttribute("data-item-page");
var counter = parseInt(pageCounter.textContent, 10) - 1;
var articles = document.getElementsByTagName("article");
if (counter === 0 || articles.length === 0) {
window.location = location.href;
}
pageCounter.textContent = counter;
switch (source) {
case "unread":
document.title = "Miniflux (" + counter + ")";
document.getElementById("nav-counter").textContent = counter;
break;
case "feed-items":
document.title = "(" + counter + ") " + pageCounter.parentNode.firstChild.nodeValue;
break;
default:
document.title = pageCounter.parentNode.firstChild.nodeValue + " (" + counter + ")";
}
}
}
function markAsRead(item)
{
var item_id = getItemID(item);
var request = new XMLHttpRequest();
request.onload = function() {
if (Miniflux.Nav.IsListing()) showItemAsRead(item);
};
request.open("POST", "?action=mark-item-read&id=" + item_id, true);
request.send();
}
function markAsUnread(item)
{
var item_id = getItemID(item);
var request = new XMLHttpRequest();
request.onload = function() {
if (Miniflux.Nav.IsListing()) showItemAsUnread(item);
};
request.open("POST", "?action=mark-item-unread&id=" + item_id, true);
request.send();
}
function markAsRemoved(item)
{
var item_id = getItemID(item);
var request = new XMLHttpRequest();
request.onload = function() {
if (Miniflux.Nav.IsListing()) hideItem(item);
};
request.open("POST", "?action=mark-item-removed&id=" + item_id, true);
request.send();
}
return {
MarkAsRead: markAsRead,
MarkAsUnread: markAsUnread,
MarkAsRemoved: markAsRemoved,
SwitchBookmark: function(item) {
var item_id = getItemID(item);
var value = item.getAttribute("data-item-bookmark") === "1" ? "0" : "1";
var request = new XMLHttpRequest();
request.onload = function() {
if (Miniflux.Nav.IsListing() && item.getAttribute("data-item-page") === "bookmarks") {
hideItem(item);
}
else {
item.setAttribute("data-item-bookmark", value);
if (Miniflux.Nav.IsListing()) {
changeBookmarkLabel(item);
}
else {
var link = item.querySelector("a.bookmark-icon");
if (link && link.hasAttribute("data-reverse-title")) {
var title = link.getAttribute("title");
link.setAttribute("title", link.getAttribute("data-reverse-title"));
link.setAttribute("data-reverse-title", title);
}
}
}
};
request.open("POST", "?action=bookmark&id=" + item_id + "&value=" + value, true);
request.send();
},
SwitchStatus: function(item) {
var status = item.getAttribute("data-item-status");
if (status === "read") {
markAsUnread(item);
}
else if (status === "unread") {
markAsRead(item);
}
},
Show: function(item) {
var link = item.querySelector("a.show");
if (link) link.click();
},
OpenOriginal: function(item) {
var link = item.querySelector("a.original");
if (link) {
if (item.getAttribute("data-item-status") === "unread") markAsRead(item);
link.removeAttribute("data-action");
link.click();
}
},
DownloadContent: function(item) {
var container = document.getElementById("download-item");
if (! container) return;
container.innerHTML = " " + container.getAttribute("data-before-message");
container.className = "loading-icon";
var request = new XMLHttpRequest();
request.onload = function() {
var response = JSON.parse(request.responseText);
container.className = "";
if (response.result) {
var content = document.getElementById("item-content");
if (content) content.innerHTML = response.content;
container.innerHTML = container.getAttribute("data-after-message");
}
else {
container.innerHTML = container.getAttribute("data-failure-message");
}
};
var item_id = getItemID(item);
request.open("POST", "?action=download-item&id=" + item_id, true);
request.send();
},
MarkListingAsRead: function(redirect) {
var articles = document.getElementsByTagName("article");
var listing = [];
for (var i = 0, ilen = articles.length; i < ilen; i++) {
listing.push(getItemID(articles[i]));
}
var request = new XMLHttpRequest();
request.onload = function() {
window.location.href = redirect;
};
request.open("POST", "?action=mark-items-as-read", true);
request.send(JSON.stringify(listing));
},
ToggleRTLMode: function() {
var tags = [
"#current-item h1",
"#item-content",
"#current-item h2",
"#current-item .preview"
];
for (var i = 0; i < tags.length; i++) {
var tag = document.querySelector(tags[i]);
if (tag) {
tag.dir = tag.dir == "" ? "rtl" : "";
}
}
}
};
})();