Added directive counter for device stats (it was not updating the numbers smoothly).

This commit is contained in:
Gunther Brunner
2014-07-09 21:44:09 +09:00
parent 2c7940f6ea
commit 84e2a4ae1c
5 changed files with 96 additions and 6 deletions

View File

@@ -0,0 +1,63 @@
module.exports = function counterDirective($timeout, $$rAF) {
return {
replace: false,
scope: true,
link: function (scope, element, attrs) {
// TODO: use $$rAF later
var el = element[0]
var num, refreshInterval, duration, steps, step, countTo, increment
var calculate = function () {
refreshInterval = 32
step = 0
scope.timoutId = null
countTo = parseInt(attrs.countTo) || 0
scope.value = parseInt(attrs.value, 10) || 0
duration = parseFloat(attrs.duration) || 0
steps = Math.ceil(duration / refreshInterval)
increment = ((countTo - scope.value) / steps)
num = scope.value
}
var tick = function () {
scope.timoutId = $timeout(function () {
num += increment
step++
if (step >= steps) {
$timeout.cancel(scope.timoutId)
num = countTo
el.innerText = countTo
} else {
el.innerText = Math.round(num)
tick()
}
}, refreshInterval)
}
var start = function () {
if (scope.timoutId) {
$timeout.cancel(scope.timoutId)
}
calculate()
tick()
}
attrs.$observe('countTo', function (val) {
if (val) {
start()
}
})
attrs.$observe('value', function (val) {
start()
})
return true
}
}
}

View File

@@ -0,0 +1,23 @@
describe('counter', function () {
beforeEach(module('stf.counter'));
var scope, compile;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
compile = $compile;
}));
it('should ...', function () {
/*
To test your directive, you need to create some html that would use your directive,
send that through compile() then compare the results.
var element = compile('<div counter name="name">hi</div>')(scope);
expect(element.text()).toBe('hello, world');
*/
});
});

View File

@@ -0,0 +1,4 @@
module.exports = angular.module('stf.counter', [
])
.directive('counter', require('./counter-directive'))

View File

@@ -10,5 +10,6 @@ module.exports = angular.module('stf/common-ui', [
//require('./tree').name,
require('./modals').name,
require('./include-cached').name,
require('./text-focus-select').name
require('./text-focus-select').name,
require('./counter').name
])