2015-03-29 20:10:53 +02:00
|
|
|
import Ember from 'ember';
|
2015-05-27 15:10:47 -05:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
2015-03-29 20:10:53 +02:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
const {Route, inject, run} = Ember;
|
|
|
|
|
|
|
|
let DownloadCountPoller = Ember.Object.extend({
|
2015-05-27 15:10:47 -05:00
|
|
|
url: null,
|
2015-09-02 12:03:01 +02:00
|
|
|
count: '',
|
2015-05-27 15:10:47 -05:00
|
|
|
runId: null,
|
2015-03-29 20:10:53 +02:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
init() {
|
2015-05-27 15:10:47 -05:00
|
|
|
this.downloadCounter();
|
|
|
|
this.poll();
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
poll() {
|
|
|
|
let interval = Ember.testing ? 20 : 2000;
|
|
|
|
let runId = run.later(this, function () {
|
2015-05-27 15:10:47 -05:00
|
|
|
this.downloadCounter();
|
2015-11-13 11:48:59 +00:00
|
|
|
if (!Ember.testing) {
|
|
|
|
this.poll();
|
|
|
|
}
|
2015-05-27 15:10:47 -05:00
|
|
|
}, interval);
|
|
|
|
|
|
|
|
this.set('runId', runId);
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
downloadCounter() {
|
|
|
|
ajax(this.get('url')).then((data) => {
|
|
|
|
let pattern = /(-?\d+)(\d{3})/;
|
|
|
|
let count = data.count.toString();
|
2015-08-25 14:48:02 +01:00
|
|
|
|
|
|
|
while (pattern.test(count)) {
|
|
|
|
count = count.replace(pattern, '$1,$2');
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
this.set('count', count);
|
|
|
|
}).catch(() => {
|
|
|
|
this.set('count', '');
|
2015-05-27 15:10:47 -05:00
|
|
|
});
|
2015-03-29 20:10:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
export default Route.extend({
|
|
|
|
ghostPaths: inject.service('ghost-paths'),
|
2015-05-27 15:10:47 -05:00
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
model() {
|
2015-05-27 15:10:47 -05:00
|
|
|
return DownloadCountPoller.create({url: this.get('ghostPaths.count')});
|
|
|
|
},
|
|
|
|
|
2015-10-28 11:36:45 +00:00
|
|
|
resetController(controller, isExiting) {
|
2015-05-27 15:10:47 -05:00
|
|
|
if (isExiting) {
|
2015-10-28 11:36:45 +00:00
|
|
|
run.cancel(controller.get('model.runId'));
|
2015-05-27 15:10:47 -05:00
|
|
|
controller.set('model', null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|