Skip to content

Commit f75b4b0

Browse files
committed
Refactor paginator selection helper
1 parent 1379b20 commit f75b4b0

6 files changed

Lines changed: 36 additions & 16 deletions

File tree

src/app/courses/courses.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { CouchService } from '../shared/couchdb.service';
2525
import { PlanetMessageService } from '../shared/planet-message.service';
2626
import { DialogsPromptComponent } from '../shared/dialogs/dialogs-prompt.component';
2727
import { CoursesService } from './courses.service';
28-
import { dedupeShelfReduce, findByIdInArray, calculateMdAdjustedLimit } from '../shared/utils';
28+
import { dedupeShelfReduce, findByIdInArray, calculateMdAdjustedLimit, itemsShown } from '../shared/utils';
2929
import { StateService } from '../shared/state.service';
3030
import { DialogsLoadingService } from '../shared/dialogs/dialogs-loading.service';
3131
import { TagsService } from '../shared/forms/tags.service';
@@ -307,8 +307,8 @@ export class CoursesComponent implements OnInit, OnChanges, AfterViewInit, OnDes
307307

308308
/** Whether the number of selected elements matches the total number of rows. */
309309
isAllSelected() {
310-
const itemsShown = Math.min(this.paginator.length - (this.paginator.pageIndex * this.paginator.pageSize), this.paginator.pageSize);
311-
return this.selection.selected.length === itemsShown;
310+
const visibleItems = itemsShown(this.paginator);
311+
return this.selection.selected.length === visibleItems;
312312
}
313313

314314
/** Selects all rows if they are not all selected; otherwise clear selection. */

src/app/manager-dashboard/manager-fetch.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SelectionModel } from '@angular/cdk/collections';
99
import { MatLegacyPaginator as MatPaginator, LegacyPageEvent as PageEvent } from '@angular/material/legacy-paginator';
1010
import { MatSort } from '@angular/material/sort';
1111
import { MatLegacyTableDataSource as MatTableDataSource } from '@angular/material/legacy-table';
12-
import { findByIdInArray } from '../shared/utils';
12+
import { findByIdInArray, itemsShown } from '../shared/utils';
1313
import { SyncService } from '../shared/sync.service';
1414
import { PlanetMessageService } from '../shared/planet-message.service';
1515

@@ -62,8 +62,8 @@ export class ManagerFetchComponent implements OnInit, AfterViewInit {
6262

6363
/** Whether the number of selected elements matches the total number of rows. */
6464
isAllSelected() {
65-
const itemsShown = Math.min(this.paginator.length - (this.paginator.pageIndex * this.paginator.pageSize), this.paginator.pageSize);
66-
return this.selection.selected.length === itemsShown;
65+
const visibleItems = itemsShown(this.paginator);
66+
return this.selection.selected.length === visibleItems;
6767
}
6868

6969
/** Selects all rows if they are not all selected; otherwise clear selection. */

src/app/meetups/meetups.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { takeUntil } from 'rxjs/operators';
1313
import { MeetupService } from './meetups.service';
1414
import { StateService } from '../shared/state.service';
1515
import { DialogsLoadingService } from '../shared/dialogs/dialogs-loading.service';
16-
import { findByIdInArray } from '../shared/utils';
16+
import { findByIdInArray, itemsShown } from '../shared/utils';
1717

1818
@Component({
1919
templateUrl: './meetups.component.html',
@@ -87,10 +87,10 @@ export class MeetupsComponent implements OnInit, AfterViewInit, OnDestroy {
8787
this.meetups.sort = this.sort;
8888
}
8989

90-
isAllSelected() {
91-
const itemsShown = Math.min(this.paginator.length - (this.paginator.pageIndex * this.paginator.pageSize), this.paginator.pageSize);
92-
return this.selection.selected.length === itemsShown;
93-
}
90+
isAllSelected() {
91+
const visibleItems = itemsShown(this.paginator);
92+
return this.selection.selected.length === visibleItems;
93+
}
9494
onPaginateChange(e: PageEvent) {
9595
this.selection.clear();
9696
}

src/app/resources/resources.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { FormControl } from '../../../node_modules/@angular/forms';
2424
import { PlanetTagInputComponent } from '../shared/forms/planet-tag-input.component';
2525
import { DialogsListService } from '../shared/dialogs/dialogs-list.service';
2626
import { DialogsListComponent } from '../shared/dialogs/dialogs-list.component';
27-
import { findByIdInArray, calculateMdAdjustedLimit } from '../shared/utils';
27+
import { findByIdInArray, calculateMdAdjustedLimit, itemsShown } from '../shared/utils';
2828
import { StateService } from '../shared/state.service';
2929
import { DialogsLoadingService } from '../shared/dialogs/dialogs-loading.service';
3030
import { ResourcesSearchComponent } from './search-resources/resources-search.component';
@@ -202,8 +202,8 @@ export class ResourcesComponent implements OnInit, AfterViewInit, OnDestroy {
202202

203203
/** Whether the number of selected elements matches the total number of rows. */
204204
isAllSelected() {
205-
const itemsShown = Math.min(this.paginator.length - (this.paginator.pageIndex * this.paginator.pageSize), this.paginator.pageSize);
206-
return this.selection.selected.length === itemsShown;
205+
const visibleItems = itemsShown(this.paginator);
206+
return this.selection.selected.length === visibleItems;
207207
}
208208

209209
applyResFilter(filterResValue: string) {

src/app/shared/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,27 @@ export const urlToParamObject = (url: string) => url.split(';').reduce((params,
7979

8080
export const toProperCase = (string: string) => `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;
8181

82+
export const itemsShown = (paginator?: { length: number; pageIndex: number; pageSize: number }) => {
83+
if (!paginator) {
84+
return 0;
85+
}
86+
const remaining = paginator.length - (paginator.pageIndex * paginator.pageSize);
87+
return Math.min(Math.max(remaining, 0), paginator.pageSize);
88+
};
89+
8290
export const stringToHex = (string: string) => string.split('').map(char => char.charCodeAt(0).toString(16)).join('');
8391

92+
export const hexToString = (hex: string) => {
93+
if (!hex) {
94+
return '';
95+
}
96+
const matches = hex.match(/.{1,2}/g);
97+
if (!matches) {
98+
return '';
99+
}
100+
return matches.map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
101+
};
102+
84103
export const ageFromBirthDate = (currentTime: number, birthDate: string) => {
85104
const now = new Date(currentTime);
86105
const birth = new Date(birthDate);

src/app/users/users-table.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { DialogsPromptComponent } from '../shared/dialogs/dialogs-prompt.compone
1919
import { UsersService } from './users.service';
2020
import { PlanetMessageService } from '../shared/planet-message.service';
2121
import { UserProfileDialogComponent } from './users-profile/users-profile-dialog.component';
22+
import { itemsShown } from '../shared/utils';
2223

2324
export class TableState {
2425
isOnlyManagerSelected = false;
@@ -157,8 +158,8 @@ export class UsersTableComponent implements OnInit, OnDestroy, AfterViewInit, On
157158
}
158159

159160
isAllSelected() {
160-
const itemsShown = Math.min(this.paginator.length - (this.paginator.pageIndex * this.paginator.pageSize), this.paginator.pageSize);
161-
return this.selection.selected.length === itemsShown;
161+
const visibleItems = itemsShown(this.paginator);
162+
return this.selection.selected.length === visibleItems;
162163
}
163164

164165
onlyManagerSelected() {

0 commit comments

Comments
 (0)