ng2-pagination
Version:
Pagination for Angular
104 lines (103 loc) • 3.52 kB
JavaScript
"use strict";
var core_1 = require('@angular/core');
var PaginationService = (function () {
function PaginationService() {
this.change = new core_1.EventEmitter();
this.instances = {};
this.DEFAULT_ID = 'DEFAULT_PAGINATION_ID';
}
PaginationService.prototype.defaultId = function () { return this.DEFAULT_ID; };
PaginationService.prototype.register = function (instance) {
if (!instance.id) {
instance.id = this.DEFAULT_ID;
}
if (!this.instances[instance.id]) {
this.instances[instance.id] = instance;
this.change.emit(instance.id);
}
else {
var changed = this.updateInstance(instance);
if (changed) {
this.change.emit(instance.id);
}
}
};
/**
* Check each property of the instance and update any that have changed. Return
* true if any changes were made, else return false.
*/
PaginationService.prototype.updateInstance = function (instance) {
var changed = false;
for (var prop in this.instances[instance.id]) {
if (instance[prop] !== this.instances[instance.id][prop]) {
this.instances[instance.id][prop] = instance[prop];
changed = true;
}
}
return changed;
};
/**
* Returns the current page number.
*/
PaginationService.prototype.getCurrentPage = function (id) {
if (this.instances[id]) {
return this.instances[id].currentPage;
}
};
/**
* Sets the current page number.
*/
PaginationService.prototype.setCurrentPage = function (id, page) {
if (this.instances[id]) {
var instance = this.instances[id];
var maxPage = Math.ceil(instance.totalItems / instance.itemsPerPage);
if (page <= maxPage && 1 <= page) {
this.instances[id].currentPage = page;
this.change.emit(id);
}
}
};
/**
* Sets the value of instance.totalItems
*/
PaginationService.prototype.setTotalItems = function (id, totalItems) {
if (this.instances[id] && 0 <= totalItems) {
this.instances[id].totalItems = totalItems;
this.change.emit(id);
}
};
/**
* Sets the value of instance.itemsPerPage.
*/
PaginationService.prototype.setItemsPerPage = function (id, itemsPerPage) {
if (this.instances[id]) {
this.instances[id].itemsPerPage = itemsPerPage;
this.change.emit(id);
}
};
/**
* Returns a clone of the pagination instance object matching the id. If no
* id specified, returns the instance corresponding to the default id.
*/
PaginationService.prototype.getInstance = function (id) {
if (id === void 0) { id = this.DEFAULT_ID; }
if (this.instances[id]) {
return this.clone(this.instances[id]);
}
return {};
};
/**
* Perform a shallow clone of an object.
*/
PaginationService.prototype.clone = function (obj) {
var target = {};
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
target[i] = obj[i];
}
}
return target;
};
return PaginationService;
}());
exports.PaginationService = PaginationService;