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:04:00 UTC

[14/14] git commit: updated refs/heads/index-pagination to db85b98

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/db85b985
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/db85b985
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/db85b985

Branch: refs/heads/index-pagination
Commit: db85b98565ce0bc691fdac04b9b69eeb9287dccc
Parents: d7d91ac
Author: Garren Smith <ga...@gmail.com>
Authored: Tue Sep 10 16:03:12 2013 +0200
Committer: Garren Smith <ga...@gmail.com>
Committed: Tue Sep 10 16:03:12 2013 +0200

----------------------------------------------------------------------
 src/fauxton/app/modules/databases/routes.js     |  1 -
 src/fauxton/app/modules/documents/resources.js  | 51 ++++++++++++++++++--
 src/fauxton/app/modules/documents/routes.js     | 13 ++++-
 .../modules/documents/tests/resourcesSpec.js    |  2 +-
 src/fauxton/app/modules/documents/views.js      | 13 +++--
 src/fauxton/app/modules/fauxton/paginate.js     |  7 ++-
 .../app/templates/documents/all_docs_list.html  |  2 +-
 src/fauxton/test/core/paginateSpec.js           | 21 ++++++++
 8 files changed, 96 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb/blob/db85b985/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/db85b985/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 065489e..911a65d 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,34 @@ 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,
         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) {
+        console.log('skipping');
+        rows = rows.splice(1);
+      }
+      return _.map(rows, function(row) {
         return {
           _id: row.id,
           _rev: row.value.rev,
@@ -326,6 +350,7 @@ function(app, FauxtonAPI) {
       this.idxType = "_view";
       this.view = options.view;
       this.design = options.design.replace('_design/','');
+      this.skipFirstItem = false;
     },
 
     url: function(context) {
@@ -366,6 +391,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 +412,21 @@ 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);
+        console.log('skip');
+      }
+
       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,

http://git-wip-us.apache.org/repos/asf/couchdb/blob/db85b985/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/db85b985/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/db85b985/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..921e0f2 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,22 @@ 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();
+      console.log('record', 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 +535,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/db85b985/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 6dbaa6f..506b878 100644
--- a/src/fauxton/app/modules/fauxton/paginate.js
+++ b/src/fauxton/app/modules/fauxton/paginate.js
@@ -67,13 +67,16 @@ function(app, FauxtonAPI) {
 
     previousClicked: function (event) {
       event.preventDefault();
-      FauxtonAPI.navigate(this.previousUrlfn());
+      FauxtonAPI.navigate(this.previousUrlfn(), {trigger: false});
+      FauxtonAPI.triggerRouteEvent('paginate', 'previous');
     },
 
     nextClicked: function (event) {
       event.preventDefault();
       this.previousIds.push(this.collection.first().id);
-      FauxtonAPI.navigate(this.nextUrlfn());
+      console.log(this.previousIds);
+      FauxtonAPI.navigate(this.nextUrlfn(), {trigger: false});
+      FauxtonAPI.triggerRouteEvent('paginate', 'next');
     },
 
     serialize: function () {

http://git-wip-us.apache.org/repos/asf/couchdb/blob/db85b985/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/db85b985/src/fauxton/test/core/paginateSpec.js
----------------------------------------------------------------------
diff --git a/src/fauxton/test/core/paginateSpec.js b/src/fauxton/test/core/paginateSpec.js
index 4139248..981380e 100644
--- a/src/fauxton/test/core/paginateSpec.js
+++ b/src/fauxton/test/core/paginateSpec.js
@@ -55,6 +55,10 @@ define([
     });
 
     describe('#next', function () {
+      beforeEach(function () {
+        //do this so it doesn't throw an error on other unwired up components
+        FauxtonAPI.triggerRouteEvent = function () {};
+      });
 
       it('Should navigate', function () {
         var navigateMock = sinon.spy(FauxtonAPI, 'navigate');
@@ -65,6 +69,15 @@ define([
         FauxtonAPI.navigate.restore();
       });
 
+      it('Should trigger routeEvent', function () {
+        var navigateMock = sinon.spy(FauxtonAPI, 'triggerRouteEvent');
+
+        paginate.$('a#next').click();
+
+        assert.ok(navigateMock.calledOnce);
+        FauxtonAPI.triggerRouteEvent.restore();
+      });
+
     });
 
 
@@ -79,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();
+      });
 
     });