Skip to content

Commit

Permalink
feat(pagination): add custom template functionality
Browse files Browse the repository at this point in the history
Add custom template functionality
Fix tooltip and popover unit tests

Close valor-software#2044
  • Loading branch information
IraErshova committed Feb 13, 2020
1 parent 2b2f5de commit fd73917
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<pagination [totalItems]="66"
[boundaryLinks]="true"
[customFirstTemplate]="firstTemplate"
[customLastTemplate]="lastTemplate"
[customPreviousTemplate]="prevTemplate"
[customNextTemplate]="nextTemplate"
[customPageTemplate]="pageTemplate">
</pagination>

<ng-template #pageTemplate let-page let-disabled="disabled" let-currentPage="currentPage">
{{ convertToRoman(page)}}
<em *ngIf="page === currentPage">(page {{ page }})</em>
</ng-template>

<ng-template #nextTemplate let-disabled="disabled" let-currentPage="currentPage">
<ng-container *ngIf="!disabled">
➡️
</ng-container>
<ng-container *ngIf="disabled">
</ng-container>
</ng-template>

<ng-template #prevTemplate let-disabled="disabled" let-currentPage="currentPage">
<ng-container *ngIf="!disabled">
⬅️
</ng-container>
<ng-container *ngIf="disabled">
</ng-container>
</ng-template>

<ng-template #lastTemplate let-disabled="disabled" let-currentPage="currentPage">
Finish 🏁
</ng-template>

<ng-template #firstTemplate let-disabled="disabled" let-currentPage="currentPage">
Start 🏁
</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Component } from '@angular/core';

@Component({
selector: 'demo-pagination-custom-template',
templateUrl: './custom-template.html'
})
export class DemoPaginationCustomTemplateComponent {

convertToRoman(page: number): string {
const roman = {
M: 1000,
CM: 900,
D: 500,
CD: 400,
C: 100,
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};

return Object.keys(roman).reduce((acc, symbol) => {
const numeralSystem = Math.floor(page / roman[symbol]);
// tslint:disable-next-line:no-parameter-reassignment
page -= numeralSystem * roman[symbol];

return acc + symbol.repeat(numeralSystem);
}, '');
}
}
4 changes: 3 additions & 1 deletion demo/src/app/components/+pagination/demos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DemoPaginationCustomLinksContentComponent } from './custom-links-conten
import { DemoPaginationDisabledComponent } from './disabled/disabled';
import { DemoPaginationRotateComponent } from './rotate/rotate';
import { DemoPaginationContentSwitchingComponent } from './content-switching/content-switching';
import { DemoPaginationCustomTemplateComponent } from './custom-template/custom-template';

export const DEMO_COMPONENTS = [
DemoPaginationBasicComponent,
Expand All @@ -25,5 +26,6 @@ export const DEMO_COMPONENTS = [
DemoPaginationCustomLinksContentComponent,
DemoPaginationDisabledComponent,
DemoPaginationRotateComponent,
DemoPaginationContentSwitchingComponent
DemoPaginationContentSwitchingComponent,
DemoPaginationCustomTemplateComponent
];
34 changes: 34 additions & 0 deletions demo/src/app/components/+pagination/pagination-section.list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '../../docs/api-docs';
import { DemoPaginationDisabledComponent } from './demos/disabled/disabled';
import { DemoPaginationRotateComponent } from './demos/rotate/rotate';
import { DemoPaginationCustomTemplateComponent } from './demos/custom-template/custom-template';

export const demoComponentContent: ContentSection[] = [
{
Expand Down Expand Up @@ -123,6 +124,29 @@ export const demoComponentContent: ContentSection[] = [
component: require('!!raw-loader!./demos/pager/pager.ts'),
html: require('!!raw-loader!./demos/pager/pager.html'),
outlet: DemoPaginationPagerComponent
},
{
title: 'Custom Template',
anchor: 'custom-template',
description: `
<p>
It is possible to customize what exactly is displayed in each pagination link. To overwrite default
templates use these inputs:
<ul>
<li><code>customPageTemplate</code></li>
<li><code>customNextTemplate</code></li>
<li><code>customPreviousTemplate</code></li>
<li><code>customFirstTemplate</code></li>
<li><code>customLastTemplate</code></li>
</ul>
Each template has a context object. The object <code>PaginationLinkContext</code> is available for all
templates and for page numbers there is a <code>PaginationNumberLinkContext</code> that
contains the page number.
</p>
`,
component: require('!!raw-loader!./demos/custom-template/custom-template.ts'),
html: require('!!raw-loader!./demos/custom-template/custom-template.html'),
outlet: DemoPaginationCustomTemplateComponent
}
/* TODO: temporary disabled pageBtnClass option
{
Expand Down Expand Up @@ -153,6 +177,16 @@ export const demoComponentContent: ContentSection[] = [
title: 'PaginationConfig',
anchor: 'pagination-config',
outlet: NgApiDocConfigComponent
},
{
title: 'PaginationLinkContext',
anchor: 'pagination-link-context',
outlet: NgApiDocConfigComponent
},
{
title: 'PaginationNumberLinkContext',
anchor: 'pagination-number-link-context',
outlet: NgApiDocConfigComponent
}
]
}
Expand Down
76 changes: 74 additions & 2 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2504,9 +2504,25 @@ export const ngdoc: any = {
"PagesModel": {
"fileName": "src/pagination/models/index.ts",
"className": "PagesModel",
"description": "",
"description": "<p>Contain information about the page</p>\n",
"methods": [],
"properties": []
"properties": [
{
"name": "active",
"type": "boolean",
"description": "<p>If <code>true</code>, then this is the current page</p>\n"
},
{
"name": "number",
"type": "number",
"description": "<p>Page number</p>\n"
},
{
"name": "text",
"type": "string",
"description": "<p>Text, which is displayed in the link</p>\n"
}
]
},
"PagerModel": {
"fileName": "src/pagination/models/index.ts",
Expand All @@ -2515,6 +2531,37 @@ export const ngdoc: any = {
"methods": [],
"properties": []
},
"PaginationLinkContext": {
"fileName": "src/pagination/models/index.ts",
"className": "PaginationLinkContext",
"description": "<p>A context for the</p>\n<ul>\n<li><code>customPageTemplate</code></li>\n<li><code>customNextTemplate</code></li>\n<li><code>customPreviousTemplate</code></li>\n<li><code>customFirstTemplate</code></li>\n<li><code>customLastTemplate</code>\ninputs for link templates in case you want to override one</li>\n</ul>\n",
"methods": [],
"properties": [
{
"name": "currentPage",
"type": "number",
"description": "<p>The currently selected page number</p>\n"
},
{
"name": "disabled",
"type": "boolean",
"description": "<p>If <code>true</code>, the current link is disabled</p>\n"
}
]
},
"PaginationNumberLinkContext": {
"fileName": "src/pagination/models/index.ts",
"className": "PaginationNumberLinkContext",
"description": "<p>A context for the <code>pageTemplate</code> inputs for link template</p>\n",
"methods": [],
"properties": [
{
"name": "$implicit",
"type": "number",
"description": "<p>Contain the page number</p>\n"
}
]
},
"PagerComponent": {
"fileName": "src/pagination/pager.component.ts",
"className": "PagerComponent",
Expand Down Expand Up @@ -2623,6 +2670,31 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>if false first and last buttons will be hidden</p>\n"
},
{
"name": "customFirstTemplate",
"type": "TemplateRef<PaginationLinkContext>",
"description": "<p>custom template for first link</p>\n"
},
{
"name": "customLastTemplate",
"type": "TemplateRef<PaginationLinkContext>",
"description": "<p>custom template for last link</p>\n"
},
{
"name": "customNextTemplate",
"type": "TemplateRef<PaginationLinkContext>",
"description": "<p>custom template for next link</p>\n"
},
{
"name": "customPageTemplate",
"type": "TemplateRef<PaginationNumberLinkContext>",
"description": "<p>custom template for page link</p>\n"
},
{
"name": "customPreviousTemplate",
"type": "TemplateRef<PaginationLinkContext>",
"description": "<p>custom template for previous link</p>\n"
},
{
"name": "directionLinks",
"type": "boolean",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export {
PaginationComponent,
PaginationConfig,
PaginationModule,
PageChangedEvent
PageChangedEvent,
PaginationLinkContext,
PaginationNumberLinkContext,
PagesModel
} from './pagination/index';

export {
Expand Down
31 changes: 30 additions & 1 deletion src/pagination/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ export interface ConfigModel {
previousText: string;
rotate: boolean;
}

/**
* Contain information about the page
*/
export interface PagesModel {
/** Text, which is displayed in the link */
text: string;
/** Page number */
number: number;
/** If `true`, then this is the current page */
active: boolean;
}

Expand All @@ -25,3 +30,27 @@ export interface PagerModel {
pageBtnClass: string;
align: boolean;
}

/**
* A context for the
* * `customPageTemplate`
* * `customNextTemplate`
* * `customPreviousTemplate`
* * `customFirstTemplate`
* * `customLastTemplate`
* inputs for link templates in case you want to override one
*/
export interface PaginationLinkContext {
/** The currently selected page number */
currentPage: number;
/** If `true`, the current link is disabled */
disabled: boolean;
}

/**
* A context for the `pageTemplate` inputs for link template
*/
export interface PaginationNumberLinkContext extends PaginationLinkContext {
/** Contain the page number */
$implicit: number;
}
57 changes: 42 additions & 15 deletions src/pagination/pagination.component.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
<ul class="pagination" [ngClass]="classMap">
<li class="pagination-first page-item"
*ngIf="boundaryLinks"
[class.disabled]="noPrevious()||disabled">
<a class="page-link" href (click)="selectPage(1, $event)"
[innerHTML]="getText('first')"></a>
[class.disabled]="noPrevious() || disabled">
<a class="page-link" href (click)="selectPage(1, $event)">
<ng-container [ngTemplateOutlet]="customFirstTemplate || defaultFirstTemplate"
[ngTemplateOutletContext]="{disabled: noPrevious() || disabled, currentPage: page}">
</ng-container>
</a>
</li>

<li class="pagination-prev page-item"
*ngIf="directionLinks"
[class.disabled]="noPrevious()||disabled">
<a class="page-link" href (click)="selectPage(page - 1, $event)"
[innerHTML]="getText('previous')"></a>
[class.disabled]="noPrevious() || disabled">
<a class="page-link" href (click)="selectPage(page - 1, $event)">
<ng-container [ngTemplateOutlet]="customPreviousTemplate || defaultPreviousTemplate"
[ngTemplateOutletContext]="{disabled: noPrevious() || disabled, currentPage: page}">
</ng-container>
</a>
</li>

<li *ngFor="let pg of pages"
[class.active]="pg.active"
[class.disabled]="disabled&&!pg.active"
[class.disabled]="disabled && !pg.active"
class="pagination-page page-item">
<a class="page-link" href (click)="selectPage(pg.number, $event)"
[innerHTML]="pg.text"></a>
<a class="page-link" href (click)="selectPage(pg.number, $event)">
<ng-container [ngTemplateOutlet]="customPageTemplate || defaultPageTemplate"
[ngTemplateOutletContext]="{disabled: disabled, $implicit: pg.number, currentPage: page}">
</ng-container>
</a>
</li>

<li class="pagination-next page-item"
*ngIf="directionLinks"
[class.disabled]="noNext()||disabled">
<a class="page-link" href (click)="selectPage(page + 1, $event)"
[innerHTML]="getText('next')"></a></li>
[class.disabled]="noNext() || disabled">
<a class="page-link" href (click)="selectPage(page + 1, $event)">
<ng-container [ngTemplateOutlet]="customNextTemplate || defaultNextTemplate"
[ngTemplateOutletContext]="{disabled: noNext() || disabled, currentPage: page}">
</ng-container>
</a>
</li>

<li class="pagination-last page-item"
*ngIf="boundaryLinks"
[class.disabled]="noNext()||disabled">
<a class="page-link" href (click)="selectPage(totalPages, $event)"
[innerHTML]="getText('last')"></a></li>
[class.disabled]="noNext() || disabled">
<a class="page-link" href (click)="selectPage(totalPages, $event)">
<ng-container [ngTemplateOutlet]="customLastTemplate || defaultLastTemplate"
[ngTemplateOutletContext]="{disabled: noNext() || disabled, currentPage: page}">
</ng-container>
</a>
</li>
</ul>

<ng-template #defaultPageTemplate let-page>{{ page }}</ng-template>

<ng-template #defaultNextTemplate>{{ getText('next') }}</ng-template>

<ng-template #defaultPreviousTemplate>{{ getText('previous') }}</ng-template>

<ng-template #defaultFirstTemplate>{{ getText('first') }}</ng-template>

<ng-template #defaultLastTemplate>{{ getText('last') }}</ng-template>
Loading

0 comments on commit fd73917

Please sign in to comment.