Put column watcher back into total total sums directive

pull/1288/head
Till Breuer 11 years ago
parent 3d1cd19abc
commit d3d933049e
  1. 15
      app/assets/javascripts/angular/directives/work_packages/query-form-directive.js
  2. 37
      app/assets/javascripts/angular/directives/work_packages/work-package-total-sums-directive.js
  3. 46
      karma/tests/directives/work_packages/work-package-total-sums-directive-test.js

@ -38,15 +38,6 @@ angular.module('openproject.workPackages.directives')
pre: function(scope) {
scope.showQueryOptions = false;
function fetchSums() {
scope.withLoading(WorkPackageService.getWorkPackagesSums, [scope.projectIdentifier, scope.query, scope.columns])
.then(function(data){
angular.forEach(scope.columns, function(column, i){
column.total_sum = data.column_sums[i];
});
});
}
function querySwitched(currentProperties, formerProperties) {
if (formerProperties === undefined) {
return true;
@ -73,8 +64,7 @@ angular.module('openproject.workPackages.directives')
return {
id: query.id,
groupBy: query.groupBy,
sortElements: query.sortation.sortElements,
columnNames: query.getColumnNames()
sortElements: query.sortation.sortElements
};
}
}
@ -84,9 +74,6 @@ angular.module('openproject.workPackages.directives')
if (queryPropertiesChanged(newProperties, oldProperties)) {
scope.updateResults();
scope.updateBackUrl();
} else if (!angular.equals(newProperties.columnNames, oldProperties.columnNames)) {
fetchSums();
scope.updateBackUrl();
}
}
}, true);

@ -36,6 +36,41 @@ angular.module('openproject.workPackages.directives')
return {
restrict: 'A',
scope: true
scope: true,
link: function(scope) {
function fetchTotalSums() {
scope.withLoading(WorkPackageService.getWorkPackagesSums, [scope.projectIdentifier, scope.query, scope.columns])
.then(function(data){
angular.forEach(scope.columns, function(column, i){
column.total_sum = data.column_sums[i];
});
});
}
function totalSumsFetched() {
return scope.columns.length > 0 && scope.columns[0].hasOwnProperty('total_sum');
}
if (!totalSumsFetched()) fetchTotalSums(); // don't reload on every toggle
function observedProperties() {
if (scope.query !== undefined) {
return {
queryId: scope.query && scope.query.id,
columnNames: scope.columns.map(function(column) {
return column.name;
})
};
}
}
scope.$watch(observedProperties, function(newProperties, oldProperties) {
if (oldProperties && oldProperties.queryId === newProperties.queryId) {
if (!angular.equals(newProperties.columnNames, oldProperties.columnNames)) {
fetchTotalSums();
scope.updateBackUrl();
}
}
}, true);
}
};
}]);

@ -48,28 +48,40 @@ describe('workPackageTotalSums Directive', function() {
describe('element', function() {
beforeEach(inject(function($q) {
var sumsData = [1, 2];
scope.columns = [];
scope.updateBackUrl = function(){ return 0; };
scope.withLoading = function(callback, params) {
// Note: Seems easier to mock out the withLoading method instead of WorkPackageService.getWorkPackagesSums which can be seperately unit tested
deferred = $q.defer();
deferred.resolve({ column_sums: sumsData } );
return deferred.promise;
};
scope.query = Factory.build('Query', {
id: null,
columns: [{ name: 'cheese', total_sum: 1 }]
});
scope.columns = scope.query.columns;
}));
beforeEach(function(){
compile();
});
it('should render a tr', function() {
expect(element.prop('tagName')).to.equal('TR');
});
it('should set the sums', function() {
var td = element.find('td');
expect(td.length).to.equal(1);
expect(td.first().text()).to.equal('1');
});
describe('setting total sums for the columns', function(){
beforeEach(function(){
compile();
});
beforeEach(inject(function($q) {
var sumsData = [1, 2];
it('should render a tr', function() {
expect(element.prop('tagName')).to.equal('TR');
});
scope.updateBackUrl = function(){ return 0; };
scope.withLoading = function(callback, params) {
// Note: Seems easier to mock out the withLoading method instead of WorkPackageService.getWorkPackagesSums which can be seperately unit tested
deferred = $q.defer();
deferred.resolve({ column_sums: sumsData } );
return deferred.promise;
};
}));
it('should set the sums when the columns change', function() {
it('should fetch the sums when the columns change', function() {
scope.columns = [{ name: 'cheese' }, { name: 'toasties' }];
scope.$apply();

Loading…
Cancel
Save