mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-26 15:55:31 +02:00
Added directive counter for device stats (it was not updating the numbers smoothly).
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
23
res/app/components/stf/common-ui/counter/counter-spec.js
Normal file
23
res/app/components/stf/common-ui/counter/counter-spec.js
Normal 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');
|
||||
*/
|
||||
|
||||
});
|
||||
});
|
||||
4
res/app/components/stf/common-ui/counter/index.js
Normal file
4
res/app/components/stf/common-ui/counter/index.js
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = angular.module('stf.counter', [
|
||||
|
||||
])
|
||||
.directive('counter', require('./counter-directive'))
|
||||
@@ -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
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user