angular-utils-pagination
Advanced tools
Comparing version 0.8.2 to 0.9.2
{ | ||
"name": "angularUtils-pagination", | ||
"version": "0.8.2", | ||
"version": "0.9.2", | ||
"homepage": "https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -28,11 +28,3 @@ /** | ||
*/ | ||
var module; | ||
try { | ||
module = angular.module(moduleName); | ||
} catch(err) { | ||
// named module does not exist, so create one | ||
module = angular.module(moduleName, []); | ||
} | ||
module | ||
angular.module(moduleName, []) | ||
.directive('dirPaginate', ['$compile', '$parse', 'paginationService', dirPaginateDirective]) | ||
@@ -51,2 +43,3 @@ .directive('dirPaginateNoCompile', noCompileDirective) | ||
multiElement: true, | ||
priority: 100, | ||
compile: dirPaginationCompileFn | ||
@@ -58,6 +51,6 @@ }; | ||
var expression = tAttrs.dirPaginate; | ||
// regex taken directly from https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L211 | ||
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?\s*$/); | ||
// regex taken directly from https://github.com/angular/angular.js/blob/v1.4.x/src/ng/directive/ngRepeat.js#L339 | ||
var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/); | ||
var filterPattern = /\|\s*itemsPerPage\s*:[^|\)]*/; | ||
var filterPattern = /\|\s*itemsPerPage\s*:\s*(.*\(\s*\w*\)|([^\)]*?(?=\s+as\s+))|[^\)]*)/; | ||
if (match[2].match(filterPattern) === null) { | ||
@@ -129,3 +122,3 @@ throw 'pagination directive: the \'itemsPerPage\' filter must be set.'; | ||
if (paginationId !== DEFAULT_ID && !idDefinedInFilter) { | ||
repeatExpression = expression.replace(/(\|\s*itemsPerPage\s*:[^|]*)/, "$1 : '" + paginationId + "'"); | ||
repeatExpression = expression.replace(/(\|\s*itemsPerPage\s*:\s*[^|\s]*)/, "$1 : '" + paginationId + "'"); | ||
} else { | ||
@@ -161,3 +154,3 @@ repeatExpression = expression; | ||
angular.forEach(tElement, function(el) { | ||
if (el.nodeType === Node.ELEMENT_NODE) { | ||
if (el.nodeType === 1) { | ||
angular.element(el).attr('dir-paginate-no-compile', true); | ||
@@ -174,3 +167,3 @@ } | ||
angular.forEach(element, function(el) { | ||
if (el.nodeType === Node.ELEMENT_NODE) { | ||
if (el.nodeType === 1) { | ||
angular.element(el).removeAttr('dir-paginate-no-compile'); | ||
@@ -197,4 +190,7 @@ } | ||
} else { | ||
// if the current-page attribute was not set, we'll make our own | ||
var defaultCurrentPage = paginationId + '__currentPage'; | ||
// If the current-page attribute was not set, we'll make our own. | ||
// Replace any non-alphanumeric characters which might confuse | ||
// the $parse service and give unexpected results. | ||
// See https://github.com/michaelbromley/angularUtils/issues/233 | ||
var defaultCurrentPage = (paginationId + '__currentPage').replace(/\W/g, '_'); | ||
scope[defaultCurrentPage] = 1; | ||
@@ -220,3 +216,3 @@ currentPageGetter = $parse(defaultCurrentPage); | ||
function dirPaginationControlsTemplateInstaller($templateCache) { | ||
$templateCache.put('angularUtils.directives.dirPagination.template', '<ul class="pagination" ng-if="1 < pages.length || !autoHide"><li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == 1 }"><a href="" ng-click="setCurrent(1)">«</a></li><li ng-if="directionLinks" ng-class="{ disabled : pagination.current == 1 }"><a href="" ng-click="setCurrent(pagination.current - 1)">‹</a></li><li ng-repeat="pageNumber in pages track by $index" ng-class="{ active : pagination.current == pageNumber, disabled : pageNumber == \'...\' || ( ! autoHide && pages.length === 1 ) }"><a href="" ng-click="setCurrent(pageNumber)">{{ pageNumber }}</a></li><li ng-if="directionLinks" ng-class="{ disabled : pagination.current == pagination.last }"><a href="" ng-click="setCurrent(pagination.current + 1)">›</a></li><li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == pagination.last }"><a href="" ng-click="setCurrent(pagination.last)">»</a></li></ul>'); | ||
$templateCache.put('angularUtils.directives.dirPagination.template', '<ul class="pagination" ng-if="1 < pages.length || !autoHide"><li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == 1 }"><a href="" ng-click="setCurrent(1)">«</a></li><li ng-if="directionLinks" ng-class="{ disabled : pagination.current == 1 }"><a href="" ng-click="setCurrent(pagination.current - 1)">‹</a></li><li ng-repeat="pageNumber in pages track by tracker(pageNumber, $index)" ng-class="{ active : pagination.current == pageNumber, disabled : pageNumber == \'...\' || ( ! autoHide && pages.length === 1 ) }"><a href="" ng-click="setCurrent(pageNumber)">{{ pageNumber }}</a></li><li ng-if="directionLinks" ng-class="{ disabled : pagination.current == pagination.last }"><a href="" ng-click="setCurrent(pagination.current + 1)">›</a></li><li ng-if="boundaryLinks" ng-class="{ disabled : pagination.current == pagination.last }"><a href="" ng-click="setCurrent(pagination.last)">»</a></li></ul>'); | ||
} | ||
@@ -252,3 +248,3 @@ | ||
var idMessage = (paginationId !== DEFAULT_ID) ? ' (id: ' + paginationId + ') ' : ' '; | ||
throw 'pagination directive: the pagination controls' + idMessage + 'cannot be used without the corresponding pagination directive.'; | ||
console.warn('Pagination directive: the pagination controls' + idMessage + 'cannot be used without the corresponding pagination directive, which was not found at link time.'); | ||
} | ||
@@ -274,3 +270,5 @@ | ||
scope.$watch(function() { | ||
return (paginationService.getCollectionLength(paginationId) + 1) * paginationService.getItemsPerPage(paginationId); | ||
if (paginationService.isRegistered(paginationId)) { | ||
return (paginationService.getCollectionLength(paginationId) + 1) * paginationService.getItemsPerPage(paginationId); | ||
} | ||
}, function(length) { | ||
@@ -283,3 +281,5 @@ if (0 < length) { | ||
scope.$watch(function() { | ||
return (paginationService.getItemsPerPage(paginationId)); | ||
if (paginationService.isRegistered(paginationId)) { | ||
return (paginationService.getItemsPerPage(paginationId)); | ||
} | ||
}, function(current, previous) { | ||
@@ -292,3 +292,5 @@ if (current != previous && typeof previous !== 'undefined') { | ||
scope.$watch(function() { | ||
return paginationService.getCurrentPage(paginationId); | ||
if (paginationService.isRegistered(paginationId)) { | ||
return paginationService.getCurrentPage(paginationId); | ||
} | ||
}, function(currentPage, previousPage) { | ||
@@ -301,3 +303,3 @@ if (currentPage != previousPage) { | ||
scope.setCurrent = function(num) { | ||
if (isValidPageNumber(num)) { | ||
if (paginationService.isRegistered(paginationId) && isValidPageNumber(num)) { | ||
num = parseInt(num, 10); | ||
@@ -308,4 +310,16 @@ paginationService.setCurrentPage(paginationId, num); | ||
/** | ||
* Custom "track by" function which allows for duplicate "..." entries on long lists, | ||
* yet fixes the problem of wrongly-highlighted links which happens when using | ||
* "track by $index" - see https://github.com/michaelbromley/angularUtils/issues/153 | ||
* @param id | ||
* @param index | ||
* @returns {string} | ||
*/ | ||
scope.tracker = function(id, index) { | ||
return id + '_' + index; | ||
}; | ||
function goToPage(num) { | ||
if (isValidPageNumber(num)) { | ||
if (paginationService.isRegistered(paginationId) && isValidPageNumber(num)) { | ||
scope.pages = generatePagesArray(num, paginationService.getCollectionLength(paginationId), paginationService.getItemsPerPage(paginationId), paginationRange); | ||
@@ -323,11 +337,12 @@ scope.pagination.current = num; | ||
function generatePagination() { | ||
var page = parseInt(paginationService.getCurrentPage(paginationId)) || 1; | ||
scope.pages = generatePagesArray(page, paginationService.getCollectionLength(paginationId), paginationService.getItemsPerPage(paginationId), paginationRange); | ||
scope.pagination.current = page; | ||
scope.pagination.last = scope.pages[scope.pages.length - 1]; | ||
if (scope.pagination.last < scope.pagination.current) { | ||
scope.setCurrent(scope.pagination.last); | ||
} else { | ||
updateRangeValues(); | ||
if (paginationService.isRegistered(paginationId)) { | ||
var page = parseInt(paginationService.getCurrentPage(paginationId)) || 1; | ||
scope.pages = generatePagesArray(page, paginationService.getCollectionLength(paginationId), paginationService.getItemsPerPage(paginationId), paginationRange); | ||
scope.pagination.current = page; | ||
scope.pagination.last = scope.pages[scope.pages.length - 1]; | ||
if (scope.pagination.last < scope.pagination.current) { | ||
scope.setCurrent(scope.pagination.last); | ||
} else { | ||
updateRangeValues(); | ||
} | ||
} | ||
@@ -341,11 +356,12 @@ } | ||
function updateRangeValues() { | ||
var currentPage = paginationService.getCurrentPage(paginationId), | ||
itemsPerPage = paginationService.getItemsPerPage(paginationId), | ||
totalItems = paginationService.getCollectionLength(paginationId); | ||
if (paginationService.isRegistered(paginationId)) { | ||
var currentPage = paginationService.getCurrentPage(paginationId), | ||
itemsPerPage = paginationService.getItemsPerPage(paginationId), | ||
totalItems = paginationService.getCollectionLength(paginationId); | ||
scope.range.lower = (currentPage - 1) * itemsPerPage + 1; | ||
scope.range.upper = Math.min(currentPage * itemsPerPage, totalItems); | ||
scope.range.total = totalItems; | ||
scope.range.lower = (currentPage - 1) * itemsPerPage + 1; | ||
scope.range.upper = Math.min(currentPage * itemsPerPage, totalItems); | ||
scope.range.total = totalItems; | ||
} | ||
} | ||
function isValidPageNumber(num) { | ||
@@ -442,3 +458,3 @@ return (numberRegex.test(num) && (0 < num && num <= scope.pagination.last)); | ||
var start; | ||
if (collection instanceof Array) { | ||
if (angular.isObject(collection)) { | ||
itemsPerPage = parseInt(itemsPerPage) || 9999999999; | ||
@@ -453,3 +469,14 @@ if (paginationService.isAsyncMode(paginationId)) { | ||
return collection.slice(start, end); | ||
if (collection instanceof Array) { | ||
// the array just needs to be sliced | ||
return collection.slice(start, end); | ||
} else { | ||
// in the case of an object, we need to get an array of keys, slice that, then map back to | ||
// the original object. | ||
var slicedObject = {}; | ||
angular.forEach(keys(collection).slice(start, end), function(key) { | ||
slicedObject[key] = collection[key]; | ||
}); | ||
return slicedObject; | ||
} | ||
} else { | ||
@@ -462,2 +489,21 @@ return collection; | ||
/** | ||
* Shim for the Object.keys() method which does not exist in IE < 9 | ||
* @param obj | ||
* @returns {Array} | ||
*/ | ||
function keys(obj) { | ||
if (!Object.keys) { | ||
var objKeys = []; | ||
for (var i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
objKeys.push(i); | ||
} | ||
} | ||
return objKeys; | ||
} else { | ||
return Object.keys(obj); | ||
} | ||
} | ||
/** | ||
* This service allows the various parts of the module to communicate and stay in sync. | ||
@@ -464,0 +510,0 @@ */ |
Package.describe({ | ||
name: 'angularutils:pagination', | ||
summary: 'Magical automatic pagination for anything in AngularJS', | ||
version: '0.7.0', | ||
git: 'https://github.com/Urigo/angularUtils-pagination' | ||
version: '0.9.2', | ||
git: 'https://github.com/michaelbromley/angularUtils-pagination' | ||
}); | ||
@@ -11,5 +11,5 @@ | ||
api.use('angular:angular@1.2.24', 'client'); | ||
api.use('angular:angular@1.4.0', 'client'); | ||
api.addFiles('dirPagination.js', 'client'); | ||
}); |
{ | ||
"name": "angular-utils-pagination", | ||
"version": "0.8.2", | ||
"version": "0.9.2", | ||
"description": "Magical automatic pagination for anything in AngularJS", | ||
"main": "index.js", | ||
"main": "dirPagination.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
Sorry, the diff of this file is not supported yet
29990
547
9