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/03/03 17:58:46 UTC

[34/34] couchdb commit: updated refs/heads/paginate-api-options to b63c791

Code improvements to pagination


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

Branch: refs/heads/paginate-api-options
Commit: b63c79130e15c08810b1288495e0c0ba89358c3d
Parents: 7df61e7
Author: Garren Smith <ga...@gmail.com>
Authored: Mon Mar 3 18:28:09 2014 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Mon Mar 3 18:28:09 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/resources.js |  9 ++-
 src/fauxton/app/addons/documents/routes.js    | 68 +++++++++++++++-------
 src/fauxton/app/addons/documents/views.js     |  7 ++-
 src/fauxton/app/addons/fauxton/components.js  |  6 +-
 4 files changed, 57 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/b63c7913/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 8bc52b0..73d9a0a 100644
--- a/src/fauxton/app/addons/documents/resources.js
+++ b/src/fauxton/app/addons/documents/resources.js
@@ -34,7 +34,6 @@ function(app, FauxtonAPI) {
         throw "Require docs to paginate";
       }
 
-
       // defaultParams should always override the user-specified parameters
       _.extend(currentParams, defaultParams);
 
@@ -51,12 +50,12 @@ function(app, FauxtonAPI) {
 
       // Set parameters to paginate
       if (isView) {
-        currentParams.startkey_docid = docId; 
+        currentParams.startkey_docid = docId;
         currentParams.startkey = key;
       } else if (currentParams.startkey) {
         currentParams.startkey = key;
       } else {
-        currentParams.startkey_docid = docId; 
+        currentParams.startkey_docid = docId;
       }
 
       return currentParams;
@@ -349,7 +348,7 @@ function(app, FauxtonAPI) {
       this.on("remove",this.decrementTotalRows , this);
       this.perPageLimit = options.perPageLimit || 20;
 
-      if (this.params.limit > this.perPageLimit) {
+      if (!this.params.limit) {
         this.params.limit = this.perPageLimit; 
       }
     },
@@ -454,7 +453,7 @@ function(app, FauxtonAPI) {
       this.skipFirstItem = false;
       this.perPageLimit = options.perPageLimit || 20;
 
-      if (this.params.limit > this.perPageLimit) {
+      if (!this.params.limit) {
         this.params.limit = this.perPageLimit; 
       }
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/b63c7913/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 168807a..21e76e0 100644
--- a/src/fauxton/app/addons/documents/routes.js
+++ b/src/fauxton/app/addons/documents/routes.js
@@ -160,9 +160,6 @@ function(app, FauxtonAPI, Documents, Databases) {
     },
 
     initialize: function (route, masterLayout, options) {
-      var docParams = app.getParams();
-      docParams.include_docs = true;
-
       this.databaseName = options[0];
 
       this.data = {
@@ -171,9 +168,11 @@ function(app, FauxtonAPI, Documents, Databases) {
 
       this.data.designDocs = new Documents.AllDocs(null, {
         database: this.data.database,
-        params: {startkey: '"_design"',
+        params: {
+          startkey: '"_design"',
           endkey: '"_design1"',
-          include_docs: true}
+          include_docs: true
+        }
       });
 
       this.sidebar = this.setView("#sidebar-content", new Documents.Views.Sidebar({
@@ -190,7 +189,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       var urlParams = app.getParams(options);
       return {
         urlParams: urlParams,
-        docParams: _.extend(_.clone(urlParams), {limit: 20})
+        docParams: _.extend(_.clone(urlParams), {limit: this.getDocPerPageLimit(urlParams, 20)})
       };
     },
 
@@ -209,7 +208,6 @@ function(app, FauxtonAPI, Documents, Databases) {
         return;
       }
 
-      docParams.limit = this.getDocPerPageLimit(urlParams, 20);
       this.data.database.buildAllDocs(docParams);
 
       if (docParams.startkey && docParams.startkey.indexOf('_design') > -1) {
@@ -243,6 +241,7 @@ function(app, FauxtonAPI, Documents, Databases) {
       ];
 
       this.apiUrl = [this.data.database.allDocs.url("apiurl", urlParams), this.data.database.allDocs.documentation() ];
+      //reset the pagination history - the history is used for pagination.previous
       Documents.paginate.reset();
     },
 
@@ -252,7 +251,6 @@ function(app, FauxtonAPI, Documents, Databases) {
           docParams = params.docParams;
           decodeDdoc = decodeURIComponent(ddoc);
 
-      docParams.limit = this.getDocPerPageLimit(urlParams, 20);
       view = view.replace(/\?.*$/,'');
 
       this.data.indexedDocs = new Documents.IndexCollection(null, {
@@ -321,6 +319,8 @@ function(app, FauxtonAPI, Documents, Databases) {
           {"name": this.data.database.id, "link": Databases.databaseUrl(this.data.database)},
         ];
       };
+
+      Documents.paginate.reset();
     },
 
     updateAllDocsFromView: function (event) {
@@ -349,7 +349,6 @@ function(app, FauxtonAPI, Documents, Databases) {
 
       }
 
-
       this.documentsView.setCollection(collection);
       this.documentsView.setParams(docParams, urlParams);
 
@@ -381,30 +380,33 @@ function(app, FauxtonAPI, Documents, Databases) {
       var params = app.getParams();
       this.perPage = perPage;
       this.documentsView.updatePerPage(perPage);
+      this.documentsView.forceRender();
       params.limit = perPage;
       this.documentsView.collection.params = params;
-      this.documentsView.forceRender();
+      this.setDocPerPageLimit(perPage);
     },
 
     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);
-      });
+      // this is really ugly. But we basically need to make sure that
+      // all parameters are in the correct state and have been parsed before we
+      // calculate how to paginate the collection
+      _.each(['startkey', 'endkey', 'key'], function (key) {
+        if (_.has(collection.params, key)) {
+          collection.params[key] = JSON.parse(collection.params[key]);
+        }
 
-      _.each(urlParams, function (val, key) {
-        urlParams[key] = JSON.parse(val);
+        if (_.has(urlParams, key)) {
+          urlParams[key] = JSON.parse(urlParams[key]);
+        }
       });
 
-
       if (options.direction === 'next') {
           params = Documents.paginate.next(rawCollection, 
                                            collection.params,
@@ -416,12 +418,19 @@ function(app, FauxtonAPI, Documents, Databases) {
                                                options.perPage, 
                                                !!collection.isAllDocs);
       }
+
+      // use the perPage sent from IndexPagination as it calculates how many
+      // docs to fetch for next page
       params.limit = options.perPage;
+
+      // again not pretty but need to make sure all the parameters can be correctly
+      // built into a query
       _.each(['startkey', 'endkey', 'key'], function (key) {
         if (_.has(params, key)) {
           params[key] = JSON.stringify(params[key]);
         }
-    });
+      });
+
       collection.updateParams(params);
     },
 
@@ -433,10 +442,27 @@ function(app, FauxtonAPI, Documents, Databases) {
       }
     },
 
+    setDocPerPageLimit: function (perPage) {
+      window.localStorage.setItem('fauxton:perpage', perPage);
+    },
+
 
     getDocPerPageLimit: function (urlParams, perPage) {
-      if (!urlParams.limit || urlParams.limit > perPage) {
-        return perPage;
+      var storedPerPage = perPage;
+
+      if (window.localStorage) {
+        storedPerPage = window.localStorage.getItem('fauxton:perpage');
+
+        if (!storedPerPage) {
+          this.setDocPerPageLimit(perPage);
+          storedPerPage = perPage;
+        } else {
+          storedPerPage = parseInt(storedPerPage, 10);
+        }
+      } 
+
+      if (!urlParams.limit || urlParams.limit > storedPerPage) {
+        return storedPerPage;
       } else {
         return urlParams.limit;
       }

http://git-wip-us.apache.org/repos/asf/couchdb/blob/b63c7913/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 1d07dae..f828d71 100644
--- a/src/fauxton/app/addons/documents/views.js
+++ b/src/fauxton/app/addons/documents/views.js
@@ -418,7 +418,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
       _.bindAll(this);
       
       this._perPage = options.perPageDefault || 20;
-
       this.listenTo(this.collection, 'totalRows:decrement', this.render);
     },
 
@@ -579,7 +578,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
       this.docParams = options.docParams;
       this.params = options.params || {};
       this.expandDocs = true;
-      this.perPageDefault = options.perPageDefault || 20;
+      this.perPageDefault = this.docParams.limit || 20;
     },
 
     establish: function() {
@@ -676,7 +675,8 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
       this.pagination = new Components.IndexPagination({
         collection: this.collection,
         scrollToSelector: '#dashboard-content',
-        docLimit: this.params.limit
+        docLimit: this.params.limit,
+        perPage: this.perPageDefault
       });
     },
 
@@ -725,6 +725,7 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
     setParams: function (docParams, urlParams) {
       this.docParams = docParams;
       this.params = urlParams;
+      this.perPageDefault = this.docParams.limit;
 
       if (this.params.limit) {
         this.pagination.docLimit = this.params.limit;

http://git-wip-us.apache.org/repos/asf/couchdb/blob/b63c7913/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 1174193..7dcf2d7 100644
--- a/src/fauxton/app/addons/fauxton/components.js
+++ b/src/fauxton/app/addons/fauxton/components.js
@@ -72,7 +72,7 @@ function(app, FauxtonAPI, ace, spin) {
       this.scrollToSelector = options.scrollToSelector;
       _.bindAll(this);
       this.docLimit = options.docLimit || 1000000;
-      this.perPage = 20;
+      this.perPage = options.perPage || 20;
       this.setDefaults();
     },
 
@@ -84,9 +84,7 @@ function(app, FauxtonAPI, ace, spin) {
     },
 
     canShowPreviousfn: function () {
-      if (!this.enabled) { return this.enabled; }
-
-      if (this._pageStart === 1) {
+      if (this._pageStart === 1 || !this.enabled) {
         return false;
       }
       return true;