diff --git a/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.js b/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.js new file mode 100644 index 000000000..bff50749e --- /dev/null +++ b/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.js @@ -0,0 +1,1296 @@ +/* + backbone-pageable 1.2.1 + http://github.com/wyuenho/backbone-pageable + + Copyright (c) 2013 Jimmy Yuen Ho Wong + Licensed under the MIT @license. +*/ + +(function (factory) { + + // CommonJS + if (typeof exports == "object") { + module.exports = factory(require("underscore"), require("backbone")); + } + // AMD + else if (typeof define == "function" && define.amd) { + define(["underscore", "backbone"], factory); + } + // Browser + else if (typeof _ !== "undefined" && typeof Backbone !== "undefined") { + var oldPageableCollection = Backbone.PageableCollection; + var PageableCollection = Backbone.PageableCollection = factory(_, Backbone); + + /** + __BROWSER ONLY__ + + If you already have an object named `PageableCollection` attached to the + `Backbone` module, you can use this to return a local reference to this + Backbone.PageableCollection class and reset the name + Backbone.PageableCollection to its previous definition. + + // The left hand side gives you a reference to this + // Backbone.PageableCollection implementation, the right hand side + // resets Backbone.PageableCollection to your other + // Backbone.PageableCollection. + var PageableCollection = Backbone.PageableCollection.noConflict(); + + @static + @member Backbone.PageableCollection + @return {Backbone.PageableCollection} + */ + Backbone.PageableCollection.noConflict = function () { + Backbone.PageableCollection = oldPageableCollection; + return PageableCollection; + }; + } + +}(function (_, Backbone) { + + "use strict"; + + var _extend = _.extend; + var _omit = _.omit; + var _clone = _.clone; + var _each = _.each; + var _pick = _.pick; + var _contains = _.contains; + var _isEmpty = _.isEmpty; + var _pairs = _.pairs; + var _invert = _.invert; + var _isArray = _.isArray; + var _isFunction = _.isFunction; + var _isObject = _.isObject; + var _keys = _.keys; + var _isUndefined = _.isUndefined; + var _result = _.result; + var ceil = Math.ceil; + var max = Math.max; + + var BBColProto = Backbone.Collection.prototype; + + function finiteInt (val, name) { + val *= 1; + if (!_.isNumber(val) || _.isNaN(val) || !_.isFinite(val) || ~~val !== val) { + throw new TypeError("`" + name + "` must be a finite integer"); + } + return val; + } + + function queryStringToParams (qs) { + var kvp, k, v, ls, params = {}, decode = decodeURIComponent; + var kvps = qs.split('&'); + for (var i = 0, l = kvps.length; i < l; i++) { + var param = kvps[i]; + kvp = param.split('='), k = kvp[0], v = kvp[1] || true; + k = decode(k), ls = params[k]; + if (_isArray(ls)) ls.push(v); + else if (ls) params[k] = [ls, v]; + else params[k] = v; + } + return params; + } + + // Quickly reset a collection by temporarily detaching the comparator of the + // given collection, reset and then attach the comparator back to the + // collection and sort. + + // @param {Backbone.Collection} collection + // @param {...*} resetArgs + // @return {Backbone.Collection} collection The same collection instance after + // reset. + function resetQuickly () { + + var collection = arguments[0]; + var resetArgs = _.toArray(arguments).slice(1); + + var comparator = collection.comparator; + collection.comparator = null; + + try { + collection.reset.apply(collection, resetArgs); + } + finally { + collection.comparator = comparator; + if (comparator) collection.sort(); + } + + return collection; + } + + var PARAM_TRIM_RE = /[\s'"]/g; + var URL_TRIM_RE = /[<>\s'"]/g; + + /** + Drop-in replacement for Backbone.Collection. Supports server-side and + client-side pagination and sorting. Client-side mode also support fully + multi-directional synchronization of changes between pages. + + @class Backbone.PageableCollection + @extends Backbone.Collection + */ + var PageableCollection = Backbone.Collection.extend({ + + /** + The container object to store all pagination states. + + You can override the default state by extending this class or specifying + them in an `options` hash to the constructor. + + @property {Object} state + + @property {0|1} [state.firstPage=1] The first page index. Set to 0 if + your server API uses 0-based indices. You should only override this value + during extension, initialization or reset by the server after + fetching. This value should be read only at other times. + + @property {number} [state.lastPage=null] The last page index. This value + is __read only__ and it's calculated based on whether `firstPage` is 0 or + 1, during bootstrapping, fetching and resetting. Please don't change this + value under any circumstances. + + @property {number} [state.currentPage=null] The current page index. You + should only override this value during extension, initialization or reset + by the server after fetching. This value should be read only at other + times. Can be a 0-based or 1-based index, depending on whether + `firstPage` is 0 or 1. If left as default, it will be set to `firstPage` + on initialization. + + @property {number} [state.pageSize=25] How many records to show per + page. This value is __read only__ after initialization, if you want to + change the page size after initialization, you must call #setPageSize. + + @property {number} [state.totalPages=null] How many pages there are. This + value is __read only__ and it is calculated from `totalRecords`. + + @property {number} [state.totalRecords=null] How many records there + are. This value is __required__ under server mode. This value is optional + for client mode as the number will be the same as the number of models + during bootstrapping and during fetching, either supplied by the server + in the metadata, or calculated from the size of the response. + + @property {string} [state.sortKey=null] The model attribute to use for + sorting. + + @property {-1|0|1} [state.order=-1] The order to use for sorting. Specify + -1 for ascending order or 1 for descending order. If 0, no client side + sorting will be done and the order query parameter will not be sent to + the server during a fetch. + */ + state: { + firstPage: 1, + lastPage: null, + currentPage: null, + pageSize: 25, + totalPages: null, + totalRecords: null, + sortKey: null, + order: -1 + }, + + /** + @property {"server"|"client"|"infinite"} [mode="server"] The mode of + operations for this collection. `"server"` paginates on the server-side, + `"client"` paginates on the client-side and `"infinite"` paginates on the + server-side for APIs that do not support `totalRecords`. + */ + mode: "server", + + /** + A translation map to convert Backbone.PageableCollection state attributes + to the query parameters accepted by your server API. + + You can override the default state by extending this class or specifying + them in `options.queryParams` object hash to the constructor. + + @property {Object} queryParams + @property {string} [queryParams.currentPage="page"] + @property {string} [queryParams.pageSize="per_page"] + @property {string} [queryParams.totalPages="total_pages"] + @property {string} [queryParams.totalRecords="total_entries"] + @property {string} [queryParams.sortKey="sort_by"] + @property {string} [queryParams.order="order"] + @property {string} [queryParams.directions={"-1": "asc", "1": "desc"}] A + map for translating a Backbone.PageableCollection#state.order constant to + the ones your server API accepts. + */ + queryParams: { + currentPage: "page", + pageSize: "per_page", + totalPages: "total_pages", + totalRecords: "total_entries", + sortKey: "sort_by", + order: "order", + directions: { + "-1": "asc", + "1": "desc" + } + }, + + /** + __CLIENT MODE ONLY__ + + This collection is the internal storage for the bootstrapped or fetched + models. You can use this if you want to operate on all the pages. + + @property {Backbone.Collection} fullCollection + */ + + /** + Given a list of models or model attributues, bootstraps the full + collection in client mode or infinite mode, or just the page you want in + server mode. + + If you want to initialize a collection to a different state than the + default, you can specify them in `options.state`. Any state parameters + supplied will be merged with the default. If you want to change the + default mapping from #state keys to your server API's query parameter + names, you can specifiy an object hash in `option.queryParams`. Likewise, + any mapping provided will be merged with the default. Lastly, all + Backbone.Collection constructor options are also accepted. + + See: + + - Backbone.PageableCollection#state + - Backbone.PageableCollection#queryParams + - [Backbone.Collection#initialize](http://backbonejs.org/#Collection-constructor) + + @param {Array.} [models] + + @param {Object} [options] + + @param {function(*, *): number} [options.comparator] If specified, this + comparator is set to the current page under server mode, or the + #fullCollection otherwise. + + @param {boolean} [options.full] If `false` and either a + `options.comparator` or `sortKey` is defined, the comparator is attached + to the current page. Default is `true` under client or infinite mode and + the comparator will be attached to the #fullCollection. + + @param {Object} [options.state] The state attributes overriding the defaults. + + @param {string} [options.state.sortKey] The model attribute to use for + sorting. If specified instead of `options.comparator`, a comparator will + be automatically created using this value, and optionally a sorting order + specified in `options.state.order`. The comparator is then attached to + the new collection instance. + + @param {-1|1} [options.state.order] The order to use for sorting. Specify + -1 for ascending order and 1 for descending order. + + @param {Object} [options.queryParam] + */ + initialize: function (models, options) { + + options = options || {}; + + var mode = this.mode = options.mode || this.mode || PageableProto.mode; + + var queryParams = _extend({}, PageableProto.queryParams, this.queryParams, + options.queryParams || {}); + + queryParams.directions = _extend({}, + PageableProto.queryParams.directions, + this.queryParams.directions, + queryParams.directions || {}); + + this.queryParams = queryParams; + + var state = this.state = _extend({}, PageableProto.state, this.state, + options.state || {}); + + state.currentPage = state.currentPage == null ? + state.firstPage : + state.currentPage; + + if (mode != "server" && state.totalRecords == null && !_isEmpty(models)) { + state.totalRecords = models.length; + } + + this.switchMode(mode, _extend({fetch: false, + resetState: false, + models: models}, options)); + + var comparator = options.comparator; + + if (state.sortKey && !comparator) { + this.setSorting(state.sortKey, state.order, options); + } + + if (mode != "server") { + + if (comparator && options.full) { + delete this.comparator; + var fullCollection = this.fullCollection; + fullCollection.comparator = comparator; + fullCollection.sort(); + } + + // make sure the models in the current page and full collection have the + // same references + if (models && !_isEmpty(models)) { + this.getPage(state.currentPage); + models.splice.apply(models, [0, models.length].concat(this.models)); + } + } + + this._initState = _clone(this.state); + }, + + /** + Makes a Backbone.Collection that contains all the pages. + + @private + @param {Array.} models + @param {Object} options Options for Backbone.Collection constructor. + @return {Backbone.Collection} + */ + _makeFullCollection: function (models, options) { + + var properties = ["url", "model", "sync", "comparator"]; + var thisProto = this.constructor.prototype; + var i, length, prop; + + var proto = {}; + for (i = 0, length = properties.length; i < length; i++) { + prop = properties[i]; + if (!_isUndefined(thisProto[prop])) { + proto[prop] = thisProto[prop]; + } + } + + var fullCollection = new (Backbone.Collection.extend(proto))(models, options); + + for (i = 0, length = properties.length; i < length; i++) { + prop = properties[i]; + if (this[prop] !== thisProto[prop]) { + fullCollection[prop] = this[prop]; + } + } + + return fullCollection; + }, + + /** + Factory method that returns a Backbone event handler that responses to + the `add`, `remove`, `reset`, and the `sort` events. The returned event + handler will synchronize the current page collection and the full + collection's models. + + @private + + @param {Backbone.PageableCollection} pageCol + @param {Backbone.Collection} fullCol + + @return {function(string, Backbone.Model, Backbone.Collection, Object)} + Collection event handler + */ + _makeCollectionEventHandler: function (pageCol, fullCol) { + + return function collectionEventHandler (event, model, collection, options) { + var handlers = pageCol._handlers; + _each(_keys(handlers), function (event) { + var handler = handlers[event]; + pageCol.off(event, handler); + fullCol.off(event, handler); + }); + + var state = _clone(pageCol.state); + var firstPage = state.firstPage; + var currentPage = firstPage === 0 ? + state.currentPage : + state.currentPage - 1; + var pageSize = state.pageSize; + var pageStart = currentPage * pageSize, pageEnd = pageStart + pageSize; + + if (event == "add") { + var pageIndex, fullIndex, addAt, colToAdd, options = options || {}; + if (collection == fullCol) { + fullIndex = fullCol.indexOf(model); + if (fullIndex >= pageStart && fullIndex < pageEnd) { + colToAdd = pageCol; + pageIndex = addAt = fullIndex - pageStart; + } + } + else { + pageIndex = pageCol.indexOf(model); + fullIndex = pageStart + pageIndex; + colToAdd = fullCol; + var addAt = !_isUndefined(options.at) ? + options.at + pageStart : + fullIndex; + } + + ++state.totalRecords; + pageCol.state = pageCol._checkState(state); + + if (colToAdd) { + colToAdd.add(model, _extend({}, options || {}, {at: addAt})); + var modelToRemove = pageIndex >= pageSize ? + model : + !_isUndefined(options.at) && addAt < pageEnd && pageCol.length > pageSize ? + pageCol.at(pageSize) : + null; + if (modelToRemove) { + var addHandlers = collection._events.add || [], + popOptions = {onAdd: true}; + if (addHandlers.length) { + var lastAddHandler = addHandlers[addHandlers.length - 1]; + var oldCallback = lastAddHandler.callback; + lastAddHandler.callback = function () { + try { + oldCallback.apply(this, arguments); + pageCol.remove(modelToRemove, popOptions); + } + finally { + lastAddHandler.callback = oldCallback; + } + }; + } + else pageCol.remove(modelToRemove, popOptions); + } + } + } + + // remove the model from the other collection as well + if (event == "remove") { + if (!options.onAdd) { + // decrement totalRecords and update totalPages and lastPage + if (!--state.totalRecords) { + state.totalRecords = null; + state.totalPages = null; + } + else { + var totalPages = state.totalPages = ceil(state.totalRecords / pageSize); + state.lastPage = firstPage === 0 ? totalPages - 1 : totalPages; + if (state.currentPage > totalPages) state.currentPage = state.lastPage; + } + pageCol.state = pageCol._checkState(state); + + var nextModel, removedIndex = options.index; + if (collection == pageCol) { + if (nextModel = fullCol.at(pageEnd)) pageCol.push(nextModel); + fullCol.remove(model); + } + else if (removedIndex >= pageStart && removedIndex < pageEnd) { + pageCol.remove(model); + nextModel = fullCol.at(currentPage * (pageSize + removedIndex)); + if (nextModel) pageCol.push(nextModel); + } + } + else delete options.onAdd; + } + + if (event == "reset" || event == "sort") { + options = collection; + collection = model; + + if (collection == pageCol && event == "reset") { + var head = fullCol.models.slice(0, pageStart); + var tail = fullCol.models.slice(pageStart + pageCol.models.length); + options = _extend(options, {silent: true}); + resetQuickly(fullCol, head.concat(pageCol.models).concat(tail), + options); + } + + if (event == "reset" || collection == fullCol) { + if (!(state.totalRecords = fullCol.models.length)) { + state.totalRecords = null; + state.totalPages = null; + state.lastPage = state.currentPage = state.firstPage; + } + pageCol.state = pageCol._checkState(state); + if (collection == pageCol) fullCol.trigger(event, fullCol, options); + resetQuickly(pageCol, fullCol.models.slice(pageStart, pageEnd), + options); + } + } + + _each(_keys(handlers), function (event) { + var handler = handlers[event]; + _each([pageCol, fullCol], function (col) { + col.on(event, handler); + var callbacks = col._events[event] || []; + callbacks.unshift(callbacks.pop()); + }); + }); + }; + }, + + /** + Sanity check this collection's pagination states. Only perform checks + when all the required pagination state values are defined and not null. + If `totalPages` is undefined or null, it is set to `totalRecords` / + `pageSize`. `lastPage` is set according to whether `firstPage` is 0 or 1 + when no error occurs. + + @private + + @throws {TypeError} If `totalRecords`, `pageSize`, `currentPage` or + `firstPage` is not a finite integer. + + @throws {RangeError} If `pageSize`, `currentPage` or `firstPage` is out + of bounds. + + @return {Object} Returns the `state` object if no error was found. + */ + _checkState: function (state) { + + var mode = this.mode; + var links = this.links; + var totalRecords = state.totalRecords; + var pageSize = state.pageSize; + var currentPage = state.currentPage; + var firstPage = state.firstPage; + var totalPages = state.totalPages; + + if (totalRecords != null && pageSize != null && currentPage != null && + firstPage != null && (mode == "infinite" ? links : true)) { + + totalRecords = finiteInt(totalRecords, "totalRecords"); + pageSize = finiteInt(pageSize, "pageSize"); + currentPage = finiteInt(currentPage, "currentPage"); + firstPage = finiteInt(firstPage, "firstPage"); + + if (pageSize < 1) { + throw new RangeError("`pageSize` must be >= 1"); + } + + totalPages = state.totalPages = ceil(totalRecords / pageSize); + + if (firstPage < 0 || firstPage > 1) { + throw new RangeError("`firstPage must be 0 or 1`"); + } + + state.lastPage = firstPage === 0 ? max(0, totalPages - 1) : totalPages; + + if (mode == "infinite") { + if (!links[currentPage + '']) { + throw new RangeError("No link found for page " + currentPage); + } + } + else { + if (firstPage === 0 && (currentPage < firstPage || (currentPage >= totalPages && totalPages > 0))) { + throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got " + currentPage + '.'); + } + else if (firstPage === 1 && (currentPage < firstPage || currentPage > totalPages)) { + throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got " + currentPage + '.'); + } + } + } + + return state; + }, + + /** + Change the page size of this collection. + + For server mode operations, changing the page size will trigger a #fetch + and subsequently a `reset` event. + + For client mode operations, changing the page size will `reset` the + current page by recalculating the current page boundary on the client + side. + + If `options.fetch` is true, a fetch can be forced if the collection is in + client mode. + + @param {number} pageSize The new page size to set to #state. + @param {Object} [options] {@link #fetch} options. + @param {boolean} [options.fetch] If `true`, force a fetch in client mode. + + @throws {TypeError} If `pageSize` is not a finite integer. + @throws {RangeError} If `pageSize` is less than 1. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + setPageSize: function (pageSize, options) { + pageSize = finiteInt(pageSize, "pageSize"); + + options = options || {}; + + this.state = this._checkState(_extend({}, this.state, { + pageSize: pageSize, + totalPages: ceil(this.state.totalRecords / pageSize) + })); + + return this.getPage(this.state.currentPage, options); + }, + + /** + Switching between client, server and infinite mode. + + If switching from client to server mode, the #fullCollection is emptied + first and then deleted and a fetch is immediately issued for the current + page from the server. Pass `false` to `options.fetch` to skip fetching. + + If switching to infinite mode, and if `options.models` is given for an + array of models, #links will be populated with a URL per page, using the + default URL for this collection. + + If switching from server to client mode, all of the pages are immediately + refetched. If you have too many pages, you can pass `false` to + `options.fetch` to skip fetching. + + If switching to any mode from infinite mode, the #links will be deleted. + + @param {"server"|"client"|"infinite"} [mode] The mode to switch to. + + @param {Object} [options] + + @param {boolean} [options.fetch=true] If `false`, no fetching is done. + + @param {boolean} [options.resetState=true] If 'false', the state is not + reset, but checked for sanity instead. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this if `options.fetch` is `false`. + */ + switchMode: function (mode, options) { + + if (!_contains(["server", "client", "infinite"], mode)) { + throw new TypeError('`mode` must be one of "server", "client" or "infinite"'); + } + + options = options || {fetch: true, resetState: true}; + + var state = this.state = options.resetState ? + _clone(this._initState) : + this._checkState(_extend({}, this.state)); + + this.mode = mode; + + var self = this; + var fullCollection = this.fullCollection; + var handlers = this._handlers = this._handlers || {}, handler; + if (mode != "server" && !fullCollection) { + fullCollection = this._makeFullCollection(options.models || []); + fullCollection.pageableCollection = this; + this.fullCollection = fullCollection; + var allHandler = this._makeCollectionEventHandler(this, fullCollection); + _each(["add", "remove", "reset", "sort"], function (event) { + handlers[event] = handler = _.bind(allHandler, {}, event); + self.on(event, handler); + fullCollection.on(event, handler); + }); + fullCollection.comparator = this._fullComparator; + } + else if (mode == "server" && fullCollection) { + _each(_keys(handlers), function (event) { + handler = handlers[event]; + self.off(event, handler); + fullCollection.off(event, handler); + }); + delete this._handlers; + this._fullComparator = fullCollection.comparator; + delete this.fullCollection; + } + + if (mode == "infinite") { + var links = this.links = {}; + var firstPage = state.firstPage; + var totalPages = ceil(state.totalRecords / state.pageSize); + var lastPage = firstPage === 0 ? max(0, totalPages - 1) : totalPages || firstPage; + for (var i = state.firstPage; i <= lastPage; i++) { + links[i] = this.url; + } + } + else if (this.links) delete this.links; + + return options.fetch ? + this.fetch(_omit(options, "fetch", "resetState")) : + this; + }, + + /** + @return {boolean} `true` if this collection can page backward, `false` + otherwise. + */ + hasPrevious: function () { + var state = this.state; + var currentPage = state.currentPage; + if (this.mode != "infinite") return currentPage > state.firstPage; + return !!this.links[currentPage - 1]; + }, + + /** + @return {boolean} `true` if this collection can page forward, `false` + otherwise. + */ + hasNext: function () { + var state = this.state; + var currentPage = this.state.currentPage; + if (this.mode != "infinite") return currentPage < state.lastPage; + return !!this.links[currentPage + 1]; + }, + + /** + Fetch the first page in server mode, or reset the current page of this + collection to the first page in client or infinite mode. + + @param {Object} options {@link #getPage} options. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + getFirstPage: function (options) { + return this.getPage("first", options); + }, + + /** + Fetch the previous page in server mode, or reset the current page of this + collection to the previous page in client or infinite mode. + + @param {Object} options {@link #getPage} options. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + getPreviousPage: function (options) { + return this.getPage("prev", options); + }, + + /** + Fetch the next page in server mode, or reset the current page of this + collection to the next page in client mode. + + @param {Object} options {@link #getPage} options. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + getNextPage: function (options) { + return this.getPage("next", options); + }, + + /** + Fetch the last page in server mode, or reset the current page of this + collection to the last page in client mode. + + @param {Object} options {@link #getPage} options. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + getLastPage: function (options) { + return this.getPage("last", options); + }, + + /** + Given a page index, set #state.currentPage to that index. If this + collection is in server mode, fetch the page using the updated state, + otherwise, reset the current page of this collection to the page + specified by `index` in client mode. If `options.fetch` is true, a fetch + can be forced in client mode before resetting the current page. Under + infinite mode, if the index is less than the current page, a reset is + done as in client mode. If the index is greater than the current page + number, a fetch is made with the results **appended** to + #fullCollection. The current page will then be reset after fetching. + + @param {number|string} index The page index to go to, or the page name to + look up from #links in infinite mode. + @param {Object} [options] {@link #fetch} options or + [reset](http://backbonejs.org/#Collection-reset) options for client mode + when `options.fetch` is `false`. + @param {boolean} [options.fetch=false] If true, force a {@link #fetch} in + client mode. + + @throws {TypeError} If `index` is not a finite integer under server or + client mode, or does not yield a URL from #links under infinite mode. + + @throws {RangeError} If `index` is out of bounds. + + @chainable + @return {XMLHttpRequest|Backbone.PageableCollection} The XMLHttpRequest + from fetch or this. + */ + getPage: function (index, options) { + + var mode = this.mode, fullCollection = this.fullCollection; + + options = options || {fetch: false}; + + var state = this.state, + firstPage = state.firstPage, + currentPage = state.currentPage, + lastPage = state.lastPage, + pageSize = state.pageSize; + + var pageNum = index; + switch (index) { + case "first": pageNum = firstPage; break; + case "prev": pageNum = currentPage - 1; break; + case "next": pageNum = currentPage + 1; break; + case "last": pageNum = lastPage; break; + default: pageNum = finiteInt(index, "index"); + } + + this.state = this._checkState(_extend({}, state, {currentPage: pageNum})); + + var pageStart = (firstPage === 0 ? pageNum : pageNum - 1) * pageSize; + var pageModels = fullCollection && fullCollection.length ? + fullCollection.models.slice(pageStart, pageStart + pageSize) : + []; + if ((mode == "client" || (mode == "infinite" && !_isEmpty(pageModels))) && + !options.fetch) { + return resetQuickly(this, pageModels, _omit(options, "fetch")); + } + + if (mode == "infinite") options.url = this.links[pageNum]; + + return this.fetch(_omit(options, "fetch")); + }, + + /** + Overidden to make `getPage` compatible with Zepto. + + @param {string} method + @param {Backbone.Model|Backbone.Collection} model + @param {Object} [options] + + @return {XMLHttpRequest} + */ + sync: function (method, model, options) { + var self = this; + if (self.mode == "infinite") { + var success = options.success; + var currentPage = self.state.currentPage; + options.success = function (resp, status, xhr) { + var links = self.links; + var newLinks = self.parseLinks(resp, _extend({xhr: xhr}, options)); + if (newLinks.first) links[self.state.firstPage] = newLinks.first; + if (newLinks.prev) links[currentPage - 1] = newLinks.prev; + if (newLinks.next) links[currentPage + 1] = newLinks.next; + if (success) success(resp, status, xhr); + }; + } + + return (BBColProto.sync || Backbone.sync).call(self, method, model, options); + }, + + /** + Parse pagination links from the server response. Only valid under + infinite mode. + + Given a response body and a XMLHttpRequest object, extract pagination + links from them for infinite paging. + + This default implementation parses the RFC 5988 `Link` header and extract + 3 links from it - `first`, `prev`, `next`. If a `previous` link is found, + it will be found in the `prev` key in the returned object hash. Any + subclasses overriding this method __must__ return an object hash having + only the keys above. If `first` is missing, the collection's default URL + is assumed to be the `first` URL. If `prev` or `next` is missing, it is + assumed to be `null`. An empty object hash must be returned if there are + no links found. If either the response or the header contains information + pertaining to the total number of records on the server, + #state.totalRecords must be set to that number. The default + implementation uses the `last` link from the header to calculate it. + + @param {*} resp The deserialized response body. + @param {Object} [options] + @param {XMLHttpRequest} [options.xhr] The XMLHttpRequest object for this + response. + @return {Object} + */ + parseLinks: function (resp, options) { + var links = {}; + var linkHeader = options.xhr.getResponseHeader("Link"); + if (linkHeader) { + var relations = ["first", "prev", "previous", "next", "last"]; + _each(linkHeader.split(","), function (linkValue) { + var linkParts = linkValue.split(";"); + var url = linkParts[0].replace(URL_TRIM_RE, ''); + var params = linkParts.slice(1); + _each(params, function (param) { + var paramParts = param.split("="); + var key = paramParts[0].replace(PARAM_TRIM_RE, ''); + var value = paramParts[1].replace(PARAM_TRIM_RE, ''); + if (key == "rel" && _contains(relations, value)) { + if (value == "previous") links.prev = url; + else links[value] = url; + } + }); + }); + + var last = links.last || '', qsi, qs; + if (qs = (qsi = last.indexOf('?')) ? last.slice(qsi + 1) : '') { + var params = queryStringToParams(qs); + + var state = _clone(this.state); + var queryParams = this.queryParams; + var pageSize = state.pageSize; + + var totalRecords = params[queryParams.totalRecords] * 1; + var pageNum = params[queryParams.currentPage] * 1; + var totalPages = params[queryParams.totalPages]; + + if (!totalRecords) { + if (pageNum) totalRecords = (state.firstPage === 0 ? + pageNum + 1 : + pageNum) * pageSize; + else if (totalPages) totalRecords = totalPages * pageSize; + } + + if (totalRecords) state.totalRecords = totalRecords; + + this.state = this._checkState(state); + } + } + + delete links.last; + + return links; + }, + + /** + Parse server response data. + + This default implementation assumes the response data is in one of two + structures: + + [ + {}, // Your new pagination state + [{}, ...] // An array of JSON objects + ] + + Or, + + [{}] // An array of JSON objects + + The first structure is the preferred form because the pagination states + may have been updated on the server side, sending them down again allows + this collection to update its states. If the response has a pagination + state object, it is checked for errors. + + The second structure is the + [Backbone.Collection#parse](http://backbonejs.org/#Collection-parse) + default. + + **Note:** this method has been further simplified since 1.1.7. While + existing #parse implementations will continue to work, new code is + encouraged to override #parseState and #parseRecords instead. + + @param {Object} resp The deserialized response data from the server. + + @return {Array.} An array of model objects + */ + parse: function (resp) { + var newState = this.parseState(resp, _clone(this.queryParams), _clone(this.state)); + if (newState) this.state = this._checkState(_extend({}, this.state, newState)); + return this.parseRecords(resp); + }, + + /** + Parse server response for server pagination state updates. + + This default implementation first checks whether the response has any + state object as documented in #parse. If it exists, a state object is + returned by mapping the server state keys to this pageable collection + instance's query parameter keys using `queryParams`. + + It is __NOT__ neccessary to return a full state object complete with all + the mappings defined in #queryParams. Any state object resulted is merged + with a copy of the current pageable collection state and checked for + sanity before actually updating. Most of the time, simply providing a new + `totalRecords` value is enough to trigger a full pagination state + recalculation. + + parseState: function (resp, queryParams, state) { + return {totalRecords: resp.total_entries}; + } + + __Note__: `totalRecords` cannot be set to 0 for compatibility reasons, + use `null` instead of 0 for all cases where you would like to set it to + 0. You can do this either on the server-side or in your overridden #parseState + method. + + This method __MUST__ return a new state object instead of directly + modifying the #state object. The behavior of directly modifying #state is + undefined. + + @param {Object} resp The deserialized response data from the server. + @param {Object} queryParams A copy of #queryParams. + @param {Object} state A copy of #state. + + @return {Object} A new (partial) state object. + */ + parseState: function (resp, queryParams, state) { + if (resp && resp.length === 2 && _isObject(resp[0]) && _isArray(resp[1])) { + + var newState = _clone(state); + var serverState = resp[0]; + + _each(_pairs(_omit(queryParams, "directions")), function (kvp) { + var k = kvp[0], v = kvp[1]; + var serverVal = serverState[v]; + if (!_isUndefined(serverVal) && !_.isNull(serverVal)) newState[k] = serverState[v]; + }); + + if (serverState.order) { + newState.order = _invert(queryParams.directions)[serverState.order] * 1; + } + + return newState; + } + }, + + /** + Parse server response for an array of model objects. + + This default implementation first checks whether the response has any + state object as documented in #parse. If it exists, the array of model + objects is assumed to be the second element, otherwise the entire + response is returned directly. + + @param {Object} resp The deserialized response data from the server. + + @return {Array.} An array of model objects + */ + parseRecords: function (resp) { + if (resp && resp.length === 2 && _isObject(resp[0]) && _isArray(resp[1])) { + return resp[1]; + } + + return resp; + }, + + /** + Fetch a page from the server in server mode, or all the pages in client + mode. Under infinite mode, the current page is refetched by default and + then reset. + + The query string is constructed by translating the current pagination + state to your server API query parameter using #queryParams. The current + page will reset after fetch. + + @param {Object} [options] Accepts all + [Backbone.Collection#fetch](http://backbonejs.org/#Collection-fetch) + options. + + @return {XMLHttpRequest} + */ + fetch: function (options) { + + options = options || {}; + + var state = this._checkState(this.state); + + var mode = this.mode; + + if (mode == "infinite" && !options.url) { + options.url = this.links[state.currentPage]; + } + + var data = options.data || {}; + + // dedup query params + var url = _result(options, "url") || _result(this, "url") || ''; + var qsi = url.indexOf('?'); + if (qsi != -1) { + _extend(data, queryStringToParams(url.slice(qsi + 1))); + url = url.slice(0, qsi); + } + + options.url = url; + options.data = data; + + // map params except directions + var queryParams = this.mode == "client" ? + _pick(this.queryParams, "sortKey", "order") : + _omit(_pick(this.queryParams, _keys(PageableProto.queryParams)), + "directions"); + + var i, kvp, k, v, kvps = _pairs(queryParams), thisCopy = _clone(this); + for (i = 0; i < kvps.length; i++) { + kvp = kvps[i], k = kvp[0], v = kvp[1]; + v = _isFunction(v) ? v.call(thisCopy) : v; + if (state[k] != null && v != null) { + data[v] = state[k]; + } + } + + // fix up sorting parameters + if (state.sortKey && state.order) { + data[queryParams.order] = this.queryParams.directions[state.order + ""]; + } + else if (!state.sortKey) delete data[queryParams.order]; + + // map extra query parameters + var extraKvps = _pairs(_omit(this.queryParams, + _keys(PageableProto.queryParams))); + for (i = 0; i < extraKvps.length; i++) { + kvp = extraKvps[i]; + v = kvp[1]; + v = _isFunction(v) ? v.call(thisCopy) : v; + data[kvp[0]] = v; + } + + var fullCollection = this.fullCollection, links = this.links; + + if (mode != "server") { + + var self = this; + var success = options.success; + options.success = function (col, resp, opts) { + + // make sure the caller's intent is obeyed + opts = opts || {}; + if (_isUndefined(options.silent)) delete opts.silent; + else opts.silent = options.silent; + + var models = col.models; + var currentPage = state.currentPage; + + if (mode == "client") resetQuickly(fullCollection, models, opts); + else if (links[currentPage]) { // refetching a page + var pageSize = state.pageSize; + var pageStart = (state.firstPage === 0 ? + currentPage : + currentPage - 1) * pageSize; + var fullModels = fullCollection.models; + var head = fullModels.slice(0, pageStart); + var tail = fullModels.slice(pageStart + pageSize); + fullModels = head.concat(models).concat(tail); + var updateFunc = fullCollection.set || fullCollection.update; + updateFunc.call(fullCollection, fullModels, + _extend({silent: true, sort: false}, opts)); + if (fullCollection.comparator) fullCollection.sort(); + fullCollection.trigger("reset", fullCollection, opts); + } + else { // fetching new page + fullCollection.add(models, _extend({at: fullCollection.length, + silent: true}, opts)); + fullCollection.trigger("reset", fullCollection, opts); + } + + if (success) success(col, resp, opts); + }; + + // silent the first reset from backbone + return BBColProto.fetch.call(self, _extend({}, options, {silent: true})); + } + + return BBColProto.fetch.call(this, options); + }, + + /** + Convenient method for making a `comparator` sorted by a model attribute + identified by `sortKey` and ordered by `order`. + + Like a Backbone.Collection, a Backbone.PageableCollection will maintain + the __current page__ in sorted order on the client side if a `comparator` + is attached to it. If the collection is in client mode, you can attach a + comparator to #fullCollection to have all the pages reflect the global + sorting order by specifying an option `full` to `true`. You __must__ call + `sort` manually or #fullCollection.sort after calling this method to + force a resort. + + While you can use this method to sort the current page in server mode, + the sorting order may not reflect the global sorting order due to the + additions or removals of the records on the server since the last + fetch. If you want the most updated page in a global sorting order, it is + recommended that you set #state.sortKey and optionally #state.order, and + then call #fetch. + + @protected + + @param {string} [sortKey=this.state.sortKey] See `state.sortKey`. + @param {number} [order=this.state.order] See `state.order`. + + See [Backbone.Collection.comparator](http://backbonejs.org/#Collection-comparator). + */ + _makeComparator: function (sortKey, order) { + + var state = this.state; + + sortKey = sortKey || state.sortKey; + order = order || state.order; + + if (!sortKey || !order) return; + + return function (left, right) { + var l = left.get(sortKey), r = right.get(sortKey), t; + if (order === 1) t = l, l = r, r = t; + if (l === r) return 0; + else if (l < r) return -1; + return 1; + }; + }, + + /** + Adjusts the sorting for this pageable collection. + + Given a `sortKey` and an `order`, sets `state.sortKey` and + `state.order`. A comparator can be applied on the client side to sort in + the order defined if `options.side` is `"client"`. By default the + comparator is applied to the #fullCollection. Set `options.full` to + `false` to apply a comparator to the current page under any mode. Setting + `sortKey` to `null` removes the comparator from both the current page and + the full collection. + + @chainable + + @param {string} sortKey See `state.sortKey`. + @param {number} [order=this.state.order] See `state.order`. + @param {Object} [options] + @param {"server"|"client"} [options.side] By default, `"client"` if + `mode` is `"client"`, `"server"` otherwise. + @param {boolean} [options.full=true] + */ + setSorting: function (sortKey, order, options) { + + var state = this.state; + + state.sortKey = sortKey; + state.order = order = order || state.order; + + var fullCollection = this.fullCollection; + + var delComp = false, delFullComp = false; + + if (!sortKey) delComp = delFullComp = true; + + var mode = this.mode; + options = _extend({side: mode == "client" ? mode : "server", full: true}, + options); + + var comparator = this._makeComparator(sortKey, order); + + var full = options.full, side = options.side; + + if (side == "client") { + if (full) { + if (fullCollection) fullCollection.comparator = comparator; + delComp = true; + } + else { + this.comparator = comparator; + delFullComp = true; + } + } + else if (side == "server" && !full) { + this.comparator = comparator; + } + + if (delComp) delete this.comparator; + if (delFullComp && fullCollection) delete fullCollection.comparator; + + return this; + } + + }); + + var PageableProto = PageableCollection.prototype; + + return PageableCollection; + +})); diff --git a/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.min.js b/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.min.js new file mode 100644 index 000000000..e05c6db48 --- /dev/null +++ b/ajax/libs/backbone-pageable/1.2.1/backbone-pageable.min.js @@ -0,0 +1,8 @@ +/* + backbone-pageable 1.2.1 + http://github.com/wyuenho/backbone-pageable + + Copyright (c) 2013 Jimmy Yuen Ho Wong + Licensed under the MIT @license. +*/ +(function(e){if("object"==typeof exports)module.exports=e(require("underscore"),require("backbone"));else if("function"==typeof define&&define.amd)define(["underscore","backbone"],e);else if("undefined"!=typeof _&&"undefined"!=typeof Backbone){var t=Backbone.PageableCollection,r=Backbone.PageableCollection=e(_,Backbone);Backbone.PageableCollection.noConflict=function(){return Backbone.PageableCollection=t,r}}})(function(e,t){"use strict";function r(t,r){if(t*=1,!e.isNumber(t)||e.isNaN(t)||!e.isFinite(t)||~~t!==t)throw new TypeError("`"+r+"` must be a finite integer");return t}function a(e){for(var t,r,a,s,i={},n=decodeURIComponent,o=e.split("&"),l=0,c=o.length;c>l;l++){var u=o[l];t=u.split("="),r=t[0],a=t[1]||!0,r=n(r),s=i[r],d(s)?s.push(a):i[r]=s?[s,a]:a}return i}function s(){var t=arguments[0],r=e.toArray(arguments).slice(1),a=t.comparator;t.comparator=null;try{t.reset.apply(t,r)}finally{t.comparator=a,a&&t.sort()}return t}var i=e.extend,n=e.omit,o=e.clone,l=e.each,c=e.pick,u=e.contains,f=e.isEmpty,h=e.pairs,g=e.invert,d=e.isArray,v=e.isFunction,P=e.isObject,p=e.keys,m=e.isUndefined,y=e.result,k=Math.ceil,b=Math.max,S=t.Collection.prototype,_=/[\s'"]/g,C=/[<>\s'"]/g,R=t.Collection.extend({state:{firstPage:1,lastPage:null,currentPage:null,pageSize:25,totalPages:null,totalRecords:null,sortKey:null,order:-1},mode:"server",queryParams:{currentPage:"page",pageSize:"per_page",totalPages:"total_pages",totalRecords:"total_entries",sortKey:"sort_by",order:"order",directions:{"-1":"asc",1:"desc"}},initialize:function(e,t){t=t||{};var r=this.mode=t.mode||this.mode||x.mode,a=i({},x.queryParams,this.queryParams,t.queryParams||{});a.directions=i({},x.queryParams.directions,this.queryParams.directions,a.directions||{}),this.queryParams=a;var s=this.state=i({},x.state,this.state,t.state||{});s.currentPage=null==s.currentPage?s.firstPage:s.currentPage,"server"==r||null!=s.totalRecords||f(e)||(s.totalRecords=e.length),this.switchMode(r,i({fetch:!1,resetState:!1,models:e},t));var n=t.comparator;if(s.sortKey&&!n&&this.setSorting(s.sortKey,s.order,t),"server"!=r){if(n&&t.full){delete this.comparator;var l=this.fullCollection;l.comparator=n,l.sort()}e&&!f(e)&&(this.getPage(s.currentPage),e.splice.apply(e,[0,e.length].concat(this.models)))}this._initState=o(this.state)},_makeFullCollection:function(e,r){var a,s,i,n=["url","model","sync","comparator"],o=this.constructor.prototype,l={};for(a=0,s=n.length;s>a;a++)i=n[a],m(o[i])||(l[i]=o[i]);var c=new(t.Collection.extend(l))(e,r);for(a=0,s=n.length;s>a;a++)i=n[a],this[i]!==o[i]&&(c[i]=this[i]);return c},_makeCollectionEventHandler:function(e,t){return function(r,a,n,c){var u=e._handlers;l(p(u),function(r){var a=u[r];e.off(r,a),t.off(r,a)});var f=o(e.state),h=f.firstPage,g=0===h?f.currentPage:f.currentPage-1,d=f.pageSize,v=g*d,P=v+d;if("add"==r){var y,b,S,_,c=c||{};if(n==t)b=t.indexOf(a),b>=v&&P>b&&(_=e,y=S=b-v);else{y=e.indexOf(a),b=v+y,_=t;var S=m(c.at)?b:c.at+v}if(++f.totalRecords,e.state=e._checkState(f),_){_.add(a,i({},c||{},{at:S}));var C=y>=d?a:!m(c.at)&&P>S&&e.length>d?e.at(d):null;if(C){var R=n._events.add||[],x={onAdd:!0};if(R.length){var w=R[R.length-1],q=w.callback;w.callback=function(){try{q.apply(this,arguments),e.remove(C,x)}finally{w.callback=q}}}else e.remove(C,x)}}}if("remove"==r)if(c.onAdd)delete c.onAdd;else{if(--f.totalRecords){var z=f.totalPages=k(f.totalRecords/d);f.lastPage=0===h?z-1:z,f.currentPage>z&&(f.currentPage=f.lastPage)}else f.totalRecords=null,f.totalPages=null;e.state=e._checkState(f);var E,K=c.index;n==e?((E=t.at(P))&&e.push(E),t.remove(a)):K>=v&&P>K&&(e.remove(a),E=t.at(g*(d+K)),E&&e.push(E))}if("reset"==r||"sort"==r){if(c=n,n=a,n==e&&"reset"==r){var N=t.models.slice(0,v),B=t.models.slice(v+e.models.length);c=i(c,{silent:!0}),s(t,N.concat(e.models).concat(B),c)}("reset"==r||n==t)&&((f.totalRecords=t.models.length)||(f.totalRecords=null,f.totalPages=null,f.lastPage=f.currentPage=f.firstPage),e.state=e._checkState(f),n==e&&t.trigger(r,t,c),s(e,t.models.slice(v,P),c))}l(p(u),function(r){var a=u[r];l([e,t],function(e){e.on(r,a);var t=e._events[r]||[];t.unshift(t.pop())})})}},_checkState:function(e){var t=this.mode,a=this.links,s=e.totalRecords,i=e.pageSize,n=e.currentPage,o=e.firstPage,l=e.totalPages;if(null!=s&&null!=i&&null!=n&&null!=o&&("infinite"==t?a:!0)){if(s=r(s,"totalRecords"),i=r(i,"pageSize"),n=r(n,"currentPage"),o=r(o,"firstPage"),1>i)throw new RangeError("`pageSize` must be >= 1");if(l=e.totalPages=k(s/i),0>o||o>1)throw new RangeError("`firstPage must be 0 or 1`");if(e.lastPage=0===o?b(0,l-1):l,"infinite"==t){if(!a[n+""])throw new RangeError("No link found for page "+n)}else{if(0===o&&(o>n||n>=l&&l>0))throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got "+n+".");if(1===o&&(o>n||n>l))throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got "+n+".")}}return e},setPageSize:function(e,t){return e=r(e,"pageSize"),t=t||{},this.state=this._checkState(i({},this.state,{pageSize:e,totalPages:k(this.state.totalRecords/e)})),this.getPage(this.state.currentPage,t)},switchMode:function(t,r){if(!u(["server","client","infinite"],t))throw new TypeError('`mode` must be one of "server", "client" or "infinite"');r=r||{fetch:!0,resetState:!0};var a=this.state=r.resetState?o(this._initState):this._checkState(i({},this.state));this.mode=t;var s,c=this,f=this.fullCollection,h=this._handlers=this._handlers||{};if("server"==t||f)"server"==t&&f&&(l(p(h),function(e){s=h[e],c.off(e,s),f.off(e,s)}),delete this._handlers,this._fullComparator=f.comparator,delete this.fullCollection);else{f=this._makeFullCollection(r.models||[]),f.pageableCollection=this,this.fullCollection=f;var g=this._makeCollectionEventHandler(this,f);l(["add","remove","reset","sort"],function(t){h[t]=s=e.bind(g,{},t),c.on(t,s),f.on(t,s)}),f.comparator=this._fullComparator}if("infinite"==t)for(var d=this.links={},v=a.firstPage,P=k(a.totalRecords/a.pageSize),m=0===v?b(0,P-1):P||v,y=a.firstPage;m>=y;y++)d[y]=this.url;else this.links&&delete this.links;return r.fetch?this.fetch(n(r,"fetch","resetState")):this},hasPrevious:function(){var e=this.state,t=e.currentPage;return"infinite"!=this.mode?t>e.firstPage:!!this.links[t-1]},hasNext:function(){var e=this.state,t=this.state.currentPage;return"infinite"!=this.mode?e.lastPage>t:!!this.links[t+1]},getFirstPage:function(e){return this.getPage("first",e)},getPreviousPage:function(e){return this.getPage("prev",e)},getNextPage:function(e){return this.getPage("next",e)},getLastPage:function(e){return this.getPage("last",e)},getPage:function(e,t){var a=this.mode,o=this.fullCollection;t=t||{fetch:!1};var l=this.state,c=l.firstPage,u=l.currentPage,h=l.lastPage,g=l.pageSize,d=e;switch(e){case"first":d=c;break;case"prev":d=u-1;break;case"next":d=u+1;break;case"last":d=h;break;default:d=r(e,"index")}this.state=this._checkState(i({},l,{currentPage:d}));var v=(0===c?d:d-1)*g,P=o&&o.length?o.models.slice(v,v+g):[];return"client"!=a&&("infinite"!=a||f(P))||t.fetch?("infinite"==a&&(t.url=this.links[d]),this.fetch(n(t,"fetch"))):s(this,P,n(t,"fetch"))},sync:function(e,r,a){var s=this;if("infinite"==s.mode){var n=a.success,o=s.state.currentPage;a.success=function(e,t,r){var l=s.links,c=s.parseLinks(e,i({xhr:r},a));c.first&&(l[s.state.firstPage]=c.first),c.prev&&(l[o-1]=c.prev),c.next&&(l[o+1]=c.next),n&&n(e,t,r)}}return(S.sync||t.sync).call(s,e,r,a)},parseLinks:function(e,t){var r={},s=t.xhr.getResponseHeader("Link");if(s){var i=["first","prev","previous","next","last"];l(s.split(","),function(e){var t=e.split(";"),a=t[0].replace(C,""),s=t.slice(1);l(s,function(e){var t=e.split("="),s=t[0].replace(_,""),n=t[1].replace(_,"");"rel"==s&&u(i,n)&&("previous"==n?r.prev=a:r[n]=a)})});var n,c,f=r.last||"";if(c=(n=f.indexOf("?"))?f.slice(n+1):""){var h=a(c),g=o(this.state),d=this.queryParams,v=g.pageSize,P=1*h[d.totalRecords],p=1*h[d.currentPage],m=h[d.totalPages];P||(p?P=(0===g.firstPage?p+1:p)*v:m&&(P=m*v)),P&&(g.totalRecords=P),this.state=this._checkState(g)}}return delete r.last,r},parse:function(e){var t=this.parseState(e,o(this.queryParams),o(this.state));return t&&(this.state=this._checkState(i({},this.state,t))),this.parseRecords(e)},parseState:function(t,r,a){if(t&&2===t.length&&P(t[0])&&d(t[1])){var s=o(a),i=t[0];return l(h(n(r,"directions")),function(t){var r=t[0],a=t[1],n=i[a];m(n)||e.isNull(n)||(s[r]=i[a])}),i.order&&(s.order=1*g(r.directions)[i.order]),s}},parseRecords:function(e){return e&&2===e.length&&P(e[0])&&d(e[1])?e[1]:e},fetch:function(e){e=e||{};var t=this._checkState(this.state),r=this.mode;"infinite"!=r||e.url||(e.url=this.links[t.currentPage]);var l=e.data||{},u=y(e,"url")||y(this,"url")||"",f=u.indexOf("?");-1!=f&&(i(l,a(u.slice(f+1))),u=u.slice(0,f)),e.url=u,e.data=l;var g,d,P,k,b="client"==this.mode?c(this.queryParams,"sortKey","order"):n(c(this.queryParams,p(x.queryParams)),"directions"),_=h(b),C=o(this);for(g=0;_.length>g;g++)d=_[g],P=d[0],k=d[1],k=v(k)?k.call(C):k,null!=t[P]&&null!=k&&(l[k]=t[P]);t.sortKey&&t.order?l[b.order]=this.queryParams.directions[t.order+""]:t.sortKey||delete l[b.order];var R=h(n(this.queryParams,p(x.queryParams)));for(g=0;R.length>g;g++)d=R[g],k=d[1],k=v(k)?k.call(C):k,l[d[0]]=k;var w=this.fullCollection,q=this.links;if("server"!=r){var z=this,E=e.success;return e.success=function(a,n,o){o=o||{},m(e.silent)?delete o.silent:o.silent=e.silent;var l=a.models,c=t.currentPage;if("client"==r)s(w,l,o);else if(q[c]){var u=t.pageSize,f=(0===t.firstPage?c:c-1)*u,h=w.models,g=h.slice(0,f),d=h.slice(f+u);h=g.concat(l).concat(d);var v=w.set||w.update;v.call(w,h,i({silent:!0,sort:!1},o)),w.comparator&&w.sort(),w.trigger("reset",w,o)}else w.add(l,i({at:w.length,silent:!0},o)),w.trigger("reset",w,o);E&&E(a,n,o)},S.fetch.call(z,i({},e,{silent:!0}))}return S.fetch.call(this,e)},_makeComparator:function(e,t){var r=this.state;return e=e||r.sortKey,t=t||r.order,e&&t?function(r,a){var s,i=r.get(e),n=a.get(e);return 1===t&&(s=i,i=n,n=s),i===n?0:n>i?-1:1}:void 0},setSorting:function(e,t,r){var a=this.state;a.sortKey=e,a.order=t=t||a.order;var s=this.fullCollection,n=!1,o=!1;e||(n=o=!0);var l=this.mode;r=i({side:"client"==l?l:"server",full:!0},r);var c=this._makeComparator(e,t),u=r.full,f=r.side;return"client"==f?u?(s&&(s.comparator=c),n=!0):(this.comparator=c,o=!0):"server"!=f||u||(this.comparator=c),n&&delete this.comparator,o&&s&&delete s.comparator,this}}),x=R.prototype;return R}); \ No newline at end of file diff --git a/ajax/libs/backbone-pageable/package.json b/ajax/libs/backbone-pageable/package.json index 3dbceee54..67232427b 100644 --- a/ajax/libs/backbone-pageable/package.json +++ b/ajax/libs/backbone-pageable/package.json @@ -1,7 +1,7 @@ { "name": "backbone-pageable", "description": "A pageable Backbone.Collection superset. Supports server-side/client-side/infinite pagination and sorting.", - "version": "1.2.0", + "version": "1.2.1", "filename": "backbone-pageable.min.js", "repository": { "type": "git",