storyboard-webclient/src/app/projects/controller/project_story_list_controll...

142 lines
4.6 KiB
JavaScript

/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
*
* 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.
*/
/**
* This controller manages stories within the scope of a particular project.
*/
angular.module('sb.projects').controller('ProjectStoryListController',
function ($scope, $state, $stateParams, Story, NewStoryService,
Preference, SubscriptionList, CurrentUser) {
'use strict';
var pageSize = Preference.get('project_detail_page_size');
// Variables and methods available to the template...
function resetScope() {
$scope.storyCount = 0;
$scope.stories = [];
$scope.error = {};
}
$scope.searchQuery = '';
$scope.isSearching = false;
$scope.filter = 'active';
$scope.offsets = {
'active': 0,
'merged': 0,
'invalid': 0
};
/**
* Set the filter and refresh the search.
*/
$scope.setFilter = function (state) {
$scope.offsets[$scope.filter] = $scope.searchOffset;
$scope.filter = state;
$scope.searchOffset = $scope.offsets[$scope.filter];
$scope.search();
};
/**
* The search method.
*/
$scope.search = function () {
// Clear the scope and set the progress flag.
resetScope();
$scope.isSearching = true;
// Execute the story query.
Story.browse({
project_id: $scope.project.id,
status: $scope.filter || null,
offset: $scope.searchOffset,
limit: pageSize,
sort_dir: 'desc'
},
function (result, headers) {
// Successful search results, apply the results to the
// scope and unset our progress flag.
$scope.storyCount =
parseInt(headers('X-Total')) || result.length;
$scope.searchOffset = parseInt(headers('X-Offset')) || 0;
$scope.searchLimit = parseInt(headers('X-Limit')) || 0;
$scope.stories = result;
$scope.isSearching = false;
},
function (error) {
// Error search results, show the error in the UI and
// unset our progress flag.
$scope.error = error;
$scope.isSearching = false;
}
);
};
/**
* Update the page size preference and re-search.
*/
$scope.updatePageSize = function (value) {
Preference.set('project_detail_page_size', value).then(
function () {
pageSize = value;
$scope.search();
}
);
};
/**
* Next page of the results.
*/
$scope.nextPage = function () {
$scope.searchOffset += pageSize;
$scope.search();
};
/**
* Previous page of the results.
*/
$scope.previousPage = function () {
$scope.searchOffset -= pageSize;
if ($scope.searchOffset < 0) {
$scope.searchOffset = 0;
}
$scope.search();
};
$scope.newStory = function () {
NewStoryService.showNewStoryModal($scope.project.id)
.then(function (story) {
// On success, go to the story detail
$state.go('sb.story.detail', {storyId: story.id});
}
);
};
// Initialize the view with a default search.
resetScope();
$scope.project.$promise.then(function() {
$scope.search();
});
// GET list of story subscriptions
var cuPromise = CurrentUser.resolve();
cuPromise.then(function(user){
$scope.storySubscriptions = SubscriptionList.subsList(
'story', user);
});
});