diff options
author | David Thompson <dthompson2@worcester.edu> | 2014-10-16 22:20:04 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2014-10-16 22:20:04 -0400 |
commit | 8ced57e86f518cecea4fa5c6499ad33459823b77 (patch) | |
tree | 333082b73c768c6e1854f613070dc32649bcc8b3 /js/controller | |
parent | 93921cef3d2e76b53789140fad7153c025a7c340 (diff) |
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.
Diffstat (limited to 'js/controller')
-rw-r--r-- | js/controller/packages.js | 47 |
1 files changed, 15 insertions, 32 deletions
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 // <http://www.gnu.org/licenses/>. -(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); +})(); |