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/model | |
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/model')
-rw-r--r-- | js/model/packages.js | 54 |
1 files changed, 51 insertions, 3 deletions
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 // <http://www.gnu.org/licenses/>. -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); +})(); |