Merge pull request #1068 from opf/feature/angular-api-work-package-loading-sums-rework

Feature/angular api work package loading sums rework
pull/1072/head
Alex Coles 11 years ago
commit 47c6a68fd7
  1. 8
      app/assets/javascripts/angular/controllers/work-packages-controller.js
  2. 4
      app/assets/javascripts/angular/directives/work_packages/query-columns-directive.js
  3. 4
      app/assets/javascripts/angular/directives/work_packages/work-package-total-sums-directive.js
  4. 20
      app/assets/javascripts/angular/services/work-package-service.js
  5. 30
      app/controllers/api/v3/work_packages_controller.rb
  6. 2
      app/views/api/v3/work_packages/column_data.api.rabl
  7. 8
      public/templates/work_packages/work_packages_table.html

@ -54,8 +54,6 @@ angular.module('openproject.workPackages.controllers')
};
$scope.setupWorkPackagesTable = function(json) {
// TODO: We need to set the columns based on what's returned by the query for when we are loading using a query id.
// Also perhaps the filters... and everything:/
var meta = json.meta;
if (!$scope.columns) $scope.columns = meta.columns;
@ -67,9 +65,11 @@ angular.module('openproject.workPackages.controllers')
$scope.rows = WorkPackagesTableHelper.getRows(json.work_packages, $scope.query.group_by);
$scope.workPackageCountByGroup = meta.work_package_count_by_group;
$scope.totalSums = meta.sums;
$scope.groupSums = meta.group_sums;
$scope.totalEntries = meta.total_entries;
angular.forEach($scope.columns, function(column, i){
column.total_sum = meta.sums[i];
if (meta.group_sums) column.group_sums = meta.group_sums[i];
});
};
$scope.updateResults = function() {

@ -31,7 +31,9 @@ angular.module('openproject.workPackages.directives')
var newColumns = WorkPackagesTableHelper.selectColumnsByName(scope.columns, columnNames);
// work package rows
scope.withLoading(WorkPackageService.augmentWorkPackagesWithColumnsData, [workPackages, newColumns]);
var params = [workPackages, newColumns];
if( scope.groupByColumn) params.push(scope.groupByColumn.name);
scope.withLoading(WorkPackageService.augmentWorkPackagesWithColumnsData, params);
}
function removeColumn(columnName, columns, callback) {

@ -11,7 +11,9 @@ angular.module('openproject.workPackages.directives')
function fetchSums() {
scope.withLoading(WorkPackageService.getWorkPackagesSums, [scope.projectIdentifier, scope.columns])
.then(function(data){
scope.sums = data.column_sums;
angular.forEach(scope.columns, function(column, i){
column.total_sum = data.column_sums[i];
});
});
}

@ -23,14 +23,15 @@ angular.module('openproject.services')
return WorkPackageService.doQuery(url, params);
},
loadWorkPackageColumnsData: function(workPackages, columnNames) {
loadWorkPackageColumnsData: function(workPackages, columnNames, group_by) {
var url = PathHelper.apiWorkPackagesColumnDataPath();
var params = {
'ids[]': workPackages.map(function(workPackage){
return workPackage.id;
}),
'column_names[]': columnNames
'column_names[]': columnNames,
'group_by': group_by
};
return WorkPackageService.doQuery(url, params);
@ -51,17 +52,22 @@ angular.module('openproject.services')
return WorkPackageService.doQuery(url, params);
},
augmentWorkPackagesWithColumnsData: function(workPackages, columns) {
augmentWorkPackagesWithColumnsData: function(workPackages, columns, group_by) {
var columnNames = columns.map(function(column) {
return column.name;
});
return WorkPackageService.loadWorkPackageColumnsData(workPackages, columnNames)
return WorkPackageService.loadWorkPackageColumnsData(workPackages, columnNames, group_by)
.then(function(data){
var columnsData = data.columns_data;
angular.forEach(workPackages, function(workPackage, i) {
angular.forEach(columns, function(column, j){
WorkPackagesHelper.augmentWorkPackageWithData(workPackage, column.name, !!column.custom_field, columnsData[j][i]);
var columnsMeta = data.columns_meta;
angular.forEach(columns, function(column, i){
column.total_sum = columnsMeta.total_sums[i];
if (columnsMeta.group_sums) column.group_sums = columnsMeta.group_sums[i];
angular.forEach(workPackages, function(workPackage, j) {
WorkPackagesHelper.augmentWorkPackageWithData(workPackage, column.name, !!column.custom_field, columnsData[i][j]);
});
});

@ -44,6 +44,10 @@ module Api
work_packages = Array.wrap(WorkPackage.visible.find(*ids)).sort {|a,b| ids.index(a.id) <=> ids.index(b.id)}
@columns_data = fetch_columns_data(column_names, work_packages)
@columns_meta = {
total_sums: columns_total_sums(column_names, work_packages),
group_sums: columns_group_sums(column_names, work_packages, params[:group_by])
}
end
def column_sums
@ -53,13 +57,33 @@ module Api
project = Project.find_visible(current_user, params[:project_id])
work_packages = project.work_packages
@column_sums = column_names.map do |column_name|
fetch_column_data(column_name, work_packages, false).map{|c| c.nil? ? 0 : c}.compact.sum if column_should_be_summed_up?(column_name)
@column_sums = columns_total_sums(column_names, work_packages)
end
private
def columns_total_sums(column_names, work_packages)
column_names.map do |column_name|
column_sum(column_name, work_packages)
end
end
def column_sum(column_name, work_packages)
fetch_column_data(column_name, work_packages, false).map{|c| c.nil? ? 0 : c}.compact.sum if column_should_be_summed_up?(column_name)
end
private
def columns_group_sums(column_names, work_packages, group_by)
# NOTE RS: This is basically the grouped_sums method from sums.rb but we have no query to play with here
return unless group_by
column_names.map do |column_name|
work_packages.map { |wp| wp.send(group_by) }
.uniq
.inject({}) do |group_sums, current_group|
work_packages_in_current_group = work_packages.select{|wp| wp.send(group_by) == current_group}
group_sums.merge current_group => column_sum(column_name, work_packages_in_current_group)
end
end
end
def load_query
@query ||= retrieve_query

@ -29,3 +29,5 @@
object false
node(:columns_data) { @columns_data }
node(:columns_meta) { @columns_meta }

@ -134,8 +134,8 @@
{{ I18n.t('js.label_sum_for') }}
<span work-package-column work-package="row.object" column="groupByColumn"/>
</td>
<td ng-repeat="sum in sums track by $index">
{{ sum }}
<td ng-repeat="column in columns">
{{ column.group_sums[row.groupName] }}
</td>
</tr>
@ -145,8 +145,8 @@
ng-if="displaySums"
class="sum group all issue work_package">
<td colspan="2">{{ I18n.t('js.label_sum_for') }} {{ I18n.t('js.label_all_work_packages') }}</td>
<td ng-repeat="sum in sums track by $index">
{{ sum }}
<td ng-repeat="column in columns">
{{ column.total_sum }}
</td>
</tr>

Loading…
Cancel
Save