Add helper to format number with commas

This commit is contained in:
Toby Zerner 2015-05-30 08:40:29 +09:30
parent 2dbd73c11e
commit 8859e49241
3 changed files with 13 additions and 8 deletions

View File

@ -3,6 +3,7 @@ import icon from 'flarum/helpers/icon';
import ScrollListener from 'flarum/utils/scroll-listener'; import ScrollListener from 'flarum/utils/scroll-listener';
import SubtreeRetainer from 'flarum/utils/subtree-retainer'; import SubtreeRetainer from 'flarum/utils/subtree-retainer';
import computed from 'flarum/utils/computed'; import computed from 'flarum/utils/computed';
import formatNumber from 'flarum/utils/format-number';
/** /**
@ -63,9 +64,10 @@ export default class PostScrubber extends Component {
var unreadCount = this.props.stream.discussion.unreadCount(); var unreadCount = this.props.stream.discussion.unreadCount();
var unreadPercent = unreadCount / this.count(); var unreadPercent = unreadCount / this.count();
// @todo clean up duplication
return m('div.stream-scrubber.dropdown'+(this.disabled() ? '.disabled' : ''), {config: this.onload.bind(this)}, [ return m('div.stream-scrubber.dropdown'+(this.disabled() ? '.disabled' : ''), {config: this.onload.bind(this)}, [
m('a.btn.btn-default.dropdown-toggle[href=javascript:;][data-toggle=dropdown]', [ m('a.btn.btn-default.dropdown-toggle[href=javascript:;][data-toggle=dropdown]', [
m('span.index', retain || this.visibleIndex()), ' of ', m('span.count', this.count()), ' posts ', m('span.index', retain || formatNumber(this.visibleIndex())), ' of ', m('span.count', formatNumber(this.count())), ' posts ',
icon('sort icon-glyph') icon('sort icon-glyph')
]), ]),
m('div.dropdown-menu', [ m('div.dropdown-menu', [
@ -80,7 +82,7 @@ export default class PostScrubber extends Component {
m('div.scrubber-slider', [ m('div.scrubber-slider', [
m('div.scrubber-handle'), m('div.scrubber-handle'),
m('div.scrubber-info', [ m('div.scrubber-info', [
m('strong', [m('span.index', retain || this.visibleIndex()), ' of ', m('span.count', this.count()), ' posts']), m('strong', [m('span.index', retain || formatNumber(this.visibleIndex())), ' of ', m('span.count', formatNumber(this.count())), ' posts']),
m('span.description', retain || this.description()) m('span.description', retain || this.description())
]) ])
]), ]),
@ -95,7 +97,7 @@ export default class PostScrubber extends Component {
} }
context.oldStyle = newStyle; context.oldStyle = newStyle;
} }
}, unreadCount+' unread') : '' }, formatNumber(unreadCount)+' unread') : ''
]), ]),
m('a.scrubber-last[href=javascript:;]', {onclick: () => { m('a.scrubber-last[href=javascript:;]', {onclick: () => {
stream.goToLast(); stream.goToLast();
@ -264,7 +266,7 @@ export default class PostScrubber extends Component {
var visible = this.visible(); var visible = this.visible();
var $scrubber = this.$(); var $scrubber = this.$();
$scrubber.find('.index').text(this.visibleIndex()); $scrubber.find('.index').text(formatNumber(this.visibleIndex()));
$scrubber.find('.description').text(this.description()); $scrubber.find('.description').text(this.description());
$scrubber.toggleClass('disabled', this.disabled()); $scrubber.toggleClass('disabled', this.disabled());
@ -318,7 +320,7 @@ export default class PostScrubber extends Component {
scrollbar.css('max-height', $(window).height() - scrollbar.offset().top + $(window).scrollTop() - parseInt($('.global-page').css('padding-bottom'))); scrollbar.css('max-height', $(window).height() - scrollbar.offset().top + $(window).scrollTop() - parseInt($('.global-page').css('padding-bottom')));
} }
onmousedown() { onmousedown(e) {
this.mouseStart = e.clientY || e.originalEvent.touches[0].clientY; this.mouseStart = e.clientY || e.originalEvent.touches[0].clientY;
this.indexStart = this.index(); this.indexStart = this.index();
this.dragging = true; this.dragging = true;
@ -326,7 +328,7 @@ export default class PostScrubber extends Component {
$('body').css('cursor', 'move'); $('body').css('cursor', 'move');
} }
onmousemove() { onmousemove(e) {
if (! this.dragging) { return; } if (! this.dragging) { return; }
// Work out how much the mouse has moved by - first in pixels, then // Work out how much the mouse has moved by - first in pixels, then
@ -342,7 +344,7 @@ export default class PostScrubber extends Component {
this.renderScrollbar(); this.renderScrollbar();
} }
onmouseup() { onmouseup(e) {
if (!this.dragging) { return; } if (!this.dragging) { return; }
this.mouseStart = 0; this.mouseStart = 0;
this.indexStart = 0; this.indexStart = 0;

View File

@ -4,6 +4,6 @@ export default function(number) {
} else if (number >= 1000) { } else if (number >= 1000) {
return Math.floor(number / 1000)+'K'; return Math.floor(number / 1000)+'K';
} else { } else {
return ''+number; return number.toString();
} }
} }

View File

@ -0,0 +1,3 @@
export default function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}