From 8ced57e86f518cecea4fa5c6499ad33459823b77 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 16 Oct 2014 22:20:04 -0400 Subject: js: Extract pagination model. * js/utils.js (guix.clamp): New function. * js/controller/packages.js (guix.packages.controller): Remove 'pages', 'currentPageIndex', and 'pageSize' properties. Add 'pager' prop. Delete 'paginate', 'currentPage', 'isFirstPage', 'isLastPage', and 'isCurrentPage' methods. * js/model/packages.js (guix.packages.Pager): New function. * js/view/packages.js (guix.packages.view): Use new Pager API. --- js/model/packages.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'js/model') diff --git a/js/model/packages.js b/js/model/packages.js index 652d11a..1c61d25 100644 --- a/js/model/packages.js +++ b/js/model/packages.js @@ -15,9 +15,11 @@ // License along with this program. If not, see // . -guix.packages = {}; -(function(packages) { + +(function() { + var packages = guix.packages = {}; + packages.Packages = function() { return m.request({ method: "GET", @@ -45,9 +47,55 @@ guix.packages = {}; return Sorter; })(); + packages.Pager = (function() { + function Pager(items, pageSize) { + this.pageSize= pageSize; + this.pageIndex = 0; + this.pages = guix.chunk(items, pageSize); + } + + Pager.prototype.currentPage = function() { + return this.pages[this.pageIndex]; + }; + + Pager.prototype.pageCount = function() { + return this.pages.length; + }; + + Pager.prototype.isEmpty = function() { + return this.pageCount() === 0; + }; + + Pager.prototype.isFirstPage = function() { + return this.pageIndex === 0; + }; + + Pager.prototype.isLastPage = function() { + return this.pageIndex === this.pages.length - 1; + }; + + Pager.prototype.isCurrentPage = function(i) { + return this.pageIndex === i; + }; + + Pager.prototype.nextPage = function() { + this.pageIndex = Math.min(this.pageCount() - 1, this.pageIndex + 1); + }; + + Pager.prototype.previousPage = function() { + this.pageIndex = Math.max(0, this.pageIndex - 1); + }; + + Pager.prototype.gotoPage = function(page) { + this.pageIndex = guix.clamp(page, 0, this.pageCount() -1); + }; + + return Pager; + })(); + packages.PHASE_NONE = 0; packages.PHASE_PROMPT = 1; packages.PHASE_DERIVATION = 2; packages.PHASE_SUCCESS = 3; packages.PHASE_ERROR = 4; -})(guix.packages); +})(); -- cgit v1.2.3