Commit 999e8e60 authored by yPhil's avatar yPhil
Browse files

PTL.feed.js backPorts

parent de6626a9
Pipeline #417379 passed with stage
in 56 seconds
......@@ -2,15 +2,11 @@
PTL.feed = {
add:function($column, url, name, type, limit, status, iconhash, nbitems, lastitem, clickNew, isQueryString, progress) {
add: function($column, url, name, type, limit, status, iconhash, nbitems, lastitem, clickNew, isQueryString, progress) {
const $feed = $('<li>')
.attr('class', 'feed');
// const errVar = true;
// errVar = 'plop';
const $feedImg = $('<img>')
.attr({
src: '/static/images/rss.gif',
......@@ -20,14 +16,18 @@ PTL.feed = {
})
.on("error", function() {
$(this).attr('src', '/static/images/rss.gif');
$(this).parent().parent().children('div.dataStore').data('iconhash', '');
PTL.tab.saveTabs();
});
const $newItemsBadge = $('<div>')
.attr('class', 'newItemsBadge hidden');
.attr({class: 'newItemsBadge hidden', title:PTL.tr('New items')});
const $feedIcon = $('<i>')
.attr({'class': 'feed-control feedIcon translate',
'title': PTL.tr('Fold / unfold this feed (%1)', url)})
.attr({
'class': 'feed-control feedIcon translate',
'title': PTL.tr('Fold / unfold this feed (%1)', url)
})
.data('title', 'Fold / unfold this feed (%1)', url)
.click(function() {
......@@ -65,7 +65,7 @@ PTL.feed = {
});
const $deleteIcon = $('<i>')
.attr('class', 'feed-control translate icon-cancel feed-delete')
.attr('class', 'feed-control translate icon-trash-empty feed-delete')
.data('title', 'Delete this feed (%1)', url)
.attr('title', PTL.tr('Delete this feed (%1)', url))
.click(function() {
......@@ -89,7 +89,6 @@ PTL.feed = {
.data('title', 'Refresh this feed (%1)', url)
.attr('title', PTL.tr('Refresh this feed (%1)', url))
.click(async function() {
// var plop = $(this).parent().parent().parent().next('div.feedBody').find('li').lengh;
$('.selected').removeClass('selected');
$('.icon-checked').toggleClass('icon-checked icon-checkbox');
await PTL.feed.populate($(this), progress);
......@@ -128,7 +127,7 @@ PTL.feed = {
.data('title', PTL.tr('Move this feed (%1)', url))
.attr({
title: PTL.tr('Move this feed (%1)', url),
class:'feedHandle'
class: 'feedHandle'
});
const $feedBody = $('<div>').attr('class', 'feedBody').css('height', limit),
......@@ -142,7 +141,7 @@ PTL.feed = {
const $titleDiv = $('<div>')
.attr({
title: name || PTL.tr('New feed'),
class:'feedTitle trucate'
class: 'feedTitle trucate'
});
const $titleLink = $('<a>')
......@@ -150,9 +149,9 @@ PTL.feed = {
.attr('target', '_blank')
.html(name || PTL.tr('New feed'));
$feedControls.hover (
function() {$(this).find('.collapsible').show();},
function() {$(this).find('.collapsible').hide('fade', 'fast');}
$feedControls.hover(
function() { $(this).find('.collapsible').show(); },
function() { $(this).find('.collapsible').hide('fade', 'fast'); }
);
if (!PTL.util.isMobile()) {
......@@ -164,7 +163,7 @@ PTL.feed = {
}
$feedHeader.hover (function() {
$feedHeader.hover(function() {
$(this).find('img.favicon').hide();
$feedIcon.removeClass('icon-rss').addClass('icon-down-circle');
......@@ -193,12 +192,10 @@ PTL.feed = {
}
},
lastItems:function(feedItems, $dataStore) {
lastItems: function(feedItems, $dataStore) {
return new Promise((resolve, reject) => {
// console.log('feedItems: %s (%s)', JSON.stringify(feedItems));
const $feedBodyUl = $('<ul>').attr('class', 'feedBody'),
feedUrl = $dataStore.data('url'),
nbItems = $dataStore.data('nbitems'),
......@@ -265,7 +262,6 @@ PTL.feed = {
if (!imageUrl && typeof $tempDom.find('img').attr('src') !== 'undefined') {
imageUrl = $tempDom.find('img').attr('src');
if (typeof $tempDom.find('img').attr('title') !== 'undefined') {
// XKCD summary in the title of the description XML tag
$feedItem.attr('title', $tempDom.find('img').attr('title'));
}
......@@ -316,7 +312,7 @@ PTL.feed = {
if (!videoUrl && imageUrl && typeof imageUrl !== 'undefined' && !imageUrl.includes('pixel')) {
$imageLink
.attr('href', imageUrl.replace('http://','https://'))
.attr('href', imageUrl.replace('http://', 'https://'))
.attr('title', $summary.trim())
.attr('data-fancybox', 'gallery')
.attr('data-caption', '<a href="' + item.link + '" class="ui-button ui-corner-all" title="' + $summary.trim() + '">' + item.title + '</a>');
......@@ -327,7 +323,7 @@ PTL.feed = {
$image = $('<img>')
.attr('src', '/static/images/loading.gif')
.attr('data-srcset', imageUrl.replace('http://','https://'))
.attr('data-srcset', imageUrl.replace('http://', 'https://'))
.attr('srcset', '/static/images/loading.gif')
.attr('title', $summary.trim())
.attr('alt', item['mastodon:scope'] ? $summary.trim() : item.title)
......@@ -366,7 +362,7 @@ PTL.feed = {
});
},
appendAudioPlayer:function($itemDiv, audioUrl, audioType) {
appendAudioPlayer: function($itemDiv, audioUrl, audioType) {
const $audioLink = $('<a>').attr('target', '_blank').attr('class', 'audioLink'),
$audioIcon = $('<i>'),
......@@ -387,7 +383,7 @@ PTL.feed = {
.appendTo($audioLink);
},
appendVideoPlayer:function($itemDiv, videoUrl, videoType) {
appendVideoPlayer: function($itemDiv, videoUrl, videoType) {
const videoPlayer = document.createElement('video'),
$videoIcon = $('<i>'),
......@@ -410,33 +406,41 @@ PTL.feed = {
return $itemDiv;
},
errorFeed:function(error, feedUrl) {
console.log('pow: %s', feedUrl);
errorFeed: function(error, feedUrl) {
const type = (error.statusText) ? error.statusText : PTL.tr('Unknown error');
const errno = (error.responseJSON) ? error.responseJSON.errno : '0';
const message = (error.responseJSON) ? error.responseJSON.message : 'Empty';
const status = error.status;
const message = error.message;
const type = error.type;
const $validateLink = $('<a>')
.attr('href', 'https://validator.w3.org/feed/check.cgi?url=' + feedUrl);
const $validateLinkIcon = $('<i>')
.attr('class', 'itemIcon icon-w3c')
.attr('title', PTL.tr('Validate /verify this feed file with the W3C'))
.appendTo($validateLink);
const $reportLink = $('<a>')
.attr('href', 'https://framagit.org/yphil/petrolette/-/issues/new?issue[title]=Feed%20error&issue[description]=' + feedUrl + ' (' + type + ')');
const $reportLinkIcon = $('<i>')
.attr('class', 'itemIcon icon-petrolette')
.attr('title', PTL.tr('Report feed error'))
.appendTo($reportLink);
const $errKey = $('<strong>')
.attr('class', 'translate key')
.data('content', PTL.tr('Error:'))
.text(PTL.tr('Error:'));
.data('content', PTL.tr('Type'))
.text(PTL.tr('Type'));
const $msgKey = $('<strong>')
.attr('class', 'translate key')
.data('content', PTL.tr('Message:'))
.text(PTL.tr('Message:'));
.data('content', PTL.tr('Message'))
.text(PTL.tr('Message'));
const $errValue = $('<strong>')
.attr('class', 'value')
.text(type + ' (' + errno + ')');
.text(type + ' (' + status + ')');
const $msgValue = $('<strong>')
.attr('class', 'value')
......@@ -444,13 +448,14 @@ PTL.feed = {
const $errorItem = $('<li>')
.attr('class', 'feedItem error')
.append($validateLink, $reportLink)
.append($validateLink)
.append($reportLink)
.append($errKey)
.append('&nbsp;')
.append('&nbsp;:&nbsp;')
.append($errValue)
.append('<br>')
.append($msgKey)
.append('&nbsp;')
.append('&nbsp;:&nbsp;')
.append($msgValue);
const $feedBodyUl = $('<ul>').attr('class', 'feedBody')
......@@ -459,7 +464,7 @@ PTL.feed = {
return $feedBodyUl;
},
fetchIcon:function(feedHost) {
fetchIcon: function(feedHost) {
return new Promise((resolve, reject) => {
......@@ -478,7 +483,7 @@ PTL.feed = {
});
},
fetchFeed:function(feedUrl, lastItem, nbItems) {
fetchFeed: function(feedUrl, lastItem, nbItems) {
return new Promise((resolve, reject) => {
$.get("/feed", {
......@@ -486,15 +491,15 @@ PTL.feed = {
dataType: 'json',
lastItem: lastItem,
nbItems: nbItems
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, _textStatus, jqXHR) {
resolve(data);
}).fail(function(jqXHR, textStatus, errorThrown) {
reject(jqXHR);
reject(jqXHR, textStatus, errorThrown);
});
});
},
populate:async function($button, progress, newLimit) {
populate: async function($button, progress, newLimit) {
const $dataStore = $button.parent().parent(),
$refreshButton = $dataStore.find('i.feedRefresh.icon-refresh').addClass('spin'),
......@@ -517,8 +522,6 @@ PTL.feed = {
var feedLastItem = $dataStore.data('lastitem');
$feedBodyUl.css('border', '1px solid red');
if ($dataStore.data('status') == 'on') {
$feedIcon.removeClass('fold');
} else {
......@@ -529,22 +532,35 @@ PTL.feed = {
.addClass('folded');
}
// console.log('lastItem: %s', feedLastItem, feedUrl);
if ($dataStore.data('status') == 'on') {
try {
$feedIcon.removeClass('fold');
$refreshButton.addClass('spin');
let fetchFeed = await PTL.feed.fetchFeed(feedUrl, feedLastItem);
let lastItems = await PTL.feed.lastItems(fetchFeed.feedItems, $dataStore);
if (fetchFeed.error) {
$feedBody
.empty()
.append(PTL.feed.errorFeed(fetchFeed.error, feedUrl))
.css('height', '');
$feedLink.addClass('danger');
$refreshButton.removeClass('spin');
} else {
$feedBody.html(lastItems[0]);
$refreshButton.removeClass('spin');
$feedLink
.text(fetchFeed.feedTitle)
.attr('href', fetchFeed.feedLink);
$dataStore.data('name', fetchFeed.feedTitle);
if (fetchFeed.totalNewItems > 0) {
$dataStore.data('lastitem', fetchFeed.lastItem);
PTL.tab.saveTabs();
......@@ -553,97 +569,35 @@ PTL.feed = {
$badge.fadeOut('slow');
}
}
} else {
$dataStore
.parent()
.parent()
.children('div.feedBody')
.addClass('folded');
}
if (feedIconHash) {
$favIcon.attr('src', '/favicons/' + feedIconHash + '.favicon');
} else {
await PTL.feed.fetchIcon(feedHost).then((iconhash) => {
await PTL.feed.fetchIcon(feedHost)
.then((iconhash) => {
if (iconhash) {
$favIcon.attr('src', '/favicons/' + iconhash + '.favicon');
$dataStore.data('iconhash', iconhash);
PTL.tab.saveTabs();
}
}).catch((_error) => {
console.log('Fav error: %s (%s)', feedIconHash, feedUrl);
$favIcon.addClass('icon-rss');
$dataStore.data('iconhash', '');
PTL.tab.saveTabs();
});
}
} catch (error) {
console.log('Catched error: %s (%s)', JSON.stringify(error));
$feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
$feedBody.css('height', '');
$refreshButton.removeClass('spin');
}
} else {
$dataStore
.parent()
.parent()
.children('div.feedBody')
.addClass('folded');
}
if (progress) progress.increment();
// try {
// let feedInDb = await PTL.db.get(feedUrl);
// if (feedInDb) {
// $feedBody.html(feedInDb.content);
// } else {
// feedLastItem = 'none';
// }
// try {
// console.log('feedLastItem: %s (%s)', feedLastItem);
// let fetchFeed = await PTL.feed.fetchFeed(feedUrl, feedLastItem);
// console.log('fetchFeed: %s (%s)', JSON.stringify(fetchFeed));
// // $feedLink
// // .text(fetchFeed.feedTitle)
// // .attr('title', fetchFeed.feedTitle + ' (' + feedUrl + ')');
// if (fetchFeed.thereAreNewItems) {
// $dataStore.data('lastitem', fetchFeed.lastItem);
// let lastItems = await PTL.feed.lastItems(fetchFeed.feedItems, $dataStore);
// $feedBody.prepend(lastItems[0]);
// PTL.db.put(feedUrl, $feedBody.html());
// $badge.text(lastItems[1]).fadeIn('slow');
// } else {
// // $dataStore.data('lastitem', 'none');
// $badge.fadeOut('slow');
// }
// $refreshButton
// .prop('title', PTL.tr('Refresh this feed (%1 - %2)', fetchFeed.feedTitle, timeStamp))
// .removeClass('spin');
// } catch (error) {
// console.log('Catched error: %s (%s)', JSON.stringify(error));
// $feedBody.empty().append(PTL.feed.errorFeed(error, feedUrl));
// $feedBody.css('height', '');
// $refreshButton.removeClass('spin');
// }
// } catch (error) {
// console.log('getFeed caught: %s (%s)', error);
// }
if (progress) progress.increment();
}
......
......@@ -88,7 +88,6 @@
<script src="/bower/DOMPurify/dist/purify.min.js"></script>
<script src="/static/js/PTL.main.js"></script>
<script src="/static/js/PTL.db.js"></script>
<script src="/static/js/PTL.prefs.js"></script>
<script src="/static/js/PTL.i18n.js"></script>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment