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/controller/packages.js | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) (limited to 'js/controller') diff --git a/js/controller/packages.js b/js/controller/packages.js index db94d72..89f01e9 100644 --- a/js/controller/packages.js +++ b/js/controller/packages.js @@ -15,14 +15,19 @@ // License along with this program. If not, see // . -(function(packages) { +(function() { + var packages = guix.packages; + packages.controller = (function() { + var PAGE_SIZE = 20; + + function Pager(items) { + return new packages.Pager(items, PAGE_SIZE); + } + function controller() { var self = this; - this.pages = m.prop([]); - this.currentPageIndex = 0; - this.pageSize = 20; this.searchTerm = m.prop(""); this.columns = [ { header: "Name", sortField: "name" }, @@ -43,40 +48,20 @@ } ]; this.sorter = m.prop(new packages.Sorter("name")); + this.pager = m.prop(Pager([])); this.phase = m.prop(packages.PHASE_NONE); this.selectedPackage = m.prop(null); this.packages = packages.Packages() .then(function(packages) { // All packages are visible initially - self.pages(self.paginate(packages, self.pageSize)); + self.pager(Pager(packages)); }) .then(m.redraw); }; - - controller.prototype.paginate = function(array, pageSize) { - return guix.chunk(this.sorter().sort(array), pageSize); - }; - - controller.prototype.currentPage = function() { - return this.pages()[this.currentPageIndex] || []; - }; - - controller.prototype.isFirstPage = function() { - return this.currentPageIndex === 0; - }; - - controller.prototype.isLastPage = function() { - return this.currentPageIndex === this.pages().length - 1; - }; - - controller.prototype.isCurrentPage = function(i) { - return this.currentPageIndex === i; - }; - controller.prototype.packageCount = function() { - return this.pages().reduce(function(memo, page) { + return this.pager().pages.reduce(function(memo, page) { return memo + page.length; }, 0); }; @@ -84,12 +69,10 @@ controller.prototype.doSearch = function() { var regexp = new RegExp(this.searchTerm(), "i"); - this.pages(this.paginate(this.packages().filter(function(package) { + this.pager(Pager(this.packages().filter(function(package) { return regexp.test(package.name) || regexp.test(package.synopsis); - }), this.pageSize)); - // Reset pagination - this.currentPageIndex = 0; + }))); }; controller.prototype.sortBy = function(field) { @@ -122,4 +105,4 @@ return controller; })(); -})(guix.packages); +})(); -- cgit v1.2.3