diff options
author | Garren Smith <garren.smith@gmail.com> | 2014-03-03 18:28:09 +0200 |
---|---|---|
committer | Garren Smith <garren.smith@gmail.com> | 2014-03-03 18:28:09 +0200 |
commit | b63c79130e15c08810b1288495e0c0ba89358c3d (patch) | |
tree | ad27116acd0edf154c916ba79262a40b18f39237 | |
parent | 7df61e79084a4a63ef0661d2e5eac556f6b8f5d4 (diff) | |
download | couchdb-b63c79130e15c08810b1288495e0c0ba89358c3d.tar.gz |
Code improvements to pagination
-rw-r--r-- | src/fauxton/app/addons/documents/resources.js | 9 | ||||
-rw-r--r-- | src/fauxton/app/addons/documents/routes.js | 68 | ||||
-rw-r--r-- | src/fauxton/app/addons/documents/views.js | 7 | ||||
-rw-r--r-- | src/fauxton/app/addons/fauxton/components.js | 6 |
4 files changed, 57 insertions, 33 deletions
diff --git a/src/fauxton/app/addons/documents/resources.js b/src/fauxton/app/addons/documents/resources.js index 8bc52b025..73d9a0a8f 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; } diff --git a/src/fauxton/app/addons/documents/routes.js b/src/fauxton/app/addons/documents/routes.js index 168807a44..21e76e022 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; } diff --git a/src/fauxton/app/addons/documents/views.js b/src/fauxton/app/addons/documents/views.js index 1d07dae41..f828d7114 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; diff --git a/src/fauxton/app/addons/fauxton/components.js b/src/fauxton/app/addons/fauxton/components.js index 117419320..7dcf2d7d0 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; |