Improvements around parameter parsing
Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/27034ef2 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/27034ef2 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/27034ef2 Branch: refs/heads/paginate-api-options Commit: 27034ef2343c5e0144322a681e31825412f9b712 Parents: b955b2b Author: Garren Smith <garren.sm...@gmail.com> Authored: Fri Feb 21 15:50:24 2014 +0200 Committer: Garren Smith <garren.sm...@gmail.com> Committed: Fri Feb 21 15:50:24 2014 +0200 ---------------------------------------------------------------------- src/fauxton/app/addons/documents/resources.js | 34 +++++++++------------- src/fauxton/app/addons/documents/routes.js | 23 +++++++++++++-- src/fauxton/app/addons/documents/views.js | 9 +++++- src/fauxton/app/addons/fauxton/components.js | 18 ++++++++---- 4 files changed, 54 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/27034ef2/src/fauxton/app/addons/documents/resources.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/documents/resources.js b/src/fauxton/app/addons/documents/resources.js index 69c9e1a..fd1a558 100644 --- a/src/fauxton/app/addons/documents/resources.js +++ b/src/fauxton/app/addons/documents/resources.js @@ -26,22 +26,21 @@ function(app, FauxtonAPI) { key; if (currentParams.keys) { - throw "Cannot paginate _all_docs with keys"; + throw "Cannot paginate when keys is specfied"; } if (_.isUndefined(doc)) { throw "Require docs to paginate"; } - var params = _.reduce(['reduce', 'keys', 'key', 'endkey', 'descending', 'inclusive_end'], function (params, key) { - if (_.has(currentParams, key)) { - params[key] = currentParams[key]; - } - return params; - }, defaultParams); + + // defaultParams should always override the user-specified parameters + _.extend(currentParams, defaultParams); lastId = doc.id || doc._id; + // If we are paginating on a view, we need to set a ``key`` and a ``docId`` + // and expect that they are different values. if (isView) { key = doc.key; docId = lastId; @@ -49,29 +48,24 @@ function(app, FauxtonAPI) { docId = key = lastId; } - if (isView && !currentParams.keys) { - params.startkey_docid = docId; - params.startkey = key; + // Set parameters to paginate + if (isView) { + currentParams.startkey_docid = docId; + currentParams.startkey = key; } else if (currentParams.startkey) { - params.startkey = key; + currentParams.startkey = key; } else { - params.startkey_docid = docId; + currentParams.startkey_docid = docId; } - _.each(['startkey', 'endkey', 'key'], function (key) { - if (_.has(params, key)) { - params[key] = JSON.stringify(params[key]); - } - }); - - return params; + return currentParams; }, next: function (docs, currentParams, perPage, _isAllDocs) { var params = {limit: perPage, skip: 1}, doc = _.last(docs); - return this.calculate(doc, params, currentParams, _isAllDocs); + return this.calculate(doc, params, currentParams, _isAllDocs); }, previous: function (docs, currentParams, perPage, _isAllDocs) { http://git-wip-us.apache.org/repos/asf/couchdb/blob/27034ef2/src/fauxton/app/addons/documents/routes.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/documents/routes.js b/src/fauxton/app/addons/documents/routes.js index d866241..16cc6d8 100644 --- a/src/fauxton/app/addons/documents/routes.js +++ b/src/fauxton/app/addons/documents/routes.js @@ -375,34 +375,51 @@ function(app, FauxtonAPI, Documents, Databases) { }, perPageChange: function (perPage) { - console.log('pp', perPage); this.perPage = perPage; - this.documentsView.collection.updateLimit(perPage); + this.documentsView.updatePerPage(perPage); this.documentsView.forceRender(); }, paginate: function (options) { var params = {}, urlParams = app.getParams(), + currentPage = options.currentPage, collection = this.documentsView.collection; this.documentsView.forceRender(); var rawCollection = collection.map(function (item) { return item.toJSON(); }); + collection.reverse = false; + + _.each(collection.params, function (val, key) { + collection.params[key] = JSON.parse(val); + }); + + _.each(urlParams, function (val, key) { + urlParams[key] = JSON.parse(val); + }); + if (options.direction === 'next') { - collection.reverse = false; params = Documents.paginate.next(rawCollection, collection.params, options.perPage, !!collection.isAllDocs); } else { + if (currentPage <= 1) { + params = _.clone(urlParams); + params.limit = collection.params.limit; + } else { collection.reverse = true; params = Documents.paginate.previous(rawCollection, collection.params, options.perPage, !!collection.isAllDocs); + } } params.limit = options.perPage; + _.each(params, function (val, key) { + params[key] = JSON.stringify(val); + }); collection.updateParams(params); }, http://git-wip-us.apache.org/repos/asf/couchdb/blob/27034ef2/src/fauxton/app/addons/documents/views.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/documents/views.js b/src/fauxton/app/addons/documents/views.js index f80d07e..f222b93 100644 --- a/src/fauxton/app/addons/documents/views.js +++ b/src/fauxton/app/addons/documents/views.js @@ -439,7 +439,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum serialize: function () { var totalRows = 0, updateSeq = false, - recordStart = 0, pageStart = 0, pageEnd = 20; @@ -738,6 +737,14 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum perPage: function () { return this.allDocsNumber.perPage(); + }, + + updatePerPage: function (newPerPage) { + if (this.collection.reverse) { + delete this.collection.params.descending + this.collection.reverse = false; + } + this.collection.updateLimit(newPerPage); } }); http://git-wip-us.apache.org/repos/asf/couchdb/blob/27034ef2/src/fauxton/app/addons/fauxton/components.js ---------------------------------------------------------------------- diff --git a/src/fauxton/app/addons/fauxton/components.js b/src/fauxton/app/addons/fauxton/components.js index 19211aa..3dd8f11 100644 --- a/src/fauxton/app/addons/fauxton/components.js +++ b/src/fauxton/app/addons/fauxton/components.js @@ -80,6 +80,7 @@ function(app, FauxtonAPI, ace, spin) { this._pageNumber = []; this._pageStart = 1; this.enabled = true; + this.currentPage = 1; }, canShowPreviousfn: function () { @@ -119,6 +120,7 @@ function(app, FauxtonAPI, ace, spin) { FauxtonAPI.triggerRouteEvent('paginate', { direction: 'previous', perPage: this.perPage, + currentPage: this.currentPage }); }, @@ -142,7 +144,8 @@ function(app, FauxtonAPI, ace, spin) { FauxtonAPI.triggerRouteEvent('paginate', { direction: 'next', - perPage: this.documentsLeftToFetch() + perPage: this.documentsLeftToFetch(), + currentPage: this.currentPage }); }, @@ -154,8 +157,10 @@ function(app, FauxtonAPI, ace, spin) { }; }, - updatePerPage: function (perPage) { - this.perPage = perPage; + updatePerPage: function (newPerPage) { + var docsView = this.page() + newPerPage; + this.currentPage = Math.ceil(docsView / newPerPage); + this.perPage = newPerPage; }, page: function () { @@ -163,6 +168,7 @@ function(app, FauxtonAPI, ace, spin) { }, incPageNumber: function () { + this.currentPage = this.currentPage + 1; this._pageNumber.push({perPage: this.perPage}); this._pageStart = this._pageStart + this.perPage; }, @@ -174,11 +180,11 @@ function(app, FauxtonAPI, ace, spin) { }, decPageNumber: function () { + this.currentPage = this.currentPage - 1; this._pageNumber.pop(); var val = this._pageStart - this.perPage; if (val < 1) { - this._pageStart = 1; - return; + val = 1; } this._pageStart = val; @@ -203,7 +209,7 @@ function(app, FauxtonAPI, ace, spin) { setCollection: function (collection) { this.collection = collection; this.setDefaults(); - } + }, });