1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 18:21:12 +03:00

Merge pull request #1178 from mabashian/1161-templates-list-pagination

Fixed templates list pagination
This commit is contained in:
Michael Abashian 2018-04-02 13:44:33 -04:00 committed by GitHub
commit 8ad9d07896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 51 deletions

View File

@ -46,17 +46,19 @@ function ListTemplatesController(
$scope.canAdd = ($scope.canAddJobTemplate || $scope.canAddWorkflowJobTemplate); $scope.canAdd = ($scope.canAddJobTemplate || $scope.canAddWorkflowJobTemplate);
// smart-search // smart-search
const name = 'templates'; $scope.list = {
const iterator = 'template'; iterator: 'template',
const key = 'template_dataset'; name: 'templates'
};
$scope.list = { iterator, name }; $scope.collection = {
$scope.collection = { iterator, basePath: 'unified_job_templates' }; iterator: 'template',
$scope[key] = Dataset.data; basePath: 'unified_job_templates'
$scope[name] = Dataset.data.results; };
$scope.template_dataset = Dataset.data;
$scope.templates = Dataset.data.results;
$scope.$on('updateDataset', (e, dataset) => { $scope.$on('updateDataset', (e, dataset) => {
$scope[key] = dataset; $scope.template_dataset = dataset;
$scope[name] = dataset.results; $scope.templates = dataset.results;
}); });
vm.isInvalid = (template) => { vm.isInvalid = (template) => {

View File

@ -114,11 +114,10 @@
</at-row> </at-row>
</at-list> </at-list>
<paginate <paginate
collection="collection" collection="templates"
dataset="template_dataset" dataset="template_dataset"
iterator="template" iterator="template"
base-path="unified_job_templates" base-path="unified_job_templates">
query-set="querySet">
</paginate> </paginate>
</at-panel-body> </at-panel-body>
</at-panel> </at-panel>

View File

@ -5,27 +5,9 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
queryset, path; queryset, path;
$scope.pageSize = pageSize; $scope.pageSize = pageSize;
$scope.basePageSize = parseInt(pageSize) === 5 ? 5 : 20; $scope.basePageSize = parseInt(pageSize) === 5 ? 5 : 20;
$scope.maxVisiblePages = $scope.maxVisiblePages ? parseInt($scope.maxVisiblePages) : 10; $scope.maxVisiblePages = $scope.maxVisiblePages ? parseInt($scope.maxVisiblePages) : 10;
function init() {
let updatePaginationVariables = function() {
$scope.pageSize = calcPageSize();
$scope.current = calcCurrent();
$scope.last = calcLast();
$scope.pageRange = calcPageRange($scope.current, $scope.last);
$scope.dataRange = calcDataRange();
};
updatePaginationVariables();
$scope.$watch('collection', function(){
updatePaginationVariables();
});
}
$scope.filter = function(id){ $scope.filter = function(id){
let pageSize = Number(id); let pageSize = Number(id);
$('#period-dropdown') $('#period-dropdown')
@ -35,8 +17,7 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
if ($scope.querySet){ if ($scope.querySet){
let origQuerySet = _.cloneDeep($scope.querySet); let origQuerySet = _.cloneDeep($scope.querySet);
queryset = _.merge(origQuerySet, { page_size: pageSize }); queryset = _.merge(origQuerySet, { page_size: pageSize });
} } else {
else {
queryset = _.merge($stateParams[`${$scope.iterator}_search`], { page_size: pageSize, page: 1 }); queryset = _.merge($stateParams[`${$scope.iterator}_search`], { page_size: pageSize, page: 1 });
} }
$scope.toPage(); $scope.toPage();
@ -59,8 +40,7 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
let origQuerySet = _.cloneDeep($scope.querySet); let origQuerySet = _.cloneDeep($scope.querySet);
queryset = _.merge(origQuerySet, { page: page }); queryset = _.merge(origQuerySet, { page: page });
} } else {
else {
queryset = _.merge($stateParams[`${$scope.iterator}_search`], { page: page }); queryset = _.merge($stateParams[`${$scope.iterator}_search`], { page: page });
} }
if (!$scope.querySet) { if (!$scope.querySet) {
@ -85,8 +65,7 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
function calcCurrent() { function calcCurrent() {
if ($scope.querySet) { if ($scope.querySet) {
return parseInt($scope.querySet.page || '1'); return parseInt($scope.querySet.page || '1');
} } else {
else {
return parseInt($stateParams[`${$scope.iterator}_search`].page || '1'); return parseInt($stateParams[`${$scope.iterator}_search`].page || '1');
} }
} }
@ -100,19 +79,16 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
// It's an odd number // It's an odd number
pagesLeft = (maxVisiblePages - 1) / 2; pagesLeft = (maxVisiblePages - 1) / 2;
pagesRight = ((maxVisiblePages - 1) / 2) + 1; pagesRight = ((maxVisiblePages - 1) / 2) + 1;
} } else {
else {
// Its an even number // Its an even number
pagesLeft = pagesRight = maxVisiblePages / 2; pagesLeft = pagesRight = maxVisiblePages / 2;
} }
if (last < maxVisiblePages) { if (last < maxVisiblePages) {
// Don't have enough pages to exceed the max range - just show all of them // Don't have enough pages to exceed the max range - just show all of them
result = _.range(1, last + 1); result = _.range(1, last + 1);
} } else if (current === last) {
else if(current === last) {
result = _.range(last + 1 - maxVisiblePages, last + 1); result = _.range(last + 1 - maxVisiblePages, last + 1);
} } else {
else {
let topOfRange = current + pagesRight > maxVisiblePages + 1 ? (current + pagesRight < last + 1 ? current + pagesRight : last + 1) : maxVisiblePages + 1; let topOfRange = current + pagesRight > maxVisiblePages + 1 ? (current + pagesRight < last + 1 ? current + pagesRight : last + 1) : maxVisiblePages + 1;
let bottomOfRange = (topOfRange === last + 1) ? last + 1 - maxVisiblePages : (current - pagesLeft > 0 ? current - pagesLeft : 1); let bottomOfRange = (topOfRange === last + 1) ? last + 1 - maxVisiblePages : (current - pagesLeft > 0 ? current - pagesLeft : 1);
result = _.range(bottomOfRange, topOfRange); result = _.range(bottomOfRange, topOfRange);
@ -137,6 +113,18 @@ export default ['$scope', '$stateParams', '$state', '$filter', 'GetBasePath', 'Q
return Number(pageSize) ; return Number(pageSize) ;
} }
init(); let updatePaginationVariables = function() {
$scope.pageSize = calcPageSize();
$scope.current = calcCurrent();
$scope.last = calcLast();
$scope.pageRange = calcPageRange($scope.current, $scope.last);
$scope.dataRange = calcDataRange();
};
updatePaginationVariables();
$scope.$watch('collection', function(){
updatePaginationVariables();
});
} }
]; ];