Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | 1123x 1123x 1481x 1481x 1481x 1458x 1245x 778x 1458x 1481x 1481x 1245x 1481x 1481x 1245x 1481x 1481x 1481x 1350x 1350x 1350x 1350x 1636x 1636x 1636x 1487x 145x 1487x 1106x 1487x 63x 63x 63x 30x 1487x 1473x 1487x 1106x 1106x 1106x 1106x 1106x 5977x 5977x 5977x 10868x 10868x 10838x 10838x 4504x 6334x 5977x 2322x 2322x 2322x 2322x 2322x 4913x 4913x 4913x 4913x 4913x 4913x 4913x 4913x 3432x 3432x 3432x 3432x 3432x 3432x 3432x 3432x 3432x 3432x 734x 3432x 3432x 734x 3432x 3432x 2698x 2698x 734x 4913x 13728x 13728x 13728x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 1106x 8848x 8848x 8848x 4424x 1106x 1106x 949x 949x 1106x 1106x 1106x 1106x 1106x 778x 778x 1556x 778x 778x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1123x | /* global define */ define(['underscore', 'backbone', 'backbone/pageable-collection', 'oro/app'], function(_, Backbone, BackbonePageableCollection, app) { 'use strict'; /** * Pageable collection * * Additional events: * beforeReset: Fired when collection is about to reset data (e.g. after success response) * updateState: Fired when collection state is updated using updateState method * beforeFetch: Fired when collection starts to fetch, before request is formed * * @export oro/pageable-collection * @class oro.PageableCollection * @extends Backbone.PageableCollection */ var PageableCollection = BackbonePageableCollection.extend({ /** * Basic model to store row data * * @property {Function} */ model: Backbone.Model, /** * Initial state of collection * * @property */ initialState: {}, /** * Declaration of URL parameters * * @property {Object} */ queryParams: _.extend({}, BackbonePageableCollection.prototype.queryParams, { directions: { "-1": "ASC", "1": "DESC" }, totalRecords: undefined, totalPages: undefined }), /** * Object declares state keys that will be involved in URL-state saving with their shorthands * * @property {Object} */ stateShortKeys: { currentPage: 'i', pageSize: 'p', sorters: 's', filters: 'f', gridName: 't', gridView: 'v' }, /** * @property {Object} */ additionalParameters: { view: 'gridView' }, /** * Whether multiple sorting is allowed * * @property {Boolean} */ multipleSorting: true, /** * Initialize basic parameters from source options * * @param models * @param options */ initialize: function(models, options) { this.state = {}; options = options || {}; if (options.state) { if (options.state.sorters) { _.each(options.state.sorters, function(direction, field) { options.state.sorters[field] = this.getSortDirectionKey(direction); }, this); } _.extend(this.state, options.state); } this.initialState = app.deepClone(this.state); if (options.url) { this.url = options.url; } Iif (options.model) { this.model = options.model; } if (options.inputName) { this.inputName = options.inputName; } _.extend(this.queryParams, { currentPage: this.inputName + '[_pager][_page]', pageSize: this.inputName + '[_pager][_per_page]', sortBy: this.inputName + '[_sort_by][%field%]', parameters: this.inputName + '[_parameters]' }); this.on('remove', this.onRemove, this); BackbonePageableCollection.prototype.initialize.apply(this, arguments); }, /** * Triggers when model is removed from collection. * * Ensure that state is changed after concrete model removed from collection. * * @protected */ onRemove: function() { if (this.state.totalRecords > 0) { this.state.totalRecords--; } }, /** * Encode state object to string * * @param {Object} stateObject * @return {String} */ encodeStateData: function(stateObject) { var data = _.pick(stateObject, _.keys(this.stateShortKeys)); data.gridName = this.inputName; data = app.invertKeys(data, this.stateShortKeys); return app.packToQueryString(data); }, /** * Decode state object from string, operation is invert for encodeStateData. * * @param {String} stateString * @return {Object} */ decodeStateData: function(stateString) { var data = app.unpackFromQueryString(stateString); data = app.invertKeys(data, _.invert(this.stateShortKeys)); return data; }, /** * Adds filter parameters to data * * @param {Object} data * @param {Object} state * @param {String} prefix * @return {Object} */ processFiltersParams: function(data, state, prefix) { if (!state) { state = this.state; } if (!prefix) { prefix = this.inputName + '[_filter]' } if (this.inputName && !_.isUndefined(data[this.inputName]) && !_.isUndefined(data[this.inputName]._filter) ) { const scope = !_.isUndefined(data[this.inputName]._filter.scope) ? data[this.inputName]._filter.scope : null; data[this.inputName]._filter = {}; if (null !== scope) { data[this.inputName]._filter.scope = scope } } if (state.filters) { _.extend( data, this.generateParameterStrings(state.filters, prefix) ); } return data; }, /** * Adds additional parameters to state * * @param {Object} state * @return {Object} */ processAdditionalParams: function (state) { var state = app.deepClone(state); state.parameters = state.parameters || {}; _.each(this.additionalParameters, _.bind(function(value, key) { Iif (!_.isUndefined(state[value])) { state.parameters[key] = state[value] } }, this)); return state; }, /** * Get list of request parameters * * @param {Object} parameters * @param {String} prefix * @return {Object} */ generateParameterStrings: function(parameters, prefix) { var localStrings = {}; var localPrefix = prefix; _.each(parameters, function(filterParameters, filterKey) { filterKey = filterKey.toString(); if (filterKey.substr(0, 2) != '__') { var filterKeyString = localPrefix + '[' + filterKey + ']'; if (_.isObject(filterParameters)) { _.extend( localStrings, this.generateParameterStrings(filterParameters, filterKeyString) ); } else { localStrings[filterKeyString] = filterParameters; } } }, this); return localStrings; }, /** * Parse AJAX response * * @param resp * @param options * @return {Object} */ parse: function(resp, options) { this.state.totalRecords = 'undefined' !== typeof(resp.totalRecords) ? resp.totalRecords : resp.options.totalRecords; this.state = this._checkState(this.state); return resp.data; }, /** * Reset collection object * * @param models * @param options */ reset: function(models, options) { this.trigger('beforeReset', this, options); BackbonePageableCollection.prototype.reset.apply(this, arguments); }, /** * Updates and checks state * * @param {Object} state */ updateState: function(state) { var newState = _.extend({}, this.state, state); this.state = this._checkState(newState); this.trigger('updateState', this, this.state); }, /** * @inheritDoc */ _checkState: function (state) { var mode = this.mode; var links = this.links; var totalRecords = state.totalRecords; var pageSize = state.pageSize; var currentPage = (state.currentPage < 0) ? 1 : state.currentPage; var firstPage = state.firstPage; var totalPages = state.totalPages; if (totalRecords != null && pageSize != null && currentPage != null && firstPage != null && (mode == "infinite" ? links : true)) { state.totalRecords = totalRecords = this.finiteInt(totalRecords, "totalRecords"); state.pageSize = pageSize = this.finiteInt(pageSize, "pageSize"); state.currentPage = currentPage = this.finiteInt(currentPage, "currentPage"); state.firstPage = firstPage = this.finiteInt(firstPage, "firstPage"); Iif (pageSize < 0) { throw new RangeError("`pageSize` must be >= 0"); } state.totalPages = pageSize == 0 ? 1 : totalPages = state.totalPages = Math.ceil(totalRecords / pageSize); Iif (firstPage < 0 || firstPage > 1) { throw new RangeError("`firstPage` must be 0 or 1"); } state.lastPage = firstPage === 0 ? totalPages - 1 : totalPages; state.lastPage = state.lastPage < 0 ? 1 : state.lastPage; // page out of range if (currentPage > state.lastPage && state.pageSize > 0) { state.currentPage = currentPage = state.lastPage; } Iif (state.pageSize == 0) { state.currentPage = currentPage = 1; } // no results returned if (totalRecords == 0) { state.currentPage = currentPage = firstPage; } Iif (mode == "infinite") { if (!links[currentPage + '']) { throw new RangeError("No link found for page " + currentPage); } } else if (totalPages > 0) { Iif (firstPage === 0 && (currentPage < firstPage || currentPage >= totalPages)) { throw new RangeError("`currentPage` must be firstPage <= currentPage < totalPages if 0-based. Got " + currentPage + '.'); } else Iif (firstPage === 1 && (currentPage < firstPage || currentPage > totalPages)) { throw new RangeError("`currentPage` must be firstPage <= currentPage <= totalPages if 1-based. Got " + currentPage + '.'); } } else Iif (currentPage !== firstPage ) { throw new RangeError("`currentPage` must be " + firstPage + ". Got " + currentPage + '.'); } } return state; }, /** * Asserts that val is finite integer. * * @param {*} val * @param {String} name * @return {Number} * @protected */ finiteInt: function(val, name) { val *= 1; Iif (!_.isNumber(val) || _.isNaN(val) || !_.isFinite(val) || ~~val !== val) { throw new TypeError("`" + name + "` must be a finite integer"); } return val; }, /** * Fetch collection data */ fetch: function (options) { this.trigger('beforeFetch', this, options); var BBColProto = Backbone.Collection.prototype; options = options || {}; var state = this._checkState(this.state); var mode = this.mode; Iif (mode == "infinite" && !options.url) { options.url = this.links[state.currentPage]; } var data = options.data || {}; // set up query params var url = options.url || _.result(this, "url") || ''; var qsi = url.indexOf('?'); Eif (qsi != -1) { _.extend(data, app.unpackFromQueryString(url.slice(qsi + 1))); url = url.slice(0, qsi); } options.url = url; options.data = data; data = this.processQueryParams(data, state); data = this.processFiltersParams(data, state); var fullCollection = this.fullCollection, links = this.links; Iif (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); fullCollection.update(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); }, /** * Process parameters which are sending to server * * @param {Object} data * @param {Object} state * @return {Object} */ processQueryParams: function(data, state) { state = this.processAdditionalParams(state); var pageablePrototype = PageableCollection.prototype; // map params except directions var queryParams = this.mode == "client" ? _.pick(this.queryParams, "sorters") : _.omit(_.pick(this.queryParams, _.keys(pageablePrototype.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]; } } // set sorting parameters Eif (state.sorters) { _.each(state.sorters, function(direction, field) { var key = this.queryParams.sortBy.replace('%field%', field); data[key] = this.queryParams.directions[direction]; }, this); } // map extra query parameters var extraKvps = _.pairs(_.omit(this.queryParams, _.keys(pageablePrototype.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; } // unused parameters delete data[queryParams.order]; delete data[queryParams.sortKey]; return data; }, /** * Convert direction value to direction key * * @param {String} directionValue * @return {String} */ getSortDirectionKey: function(directionValue) { var directionKey = null; _.each(this.queryParams.directions, function(value, key) { if (value == directionValue) { directionKey = key; } }); return directionKey; }, /** * Set sorting order * * @param {String} sortKey * @param {String} order * @param {Object} options * @return {*} */ setSorting: function (sortKey, order, options) { var state = this.state; state.sorters = state.sorters || {}; Iif (this.multipleSorting) { // there is always must be at least one sorted column if (_.keys(state.sorters).length <= 1 && !order) { order = this.getSortDirectionKey("ASC"); // default order } // last sorting has the lowest priority delete state.sorters[sortKey]; } else { state.sorters = {}; } Eif (order) { state.sorters[sortKey] = order; } var fullCollection = this.fullCollection; var delComp = false, delFullComp = false; Iif (!order) 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; Iif (side == "client") { if (full) { if (fullCollection) fullCollection.comparator = comparator; delComp = true; } else { this.comparator = comparator; delFullComp = true; } } else Iif (side == "server" && !full) { this.comparator = comparator; } Iif (delComp) delete this.comparator; Iif (delFullComp && fullCollection) delete fullCollection.comparator; return this; }, /** * Clone collection * * @return {PageableCollection} */ clone: function() { var collectionOptions = {}; collectionOptions.url = this.url; collectionOptions.inputName = this.inputName; var newCollection = new PageableCollection(this.toJSON(), collectionOptions); newCollection.state = app.deepClone(this.state); return newCollection; } }); return PageableCollection; }); |