angular.module('bw.paging', []).directive('paging', function () {
return {
restrict: 'EA',
link: fieldLink,
scope: {
page: '=',
pageSize: '=',
total: '=',
dots: '@',
hideIfEmpty: '@',
ulClass: '@',
activeClass: '@',
disabledClass: '@',
adjacent: '@',
scrollTop: '@',
showPrevNext: '@',
pagingAction: '&'
},
template:
'
'
};
function fieldLink (scope, el, attrs) {
scope.$watchCollection('[page,pageSize,total]', function () {
build(scope, attrs);
});
}
function setScopeValues(scope, attrs) {
scope.List = [];
scope.Hide = false;
scope.dots = scope.dots || '...';
scope.page = parseInt(scope.page) || 1;
scope.total = parseInt(scope.total) || 0;
scope.ulClass = scope.ulClass || 'pagination';
scope.adjacent = parseInt(scope.adjacent) || 2;
scope.activeClass = scope.activeClass || 'active';
scope.disabledClass = scope.disabledClass || 'disabled';
scope.scrollTop = scope.$eval(attrs.scrollTop);
scope.hideIfEmpty = scope.$eval(attrs.hideIfEmpty);
scope.showPrevNext = scope.$eval(attrs.showPrevNext);
}
function validateScopeValues(scope, pageCount) {
if (scope.page > pageCount) {
scope.page = pageCount;
}
if (scope.page <= 0) { scope.page=1; } if (scope.adjacent <=0) { scope.adjacent=2; } if (pageCount <=1) {
scope.Hide=scope.hideIfEmpty; } } function internalAction(scope, page) { if (scope.page==page) { return; }
scope.page=page; scope.pagingAction({ page: scope.page, pageSize: scope.pageSize, total: scope.total }); if
(scope.scrollTop) { scrollTo(0, 0); } } function addPrevNext(scope, pageCount, mode){ if (!scope.showPrevNext ||
pageCount < 1) { return; } var disabled, alpha, beta; if(mode==='prev' ) { disabled=scope.page - 1 <=0; var
prevPage=scope.page - 1 <=0 ? 1 : scope.page - 1; alpha={ value : "<<" , title: 'Boshiga o`tish' , page:
1 }; beta={ value: "<" , title: 'Oldingisiga o`tish' , page: prevPage }; } else { disabled=scope.page +
1> pageCount;
var nextPage = scope.page + 1 >= pageCount ? pageCount : scope.page + 1;
alpha = { value : ">", title: 'Keyingisiga o`tish', page: nextPage };
beta = { value: ">>", title: 'Ohiriga o`tish', page: pageCount };
}
var addItem = function(item, disabled){
scope.List.push({
value: item.value,
title: item.title,
liClass: disabled ? scope.disabledClass : '',
action: function(){
if(!disabled) {
internalAction(scope, item.page);
}
}
});
};
addItem(alpha, disabled);
addItem(beta, disabled);
}
function addRange(start, finish, scope) {
var i = 0;
for (i = start; i <= finish; i++) { var item={ value: i, title: 'Saxifa raqami - ' + i, liClass:
scope.page==i ? scope.activeClass : '' , action: function () { internalAction(scope, this.value); } };
scope.List.push(item); } } function addDots(scope) { scope.List.push({ value: scope.dots }); } function
addFirst(scope, next) { addRange(1, 2, scope); if(next !=3){ addDots(scope); } } function addLast(pageCount,
scope, prev) { if(prev !=pageCount - 2){ addDots(scope); } addRange(pageCount - 1, pageCount, scope); } function
build(scope, attrs) { if (!scope.pageSize || scope.pageSize <=0) { scope.pageSize=1; } var
pageCount=Math.ceil(scope.total / scope.pageSize); setScopeValues(scope, attrs); validateScopeValues(scope,
pageCount); var start, finish; var fullAdjacentSize=(scope.adjacent * 2) + 2; addPrevNext(scope,
pageCount, 'prev' ); if (pageCount <=(fullAdjacentSize + 2)) { start=1; addRange(start, pageCount, scope); }
else { if (scope.page - scope.adjacent <=2) { start=1; finish=1 + fullAdjacentSize; addRange(start, finish,
scope); addLast(pageCount, scope, finish); } else if (scope.page < pageCount - (scope.adjacent + 2)) {
start=scope.page - scope.adjacent; finish=scope.page + scope.adjacent; addFirst(scope, start); addRange(start,
finish, scope); addLast(pageCount, scope, finish); } else { start=pageCount - fullAdjacentSize;
finish=pageCount; addFirst(scope, start); addRange(start, finish, scope); } } addPrevNext(scope,
pageCount, 'next' ); } });