Merge "Move the parameter parsing into a service for reuse"

This commit is contained in:
Jenkins 2017-07-05 19:55:30 +00:00 committed by Gerrit Code Review
commit 7b1f577d91
2 changed files with 124 additions and 50 deletions

View File

@ -0,0 +1,120 @@
/**
* Copyright (c) 2016 Codethink Ltd
* Copyright (c) 2017 Adam Coldrick
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
/**
* A service providing helper functions for search views.
*/
angular.module('sb.search').factory('SearchHelper',
function(User, Project, ProjectGroup, Story, Task, Criteria, $filter) {
'use strict';
/**
* Create search criteria based on some given parameters.
*/
function parseParameters(params, criteria) {
if (params.q) {
criteria.push(
Criteria.create('Text', params.q)
);
}
if (params.title) {
criteria.push(
Criteria.create('Text', params.title)
);
}
if (params.status) {
criteria.push(
Criteria.create('StoryStatus', params.status,
$filter('capitalize')(params.status))
);
}
if (params.tags) {
if (params.tags.constructor === Array) {
angular.forEach(params.tags, function(tag) {
criteria.push(
Criteria.create('Tags', tag, tag)
);
});
} else {
criteria.push(
Criteria.create('Tags', params.tags, params.tags)
);
}
}
if (params.assignee_id || params.creator_id) {
var id = params.assignee_id || params.creator_id;
User.get({'id': id}).$promise
.then(function(result) {
criteria.push(
Criteria.create('User',
params.assignee_id,
result.full_name)
);
}
);
}
if (params.project_id) {
Project.get({'id': params.project_id}).$promise
.then(function(result) {
criteria.push(
Criteria.create('Project',
params.project_id,
result.name)
);
}
);
}
if (params.project_group_id) {
ProjectGroup.get({'id': params.project_group_id}).$promise
.then(function(result) {
criteria.push(
Criteria.create('ProjectGroup',
params.project_group_id,
result.title)
);
}
);
}
if (params.story_id) {
Story.get({'id': params.story_id}).$promise
.then(function(result) {
criteria.push(
Criteria.create('Story',
params.story_id,
result.title)
);
}
);
}
if (params.task_id) {
Task.get({'id': params.task_id}).$promise
.then(function(result) {
criteria.push(
Criteria.create('Task',
params.task_id,
result.title)
);
}
);
}
}
return {
parseParameters: parseParameters
};
}
);

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
* Copyright (c) 2016 Codethink Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
@ -18,9 +19,8 @@
* Controller for our story list.
*/
angular.module('sb.story').controller('StoryListController',
function ($scope, $state, Criteria, NewStoryService,
SubscriptionList, CurrentUser, $stateParams, $filter, $q,
Tags, User, Project, ProjectGroup) {
function ($scope, $state, Criteria, NewStoryService, SubscriptionList,
CurrentUser, $stateParams, SearchHelper) {
'use strict';
// search results must be of type "story"
@ -29,53 +29,7 @@ angular.module('sb.story').controller('StoryListController',
// Search result criteria default must be "active"
$scope.defaultCriteria = [];
if ($stateParams.q) {
$scope.defaultCriteria.push(
Criteria.create('Text', $stateParams.q)
);
}
if ($stateParams.status) {
$scope.defaultCriteria.push(
Criteria.create('StoryStatus', $stateParams.status,
$filter('capitalize')($stateParams.status))
);
}
if ($stateParams.tags) {
$scope.defaultCriteria.push(
Criteria.create('Tags', $stateParams.tags, $stateParams.tags)
);
}
if ($stateParams.assignee_id) {
User.get({'id': $stateParams.assignee_id}).$promise
.then(function(result) {
$scope.defaultCriteria.push(
Criteria.create('User', $stateParams.assignee_id,
result.full_name)
);
}
);
}
if ($stateParams.project_id) {
Project.get({'id': $stateParams.project_id}).$promise
.then(function(result) {
$scope.defaultCriteria.push(
Criteria.create('Project', $stateParams.project_id,
result.name)
);
}
);
}
if ($stateParams.project_group_id) {
ProjectGroup.get({'id': $stateParams.project_group_id}).$promise
.then(function(result) {
$scope.defaultCriteria.push(
Criteria.create('ProjectGroup',
$stateParams.project_group_id,
result.title)
);
}
);
}
SearchHelper.parseParameters($stateParams, $scope.defaultCriteria);
/**
* Creates a new story.