Formatage des nombres #2

https://github.com/marienfressinaud/FreshRSS/pull/398
Je suis reparti du commit 7a510af73a, 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
pull/409/head
Alexandre Alapetite 11 years ago
parent 2fe2f876ea
commit a6f122e03c
  1. 5
      app/Controllers/indexController.php
  2. 56
      p/scripts/main.js

@ -44,6 +44,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
$this->view->cat_aside = $catDAO->listCategories (); $this->view->cat_aside = $catDAO->listCategories ();
$this->view->nb_favorites = $entryDAO->countUnreadReadFavorites (); $this->view->nb_favorites = $entryDAO->countUnreadReadFavorites ();
$this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
$this->view->currentName = ''; $this->view->currentName = '';
$this->view->get_c = ''; $this->view->get_c = '';
@ -61,8 +62,6 @@ class FreshRSS_index_Controller extends Minz_ActionController {
return; return;
} }
$this->view->nb_not_read = FreshRSS_CategoryDAO::CountUnreads($this->view->cat_aside, 1);
// mise à jour des titres // mise à jour des titres
$this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title(); $this->view->rss_title = $this->view->currentName . ' | ' . Minz_View::title();
if ($this->view->nb_not_read > 0) { if ($this->view->nb_not_read > 0) {
@ -153,10 +152,12 @@ class FreshRSS_index_Controller extends Minz_ActionController {
switch ($getType) { switch ($getType) {
case 'a': case 'a':
$this->view->currentName = Minz_Translate::t ('your_rss_feeds'); $this->view->currentName = Minz_Translate::t ('your_rss_feeds');
$this->nb_not_read_cat = $this->view->nb_not_read;
$this->view->get_c = $getType; $this->view->get_c = $getType;
return true; return true;
case 's': case 's':
$this->view->currentName = Minz_Translate::t ('your_favorites'); $this->view->currentName = Minz_Translate::t ('your_favorites');
$this->nb_not_read_cat = $this->view->nb_favorites['unread'];
$this->view->get_c = $getType; $this->view->get_c = $getType;
return true; return true;
case 'c': case 'c':

@ -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) { function numberFormat(nStr) {
if (nStr < 0) {
return 0;
}
// http://www.mredkj.com/javascript/numberFormat.html // http://www.mredkj.com/javascript/numberFormat.html
nStr += ''; nStr += '';
var x = nStr.split('.'), var x = nStr.split('.'),
@ -34,33 +44,32 @@ function numberFormat(nStr) {
} }
function incLabel(p, inc) { function incLabel(p, inc) {
var i = (parseInt(p.replace(/\D/g, ''), 10) || 0) + inc; var i = str2int(p) + inc;
return i > 0 ? ' (' + numberFormat(i) + ')' : ''; return i > 0 ? ' (' + numberFormat(i) + ')' : '';
} }
function incUnreadsFeed(article, feed_id, nb) { function incUnreadsFeed(article, feed_id, nb) {
//Update unread: feed //Update unread: feed
var elem = $('#' + feed_id + '>.feed').get(0), 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,
feed_priority = elem ? (parseInt(elem.getAttribute('data-priority'), 10) || 0) : 0; feed_priority = elem ? str2int(elem.getAttribute('data-priority')) : 0;
if (elem) { if (elem) {
elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb))); elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
} }
//Update unread: category //Update unread: category
elem = $('#' + feed_id).parent().prevAll('.category').children(':first').get(0); 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) { if (elem) {
elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb))); elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
} }
//Update unread: all //Update unread: all
if (feed_priority > 0) { if (feed_priority > 0) {
elem = $('#aside_flux .all').children(':first').get(0); elem = $('#aside_flux .all').children(':first').get(0);
if (elem) { if (elem) {
feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0; feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb))); 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')) { if (article && article.closest('div').hasClass('favorite')) {
elem = $('#aside_flux .favorites').children(':first').get(0); elem = $('#aside_flux .favorites').children(':first').get(0);
if (elem) { if (elem) {
feed_unreads = elem ? (parseInt(elem.getAttribute('data-unread').replace(' ', ''), 10) || 0) : 0; feed_unreads = elem ? str2int(elem.getAttribute('data-unread')) : 0;
elem.setAttribute('data-unread', numberFormat(Math.max(0, feed_unreads + nb))); elem.setAttribute('data-unread', numberFormat(feed_unreads + nb));
} }
} }
var isCurrentView = false; var isCurrentView = false;
//Update unread: title //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); var $feed = $('#' + feed_id);
if (article || ($feed.closest('.active').length > 0 && $feed.siblings('.active').length === 0)) {
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
isCurrentView = true; 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 { } else {
return p1 + p3 + incLabel(p4, feed_priority > 0 ? nb : 0); return p1 + p2 + incLabel(p3, feed_priority > 0 ? nb : 0);
} }
}); });
return isCurrentView; return isCurrentView;
} }
@ -153,19 +156,16 @@ function mark_favorite(active) {
var favourites = $('.favorites>a').contents().last().get(0); var favourites = $('.favorites>a').contents().last().get(0);
if (favourites && favourites.textContent) { if (favourites && favourites.textContent) {
// Without javascript, the text displayed is « Favorites (1544) » where 1544 is the number unformatted. favourites.textContent = favourites.textContent.replace(/((?: \([ 0-9]+\))?\s*)$/, function (m, p1) {
// 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) {
return incLabel(p1, inc); return incLabel(p1, inc);
}); });
} }
if (active.closest('div').hasClass('not_read')) { if (active.closest('div').hasClass('not_read')) {
var elem = $('#aside_flux .favorites').children(':first').get(0), 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) { 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) { $.each(data, function(feed_id, nbUnreads) {
feed_id = 'f_' + feed_id; feed_id = 'f_' + feed_id;
var elem = $('#' + feed_id + '>.feed').get(0), 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? if ((incUnreadsFeed(null, feed_id, nbUnreads - feed_unreads) || isAll) && //Update of current view?
(nbUnreads - feed_unreads > 0)) { (nbUnreads - feed_unreads > 0)) {
$('#new-article').show(); $('#new-article').show();

Loading…
Cancel
Save