From 1a17321ece92f6cc310bb3444034560f5f602954 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 11 Jan 2016 14:37:33 -0500 Subject: [PATCH 1/3] ugly but working commit of getting pagination to work on edit of a form item --- .../client/src/helpers/PaginationHelpers.js | 1 + awx/ui/client/src/helpers/refresh.js | 112 +++++++++++++----- 2 files changed, 82 insertions(+), 31 deletions(-) diff --git a/awx/ui/client/src/helpers/PaginationHelpers.js b/awx/ui/client/src/helpers/PaginationHelpers.js index 652e17c7f2..d2019af8fc 100644 --- a/awx/ui/client/src/helpers/PaginationHelpers.js +++ b/awx/ui/client/src/helpers/PaginationHelpers.js @@ -147,6 +147,7 @@ export default new_url += (scope[iterator + 'SearchParams']) ? '&' + scope[iterator + 'SearchParams'] + '&page_size=' + scope[iterator + '_page_size'] : '&page_size=' + scope[iterator + 'PageSize']; Wait('start'); + scope.getNewPage = true; Refresh({ scope: scope, set: set, iterator: iterator, url: new_url }); }; diff --git a/awx/ui/client/src/helpers/refresh.js b/awx/ui/client/src/helpers/refresh.js index 23a1f905e5..e9821d366b 100644 --- a/awx/ui/client/src/helpers/refresh.js +++ b/awx/ui/client/src/helpers/refresh.js @@ -24,8 +24,8 @@ export default angular.module('RefreshHelper', ['RestServices', 'Utilities', 'PaginationHelpers']) - .factory('Refresh', ['ProcessErrors', 'Rest', 'Wait', 'Empty', 'PageRangeSetup', - function (ProcessErrors, Rest, Wait, Empty, PageRangeSetup) { + .factory('Refresh', ['$location', 'ProcessErrors', 'Rest', 'Wait', 'Empty', 'PageRangeSetup', + function ($location, ProcessErrors, Rest, Wait, Empty, PageRangeSetup) { return function (params) { var scope = params.scope, @@ -34,37 +34,87 @@ export default url = params.url, deferWaitStop = params.deferWaitStop; - scope.current_url = url; - Rest.setUrl(url); - Rest.get() - .success(function (data) { - var i, modifier; - PageRangeSetup({ - scope: scope, - count: data.count, - next: data.next, - previous: data.previous, - iterator: iterator + if ($location.$$url.split("/")[2] && !scope.getNewPage) { + var id = $location.$$url.split("/")[2]; + Rest.setUrl(params.url.split("?")[0] + "?id=" + id); + Rest.get() + .then(function (data) { + var name = data.data.results[0].name; + Rest.setUrl(params.url.split("?")[0] + "?name__lte=" + name); + Rest.get() + .then(function (data) { + var currentPage = Math.ceil(data.data.count/scope[iterator + '_page_size']); + scope[iterator + '_page'] = currentPage; + params.url = params.url + "&page=" + currentPage; + scope.current_url = params.url; + Rest.setUrl(params.url); + Rest.get() + .success(function (data) { + var i, modifier; + PageRangeSetup({ + scope: scope, + count: data.count, + next: data.next, + previous: data.previous, + iterator: iterator + }); + for (i = 1; i <= 3; i++) { + modifier = (i === 1) ? '' : i; + scope[iterator + 'HoldInput' + modifier] = false; + } + scope[set] = data.results; + scope[iterator + 'Loading'] = false; + scope[iterator + 'HidePaginator'] = false; + if (!deferWaitStop) { + Wait('stop'); + } + scope.$emit('PostRefresh', set); + }) + .error(function (data, status) { + scope[iterator + 'HoldInput'] = false; + ProcessErrors(scope, data, status, null, { + hdr: 'Error!', + msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status + }); + }); + }); }); - for (i = 1; i <= 3; i++) { - modifier = (i === 1) ? '' : i; - scope[iterator + 'HoldInput' + modifier] = false; - } - scope[set] = data.results; - scope[iterator + 'Loading'] = false; - scope[iterator + 'HidePaginator'] = false; - if (!deferWaitStop) { - Wait('stop'); - } - scope.$emit('PostRefresh', set); - }) - .error(function (data, status) { - scope[iterator + 'HoldInput'] = false; - ProcessErrors(scope, data, status, null, { - hdr: 'Error!', - msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status + } else { + scope.current_url = url; + Rest.setUrl(url); + Rest.get() + .success(function (data) { + var i, modifier; + PageRangeSetup({ + scope: scope, + count: data.count, + next: data.next, + previous: data.previous, + iterator: iterator + }); + for (i = 1; i <= 3; i++) { + modifier = (i === 1) ? '' : i; + scope[iterator + 'HoldInput' + modifier] = false; + } + scope[set] = data.results; + scope[iterator + 'Loading'] = false; + scope[iterator + 'HidePaginator'] = false; + if (!deferWaitStop) { + Wait('stop'); + } + scope.$emit('PostRefresh', set); + }) + .error(function (data, status) { + scope[iterator + 'HoldInput'] = false; + ProcessErrors(scope, data, status, null, { + hdr: 'Error!', + msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status + }); }); - }); + scope.getNewPage = false; + } }; + + } ]); From c6f9dc90fb9ab342083c81021af65d35c266f778 Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Mon, 11 Jan 2016 17:35:06 -0500 Subject: [PATCH 2/3] moved pagination helper to a service --- awx/ui/client/src/helpers/refresh.js | 148 +++++++++++---------------- awx/ui/client/src/shared/main.js | 2 + 2 files changed, 61 insertions(+), 89 deletions(-) diff --git a/awx/ui/client/src/helpers/refresh.js b/awx/ui/client/src/helpers/refresh.js index e9821d366b..0a15cf1b71 100644 --- a/awx/ui/client/src/helpers/refresh.js +++ b/awx/ui/client/src/helpers/refresh.js @@ -24,97 +24,67 @@ export default angular.module('RefreshHelper', ['RestServices', 'Utilities', 'PaginationHelpers']) - .factory('Refresh', ['$location', 'ProcessErrors', 'Rest', 'Wait', 'Empty', 'PageRangeSetup', - function ($location, ProcessErrors, Rest, Wait, Empty, PageRangeSetup) { - return function (params) { + .factory('Refresh', ['$location', 'ProcessErrors', 'Rest', 'Wait', 'Empty', 'PageRangeSetup', 'pagination', function ($location, ProcessErrors, Rest, Wait, Empty, PageRangeSetup, pagination) { + return function (params) { - var scope = params.scope, - set = params.set, - iterator = params.iterator, - url = params.url, - deferWaitStop = params.deferWaitStop; + var scope = params.scope, + set = params.set, + iterator = params.iterator, + deferWaitStop = params.deferWaitStop; - if ($location.$$url.split("/")[2] && !scope.getNewPage) { - var id = $location.$$url.split("/")[2]; - Rest.setUrl(params.url.split("?")[0] + "?id=" + id); - Rest.get() - .then(function (data) { - var name = data.data.results[0].name; - Rest.setUrl(params.url.split("?")[0] + "?name__lte=" + name); - Rest.get() - .then(function (data) { - var currentPage = Math.ceil(data.data.count/scope[iterator + '_page_size']); - scope[iterator + '_page'] = currentPage; - params.url = params.url + "&page=" + currentPage; - scope.current_url = params.url; - Rest.setUrl(params.url); - Rest.get() - .success(function (data) { - var i, modifier; - PageRangeSetup({ - scope: scope, - count: data.count, - next: data.next, - previous: data.previous, - iterator: iterator - }); - for (i = 1; i <= 3; i++) { - modifier = (i === 1) ? '' : i; - scope[iterator + 'HoldInput' + modifier] = false; - } - scope[set] = data.results; - scope[iterator + 'Loading'] = false; - scope[iterator + 'HidePaginator'] = false; - if (!deferWaitStop) { - Wait('stop'); - } - scope.$emit('PostRefresh', set); - }) - .error(function (data, status) { - scope[iterator + 'HoldInput'] = false; - ProcessErrors(scope, data, status, null, { - hdr: 'Error!', - msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status - }); - }); - }); - }); - } else { - scope.current_url = url; - Rest.setUrl(url); - Rest.get() - .success(function (data) { - var i, modifier; - PageRangeSetup({ - scope: scope, - count: data.count, - next: data.next, - previous: data.previous, - iterator: iterator - }); - for (i = 1; i <= 3; i++) { - modifier = (i === 1) ? '' : i; - scope[iterator + 'HoldInput' + modifier] = false; - } - scope[set] = data.results; - scope[iterator + 'Loading'] = false; - scope[iterator + 'HidePaginator'] = false; - if (!deferWaitStop) { - Wait('stop'); - } - scope.$emit('PostRefresh', set); - }) - .error(function (data, status) { - scope[iterator + 'HoldInput'] = false; - ProcessErrors(scope, data, status, null, { - hdr: 'Error!', - msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status - }); - }); - scope.getNewPage = false; - } - }; + var getPage = function(url) { + scope.current_url = url; + Rest.setUrl(url); + Rest.get() + .success(function (data) { + var i, modifier; + PageRangeSetup({ + scope: scope, + count: data.count, + next: data.next, + previous: data.previous, + iterator: iterator + }); + for (i = 1; i <= 3; i++) { + modifier = (i === 1) ? '' : i; + scope[iterator + 'HoldInput' + modifier] = false; + } + scope[set] = data.results; + scope[iterator + 'Loading'] = false; + scope[iterator + 'HidePaginator'] = false; + if (!deferWaitStop) { + Wait('stop'); + } + scope.$emit('PostRefresh', set); + }) + .error(function (data, status) { + scope[iterator + 'HoldInput'] = false; + ProcessErrors(scope, data, status, null, { + hdr: 'Error!', + msg: 'Failed to retrieve ' + set + '. GET returned status: ' + status + }); + }); + }; + // if you're editing an object, make sure you're on the right + // page to display the element you are editing + if ($location.$$url.split("/")[1] === params.set && $location.$$url.split("/")[2] && !scope.getNewPage) { + var id = $location.$$url.split("/")[2]; + var restUrl = params.url.split("?")[0]; + var pageSize = scope[iterator + '_page_size']; + pagination.getInitialPageForList(id, restUrl, pageSize) + .then(function (currentPage) { + scope[iterator + '_page'] = currentPage; + params.url = params.url + "&page=" + currentPage; + getPage(params.url); + + }); + } else { + getPage(params.url); } - ]); + }; + + + } +]); diff --git a/awx/ui/client/src/shared/main.js b/awx/ui/client/src/shared/main.js index 8996a1bca1..99f8019e21 100644 --- a/awx/ui/client/src/shared/main.js +++ b/awx/ui/client/src/shared/main.js @@ -5,6 +5,7 @@ *************************************************/ import listGenerator from './list-generator/main'; +import pagination from './pagination/main'; import title from './title.directive'; import lodashAsPromised from './lodash-as-promised'; import stringFilters from './string-filters/main'; @@ -14,6 +15,7 @@ import stateExtender from './stateExtender.provider'; export default angular.module('shared', [ listGenerator.name, + pagination.name, stringFilters.name, 'ui.router' ]) From 6a8955b8174abafcdb77ea720e7e861d3c9ace9b Mon Sep 17 00:00:00 2001 From: John Mitchell Date: Tue, 12 Jan 2016 11:54:26 -0500 Subject: [PATCH 3/3] add missing pagination folder --- awx/ui/client/src/shared/pagination/main.js | 11 ++++++++ .../shared/pagination/pagination.service.js | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 awx/ui/client/src/shared/pagination/main.js create mode 100644 awx/ui/client/src/shared/pagination/pagination.service.js diff --git a/awx/ui/client/src/shared/pagination/main.js b/awx/ui/client/src/shared/pagination/main.js new file mode 100644 index 0000000000..088bb0e399 --- /dev/null +++ b/awx/ui/client/src/shared/pagination/main.js @@ -0,0 +1,11 @@ +/************************************************* + * Copyright (c) 2015 Ansible, Inc. + * + * All Rights Reserved + *************************************************/ + +import pagination from './pagination.service'; + +export default + angular.module('pagination', []) + .factory('pagination', pagination); diff --git a/awx/ui/client/src/shared/pagination/pagination.service.js b/awx/ui/client/src/shared/pagination/pagination.service.js new file mode 100644 index 0000000000..f18d7890e9 --- /dev/null +++ b/awx/ui/client/src/shared/pagination/pagination.service.js @@ -0,0 +1,26 @@ +/************************************************* + * Copyright (c) 2015 Ansible, Inc. + * + * All Rights Reserved + *************************************************/ + +export default ['$q', '$http', function($q, $http) { + return { + getInitialPageForList: function(id, url, pageSize) { + // get the name of the object + return $http.get(url + "?id=" + id) + .then(function (data) { + var name = data.data.results[0].name; + // get how many results are less than or equal to + // the name + return $http.get(url + "?name__lte=" + name) + .then(function (data) { + // divide by the page size to get what + // page the data should be on + var count = data.data.count; + return Math.ceil(count/pageSize); + }); + }); + } + }; +}];