@onehat/data 1.4.4 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -372,6 +372,19 @@ describe('Repository Base', function() {
372
372
  expect(didFireChangeFilters).to.be.true;
373
373
  });
374
374
 
375
+ it('changing filters resets pagination', function() {
376
+ this.repository.isPaginated = true;
377
+ this.repository.setPageSize(1);
378
+ this.repository.setPage(3);
379
+
380
+ this.repository.setFilters({
381
+ b: '1',
382
+ c: '2',
383
+ });
384
+
385
+ expect(this.repository.page).to.be.eq(1);
386
+ });
387
+
375
388
  });
376
389
 
377
390
  describe('pagination', function() {
@@ -414,29 +427,73 @@ describe('Repository Base', function() {
414
427
  });
415
428
 
416
429
  it('_calculatePaginationVars', function() {
417
- let results = this.Repository._calculatePaginationVars(15, 1, 10), // Capital "R"
418
- expected = {
419
- page: 1,
420
- pageSize: 10,
421
- total: 15,
422
- totalPages: 2,
423
- pageStart: 1,
424
- pageEnd: 10,
425
- pageTotal: 10,
426
- };
427
- expect(_.isEqual(results, expected)).to.be.true;
428
-
429
- results = this.Repository._calculatePaginationVars(15, 2, 10); // Capital "R"
430
- expected = {
431
- page: 2,
432
- pageSize: 10,
433
- total: 15,
434
- totalPages: 2,
435
- pageStart: 11,
436
- pageEnd: 15,
437
- pageTotal: 5,
438
- };
439
- expect(_.isEqual(results, expected)).to.be.true;
430
+ let results;
431
+
432
+ results = this.Repository._calculatePaginationVars(0, 1, 10);
433
+ expect(results.page).to.be.eq(1);
434
+ expect(results.pageSize).to.be.eq(10);
435
+ expect(results.total).to.be.eq(0);
436
+ expect(results.totalPages).to.be.eq(1);
437
+ expect(results.pageStart).to.be.eq(0);
438
+ expect(results.pageEnd).to.be.eq(0);
439
+ expect(results.pageTotal).to.be.eq(0);
440
+
441
+ results = this.Repository._calculatePaginationVars(15, 1, 10);
442
+ expect(results.page).to.be.eq(1);
443
+ expect(results.pageSize).to.be.eq(10);
444
+ expect(results.total).to.be.eq(15);
445
+ expect(results.totalPages).to.be.eq(2);
446
+ expect(results.pageStart).to.be.eq(1);
447
+ expect(results.pageEnd).to.be.eq(10);
448
+ expect(results.pageTotal).to.be.eq(10);
449
+
450
+ results = this.Repository._calculatePaginationVars(15, 2, 10);
451
+ expect(results.page).to.be.eq(2);
452
+ expect(results.pageSize).to.be.eq(10);
453
+ expect(results.total).to.be.eq(15);
454
+ expect(results.totalPages).to.be.eq(2);
455
+ expect(results.pageStart).to.be.eq(11);
456
+ expect(results.pageEnd).to.be.eq(15);
457
+ expect(results.pageTotal).to.be.eq(5);
458
+
459
+ results = this.Repository._calculatePaginationVars(15, 2, 5);
460
+ expect(results.page).to.be.eq(2);
461
+ expect(results.pageSize).to.be.eq(5);
462
+ expect(results.total).to.be.eq(15);
463
+ expect(results.totalPages).to.be.eq(3);
464
+ expect(results.pageStart).to.be.eq(6);
465
+ expect(results.pageEnd).to.be.eq(10);
466
+ expect(results.pageTotal).to.be.eq(5);
467
+
468
+ results = this.Repository._calculatePaginationVars(7, 2, 5);
469
+ expect(results.page).to.be.eq(2);
470
+ expect(results.pageSize).to.be.eq(5);
471
+ expect(results.total).to.be.eq(7);
472
+ expect(results.totalPages).to.be.eq(2);
473
+ expect(results.pageStart).to.be.eq(6);
474
+ expect(results.pageEnd).to.be.eq(7);
475
+ expect(results.pageTotal).to.be.eq(2);
476
+
477
+ results = this.Repository._calculatePaginationVars(15, 15, 1);
478
+ expect(results.page).to.be.eq(15);
479
+ expect(results.pageSize).to.be.eq(1);
480
+ expect(results.total).to.be.eq(15);
481
+ expect(results.totalPages).to.be.eq(15);
482
+ expect(results.pageStart).to.be.eq(15);
483
+ expect(results.pageEnd).to.be.eq(15);
484
+ expect(results.pageTotal).to.be.eq(1);
485
+ });
486
+
487
+ it('reset pagination', function() {
488
+ expect(this.repository.page).to.be.eq(1);
489
+
490
+ this.repository.isPaginated = true;
491
+ this.repository.setPageSize(1);
492
+ this.repository.setPage(3);
493
+ expect(this.repository.page).to.be.eq(3);
494
+
495
+ this.repository.resetPagination();
496
+ expect(this.repository.page).to.be.eq(1);
440
497
  });
441
498
 
442
499
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.4",
3
+ "version": "1.4.8",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/OneHatData.js CHANGED
@@ -282,12 +282,15 @@ export class OneHatData extends EventEmitter {
282
282
  if (this.isDestroyed) {
283
283
  throw new Error('this.createRepositories is no longer valid. OneHatData has been destroyed.');
284
284
  }
285
- let promises = [];
286
- _.each(schemas, (schema) => {
287
- promises.push( this.createRepository({ schema, }, bound) );
288
- });
289
-
290
- await Promise.all(promises);
285
+ const schemasArray = _.map(schemas, (schema) => schema);
286
+ let i, schema, repository;
287
+ for (i = 0; i < schemasArray.length; i++) {
288
+ schema = schemasArray[i];
289
+ repository = await this.createRepository({ schema, }, bound);
290
+ if (!repository) {
291
+ throw new Error('Repository could not be created');
292
+ }
293
+ }
291
294
  return this;
292
295
  }
293
296
 
@@ -292,8 +292,8 @@ class AjaxRepository extends Repository {
292
292
  */
293
293
  _onChangeSorters = () => {
294
294
  const sorter = this.sorters[0];
295
- this.setParam(this.paramSort, sorter.name);
296
- this.setParam(this.paramDirection, sorter.direction);
295
+ this.setParam(this.paramSort, sorter.name, true); // true to set baseParam
296
+ this.setParam(this.paramDirection, sorter.direction, true);
297
297
 
298
298
  if (this.isLoaded) {
299
299
  return this.reload();
@@ -319,8 +319,8 @@ class AjaxRepository extends Repository {
319
319
  * Refreshes entities.
320
320
  */
321
321
  _onChangePagination = () => {
322
- this.setParam(this.paramPageNum, this.page);
323
- this.setParam(this.paramPageSize, this.pageSize);
322
+ this.setParam(this.paramPageNum, this.page, true); // true to set baseParam
323
+ this.setParam(this.paramPageSize, this.pageSize, true);
324
324
 
325
325
  if (this.isLoaded) {
326
326
  return this.reload();
@@ -648,6 +648,7 @@ export default class Repository extends EventEmitter {
648
648
  if (!_.isEqual(this.filters, filters)) {
649
649
  isChanged = true;
650
650
  this.filters = filters;
651
+ this.resetPagination();
651
652
  this.emit('changeFilters');
652
653
  if (this._onChangeFilters) {
653
654
  return this._onChangeFilters();
@@ -666,6 +667,17 @@ export default class Repository extends EventEmitter {
666
667
  // /_/ \__,_/\__, /_/_/ /_/\__,_/\__/\___/
667
668
  // /____/
668
669
 
670
+ /**
671
+ * Resets the pagination to page one
672
+ * @fires changePageSize
673
+ */
674
+ resetPagination = (pageSize) => {
675
+ if (this.isDestroyed) {
676
+ throw Error('this.resetPagination is no longer valid. Repository has been destroyed.');
677
+ }
678
+ this.setPage(1);
679
+ }
680
+
669
681
  /**
670
682
  * Sets the pageSize
671
683
  * @fires changePageSize
@@ -779,12 +791,38 @@ export default class Repository extends EventEmitter {
779
791
  * @static
780
792
  */
781
793
  static _calculatePaginationVars = (total, page, pageSize) => {
794
+
795
+ // Special case: empty pages
796
+ if (total < 1) {
797
+ return {
798
+ page,
799
+ pageSize,
800
+ total,
801
+ totalPages: 1,
802
+ pageStart: 0,
803
+ pageEnd: 0,
804
+ pageTotal: 0,
805
+ };
806
+ }
807
+
782
808
  const totalPages = Math.ceil(total / pageSize),
783
- pageStart = !total ? 0 : ((page -1) * pageSize) + 1,
784
- remainder = total > pageSize ? total % pageSize : 0,
785
- pageTotal = (page === 1 && totalPages === 1) ? total :
786
- (page < totalPages ? pageSize : remainder),
787
- pageEnd = !total ? 0 : pageStart -1 + pageTotal;
809
+ pageStart = ((page -1) * pageSize) + 1;
810
+
811
+ let remainder,
812
+ pageEnd,
813
+ pageTotal;
814
+
815
+ if (page === 1 && totalPages === 1) {
816
+ pageTotal = total;
817
+ } else if (page < totalPages) {
818
+ pageTotal = pageSize;
819
+ } else {
820
+ // last page
821
+ remainder = total % pageSize;
822
+ pageTotal = remainder || pageSize;
823
+ }
824
+
825
+ pageEnd = pageStart + pageTotal -1;
788
826
 
789
827
  return {
790
828
  page,
@@ -1302,6 +1340,9 @@ export default class Repository extends EventEmitter {
1302
1340
  if (!_.isArray(entities)) {
1303
1341
  entities = [entities];
1304
1342
  }
1343
+ if (!entities.length) {
1344
+ return;
1345
+ }
1305
1346
  _.each(entities, (entity) => {
1306
1347
  entity.markDeleted(); // Entity is still there, it's just marked for deletion
1307
1348
  });