couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gar...@apache.org
Subject [25/34] couchdb commit: updated refs/heads/paginate-api-options to b63c791
Date Mon, 03 Mar 2014 16:58:37 GMT
More pagination fixes


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

Branch: refs/heads/paginate-api-options
Commit: 77c06c28878015613c30f8c042aeaabdbb3423c8
Parents: 9848c76
Author: Garren Smith <garren.smith@gmail.com>
Authored: Thu Feb 20 18:02:45 2014 +0200
Committer: Garren Smith <garren.smith@gmail.com>
Committed: Mon Mar 3 11:18:42 2014 +0200

----------------------------------------------------------------------
 src/fauxton/app/addons/documents/resources.js | 163 +++++++--------------
 src/fauxton/app/addons/documents/routes.js    |  26 +++-
 src/fauxton/app/addons/documents/views.js     |   3 -
 src/fauxton/app/addons/fauxton/components.js  |   4 -
 4 files changed, 70 insertions(+), 126 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/77c06c28/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 d942cf6..c5a2e2c 100644
--- a/src/fauxton/app/addons/documents/resources.js
+++ b/src/fauxton/app/addons/documents/resources.js
@@ -19,10 +19,8 @@ function(app, FauxtonAPI) {
   var Documents = FauxtonAPI.addon();
 
   Documents.paginate = {
-    next: function (docs, currentParams, perPage, _isAllDocs) {
-      var params = {limit: perPage, skip: 1},
-          doc = _.last(docs),
-          docId = '',
+    calculate: function (doc, defaultParams, currentParams, _isAllDocs) {
+      var docId = '',
           lastId = '',
           isView = !!!_isAllDocs,
           key;
@@ -35,12 +33,12 @@ function(app, FauxtonAPI) {
         throw "Require docs to paginate";
       }
 
-      params = _.reduce(['reduce', 'keys', 'endkey', 'descending', 'inclusive_end'], function
(params, key) {
+      var params = _.reduce(['reduce', 'keys', 'endkey', 'descending', 'inclusive_end'],
function (params, key) {
         if (_.has(currentParams, key)) {
           params[key] = currentParams[key]; 
         }
         return params;
-      }, params);
+      }, defaultParams);
 
       lastId = doc.id || doc._id;
 
@@ -67,8 +65,22 @@ function(app, FauxtonAPI) {
       });
 
       return params;
+    },
+
+    next: function (docs, currentParams, perPage, _isAllDocs) {
+      var params = {limit: perPage, skip: 1},
+          doc = _.last(docs);
+          
+      return this.calculate(doc, params, currentParams, _isAllDocs);
+    },
+
+    previous: function (docs, currentParams, perPage, _isAllDocs) {
+      var params = {descending: true, limit: perPage, skip: 1},
+          doc = _.first(docs);
+
+      return this.calculate(doc, params, currentParams, _isAllDocs);
     }
- };
+  };
 
   Documents.Doc = FauxtonAPI.Model.extend({
     idAttribute: "_id",
@@ -333,7 +345,6 @@ function(app, FauxtonAPI) {
     initialize: function(_models, options) {
       this.database = options.database;
       this.params = _.clone(options.params);
-      this.skipFirstItem = false;
       this.on("remove",this.decrementTotalRows , this);
       this.perPageLimit = options.perPageLimit || 20;
 
@@ -342,9 +353,16 @@ function(app, FauxtonAPI) {
       }
     },
 
-    url: function(context) {
+    url: function(context, params) {
       var query = "";
-      if (this.params) {
+
+      if (params) {
+        if (!_.isEmpty(params)) {
+          query = "?" + $.param(params);
+        } else {
+          query = '';
+        }
+      } else if (this.params) {
         query = "?" + $.param(this.params);
       }
 
@@ -373,13 +391,6 @@ function(app, FauxtonAPI) {
 
     updateLimit: function (limit) {
       this.perPageLimit = limit;
-
-      if (this.params.startkey_docid && this.params.startkey) {
-        //we are paginating so set limit + 1
-        this.params.limit = limit + 1;
-        return;
-      }
-
       this.params.limit = limit;
     },
 
@@ -387,36 +398,6 @@ function(app, FauxtonAPI) {
       this.params = params;
     },
 
-    nextPage: function (num, lastId) {
-      if (!lastId) {
-        var doc = this.last();
-
-        if (doc) {
-          lastId = doc.id;
-        } else {
-          lastId = '';
-        }
-      }
-
-      this.params.startkey_docid = '"' + lastId + '"';
-      this.params.startkey = '"' + lastId + '"';
-      // when paginating forward, fetch 21 and don't show
-      // the first item as it was the last item in the previous list
-      this.params.limit = num + 1;
-      return this.url('app');
-    },
-
-    previousPage: function (num, params) {
-      if (params) { 
-        this.params = params;
-      } else {
-        this.params = {reduce: false};
-      }
-
-      this.params.limit = num;
-      return this.url('app'); 
-    },
-
     totalRows: function() {
       return this.viewMeta.total_rows || "unknown";
     },
@@ -432,18 +413,6 @@ function(app, FauxtonAPI) {
       return this.viewMeta.update_seq || false;
     },
 
-    recordStart: function () {
-      if (this.viewMeta.offset === 0) {
-        return 1;
-      }
-
-      if (this.skipFirstItem) {
-        return this.viewMeta.offset + 2;
-      }
-
-      return this.viewMeta.offset + 1;
-    },
-
     parse: function(resp) {
       var rows = resp.rows;
 
@@ -458,7 +427,7 @@ function(app, FauxtonAPI) {
       if (this.skipFirstItem) {
         rows = rows.splice(1);
       }
-      return _.map(rows, function(row) {
+      var mappedRows = _.map(rows, function(row) {
         return {
           _id: row.id,
           _rev: row.value.rev,
@@ -467,6 +436,12 @@ function(app, FauxtonAPI) {
           doc: row.doc || undefined
         };
       });
+
+      if (this.reverse) {
+        return _(mappedRows).reverse().value();
+      }
+
+      return mappedRows;
     }
   });
 
@@ -490,17 +465,15 @@ function(app, FauxtonAPI) {
 
     },
 
-    url: function(context) {
+    url: function(context, params) {
       var query = "";
-      if (this.params) {
-        /*this.params = _.reduce(['startkey', 'endkey', 'key', 'keys'], function (params,
key) {
-          if (_.has(params, key)) {
-            params[key] = JSON.stringify(params[key]);
-          }
-
-          return params;
-        }, this.params);*/
-
+      if (params) {
+        if (!_.isEmpty(params)) {
+          query = "?" + $.param(params);
+        } else {
+          query = '';
+        }
+      } else if (this.params) {
         query = "?" + $.param(this.params);
       }
       
@@ -517,32 +490,6 @@ function(app, FauxtonAPI) {
       return url.join("/") + query;
     },
 
-    nextPage: function (num, lastId) {
-      if (!lastId) {
-        lastDoc = this.last();
-      }
-
-      var id = lastDoc.get("id");
-      if (id) {
-        this.params.startkey_docid = id;
-      }
-
-      this.params.startkey =  JSON.stringify(lastDoc.get('key'));
-      this.params.limit = num + 1;
-      return this.url('app');
-    },
-
-     previousPage: function (num, params) {
-      if (params) { 
-        this.params = params;
-      } else {
-        this.params = {reduce: false};
-      }
-
-      this.params.limit = num;
-      return this.url('app');
-    },
-
     updateParams: function (params) {
       this.params = params;
     },
@@ -557,18 +504,6 @@ function(app, FauxtonAPI) {
       this.params.limit = limit;
     },
 
-    recordStart: function () {
-      if (this.viewMeta.offset === 0) {
-        return 1;
-      }
-
-      if (this.skipFirstItem) {
-        return this.viewMeta.offset + 2;
-      }
-
-      return this.viewMeta.offset + 1;
-    },
-
     totalRows: function() {
       if (this.params.reduce) { return "unknown_reduce";}
 
@@ -610,7 +545,7 @@ function(app, FauxtonAPI) {
         offset: resp.offset,
         update_seq: resp.update_seq
       };
-      return _.map(rows, function(row) {
+      var mappedRows =  _.map(rows, function(row) {
         return {
           value: row.value,
           key: row.key,
@@ -618,6 +553,12 @@ function(app, FauxtonAPI) {
           id: row.id
         };
       });
+
+      if (this.reverse) {
+        return _(mappedRows).reverse().value();
+      }
+
+      return mappedRows;
     },
 
     buildAllDocs: function(){
@@ -720,10 +661,6 @@ function(app, FauxtonAPI) {
       return deferred;
     },
 
-    recordStart: function () {
-      return 1;
-    },
-
     totalRows: function() {
       return this.viewMeta.total_rows || "unknown";
     },

http://git-wip-us.apache.org/repos/asf/couchdb/blob/77c06c28/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 eeca7b7..ffe0b87 100644
--- a/src/fauxton/app/addons/documents/routes.js
+++ b/src/fauxton/app/addons/documents/routes.js
@@ -242,7 +242,7 @@ function(app, FauxtonAPI, Documents, Databases) {
         {"name": this.data.database.id, "link": Databases.databaseUrl(this.data.database)}
       ];
 
-      this.apiUrl = [this.data.database.allDocs.url("apiurl"), this.data.database.allDocs.documentation()
];
+      this.apiUrl = [this.data.database.allDocs.url("apiurl", urlParams), this.data.database.allDocs.documentation()
];
     },
 
     viewFn: function (databaseName, ddoc, view) {
@@ -297,7 +297,7 @@ function(app, FauxtonAPI, Documents, Databases) {
         ];
       };
 
-      this.apiUrl = [this.data.indexedDocs.url("apiurl"), "docs"];
+      this.apiUrl = [this.data.indexedDocs.url("apiurl", urlParams), "docs"];
     },
 
     newViewEditor: function () {
@@ -345,11 +345,13 @@ function(app, FauxtonAPI, Documents, Databases) {
           params: docParams
         });
 
-        this.apiUrl = [this.data.indexedDocs.url("apiurl"), "docs"];
       }
 
+
       this.documentsView.setCollection(collection);
       this.documentsView.setParams(docParams, urlParams);
+
+      this.apiUrl = [collection.url("apiurl", urlParams), "docs"];
     },
 
     updateAllDocsFromPreview: function (event) {
@@ -373,22 +375,34 @@ function(app, FauxtonAPI, Documents, Databases) {
     },
 
     perPageChange: function (perPage) {
+      console.log('pp', perPage);
       this.perPage = perPage;
       this.documentsView.collection.updateLimit(perPage);
       this.documentsView.forceRender();
     },
 
     paginate: function (options) {
-      var params = options.params,
+      var params = {},
           urlParams = app.getParams(),
           collection = this.documentsView.collection;
 
       this.documentsView.forceRender();
+      var rawCollection = collection.map(function (item) { return item.toJSON(); });
 
       if (options.direction === 'next') {
-          params = Documents.paginate.next(collection.map(function (item) { return item.toJSON();
}), collection.params, options.perPage, !!collection.isAllDocs);
+          collection.reverse = false;
+          params = Documents.paginate.next(rawCollection, 
+                                           collection.params,
+                                           options.perPage, 
+                                           !!collection.isAllDocs);
+      } else {
+          collection.reverse = true;
+          params = Documents.paginate.previous(rawCollection, 
+                                               collection.params, 
+                                               options.perPage, 
+                                               !!collection.isAllDocs);
       }
-      
+      params.limit = options.perPage;
       collection.updateParams(params);
     },
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/77c06c28/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 dea0bc7..78a2c09 100644
--- a/src/fauxton/app/addons/documents/views.js
+++ b/src/fauxton/app/addons/documents/views.js
@@ -438,7 +438,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
 
     serialize: function () {
        var totalRows = 0,
-          recordStart = 0,
           updateSeq = false,
           pageStart = 0,
           pageEnd = 20;
@@ -448,7 +447,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
         updateSeq = this.collection.updateSeq();
       }
 
-      recordStart = this.collection.recordStart();
       if (this.pagination) {
         pageStart = this.pagination.pageStart();
         pageEnd =  this.pagination.pageEnd();
@@ -457,7 +455,6 @@ function(app, FauxtonAPI, Components, Documents, Databases, pouchdb, resizeColum
       return {
         database: app.utils.safeURLName(this.collection.database.id),
         updateSeq: updateSeq,
-        offset: recordStart,
         totalRows: totalRows,
         numModels: this.collection.models.length + recordStart - 1,
         pageStart: pageStart,

http://git-wip-us.apache.org/repos/asf/couchdb/blob/77c06c28/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 a9f4ef4..278831e 100644
--- a/src/fauxton/app/addons/fauxton/components.js
+++ b/src/fauxton/app/addons/fauxton/components.js
@@ -79,7 +79,6 @@ function(app, FauxtonAPI, ace, spin) {
     setDefaults: function () {
       this._pageNumber = [];
       this._pageStart = 1;
-      this.paramsHistory = [];
       this.enabled = true;
     },
 
@@ -116,12 +115,10 @@ function(app, FauxtonAPI, ace, spin) {
       if (!this.canShowPreviousfn()) { return; }
 
       this.decPageNumber();
-      var params = this.paramsHistory.pop();
 
       FauxtonAPI.triggerRouteEvent('paginate', {
        direction: 'previous',
        perPage: this.perPage,
-       params: params
       });
     },
 
@@ -141,7 +138,6 @@ function(app, FauxtonAPI, ace, spin) {
       event.stopPropagation();
       if (!this.canShowNextfn()) { return; }
 
-      this.paramsHistory.push(_.clone(this.collection.params));
       this.incPageNumber();
 
       FauxtonAPI.triggerRouteEvent('paginate', {


Mime
View raw message