0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/client/app/routes/setup/one.js
Hannah Wolfe 9d21a40ddf Number formatting function for download counter
refs #5652

- safari doesn't support the nice toLocaleString function
- this adds a manual, cross-browser way of adding commas in the right places to long number strings
2015-08-25 15:04:58 +01:00

57 lines
1.3 KiB
JavaScript

import Ember from 'ember';
import {request as ajax} from 'ic-ajax';
var DownloadCountPoller = Ember.Object.extend({
url: null,
count: 'many, many',
runId: null,
init: function () {
this.downloadCounter();
this.poll();
},
poll: function () {
var interval = 3000,
runId;
runId = Ember.run.later(this, function () {
this.downloadCounter();
this.poll();
}, interval);
this.set('runId', runId);
},
downloadCounter: function () {
var self = this;
ajax(this.get('url')).then(function (data) {
var count = data.count.toString(),
pattern = /(-?\d+)(\d{3})/;
while (pattern.test(count)) {
count = count.replace(pattern, '$1,$2');
}
self.set('count', count);
}).catch(function () {
self.set('count', 'many, many');
});
}
});
export default Ember.Route.extend({
ghostPaths: Ember.inject.service('ghost-paths'),
model: function () {
return DownloadCountPoller.create({url: this.get('ghostPaths.count')});
},
resetController: function (controller, isExiting) {
if (isExiting) {
Ember.run.cancel(controller.get('model.runId'));
controller.set('model', null);
}
}
});