-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add paging bar to the list of line items (basket, requisition, …
…order, quote)
- Loading branch information
Showing
12 changed files
with
275 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/app/shared/components/common/paging/paging.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<nav *ngIf="currentPage && lastPage" aria-label="Pagination"> | ||
<ul class="pagination"> | ||
<li class="page-item"> | ||
<button | ||
class="btn btn-primary mb-0" | ||
[disabled]="currentPage === 1" | ||
(click)="setPage(currentPage - 1)" | ||
aria-label="Go To Previous Page" | ||
[attr.aria-disabled]="currentPage === 1" | ||
data-testing-id="paging-previous-button" | ||
> | ||
<fa-icon [icon]="['fas', 'angle-left']"></fa-icon> | ||
</button> | ||
</li> | ||
<li | ||
*ngFor="let p of pageIndices" | ||
class="page-item pt-2" | ||
[ngClass]="{ active: p === currentPage }" | ||
data-testing-id="paging-link" | ||
> | ||
<a | ||
*ngIf="p !== -1; else more" | ||
[attr.aria-current]="p === currentPage ? 'page' : null" | ||
[attr.aria-label]="p === currentPage ? 'Current Page, Page ' + p : 'Go to Page ' + p" | ||
(click)="setPage(p)" | ||
(keyup.enter)="setPage(p)" | ||
>{{ p }}</a | ||
> | ||
<ng-template #more>...</ng-template> | ||
</li> | ||
<li class="page-item"> | ||
<button | ||
class="btn btn-primary m-0" | ||
[disabled]="currentPage === lastPage" | ||
(click)="setPage(currentPage + 1)" | ||
aria-label="Go To Next Page" | ||
[attr.aria-disabled]="currentPage === lastPage" | ||
data-testing-id="paging-next-button" | ||
> | ||
<fa-icon [icon]="['fas', 'angle-right']"></fa-icon> | ||
</button> | ||
</li> | ||
</ul> | ||
</nav> |
19 changes: 19 additions & 0 deletions
19
src/app/shared/components/common/paging/paging.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
@import 'variables'; | ||
|
||
.pagination { | ||
margin: 0; | ||
list-style: none; | ||
|
||
li { | ||
margin-right: ($space-default); | ||
|
||
&.active a { | ||
color: $text-color-primary; | ||
cursor: unset; | ||
} | ||
|
||
&:last-child { | ||
margin-right: 0; | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/app/shared/components/common/paging/paging.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FaIconComponent } from '@fortawesome/angular-fontawesome'; | ||
import { MockComponent } from 'ng-mocks'; | ||
import { spy, verify } from 'ts-mockito'; | ||
|
||
import { PagingComponent } from './paging.component'; | ||
|
||
describe('Paging Component', () => { | ||
let component: PagingComponent; | ||
let fixture: ComponentFixture<PagingComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [MockComponent(FaIconComponent), PagingComponent], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(PagingComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
|
||
component.currentPage = 1; | ||
component.lastPage = 10; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
|
||
it('should display paging navigation links if current page = 1', () => { | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
|
||
expect(JSON.stringify(component.pageIndices)).toMatchInlineSnapshot(`"[1,2,3,4,5,6,-1,10]"`); | ||
expect(element.querySelectorAll('button.btn')).toHaveLength(2); | ||
expect(element.querySelectorAll('[data-testing-id=paging-link] a')).toHaveLength(7); | ||
expect(element.innerHTML).toContain('...'); | ||
}); | ||
|
||
it('should display paging navigation links if current page = last page', () => { | ||
component.currentPage = 10; | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
|
||
expect(JSON.stringify(component.pageIndices)).toMatchInlineSnapshot(`"[1,-1,5,6,7,8,9,10]"`); | ||
expect(element.querySelectorAll('button.btn')).toHaveLength(2); | ||
expect(element.querySelectorAll('[data-testing-id=paging-link] a')).toHaveLength(7); | ||
expect(element.innerHTML).toContain('...'); | ||
}); | ||
|
||
it('should display paging navigation links if current page is in the center', () => { | ||
component.currentPage = 5; | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
|
||
expect(JSON.stringify(component.pageIndices)).toMatchInlineSnapshot(`"[1,-1,3,4,5,6,7,-1,10]"`); | ||
expect(element.querySelectorAll('[data-testing-id=paging-link] a')).toHaveLength(7); | ||
expect(element.innerHTML).toContain('...'); | ||
}); | ||
|
||
it('should navigate to the next page if the next button is clicked', () => { | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
|
||
const emitter = spy(component.goToPage); | ||
|
||
(element.querySelector('button[data-testing-id="paging-next-button"]') as HTMLElement).click(); | ||
|
||
verify(emitter.emit(2)).once(); | ||
}); | ||
|
||
it('should navigate to the previous page if the previous button is clicked', () => { | ||
component.currentPage = 5; | ||
component.ngOnChanges(); | ||
fixture.detectChanges(); | ||
|
||
const emitter = spy(component.goToPage); | ||
|
||
(element.querySelector('button[data-testing-id="paging-previous-button"]') as HTMLElement).click(); | ||
|
||
verify(emitter.emit(4)).once(); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
src/app/shared/components/common/paging/paging.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'ish-paging', | ||
templateUrl: './paging.component.html', | ||
styleUrls: ['./paging.component.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class PagingComponent implements OnChanges { | ||
@Input() currentPage: number; | ||
@Input() lastPage: number; | ||
|
||
@Output() goToPage: EventEmitter<number> = new EventEmitter<number>(); | ||
|
||
pageIndices: number[] = []; | ||
|
||
ngOnChanges(): void { | ||
if (this.currentPage && this.lastPage) { | ||
this.pageIndices = this.getPages(this.currentPage, this.lastPage); | ||
} | ||
} | ||
/** | ||
* If the user changes the page the goToPage event is emitted | ||
* | ||
* @param page : changed page number | ||
*/ | ||
setPage(page: number) { | ||
this.goToPage.emit(page); | ||
} | ||
|
||
/** | ||
* Determines the page array - elements with the value of -1 will be shown as ... | ||
* | ||
* @param current current page | ||
* @param total number of pages | ||
* @returns pages array | ||
*/ | ||
private getPages(current: number, total: number): number[] { | ||
if (total <= 8) { | ||
return [...Array(total).keys()].map(x => x + 1); | ||
} | ||
|
||
if (current > 4) { | ||
if (current >= total - 3) { | ||
return [1, -1, total - 5, total - 4, total - 3, total - 2, total - 1, total]; | ||
} else { | ||
return [1, -1, current - 2, current - 1, current, current + 1, current + 2, -1, total]; | ||
} | ||
} | ||
|
||
return [1, 2, 3, 4, 5, 6, -1, total]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters