diff --git a/app/assets/javascripts/angular/directives/work_packages/query-filter-directive.js b/app/assets/javascripts/angular/directives/work_packages/query-filter-directive.js index 7274a8e3aa..d7ab1f3a49 100644 --- a/app/assets/javascripts/angular/directives/work_packages/query-filter-directive.js +++ b/app/assets/javascripts/angular/directives/work_packages/query-filter-directive.js @@ -51,7 +51,7 @@ angular.module('openproject.workPackages.directives') // Filter updates scope.$watch('filter.operator', function(operator) { - if(operator) scope.showValuesInput = scope.filter.requiresValues(); + if(operator && scope.filter.requiresValues) scope.showValuesInput = scope.filter.requiresValues(); }); scope.$watch('filter', function(filter, oldFilter) { diff --git a/app/assets/javascripts/angular/filters/work-packages-filters.js b/app/assets/javascripts/angular/filters/work-packages-filters.js index 87ca663daf..ff53ea9f80 100644 --- a/app/assets/javascripts/angular/filters/work-packages-filters.js +++ b/app/assets/javascripts/angular/filters/work-packages-filters.js @@ -34,11 +34,13 @@ angular.module('openproject.workPackages.filters') .filter('subtractActiveFilters', [function() { return function(availableFilters, selectedFilters) { - var filters = angular.copy(availableFilters); + if(availableFilters){ + var filters = angular.copy(availableFilters); - angular.forEach(selectedFilters, function(filter) { - if(!filter.deactivated) delete filters[filter.name]; - }); + angular.forEach(selectedFilters, function(filter) { + if(!filter.deactivated) delete filters[filter.name]; + }); + } return filters; }; diff --git a/app/assets/javascripts/angular/helpers/components/path-helper.js b/app/assets/javascripts/angular/helpers/components/path-helper.js index faed1553fc..ce257aa351 100644 --- a/app/assets/javascripts/angular/helpers/components/path-helper.js +++ b/app/assets/javascripts/angular/helpers/components/path-helper.js @@ -83,6 +83,9 @@ angular.module('openproject.helpers') apiAvailableColumnsPath: function() { return PathHelper.apiPrefixV3 + '/queries/available_columns'; }, + apiCustomFieldsPath: function() { + return PathHelper.apiPrefixV3 + '/queries/custom_field_filters'; + }, apiProjectCustomFieldsPath: function(projectIdentifier) { return PathHelper.apiV3ProjectPath(projectIdentifier) + '/queries/custom_field_filters'; }, diff --git a/app/assets/javascripts/angular/models/query.js b/app/assets/javascripts/angular/models/query.js index faf0cdf9b1..8c3484655a 100644 --- a/app/assets/javascripts/angular/models/query.js +++ b/app/assets/javascripts/angular/models/query.js @@ -116,7 +116,11 @@ angular.module('openproject.models') }, getFilterType: function(filterName) { - return this.available_work_package_filters[filterName].type; + if (this.available_work_package_filters && this.available_work_package_filters[filterName]){ + return this.available_work_package_filters[filterName].type; + } else { + return 'none'; + } }, getActiveFilters: function() { diff --git a/app/assets/javascripts/angular/services/query-service.js b/app/assets/javascripts/angular/services/query-service.js index 27591331ab..768ef53c17 100644 --- a/app/assets/javascripts/angular/services/query-service.js +++ b/app/assets/javascripts/angular/services/query-service.js @@ -31,7 +31,7 @@ angular.module('openproject.services') .service('QueryService', ['$http', 'PathHelper', '$q', 'AVAILABLE_WORK_PACKAGE_FILTERS', 'StatusService', 'TypeService', 'PriorityService', 'UserService', 'VersionService', 'RoleService', 'GroupService', 'ProjectService', function($http, PathHelper, $q, AVAILABLE_WORK_PACKAGE_FILTERS, StatusService, TypeService, PriorityService, UserService, VersionService, RoleService, GroupService, ProjectService) { - var availableColumns = [], availableFilterValues = {}, availableFilters = null; + var availableColumns = [], availableFilterValues = {}, availableFilters = {}; var QueryService = { getAvailableColumns: function(projectIdentifier) { @@ -41,26 +41,55 @@ angular.module('openproject.services') }, getAvailableFilters: function(projectIdentifier){ - if(availableFilters){ - return $q.when(availableFilters); - } else{ - if (projectIdentifier){ - return QueryService.getCustomFieldFilters(projectIdentifier) - .then(function(data){ - return QueryService.storeAvailableFilters(angular.extend(AVAILABLE_WORK_PACKAGE_FILTERS, data.custom_field_filters)); - }); - } else { - return $q.when(AVAILABLE_WORK_PACKAGE_FILTERS); - } + var identifier = 'global'; + var getFilters = QueryService.getCustomFieldFilters; + var getFiltersArgs = []; + if(projectIdentifier){ + identifier = projectIdentifier; + getFilters = QueryService.getProjectCustomFieldFilters; + getFiltersArgs.push(identifier); + } + if(availableFilters[identifier]){ + return $q.when(availableFilters[identifier]); + } else { + return getFilters.apply(this, getFiltersArgs) + .then(function(data){ + return QueryService.storeAvailableFilters(identifier, angular.extend(AVAILABLE_WORK_PACKAGE_FILTERS, data.custom_field_filters)); + }); } + + + // if(projectIdentifier){ + // if(availableFilters[projectIdentifier]){ + // return $q.when(availableFilters[projectIdentifier]); + // } else { + // return QueryService.getProjectCustomFieldFilters(projectIdentifier) + // .then(function(data){ + // return QueryService.storeAvailableFilters(projectIdentifier, angular.extend(AVAILABLE_WORK_PACKAGE_FILTERS, data.custom_field_filters)); + // }); + // } + // } else { + // if(availableFilters['global']){ + // return $q.when(availableFilters['global']); + // } else { + // return QueryService.getCustomFieldFilters() + // .then(function(data){ + // return QueryService.storeAvailableFilters('global', angular.extend(AVAILABLE_WORK_PACKAGE_FILTERS, data.custom_field_filters)); + // }); + // } + // } }, - getCustomFieldFilters: function(projectIdentifier) { + getProjectCustomFieldFilters: function(projectIdentifier) { return QueryService.doQuery(PathHelper.apiProjectCustomFieldsPath(projectIdentifier)); }, + getCustomFieldFilters: function() { + return QueryService.doQuery(PathHelper.apiCustomFieldsPath()); + }, + getAvailableFilterValues: function(filterName, projectIdentifier) { - return QueryService.getAvailableFilters() + return QueryService.getAvailableFilters(projectIdentifier) .then(function(filters){ var filter = filters[filterName]; var modelName = filter.modelName @@ -125,9 +154,9 @@ angular.module('openproject.services') return values; }, - storeAvailableFilters: function(filters){ - availableFilters = filters; - return availableFilters; + storeAvailableFilters: function(projectIdentifier, filters){ + availableFilters[projectIdentifier] = filters; + return availableFilters[projectIdentifier]; }, doQuery: function(url, params) { diff --git a/app/controllers/api/v3/queries_controller.rb b/app/controllers/api/v3/queries_controller.rb index 69951c4ebf..1b63adf019 100644 --- a/app/controllers/api/v3/queries_controller.rb +++ b/app/controllers/api/v3/queries_controller.rb @@ -50,7 +50,11 @@ module Api::V3 end def custom_field_filters - custom_fields = @project.all_work_package_custom_fields + custom_fields = if @project + @project.all_work_package_custom_fields + else + WorkPackageCustomField.for_all + end @custom_field_filters = @query.get_custom_field_options(custom_fields) respond_to do |format| diff --git a/config/routes.rb b/config/routes.rb index 4b099e44e4..a453fa50d5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -123,6 +123,7 @@ OpenProject::Application.routes.draw do end resources :queries, only: [:show] do get :available_columns, on: :collection + get :custom_field_filters, on: :collection end resources :projects, only: [:show, :index] do