You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ga...@apache.org on 2014/02/21 16:54:35 UTC

[1/3] couchdb commit: updated refs/heads/paginate-api-options to 5ab96a4

Repository: couchdb
Updated Branches:
  refs/heads/paginate-api-options a9bef616d -> 5ab96a460


Continued work


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/b955b2bd
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/b955b2bd
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/b955b2bd

Branch: refs/heads/paginate-api-options
Commit: b955b2bd319c989522a7f1b6efe30a5f28a3c756
Parents: a9bef61
Author: Garren Smith <ga...@gmail.com>
Authored: Fri Feb 21 06:06:03 2014 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Fri Feb 21 06:06:17 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/resources.js | 2 +-
 src/fauxton/app/addons/documents/views.js     | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/b955b2bd/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 c5a2e2c..69c9e1a 100644
--- a/src/fauxton/app/addons/documents/resources.js
+++ b/src/fauxton/app/addons/documents/resources.js
@@ -33,7 +33,7 @@ function(app, FauxtonAPI) {
         throw "Require docs to paginate";
       }
 
-      var params = _.reduce(['reduce', 'keys', 'endkey', 'descending', 'inclusive_end'], function (params, key) {
+      var params = _.reduce(['reduce', 'keys', 'key', 'endkey', 'descending', 'inclusive_end'], function (params, key) {
         if (_.has(currentParams, key)) {
           params[key] = currentParams[key]; 
         }

http://git-wip-us.apache.org/repos/asf/couchdb/blob/b955b2bd/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 5468948..f80d07e 100644
--- a/src/fauxton/app/addons/documents/views.js
+++ b/src/fauxton/app/addons/documents/views.js
@@ -457,7 +457,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
         database: app.utils.safeURLName(this.collection.database.id),
         updateSeq: updateSeq,
         totalRows: totalRows,
-        numModels: this.collection.models.length + recordStart - 1,
         pageStart: pageStart,
         pageEnd: pageEnd
       };


[2/3] couchdb commit: updated refs/heads/paginate-api-options to 5ab96a4

Posted by ga...@apache.org.
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 <ga...@gmail.com>
Authored: Fri Feb 21 15:50:24 2014 +0200
Committer: Garren Smith <ga...@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();
-    }
+    },
 
   });
 


[3/3] couchdb commit: updated refs/heads/paginate-api-options to 5ab96a4

Posted by ga...@apache.org.
More pagination goodness


Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/5ab96a46
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/5ab96a46
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/5ab96a46

Branch: refs/heads/paginate-api-options
Commit: 5ab96a460b0e0fcdb582eb9ea19107b09a8458c9
Parents: 27034ef
Author: Garren Smith <ga...@gmail.com>
Authored: Fri Feb 21 17:53:12 2014 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Fri Feb 21 17:53:12 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/resources.js | 29 +++++++++-------------
 src/fauxton/app/addons/documents/routes.js    | 20 ++++++++-------
 src/fauxton/app/addons/documents/views.js     |  4 ---
 3 files changed, 23 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/5ab96a46/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 fd1a558..8bc52b0 100644
--- a/src/fauxton/app/addons/documents/resources.js
+++ b/src/fauxton/app/addons/documents/resources.js
@@ -19,6 +19,7 @@ function(app, FauxtonAPI) {
   var Documents = FauxtonAPI.addon();
 
   Documents.paginate = {
+    history: [],
     calculate: function (doc, defaultParams, currentParams, _isAllDocs) {
       var docId = '',
           lastId = '',
@@ -65,14 +66,20 @@ function(app, FauxtonAPI) {
       var params = {limit: perPage, skip: 1},
           doc = _.last(docs);
           
-            return this.calculate(doc, params, currentParams, _isAllDocs);
+      this.history.push(_.clone(currentParams));
+      return this.calculate(doc, params, currentParams, _isAllDocs);
     },
 
     previous: function (docs, currentParams, perPage, _isAllDocs) {
-      var params = {descending: true, limit: perPage, skip: 1},
+      var params = this.history.pop(),
           doc = _.first(docs);
 
-      return this.calculate(doc, params, currentParams, _isAllDocs);
+      params.limit = perPage;
+      return params;
+    },
+
+    reset: function () {
+      this.history = [];
     }
   };
 
@@ -421,7 +428,7 @@ function(app, FauxtonAPI) {
       if (this.skipFirstItem) {
         rows = rows.splice(1);
       }
-      var mappedRows = _.map(rows, function(row) {
+      return _.map(rows, function(row) {
         return {
           _id: row.id,
           _rev: row.value.rev,
@@ -430,12 +437,6 @@ function(app, FauxtonAPI) {
           doc: row.doc || undefined
         };
       });
-
-      if (this.reverse) {
-        return _(mappedRows).reverse().value();
-      }
-
-      return mappedRows;
     }
   });
 
@@ -539,7 +540,7 @@ function(app, FauxtonAPI) {
         offset: resp.offset,
         update_seq: resp.update_seq
       };
-      var mappedRows =  _.map(rows, function(row) {
+      return _.map(rows, function(row) {
         return {
           value: row.value,
           key: row.key,
@@ -547,12 +548,6 @@ function(app, FauxtonAPI) {
           id: row.id
         };
       });
-
-      if (this.reverse) {
-        return _(mappedRows).reverse().value();
-      }
-
-      return mappedRows;
     },
 
     buildAllDocs: function(){

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5ab96a46/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 16cc6d8..45b2825 100644
--- a/src/fauxton/app/addons/documents/routes.js
+++ b/src/fauxton/app/addons/documents/routes.js
@@ -243,6 +243,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       ];
 
       this.apiUrl = [this.data.database.allDocs.url("apiurl", urlParams), this.data.database.allDocs.documentation() ];
+      Documents.paginate.reset();
     },
 
     viewFn: function (databaseName, ddoc, view) {
@@ -298,6 +299,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       };
 
       this.apiUrl = [this.data.indexedDocs.url("apiurl", urlParams), "docs"];
+      Documents.paginate.reset();
     },
 
     newViewEditor: function () {
@@ -352,6 +354,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       this.documentsView.setParams(docParams, urlParams);
 
       this.apiUrl = [collection.url("apiurl", urlParams), "docs"];
+      Documents.paginate.reset();
     },
 
     updateAllDocsFromPreview: function (event) {
@@ -375,8 +378,11 @@ function(app, FauxtonAPI, Documents, Databases) {
     },
 
     perPageChange: function (perPage) {
+      var params = app.getParams();
       this.perPage = perPage;
       this.documentsView.updatePerPage(perPage);
+      params.limit = perPage;
+      this.documentsView.collection.params = params;
       this.documentsView.forceRender();
     },
 
@@ -405,21 +411,17 @@ function(app, FauxtonAPI, Documents, Databases) {
                                            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);
-      });
+      _.each(['startkey', 'endkey', 'key'], function (key) {
+        if (_.has(params, key)) {
+          params[key] = JSON.stringify(params[key]);
+        }
+    });
       collection.updateParams(params);
     },
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/5ab96a46/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 f222b93..1d07dae 100644
--- a/src/fauxton/app/addons/documents/views.js
+++ b/src/fauxton/app/addons/documents/views.js
@@ -740,10 +740,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
     },
 
     updatePerPage: function (newPerPage) {
-      if (this.collection.reverse) {
-        delete this.collection.params.descending
-        this.collection.reverse = false;
-      }
       this.collection.updateLimit(newPerPage);
     }
   });