Move the parameter parsing into a service for reuse
This commit moves the code for parsing the parameters representing search criteria in the URL into a separate service rather than the StoryListController so that it can be reused elsewhere. This commit also completes the implementation such that all useful parameters can be parsed. Change-Id: If2addfcab054a4e869d391dbda5f3845bbd701f6
This commit is contained in:
parent
e4137a2f53
commit
dbc72feb87
120
src/app/search/service/search_helper.js
Normal file
120
src/app/search/service/search_helper.js
Normal 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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
|
* 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
|
* 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
|
* not use this file except in compliance with the License. You may obtain
|
||||||
@ -18,9 +19,8 @@
|
|||||||
* Controller for our story list.
|
* Controller for our story list.
|
||||||
*/
|
*/
|
||||||
angular.module('sb.story').controller('StoryListController',
|
angular.module('sb.story').controller('StoryListController',
|
||||||
function ($scope, $state, Criteria, NewStoryService,
|
function ($scope, $state, Criteria, NewStoryService, SubscriptionList,
|
||||||
SubscriptionList, CurrentUser, $stateParams, $filter, $q,
|
CurrentUser, $stateParams, SearchHelper) {
|
||||||
Tags, User, Project, ProjectGroup) {
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// search results must be of type "story"
|
// search results must be of type "story"
|
||||||
@ -29,53 +29,7 @@ angular.module('sb.story').controller('StoryListController',
|
|||||||
// Search result criteria default must be "active"
|
// Search result criteria default must be "active"
|
||||||
$scope.defaultCriteria = [];
|
$scope.defaultCriteria = [];
|
||||||
|
|
||||||
if ($stateParams.q) {
|
SearchHelper.parseParameters($stateParams, $scope.defaultCriteria);
|
||||||
$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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new story.
|
* Creates a new story.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user