summaryrefslogtreecommitdiff
path: root/src/fauxton/assets/js/plugins/cloudant.pagingcollection.js
blob: 2ab5eafc3614efe77658f21020ab343a831f7e3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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.

(function(root, factory) {
  "use strict";
  // start with AMD, so paginate could then be used by ``var paginate = require('paginate');``
  if (typeof define === 'function' && define.amd) {
    define(['underscore', 'backbone', 'jquery'], function(_, Backbone, $) {
      // Export global even in AMD case in case this script is loaded with
      // others that may still expect a global paginate.
      return factory(root, null, _, Backbone, $.param);
    });

  // Next check for Node.js or CommonJS. Also look to see if either
  // underscore or lodash are the modules being used
  } else if (typeof exports !== 'undefined') {
    var Backbone = require('Backbone'),
        $param = require('querystring').stringify,
        _;
    try {
      _ = require('underscore');
    } catch(e) {
      _ = require('lodash');
    }
    factory(root, exports, _, Backbone, $param);

  // Finally, register as a browser global.
  } else {
    root.PagingCollection = factory(root, {}, root._, root.Backbone, root.$.param);
  }

}(this, function(root, exports, _, Backbone, $param) {
  "use strict";

  //PagingCollection
  //----------------

  // A PagingCollection knows how to build appropriate requests to the
  // CouchDB-like server and how to fetch. The Collection will always contain a
  // single page of documents.

  var PagingCollection = Backbone.Collection.extend({

    // initialize parameters and page size
    constructor: function() {
      Backbone.Collection.apply(this, arguments);
      this.configure.apply(this, arguments);
    },

    configure: function(collections, options) {
      var querystring = _.result(this, "url").split("?")[1] || "";
      this.paging = _.defaults((options.paging || {}), {
        defaultParams: _.defaults({}, options.params, this._parseQueryString(querystring)),
        hasNext: false,
        hasPrevious: false,
        params: {},
        pageSize: 20,
        direction: undefined
      });

      this.paging.params = _.clone(this.paging.defaultParams);
      this.updateUrlQuery(this.paging.defaultParams);
    },

    calculateParams: function(currentParams, skipIncrement, limitIncrement) {

      var params = _.clone(currentParams);
      params.skip = (parseInt(currentParams.skip, 10) || 0) + skipIncrement;

      // guard against hard limits
      if(this.paging.defaultParams.limit) {
        params.limit = Math.min(this.paging.defaultParams.limit, params.limit);
      }
      // request an extra row so we know that there are more results
      params.limit = limitIncrement + 1;
      // prevent illegal skip values
      params.skip = Math.max(params.skip, 0);

      return params;
    },

    pageSizeReset: function(pageSize, opts) {
      var options = _.defaults((opts || {}), {fetch: true});
      this.paging.direction = undefined;
      this.paging.pageSize = pageSize;
      this.paging.params = this.paging.defaultParams;
      this.paging.params.limit = pageSize;
      this.updateUrlQuery(this.paging.params);
      if (options.fetch) {
        return this.fetch();
      }
    },

    _parseQueryString: function(uri) {
      var queryString = decodeURI(uri).split(/&/);

      return _.reduce(queryString, function (parsedQuery, item) {
          var nameValue = item.split(/=/);
          if (nameValue.length === 2) {
            parsedQuery[nameValue[0]] = nameValue[1];
          }

          return parsedQuery;
      }, {});
    },

    _iterate: function(offset, opts) {
      var options = _.defaults((opts || {}), {fetch: true});

      this.paging.params = this.calculateParams(this.paging.params, offset, this.paging.pageSize);

      // Fetch the next page of documents
      this.updateUrlQuery(this.paging.params);
      if (options.fetch) {
        return this.fetch({reset: true});
      }
    },

    // `next` is called with the number of items for the next page.
    // It returns the fetch promise.
    next: function(options){
      this.paging.direction = "next";
      return this._iterate(this.paging.pageSize, options);
    },

    // `previous` is called with the number of items for the previous page.
    // It returns the fetch promise.
    previous: function(options){
      this.paging.direction = "previous";
      return this._iterate(0 - this.paging.pageSize, options);
    },

    shouldStringify: function (val) {
      try {
        JSON.parse(val);
        return false;
      } catch(e) {
        return true;
      }
    },

    // Encodes the parameters so that couchdb will understand them
    // and then sets the url with the new url.
    updateUrlQuery: function (params) {
      var url = _.result(this, "url").split("?")[0];

      _.each(['startkey', 'endkey', 'key'], function (key) {
        if (_.has(params, key) && this.shouldStringify(params[key])) {
          params[key] = JSON.stringify(params[key]);
        }
      }, this);

      this.url = url + '?' + $param(params);
    },

    fetch: function () {
      // if this is a fetch for the first time, fetch one extra to see if there is a next
      if (!this.paging.direction && this.paging.params.limit > 0) {
        this.paging.direction = 'fetch';
        this.paging.params.limit = this.paging.params.limit + 1;
        this.updateUrlQuery(this.paging.params);
      }

      return Backbone.Collection.prototype.fetch.apply(this, arguments);
    },

    parse: function (resp) {
      var rows = resp.rows;

      this.paging.hasNext = this.paging.hasPrevious = false;

      this.viewMeta = {
        total_rows: resp.total_rows,
        offset: resp.offset,
        update_seq: resp.update_seq
      };

      var skipLimit = this.paging.defaultParams.skip || 0;
      if(this.paging.params.skip > skipLimit) {
        this.paging.hasPrevious = true;
      }

      if(rows.length === this.paging.pageSize + 1) {
        this.paging.hasNext = true;

        // remove the next page marker result
        rows.pop();
        this.viewMeta.total_rows = this.viewMeta.total_rows - 1;
      }
      return rows;
    },

    hasNext: function() {
      return this.paging.hasNext;
    },

    hasPrevious: function() {
      return this.paging.hasPrevious;
    }
  });


  if (exports) {
    // Overload the Backbone.ajax method, this allows PagingCollection to be able to
    // work in node.js
    exports.setAjax = function (ajax) {
      Backbone.ajax = ajax;
    };

    exports.PagingCollection = PagingCollection;
  }

  return PagingCollection;
}));