diff --git a/src/app/project_group/controller/project_group_list_controller.js b/src/app/project_group/controller/project_group_list_controller.js index 13f52603..90ce571c 100644 --- a/src/app/project_group/controller/project_group_list_controller.js +++ b/src/app/project_group/controller/project_group_list_controller.js @@ -25,11 +25,8 @@ angular.module('sb.project_group').controller('ProjectGroupListController', // search results must be of type "ProjectGroup" $scope.resourceTypes = ['ProjectGroup']; - // Projects have no default criteria - $scope.defaultCriteria = []; - var params = $location.search(); - SearchHelper.parseParameters(params, $scope.defaultCriteria); + $scope.defaultCriteria = SearchHelper.parseParameters(params); /** * Create a new project-group. diff --git a/src/app/projects/controller/project_list_controller.js b/src/app/projects/controller/project_list_controller.js index d0fb94cd..8b0cee90 100644 --- a/src/app/projects/controller/project_list_controller.js +++ b/src/app/projects/controller/project_list_controller.js @@ -30,11 +30,8 @@ angular.module('sb.projects').controller('ProjectListController', // search results must be of type "project" $scope.resourceTypes = ['Project']; - // Projects have no default criteria - $scope.defaultCriteria = []; - var params = $location.search(); - SearchHelper.parseParameters(params, $scope.defaultCriteria); + $scope.defaultCriteria = SearchHelper.parseParameters(params); /** * Launches the add-project modal. diff --git a/src/app/search/controller/search_controller.js b/src/app/search/controller/search_controller.js index 86108ed6..3e74db5d 100644 --- a/src/app/search/controller/search_controller.js +++ b/src/app/search/controller/search_controller.js @@ -26,7 +26,8 @@ angular.module('sb.search').controller('SearchController', * * @type {Array} */ - $scope.defaultCriteria = []; + var params = $location.search(); + $scope.defaultCriteria = SearchHelper.parseParameters(params); /** * List of resource types which this view will be searching on. @@ -35,11 +36,5 @@ angular.module('sb.search').controller('SearchController', */ $scope.resourceTypes = ['TaskStatus', 'Story', 'Project', 'User', 'Task', 'ProjectGroup', 'Board', 'Worklist']; - - /** - * If a 'q' exists in the state params, go ahead and add it. - */ - var params = $location.search(); - SearchHelper.parseParameters(params, $scope.defaultCriteria); } ); diff --git a/src/app/search/controller/search_criteria_controller.js b/src/app/search/controller/search_criteria_controller.js index 4952e581..8c6240cd 100644 --- a/src/app/search/controller/search_criteria_controller.js +++ b/src/app/search/controller/search_criteria_controller.js @@ -46,7 +46,13 @@ angular.module('sb.search').controller('SearchCriteriaController', */ $scope.init = function (types, defaultCriteria) { resourceTypes = types || $scope.resourceTypes || resourceTypes; - $scope.criteria = defaultCriteria || []; + if (!!defaultCriteria) { + defaultCriteria.then(function(criteria) { + $scope.criteria = criteria; + }); + } else { + $scope.criteria = []; + } $scope.searchForCriteria = Criteria.buildCriteriaSearch(resourceTypes, 5); }; diff --git a/src/app/search/service/search_helper.js b/src/app/search/service/search_helper.js index 5610bdc0..9bdade1b 100644 --- a/src/app/search/service/search_helper.js +++ b/src/app/search/service/search_helper.js @@ -19,13 +19,16 @@ * A service providing helper functions for search views. */ angular.module('sb.search').factory('SearchHelper', - function(User, Project, ProjectGroup, Story, Task, Criteria, $filter) { + function(User, Project, ProjectGroup, Story, Task, Criteria, + $filter, $q) { 'use strict'; /** * Create search criteria based on some given parameters. */ - function parseParameters(params, criteria) { + function parseParameters(params) { + var criteria = []; + var promises = []; if (params.q) { criteria.push( Criteria.create('Text', params.q) @@ -57,8 +60,10 @@ angular.module('sb.search').factory('SearchHelper', } if (params.assignee_id || params.creator_id) { var id = params.assignee_id || params.creator_id; - User.get({'id': id}).$promise - .then(function(result) { + var userPromise = User.get({'id': id}).$promise; + promises.push(userPromise); + + userPromise.then(function(result) { criteria.push( Criteria.create('User', params.assignee_id, @@ -68,8 +73,11 @@ angular.module('sb.search').factory('SearchHelper', ); } if (params.project_id) { - Project.get({'id': params.project_id}).$promise - .then(function(result) { + var projectParams = {'id': params.project_id}; + var projectPromise = Project.get(projectParams).$promise; + promises.push(projectPromise); + + projectPromise.then(function(result) { criteria.push( Criteria.create('Project', params.project_id, @@ -79,8 +87,11 @@ angular.module('sb.search').factory('SearchHelper', ); } if (params.project_group_id) { - ProjectGroup.get({'id': params.project_group_id}).$promise - .then(function(result) { + var groupParams = {'id': params.project_group_id}; + var groupPromise = ProjectGroup.get(groupParams).$promise; + promises.push(groupPromise); + + groupPromise.then(function(result) { criteria.push( Criteria.create('ProjectGroup', params.project_group_id, @@ -90,8 +101,11 @@ angular.module('sb.search').factory('SearchHelper', ); } if (params.story_id) { - Story.get({'id': params.story_id}).$promise - .then(function(result) { + var storyParams = {'id': params.story_id}; + var storyPromise = Story.get(storyParams).$promise; + promises.push(storyPromise); + + storyPromise.then(function(result) { criteria.push( Criteria.create('Story', params.story_id, @@ -101,8 +115,11 @@ angular.module('sb.search').factory('SearchHelper', ); } if (params.task_id) { - Task.get({'id': params.task_id}).$promise - .then(function(result) { + var taskParams = {'id': params.task_id}; + var taskPromise = Task.get(taskParams).$promise; + promises.push(taskPromise); + + taskPromise.then(function(result) { criteria.push( Criteria.create('Task', params.task_id, @@ -111,6 +128,13 @@ angular.module('sb.search').factory('SearchHelper', } ); } + + var deferred = $q.defer(); + $q.all(promises).then(function() { + deferred.resolve(criteria); + }); + + return deferred.promise; } return { diff --git a/src/app/stories/controller/story_list_controller.js b/src/app/stories/controller/story_list_controller.js index 90258db5..04eb0d73 100644 --- a/src/app/stories/controller/story_list_controller.js +++ b/src/app/stories/controller/story_list_controller.js @@ -27,9 +27,7 @@ angular.module('sb.story').controller('StoryListController', $scope.resourceTypes = ['Story']; // Search result criteria default must be "active" - $scope.defaultCriteria = []; - - SearchHelper.parseParameters($stateParams, $scope.defaultCriteria); + $scope.defaultCriteria = SearchHelper.parseParameters($stateParams); /** * Creates a new story.