You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by mi...@apache.org on 2015/03/09 20:47:20 UTC

fauxton commit: updated refs/heads/master to a145a63

Repository: couchdb-fauxton
Updated Branches:
  refs/heads/master cd4d7ea25 -> a145a63a8


Truncates pagination if more than 10 pages of pagination


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

Branch: refs/heads/master
Commit: a145a63a88578866b3c2518078ad1e9f7a6f9b6e
Parents: cd4d7ea
Author: michellephung@gmail.com <mi...@gmail.com>
Authored: Fri Feb 27 15:05:17 2015 -0500
Committer: michellephung@gmail.com <mi...@gmail.com>
Committed: Mon Mar 9 15:43:20 2015 -0400

----------------------------------------------------------------------
 app/addons/fauxton/components.js             | 42 +++++++++++++--
 app/addons/fauxton/templates/pagination.html |  2 +-
 app/addons/fauxton/tests/paginateSpec.js     | 66 +++++++++++++++++++++++
 3 files changed, 106 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/a145a63a/app/addons/fauxton/components.js
----------------------------------------------------------------------
diff --git a/app/addons/fauxton/components.js b/app/addons/fauxton/components.js
index d74e816..cf0edaa 100644
--- a/app/addons/fauxton/components.js
+++ b/app/addons/fauxton/components.js
@@ -326,22 +326,58 @@ function(app, FauxtonAPI, ace, spin, ZeroClipboard) {
     className: 'pagination',
     template: "addons/fauxton/templates/pagination",
 
-    initialize: function(options) {
+    initialize: function (options) {
       this.page = parseInt(options.page, 10);
       this.perPage = options.perPage;
       this.urlFun = options.urlFun;
     },
 
-    serialize: function() {
+    serialize: function () {
       var total = this.collection.length;
       var totalPages = Math.ceil(total / this.perPage);
+
+      var visiblePagesObject = this.getVisiblePages(this.page, totalPages);
+
+      var from = visiblePagesObject.from,
+          to = visiblePagesObject.to;
+
       return {
         page: this.page,
         perPage: this.perPage,
         total: total,
         totalPages: totalPages,
-        urlFun: this.urlFun
+        urlFun: this.urlFun,
+        from: from,
+        to: to
       };
+    },
+
+    getVisiblePages: function (page, totalPages) {
+      var from, to;
+
+      if (totalPages < 10) {
+        from = 1;
+        to = totalPages + 1;
+      } else { // if totalPages is more than 10
+        from = page - 5;
+        to = page + 5;
+
+        if (from <= 1) {
+          from = 1;
+          to = 11;
+        }
+        if (to > totalPages + 1) {
+          from =  totalPages - 9;
+          to = totalPages + 1;
+        }
+
+      }
+
+      return {
+        from: from,
+        to: to
+      };
+
     }
   });
 

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/a145a63a/app/addons/fauxton/templates/pagination.html
----------------------------------------------------------------------
diff --git a/app/addons/fauxton/templates/pagination.html b/app/addons/fauxton/templates/pagination.html
index f6ad15a..7fb5b41 100644
--- a/app/addons/fauxton/templates/pagination.html
+++ b/app/addons/fauxton/templates/pagination.html
@@ -16,7 +16,7 @@ the License.
 <% } else { %>
   <li class="disabled"> <a href="<%- urlFun(page) %>">&laquo;</a></li>
 <% } %>
-<% _.each(_.range(1, totalPages+1), function(i) { %>
+<% _.each(_.range(from, to), function(i) { %>
   <li <% if (page == i) { %>class="active"<% } %>> <a href="<%- urlFun(i) %>"><%- i %></a></li>
 <% }) %>
 <% if (page < totalPages) { %>

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/a145a63a/app/addons/fauxton/tests/paginateSpec.js
----------------------------------------------------------------------
diff --git a/app/addons/fauxton/tests/paginateSpec.js b/app/addons/fauxton/tests/paginateSpec.js
new file mode 100644
index 0000000..2f49e6c
--- /dev/null
+++ b/app/addons/fauxton/tests/paginateSpec.js
@@ -0,0 +1,66 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+define([
+       'app',
+       'addons/fauxton/components',
+       'addons/documents/resources',
+       'testUtils',
+       'api'
+], function (app, Views, Models, testUtils, FauxtonAPI) {
+  var assert = testUtils.assert;
+
+  describe('DatabasePaginate', function () {
+    var totalPages, pageComponent, testObject, currentPage;
+
+    beforeEach(function () {
+      pageComponent = new Views.Pagination({page: 1});
+    });
+
+    it('Should return all the pages, if there are less than 10 total pages', function () {
+      for (totalPages = 1; totalPages < 11; totalPages++) {
+        for (currentPage = 1; currentPage < totalPages; currentPage++) {
+          testObject = pageComponent.getVisiblePages(currentPage, totalPages);
+          assert.deepEqual(testObject , {from: 1, to: totalPages + 1});
+        }
+      }
+    });
+
+    it('Should return only a ten page range, if more than 10 total pages', function () {
+      var difference;
+      for (totalPages = 10; totalPages < 50; totalPages++) {
+        for (currentPage = totalPages; currentPage < totalPages; currentPage++) {
+          testObject = pageComponent.getVisiblePages(currentPage, totalPages);
+          difference = testObject.to - testObject.from;
+          assert.equal(difference, 10);
+        }
+      }
+    });
+
+    it('Should return from 1 to 10, if accessing the first 6 pages', function () {
+      totalPages = 50;
+      for (currentPage = 1; currentPage <= 6; currentPage++) {
+        testObject = pageComponent.getVisiblePages(currentPage, totalPages);
+        assert.deepEqual(testObject , {from: 1, to: 11});
+      }
+
+    });
+
+    it('Should return from totalpages-10 to totalPages, if accessing one of the last 4 pages', function () {
+      totalPages = 50;
+      for (currentPage = 46; currentPage <= totalPages; currentPage++) {
+        testObject = pageComponent.getVisiblePages(currentPage, totalPages);
+        assert.deepEqual(testObject , {from: 41, to: 51});
+      }
+    });
+
+  });
+});