diff --git a/res/app/components/stf/user/user-service.js b/res/app/components/stf/user/user-service.js index 064f8c1a..c6f0d21f 100644 --- a/res/app/components/stf/user/user-service.js +++ b/res/app/components/stf/user/user-service.js @@ -1,8 +1,14 @@ module.exports = function UserServiceFactory($http, $rootScope, socket, $timeout) { var userService = {} - userService.user = (function () { + userService.currentUser = (function () { var userPromise = $http.get('/api/v1/user') + .then(function(response) { + if (!response.data.success) { + throw new Error('Unable to get user data') + } + return response.data.user + }) return function () { return userPromise } diff --git a/res/app/device-list/device-list-stats-directive.js b/res/app/device-list/device-list-stats-directive.js new file mode 100644 index 00000000..7728a46c --- /dev/null +++ b/res/app/device-list/device-list-stats-directive.js @@ -0,0 +1,91 @@ +module.exports = function DeviceListStatsDirective( + UserService +) { + return { + restrict: 'E' + , template: require('./device-list-stats.jade') + , scope: { + tracker: '&tracker' + } + , link: function (scope) { + var tracker = scope.tracker() + , mapping = Object.create(null) + , timer + + scope.counter = { + total: 0 + , usable: 0 + , busy: 0 + , using: 0 + } + + UserService.currentUser().then(function(user) { + scope.currentUser = user + }) + + function notify() { + clearTimeout(timer) + timer = setTimeout(function() { + scope.$apply() + }, 250) + } + + function updateStats(device) { + return (mapping[device.serial] = { + usable: device.usable ? 1 : 0 + , busy: device.owner ? 1 : 0 + , using: device.using ? 1 : 0 + }) + } + + function addListener(device) { + var stats = updateStats(device) + + scope.counter.total += 1 + scope.counter.usable += stats.usable + scope.counter.busy += stats.busy + scope.counter.using += stats.busy + + notify() + } + + function changeListener(device) { + var oldStats = mapping[device.serial] + , newStats = updateStats(device) + , diffs = Object.create(null) + + scope.counter.usable += diffs.usable = newStats.usable - oldStats.usable + scope.counter.busy += diffs.busy = newStats.busy - oldStats.busy + scope.counter.using += diffs.using = newStats.using - oldStats.using + + if (diffs.usable || diffs.busy || diffs.using) { + notify() + } + } + + function removeListener(device) { + var oldStats = mapping[device.serial] + , newStats = updateStats(device) + + scope.counter.total -= 1 + scope.counter.busy += newStats.busy - oldStats.busy + scope.counter.using += newStats.using - oldStats.using + + delete mapping[device.serial] + + notify() + } + + tracker.on('add', addListener) + tracker.on('change', changeListener) + tracker.on('remove', removeListener) + + scope.$on('$destroy', function() { + tracker.removeListener('add', addListener) + tracker.removeListener('change', changeListener) + tracker.removeListener('remove', removeListener) + clearTimeout(timer) + }) + } + } +} diff --git a/res/app/device-list/device-list-stats.jade b/res/app/device-list/device-list-stats.jade new file mode 100644 index 00000000..51081240 --- /dev/null +++ b/res/app/device-list/device-list-stats.jade @@ -0,0 +1,22 @@ +.widget-container.stats-container + .col-md-3 + .number + .icon.fa.fa-globe.visitors + span(ng-bind='counter.total') 0 + .text(translate) Total devices + .col-md-3 + .number + .icon.fa.fa-check.visitors + span(ng-bind='counter.usable') 0 + .text(translate) Usable devices + .col-md-3 + .number + .icon.fa.fa-users.visitors + span(ng-bind='counter.busy') 0 + .text(translate) Busy devices + .col-md-3 + .number + .icon.fa.fa-user.visitors + span(ng-bind='counter.using') 0 + //.text(translate) Using + .text(ng-bind='currentUser.name') diff --git a/res/app/device-list/device-list.jade b/res/app/device-list/device-list.jade index c5629e9c..3b7733c0 100644 --- a/res/app/device-list/device-list.jade +++ b/res/app/device-list/device-list.jade @@ -1,28 +1,7 @@ div.stf-device-list .row.stf-stats-container(ng-show='tracker.devices.length') .col-md-12 - .widget-container.stats-container - .col-md-3 - .number - .icon.fa.fa-globe.visitors - span(ng-bind='tracker.devices.length') - .text(translate) Total devices - .col-md-3 - .number - .icon.fa.fa-check.visitors - span(ng-bind='(tracker.devices | filter:{usable:true}).length') - .text(translate) Usable devices - .col-md-3 - .number - .icon.fa.fa-users.visitors - span(ng-bind='(tracker.devices | filter:{state:"busy"}).length') - .text(translate) Busy devices - .col-md-3 - .number - .icon.fa.fa-user.visitors - span(ng-bind='(tracker.devices | filter:{using:true}).length') - //.text(translate) Using - .text(ng-bind='currentUser.name') + device-list-stats(tracker='tracker') .row .col-md-12 diff --git a/res/app/device-list/index.js b/res/app/device-list/index.js index c7937808..960b7942 100644 --- a/res/app/device-list/index.js +++ b/res/app/device-list/index.js @@ -18,3 +18,4 @@ module.exports = angular.module('device-list', [ .controller('DeviceListCtrl', require('./device-list-controller')) .directive('deviceListDetails', require('./device-list-details-directive')) .directive('deviceListIcon', require('./device-list-icon-directive')) + .directive('deviceListStats', require('./device-list-stats-directive'))