Skip to content

Instantly share code, notes, and snippets.

@ballawala-lv
Created May 7, 2014 15:16
Show Gist options
  • Select an option

  • Save ballawala-lv/7aa13b295a615eefe900 to your computer and use it in GitHub Desktop.

Select an option

Save ballawala-lv/7aa13b295a615eefe900 to your computer and use it in GitHub Desktop.
.directive('pagination', ['GTM', function (GTM) {
return {
restrict: 'E',
scope: {
numPages: '=',
currentPage: '=',
onSelectPage: '&',
capPages: '@',
class: '@'
},
templateUrl: '/templates/common/pagination.tpl.html',
replace: true,
link: function (scope) {
scope.$watch('numPages', function (value) {
scope.updatePagesArray();
});
scope.updatePagesArray = function () {
scope.pages = [];
var start = 1,
end = scope.numPages;
if (scope.capPages) {
scope.capPages = parseInt(scope.capPages);
start = scope.currentPage - Math.floor(scope.capPages / 2);
if (scope.numPages < scope.capPages) {
end = scope.numPages;
start = 1;
}
else if (start < 1) {
end = scope.capPages;
start = 1;
}
else {
end = start + scope.capPages > end ? end : scope.capPages + start - 1;
if (scope.numPages - start < scope.capPages && end - start > 1) {
start = end - scope.capPages + 1;
}
}
}
for (var i = start; i <= end; i++) {
scope.pages.push(i)
}
if (scope.currentPage > end) {
scope.selectPage(end);
}
};
scope.isActive = function (page) {
return scope.currentPage === page;
};
scope.noPrevious = function () {
return scope.currentPage === 1;
};
scope.noNext = function () {
return scope.currentPage === scope.numPages;
};
scope.selectPage = function (page) {
if (!scope.isActive(page)) {
scope.currentPage = page;
scope.onSelectPage({ page: page });
GTM.trackEvent('manage_paginate', {'page': page}, page);
// scope.onSelectPage({page:page});
}
if (scope.capPages) {
scope.updatePagesArray();
}
};
scope.selectNext = function () {
if (!scope.noNext()) {
scope.selectPage(scope.currentPage + 1);
}
};
scope.selectPrevious = function () {
if (!scope.noPrevious()) {
scope.selectPage(scope.currentPage - 1);
}
};
}
};
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment