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 2013/09/10 16:18:35 UTC

[3/3] git commit: updated refs/heads/master to 4d557a0

Fauxton: Pagination improvements


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

Branch: refs/heads/master
Commit: 4d557a008e0bd12ce22cb19f6942422ae05034fd
Parents: e976e7c
Author: Garren Smith <ga...@gmail.com>
Authored: Mon Sep 9 10:40:37 2013 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Tue Sep 10 16:17:45 2013 +0200

----------------------------------------------------------------------
 src/fauxton/app/modules/databases/routes.js     |  1 -
 src/fauxton/app/modules/documents/resources.js  | 57 +++++++++++++++++---
 src/fauxton/app/modules/documents/routes.js     | 13 ++++-
 .../modules/documents/tests/resourcesSpec.js    |  2 +-
 src/fauxton/app/modules/documents/views.js      | 12 +++--
 src/fauxton/app/modules/fauxton/paginate.js     |  9 ++--
 .../app/templates/documents/all_docs_list.html  |  2 +-
 src/fauxton/test/core/paginateSpec.js           | 31 ++++++-----
 8 files changed, 94 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/databases/routes.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/databases/routes.js b/src/fauxton/app/modules/databases/routes.js
index 001fab6..1977264 100644
--- a/src/fauxton/app/modules/databases/routes.js
+++ b/src/fauxton/app/modules/databases/routes.js
@@ -64,7 +64,6 @@ function(app, FauxtonAPI, Databases, Views) {
 
       databases.fetch().done(function(resp) {
         FauxtonAPI.when(databases.map(function(database) {
-          console.log('fetching', database);
           return database.status.fetch();
         })).always(function(resp) {
           //make this always so that even if a user is not allowed access to a database

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/documents/resources.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/resources.js b/src/fauxton/app/modules/documents/resources.js
index a9e9836..70be81c 100644
--- a/src/fauxton/app/modules/documents/resources.js
+++ b/src/fauxton/app/modules/documents/resources.js
@@ -253,6 +253,7 @@ function(app, FauxtonAPI) {
     initialize: function(_models, options) {
       this.database = options.database;
       this.params = options.params;
+      this.skipFirstItem = false;
     },
 
     url: function(context) {
@@ -274,7 +275,9 @@ function(app, FauxtonAPI) {
 
       this.params.startkey_docid = '"' + lastId + '"';
       this.params.startkey = '"' + lastId + '"';
-      this.params.limit = num;
+      // 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');
     },
 
@@ -287,6 +290,7 @@ function(app, FauxtonAPI) {
         delete this.params.startkey;
         delete this.params.startkey_docid;
       }
+
       return this.url('app');
     },
 
@@ -298,14 +302,33 @@ 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) {
-      that = this;
+      var rows = resp.rows;
+
       this.viewMeta = {
         total_rows: resp.total_rows,
-        offest: resp.offset,
+        offset: resp.offset,
         update_seq: resp.update_seq
       };
-      return _.map(resp.rows, function(row) {
+
+      //Paginating, don't show first item as it was the last
+      //item in the previous page
+      if (this.skipFirstItem) {
+        rows = rows.splice(1);
+      }
+      return _.map(rows, function(row) {
         return {
           _id: row.id,
           _rev: row.value.rev,
@@ -322,10 +345,11 @@ function(app, FauxtonAPI) {
 
     initialize: function(_models, options) {
       this.database = options.database;
-      this.params = _.extend({limit: 10, reduce: false}, options.params);
+      this.params = _.extend({limit: 20, reduce: false}, options.params);
       this.idxType = "_view";
       this.view = options.view;
       this.design = options.design.replace('_design/','');
+      this.skipFirstItem = false;
     },
 
     url: function(context) {
@@ -366,6 +390,18 @@ function(app, FauxtonAPI) {
       return this.url('app');
     },
 
+    recordStart: function () {
+      if (this.viewMeta.offset === 0) {
+        return 1;
+      }
+
+      if (this.skipFirstItem) {
+        return this.viewMeta.offset + 2;
+      }
+
+      return this.viewMeta.offset + 1;
+    },
+
     totalRows: function() {
       return this.viewMeta.total_rows || "unknown";
     },
@@ -375,15 +411,20 @@ function(app, FauxtonAPI) {
     },
 
     parse: function(resp) {
+      var rows = resp.rows;
       this.endTime = new Date().getTime();
       this.requestDuration = (this.endTime - this.startTime);
 
+      if (this.skipFirstItem) {
+        rows = rows.splice(1);
+      }
+
       this.viewMeta = {
         total_rows: resp.total_rows,
         offset: resp.offset,
         update_seq: resp.update_seq
       };
-      return _.map(resp.rows, function(row) {
+      return _.map(rows, function(row) {
         return {
           value: row.value,
           key: row.key,
@@ -450,7 +491,7 @@ function(app, FauxtonAPI) {
       this.rows = options.rows;
       this.view = options.view;
       this.design = options.design.replace('_design/','');
-      this.params = _.extend({limit: 10, reduce: false}, options.params);
+      this.params = _.extend({limit: 20, reduce: false}, options.params);
       this.idxType = "_view";
     },
 
@@ -464,7 +505,7 @@ function(app, FauxtonAPI) {
 
       this.viewMeta = {
         total_rows: this.rows.length,
-        offest: 0,
+        offset: 0,
         update_seq: false
       };
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/documents/routes.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/routes.js b/src/fauxton/app/modules/documents/routes.js
index 730b4bf..0edf18d 100644
--- a/src/fauxton/app/modules/documents/routes.js
+++ b/src/fauxton/app/modules/documents/routes.js
@@ -159,7 +159,8 @@ function(app, FauxtonAPI, Documents, Databases) {
     events: {
       "route:updateAllDocs": "updateAllDocsFromView",
       "route:updatePreviewDocs": "updateAllDocsFromPreview",
-      "route:reloadDesignDocs": "reloadDesignDocs"
+      "route:reloadDesignDocs": "reloadDesignDocs",
+      "route:paginate": "paginate"
     },
 
     initialize: function (route, masterLayout, options) {
@@ -328,6 +329,16 @@ function(app, FauxtonAPI, Documents, Databases) {
       }));
     },
 
+    paginate: function (direction) {
+      _.extend(this.documentsView.collection.params, app.getParams());
+      this.documentsView.forceRender();
+      if (direction === 'next') {
+        this.documentsView.collection.skipFirstItem = true;
+      } else {
+        this.documentsView.collection.skipFirstItem = false;
+      }
+    },
+
     reloadDesignDocs: function (event) {
       this.sidebar.forceRender();
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/documents/tests/resourcesSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/tests/resourcesSpec.js b/src/fauxton/app/modules/documents/tests/resourcesSpec.js
index 35bbdb3..e78a3c3 100644
--- a/src/fauxton/app/modules/documents/tests/resourcesSpec.js
+++ b/src/fauxton/app/modules/documents/tests/resourcesSpec.js
@@ -68,7 +68,7 @@ define([
     it('Should return urlNext', function () {
       var url = collection.urlNextPage(20);
 
-      assert.equal(url, 'database/databaseId/_all_docs?limit=20&startkey_docid=%22myId2%22&startkey=%22myId2%22');
+      assert.equal(url, 'database/databaseId/_all_docs?limit=21&startkey_docid=%22myId2%22&startkey=%22myId2%22');
 
     });
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/documents/views.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/documents/views.js b/src/fauxton/app/modules/documents/views.js
index 08ea676..5bc6a7a 100644
--- a/src/fauxton/app/modules/documents/views.js
+++ b/src/fauxton/app/modules/documents/views.js
@@ -442,7 +442,7 @@ function(app, FauxtonAPI, Paginate, Documents, pouchdb, Codemirror, JSHint, resi
     establish: function() {
       if (this.newView) { return null; }
 
-      return this.collection.fetch().fail(function() {
+      return this.collection.fetch({reset: true}).fail(function() {
         // TODO: handle error requests that slip through
         // This should just throw a notification, not break the page
         console.log("ERROR: ", arguments);
@@ -455,17 +455,21 @@ function(app, FauxtonAPI, Paginate, Documents, pouchdb, Codemirror, JSHint, resi
 
     serialize: function() {
       var totalRows = 0,
-      updateSeq = false;
+          recordStart = 0,
+          updateSeq = false;
 
       if (!this.newView) {
         totalRows = this.collection.totalRows();
         updateSeq = this.collection.updateSeq();
       }
 
+      recordStart = this.collection.recordStart();
+
       var info = {
         updateSeq: updateSeq,
+        offset: recordStart,
         totalRows: totalRows,
-        numModels: this.collection.models.length,
+        numModels: this.collection.models.length + recordStart - 1,
         viewList: this.viewList,
         requestDuration: null
       };
@@ -530,7 +534,7 @@ function(app, FauxtonAPI, Paginate, Documents, pouchdb, Codemirror, JSHint, resi
         },
         canShowNextfn: function () {
           
-          if ((collection.viewMeta.offset + 1) === collection.viewMeta.total_rows) {
+          if ((collection.viewMeta.offset + collection.length + 2) >= collection.viewMeta.total_rows) {
             return false;
           }
 

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/modules/fauxton/paginate.js
----------------------------------------------------------------------
diff --git a/src/fauxton/app/modules/fauxton/paginate.js b/src/fauxton/app/modules/fauxton/paginate.js
index a32bbbe..074fb47 100644
--- a/src/fauxton/app/modules/fauxton/paginate.js
+++ b/src/fauxton/app/modules/fauxton/paginate.js
@@ -49,7 +49,6 @@ function(app, FauxtonAPI) {
       "click a#previous": 'previousClicked'
     },
 
-    currentDirection: 'next',
     previousIds: [],
 
     scrollTo: function () {
@@ -68,15 +67,15 @@ function(app, FauxtonAPI) {
 
     previousClicked: function (event) {
       event.preventDefault();
-      this.currentDirection = 'previous';
-      FauxtonAPI.navigate(this.previousUrlfn());
+      FauxtonAPI.navigate(this.previousUrlfn(), {trigger: false});
+      FauxtonAPI.triggerRouteEvent('paginate', 'previous');
     },
 
     nextClicked: function (event) {
       event.preventDefault();
-      this.currentDirection = 'next';
       this.previousIds.push(this.collection.first().id);
-      FauxtonAPI.navigate(this.nextUrlfn());
+      FauxtonAPI.navigate(this.nextUrlfn(), {trigger: false});
+      FauxtonAPI.triggerRouteEvent('paginate', 'next');
     },
 
     serialize: function () {

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/app/templates/documents/all_docs_list.html
----------------------------------------------------------------------
diff --git a/src/fauxton/app/templates/documents/all_docs_list.html b/src/fauxton/app/templates/documents/all_docs_list.html
index f602575..e5ab8ac 100644
--- a/src/fauxton/app/templates/documents/all_docs_list.html
+++ b/src/fauxton/app/templates/documents/all_docs_list.html
@@ -28,7 +28,7 @@ the License.
     </div>
   <% } %>
   <p>
-    Showing 1-<%= numModels %> of <%= totalRows %> rows
+  Showing <%=offset%> - <%= numModels %> of <%= totalRows %> rows
     <% if (updateSeq) { %>
       -- Update Sequence: <%= updateSeq %>
     <% } %>

http://git-wip-us.apache.org/repos/asf/couchdb/blob/4d557a00/src/fauxton/test/core/paginateSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/test/core/paginateSpec.js b/src/fauxton/test/core/paginateSpec.js
index 6b7bfb6..981380e 100644
--- a/src/fauxton/test/core/paginateSpec.js
+++ b/src/fauxton/test/core/paginateSpec.js
@@ -55,12 +55,9 @@ define([
     });
 
     describe('#next', function () {
-
-      it('should set direction as next', function () {
-        paginate.$('a#next').click();
-
-        assert.equal(paginate.currentDirection, 'next');
-
+      beforeEach(function () {
+        //do this so it doesn't throw an error on other unwired up components
+        FauxtonAPI.triggerRouteEvent = function () {};
       });
 
       it('Should navigate', function () {
@@ -72,17 +69,19 @@ define([
         FauxtonAPI.navigate.restore();
       });
 
-    });
+      it('Should trigger routeEvent', function () {
+        var navigateMock = sinon.spy(FauxtonAPI, 'triggerRouteEvent');
 
+        paginate.$('a#next').click();
 
-    describe('#previous', function () {
+        assert.ok(navigateMock.calledOnce);
+        FauxtonAPI.triggerRouteEvent.restore();
+      });
 
-      it('should set direction as next', function () {
-        paginate.$('a#previous').click();
+    });
 
-        assert.equal(paginate.currentDirection, 'previous');
 
-      });
+    describe('#previous', function () {
 
       it('Should navigate', function () {
         var navigateMock = sinon.spy(FauxtonAPI, 'navigate');
@@ -93,6 +92,14 @@ define([
         FauxtonAPI.navigate.restore();
       });
 
+      it('Should trigger routeEvent', function () {
+        var navigateMock = sinon.spy(FauxtonAPI, 'triggerRouteEvent');
+
+        paginate.$('a#previous').click();
+
+        assert.ok(navigateMock.calledOnce);
+        FauxtonAPI.triggerRouteEvent.restore();
+      });
 
     });