summaryrefslogtreecommitdiff
path: root/p/scripts
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2014-01-28 00:18:30 +0100
committerGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2014-01-28 00:18:30 +0100
commita6f122e03cebd9b2ceed029344ddd2e72af460e3 (patch)
tree640ddacbc6eabc985646dd65b0578f369ea92321 /p/scripts
parent2fe2f876eac5e5fb8730000ef3d32ab45eefa8ea (diff)
Formatage des nombres #2
https://github.com/marienfressinaud/FreshRSS/pull/398 Je suis reparti du commit 7a510af73a0ef04ce09fb7eedd98c844e7bff51c, ai ajouté la gestion des espaces à une fonction de conversion des entiers, corrigé ce qui devait être fait côté PHP, et remis manuellement les patchs intermédiaires (j'espère ne pas avoir oublié de corrections). Le code est même plus simple qu'avant. Testé aussi sur titre et favoris
Diffstat (limited to 'p/scripts')
-rw-r--r--p/scripts/main.js56
1 files changed, 28 insertions, 28 deletions
diff --git a/p/scripts/main.js b/p/scripts/main.js
index 921005e3c..f0d2bbf7b 100644
--- a/p/scripts/main.js
+++ b/p/scripts/main.js
@@ -20,7 +20,17 @@ function redirect(url, new_tab) {
}
}
+function str2int(str) {
+ if (str == '') {
+ return 0;
+ }
+ return parseInt(str.replace(/\D/g, ''), 10) || 0;
+}
+
function numberFormat(nStr) {
+ if (nStr < 0) {
+ return 0;
+ }
// http://www.mredkj.com/javascript/numberFormat.html
nStr += '';
var x = nStr.split('.'),
@@ -34,33 +44,32 @@ function numberFormat(nStr) {
}
function incLabel(p, inc) {
- var i = (parseInt(p.replace(/\D/g, ''), 10) || 0) + inc;
+ var i = str2int(p) + inc;
return i > 0 ? ' (' + numberFormat(i) + ')' : '';
}
function incUnreadsFeed(article, feed_id, nb) {
-
//Update unread: feed
var elem = $('#' + feed_id + '>.feed').get(0),
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0,
- feed_priority = elem ? (parseInt(elem.getAttribute('data-priority'), 10) || 0) : 0;
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0,
+ feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
if (elem) {
- elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb)));
+ elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
}
//Update unread: category
elem = $('#' + feed_id).parent().prevAll('.category').children(':first').get(0);
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0;
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
if (elem) {
- elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb)));
+ elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
}
//Update unread: all
if (feed_priority > 0) {
elem = $('#aside_flux .all').children(':first').get(0);
if (elem) {
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0;
- elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb)));
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
+ elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
}
}
@@ -68,28 +77,22 @@ function incUnreadsFeed(article, feed_id, nb) {
if (article && article.closest('div').hasClass('favorite')) {
elem = $('#aside_flux .favorites').children(':first').get(0);
if (elem) {
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0;
- elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb)));
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
+ elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
}
}
var isCurrentView = false;
//Update unread: title
- document.title = document.title.replace(/^([^\(]*)((?: \([0-9 ]+\))?)( · .*?)((?: \([0-9 ]+\))?)$/, function(m, p1, p2, p3, p4) {
+ document.title = document.title.replace(/((?: \([ 0-9]+\))?)( · .*?)((?: \([ 0-9]+\))?)$/, function (m, p1, p2, p3) {
var $feed = $('#' + feed_id);
-
- p2 = p2.replace(/ /g, '');
- p4 = p4.replace(/ /g, '');
-
- if ($('.category.all > .active').length == 0 && $('.category.favorites > .active').length == 0) { // If the current page is not the home page or the favorites page
+ if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) {
isCurrentView = true;
- return p1 + incLabel(p2, nb) + p3 + incLabel(p4, feed_priority > 0 ? nb : 0);
+ return incLabel(p1, nb) + p2 + incLabel(p3, feed_priority > 0 ? nb : 0);
} else {
- return p1 + p3 + incLabel(p4, feed_priority > 0 ? nb : 0);
+ return p1 + p2 + incLabel(p3, feed_priority > 0 ? nb : 0);
}
-
});
-
return isCurrentView;
}
@@ -153,19 +156,16 @@ function mark_favorite(active) {
var favourites = $('.favorites>a').contents().last().get(0);
if (favourites && favourites.textContent) {
- // Without javascript, the text displayed is « Favorites (1544) » where 1544 is the number unformatted.
- // With Javascript, we replace this with « Favorites (1 544) ». To update this, the text is converted
- // to the non-javascript format before.
- favourites.textContent = favourites.textContent.replace(/ /g, '').replace('(', ' (').replace(/((?: \(\d+\))?\s*)$/, function (m, p1) {
+ favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
return incLabel(p1, inc);
});
}
if (active.closest('div').hasClass('not_read')) {
var elem = $('#aside_flux .favorites').children(':first').get(0),
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0;
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
if (elem) {
- elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + inc)));
+ elem.setAttribute('data-unread', numberFormat(feed_unreads + inc));
}
}
});
@@ -550,7 +550,7 @@ function refreshUnreads() {
$.each(data, function(feed_id, nbUnreads) {
feed_id = 'f_' + feed_id;
var elem = $('#' + feed_id + '>.feed').get(0),
- feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0;
+ feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
(nbUnreads - feed_unreads > 0)) {
$('#new-article').show();