2016-08-05 14:37:58 +02:00
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('harbor.paginator')
|
|
|
|
.directive('paginator', paginator);
|
|
|
|
|
|
|
|
PaginatorController.$inject = [];
|
|
|
|
|
|
|
|
function PaginatorController() {
|
|
|
|
var vm = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator.$inject = [];
|
|
|
|
|
|
|
|
function paginator() {
|
|
|
|
var directive = {
|
|
|
|
'restrict': 'E',
|
|
|
|
'templateUrl': '/static/resources/js/components/paginator/paginator.directive.html',
|
|
|
|
'scope': {
|
|
|
|
'totalCount': '@',
|
|
|
|
'pageSize': '@',
|
|
|
|
'page': '=',
|
2016-08-06 09:11:20 +02:00
|
|
|
'displayCount': '@'
|
2016-08-05 14:37:58 +02:00
|
|
|
},
|
|
|
|
'link': link,
|
|
|
|
'controller': PaginatorController,
|
|
|
|
'controllerAs': 'vm',
|
|
|
|
'bindToController': true
|
|
|
|
};
|
|
|
|
return directive;
|
|
|
|
|
|
|
|
function link(scope, element, attrs, ctrl) {
|
2016-08-06 10:12:48 +02:00
|
|
|
|
2016-08-08 11:29:42 +02:00
|
|
|
scope.$watch('vm.page', function(current) {
|
|
|
|
if(current) {
|
|
|
|
ctrl.page = current;
|
|
|
|
togglePageButton();
|
|
|
|
}
|
|
|
|
});
|
2016-08-06 11:19:29 +02:00
|
|
|
|
2016-08-08 11:29:42 +02:00
|
|
|
var tc;
|
|
|
|
|
2016-08-05 14:37:58 +02:00
|
|
|
scope.$watch('vm.totalCount', function(current) {
|
|
|
|
if(current) {
|
2016-08-06 11:19:29 +02:00
|
|
|
var totalCount = current;
|
2016-08-08 11:29:42 +02:00
|
|
|
|
2016-08-06 11:19:29 +02:00
|
|
|
element.find('ul li:first a').off('click');
|
|
|
|
element.find('ul li:last a').off('click');
|
2016-08-08 11:29:42 +02:00
|
|
|
|
2016-08-06 11:19:29 +02:00
|
|
|
tc = new TimeCounter();
|
2016-08-05 14:37:58 +02:00
|
|
|
|
2016-08-06 11:19:29 +02:00
|
|
|
console.log('Total Count:' + totalCount + ', Page Size:' + ctrl.pageSize + ', Display Count:' + ctrl.displayCount + ', Page:' + ctrl.page);
|
2016-08-06 10:12:48 +02:00
|
|
|
|
2016-08-06 11:19:29 +02:00
|
|
|
ctrl.buttonCount = Math.ceil(totalCount / ctrl.pageSize);
|
2016-08-06 10:12:48 +02:00
|
|
|
|
2016-08-06 11:19:29 +02:00
|
|
|
if(ctrl.buttonCount <= ctrl.displayCount) {
|
2016-08-08 11:29:42 +02:00
|
|
|
tc.setMaximum(1);
|
2016-08-05 14:37:58 +02:00
|
|
|
}else{
|
2016-08-08 11:29:42 +02:00
|
|
|
tc.setMaximum(Math.ceil(ctrl.buttonCount / ctrl.displayCount));
|
2016-08-05 14:37:58 +02:00
|
|
|
}
|
2016-08-06 11:19:29 +02:00
|
|
|
|
2016-08-06 09:11:20 +02:00
|
|
|
element.find('ul li:first a').on('click', previous);
|
2016-08-06 11:19:29 +02:00
|
|
|
element.find('ul li:last a').on('click', next);
|
2016-08-05 14:37:58 +02:00
|
|
|
|
|
|
|
drawButtons(tc.getTime());
|
2016-08-06 09:11:20 +02:00
|
|
|
|
|
|
|
togglePrevious(tc.canDecrement());
|
|
|
|
toggleNext(tc.canIncrement());
|
2016-08-05 14:37:58 +02:00
|
|
|
|
|
|
|
togglePageButton();
|
|
|
|
|
2016-08-06 10:12:48 +02:00
|
|
|
}
|
|
|
|
});
|
2016-08-06 11:19:29 +02:00
|
|
|
|
2016-08-08 11:29:42 +02:00
|
|
|
var TimeCounter = function() {
|
2016-08-06 10:12:48 +02:00
|
|
|
this.time = 0;
|
|
|
|
this.minimum = 0;
|
|
|
|
this.maximum = 0;
|
2016-08-11 08:02:53 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.setMaximum = function(maximum) {
|
|
|
|
this.maximum = maximum;
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.increment = function() {
|
|
|
|
if(this.time < this.maximum) {
|
|
|
|
++this.time;
|
2016-08-06 11:19:29 +02:00
|
|
|
if((ctrl.page % ctrl.displayCount) != 0) {
|
|
|
|
ctrl.page = this.time * ctrl.displayCount;
|
|
|
|
}
|
2016-08-06 10:12:48 +02:00
|
|
|
++ctrl.page;
|
|
|
|
}
|
|
|
|
scope.$apply();
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.canIncrement = function() {
|
2016-08-08 11:29:42 +02:00
|
|
|
if(this.time + 1 < this.maximum) {
|
2016-08-06 10:12:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.decrement = function() {
|
2016-08-08 11:29:42 +02:00
|
|
|
if(this.time > this.minimum) {
|
2016-08-06 11:19:29 +02:00
|
|
|
if(this.time === 0) {
|
2016-08-08 11:29:42 +02:00
|
|
|
ctrl.page = ctrl.displayCount;
|
2016-08-06 11:19:29 +02:00
|
|
|
}else if((ctrl.page % ctrl.displayCount) != 0) {
|
|
|
|
ctrl.page = this.time * ctrl.displayCount;
|
|
|
|
}
|
2016-08-08 11:29:42 +02:00
|
|
|
--this.time;
|
|
|
|
--ctrl.page;
|
2016-08-06 10:12:48 +02:00
|
|
|
}
|
|
|
|
scope.$apply();
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.canDecrement = function() {
|
|
|
|
if(this.time > this.minimum) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
2016-08-06 10:12:48 +02:00
|
|
|
|
|
|
|
TimeCounter.prototype.getTime = function() {
|
|
|
|
return this.time;
|
2016-08-06 11:19:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function drawButtons(time) {
|
2016-08-06 10:12:48 +02:00
|
|
|
element.find('li[tag="pagination-button"]').remove();
|
|
|
|
var buttons = [];
|
|
|
|
for(var i = 1; i <= ctrl.displayCount; i++) {
|
|
|
|
var displayNumber = ctrl.displayCount * time + i;
|
|
|
|
if(displayNumber <= ctrl.buttonCount) {
|
|
|
|
buttons.push('<li tag="pagination-button"><a href="javascript:void(0)" page="' + displayNumber + '">' + displayNumber + '<span class="sr-only"></span></a></li>');
|
2016-08-05 14:37:58 +02:00
|
|
|
}
|
2016-08-06 10:12:48 +02:00
|
|
|
}
|
|
|
|
$(buttons.join(''))
|
|
|
|
.insertAfter(element.find('ul li:eq(0)')).end()
|
|
|
|
.on('click', buttonClickHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
function togglePrevious(status) {
|
|
|
|
if(status){
|
|
|
|
element.find('ul li:first').removeClass('disabled');
|
|
|
|
}else{
|
|
|
|
element.find('ul li:first').addClass('disabled');
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 14:37:58 +02:00
|
|
|
|
2016-08-06 10:12:48 +02:00
|
|
|
function toggleNext(status) {
|
|
|
|
if(status) {
|
|
|
|
element.find('ul li:last').removeClass('disabled');
|
|
|
|
}else{
|
|
|
|
element.find('ul li:last').addClass('disabled');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buttonClickHandler(e) {
|
|
|
|
ctrl.page = $(e.target).attr('page');
|
|
|
|
togglePageButton();
|
|
|
|
togglePrevious(tc.canDecrement());
|
|
|
|
toggleNext(tc.canIncrement());
|
|
|
|
scope.$apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
function togglePageButton() {
|
|
|
|
element.find('li[tag="pagination-button"]').removeClass('active');
|
|
|
|
element.find('li[tag="pagination-button"] a[page="' + ctrl.page + '"]').parent().addClass('active');
|
|
|
|
}
|
2016-08-05 14:37:58 +02:00
|
|
|
|
2016-08-06 10:12:48 +02:00
|
|
|
function previous() {
|
|
|
|
if(tc.canDecrement()) {
|
|
|
|
tc.decrement();
|
|
|
|
drawButtons(tc.getTime());
|
2016-08-06 11:35:57 +02:00
|
|
|
togglePageButton();
|
2016-08-08 11:29:42 +02:00
|
|
|
togglePrevious(tc.canDecrement());
|
|
|
|
toggleNext(tc.canIncrement());
|
2016-08-05 14:37:58 +02:00
|
|
|
}
|
2016-08-06 10:12:48 +02:00
|
|
|
scope.$apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
function next() {
|
|
|
|
if(tc.canIncrement()) {
|
|
|
|
tc.increment();
|
|
|
|
drawButtons(tc.getTime());
|
2016-08-06 11:35:57 +02:00
|
|
|
togglePageButton();
|
2016-08-08 11:29:42 +02:00
|
|
|
togglePrevious(tc.canDecrement());
|
|
|
|
toggleNext(tc.canIncrement());
|
2016-08-06 10:12:48 +02:00
|
|
|
}
|
|
|
|
scope.$apply();
|
|
|
|
}
|
2016-08-05 14:37:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})();
|