moved pagination helper to a service

This commit is contained in:
John Mitchell 2016-01-11 17:35:06 -05:00
parent 1a17321ece
commit c6f9dc90fb
2 changed files with 61 additions and 89 deletions

View File

@ -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);
}
]);
};
}
]);

View File

@ -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'
])