Skip to content

Instantly share code, notes, and snippets.

@MaximBalaganskiy
Last active June 14, 2020 02:19
Show Gist options
  • Select an option

  • Save MaximBalaganskiy/adf2cb1524a26176e08b79f5442672b1 to your computer and use it in GitHub Desktop.

Select an option

Save MaximBalaganskiy/adf2cb1524a26176e08b79f5442672b1 to your computer and use it in GitHub Desktop.
ux-pagination
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dumber Gist</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<base href="/">
</head>
<!--
Dumber gist uses dumber bundler, the default bundle file
is /dist/entry-bundle.js.
The starting module is pointed to aurelia-bootstrapper
(data-main attribute on script) for Aurelia,
The aurelia bootstrapper then loads up user module "main"
(aurelia-app attribute on <body>) which is your src/main.ts.
-->
<body aurelia-app="main">
<script src="/dist/entry-bundle.js" data-main="aurelia-bootstrapper"></script>
</body>
</html>
{
"dependencies": {
"aurelia-bootstrapper": "^2.3.3"
}
}
<template>
<style>
body {
font-family: 'Roboto', sans-serif;
}
</style>
<require from="./ux-pagination/ux-pagination"></require>
<!-- Try to create a css/scss/sass/less file then require it here -->
<h1>${message}</h1>
<ux-pagination active-page.bind="activePage" pages="12" visible-pages="10"></ux-pagination>
Active page ${activePage}
</template>
export class App {
public message: string = 'Hello Aurelia!';
activePage = 2;
}
import {Aurelia, PLATFORM} from 'aurelia-framework';
import {} from '@aurelia-ux/button';
import icons from '@aurelia-ux/icons/sets/full-array.json';
export function configure(aurelia: Aurelia) {
aurelia.use
.standardConfiguration()
.plugin(PLATFORM.moduleName('@aurelia-ux/core'))
.plugin(PLATFORM.moduleName('@aurelia-ux/button'))
.plugin(PLATFORM.moduleName('@aurelia-ux/icons'), { icons: icons })
.developmentLogging('info');
aurelia.start().then(() => aurelia.setRoot());
}
.ux-pagination {
display: flex;
align-items: center;
}
.ux-pagination .ux-pagination__page:not(.ux-pagination__page--active) button {
background-color: transparent !important;
box-shadow: none !important;
color: var(--aurelia-ux--design-control-foreground);
transition: none;
}
.ux-pagination .ux-pagination__page:not(.ux-pagination__page--active) button:disabled {
color: var(--aurelia-ux--design-disabled-foreground);
}
<template class="ux-pagination">
<require from="./ux-pagination.scss"></require>
<ux-button if.bind="showFirstLast" type="fab" size="small" click.delegate="setFirstPage()"
disabled.bind="activePage === 1" class="ux-pagination__page">
<ux-icon icon="first_page"></ux-icon>
</ux-button>
<ux-button if.bind="showPrevNext" type="fab" size="small" click.delegate="setPreviousPage()"
disabled.bind="activePage === 1" class="ux-pagination__page">
<ux-icon icon="chevron_left"></ux-icon>
</ux-button>
<template if.bind="showPageLinks">
<ux-button if.bind="pageLinks[0] > 1" type="fab" size="small" click.delegate="setActivePage(pageLinks[0])" class="ux-pagination__page">
<ux-icon icon="more_horiz"></ux-icon>
</ux-button>
<ux-button repeat.for="p of pageLinks" type="fab" size="small" click.delegate="setActivePage(p)" class="ux-pagination__page ${p === activePage ? 'ux-pagination__page--active' : ''}">
${p}
</ux-button>
<ux-button if.bind="pageLinks[visiblePages-1] < pages" type="fab" size="small" click.delegate="setActivePage(pageLinks[visiblePages-1])" class="ux-pagination__page">
<ux-icon icon="more_horiz"></ux-icon>
</ux-button>
</template>
<ux-button if.bind="showPrevNext" type="fab" size="small" click.delegate="setNextPage()"
disabled.bind="activePage === pages" class="ux-pagination__page">
<ux-icon icon="chevron_right"></ux-icon>
</ux-button>
<ux-button if.bind="showFirstLast" type="fab" size="small" click.delegate="setLastPage()"
disabled.bind="activePage === pages" class="ux-pagination__page">
<ux-icon icon="last_page"></ux-icon>
</ux-button>
</template>
import { customElement, useView, PLATFORM, bindingMode } from 'aurelia-framework';
import { bindable } from 'aurelia-typed-observable-plugin';
@customElement('ux-pagination')
@useView(PLATFORM.moduleName('./ux-pagination.html'))
export class UxPagination {
@bindable({ defaultBindingMode: bindingMode.twoWay })
activePage: number = 1;
@bindable.number
pages: number = 5;
pagesChanged() {
this.setActivePage(1);
}
@bindable.number
visiblePages: number = 15;
visiblePagesChanged() {
this.pageLinks = this.generatePageLinks();
}
pageLinks: number[] = [];
@bindable.booleanAttr
showFirstLast: boolean = true;
@bindable.booleanAttr
showPrevNext: boolean = true;
@bindable.booleanAttr
showPageLinks: boolean = true;
bind() {
this.pageLinks = this.generatePageLinks();
}
setActivePage(page: number) {
this.activePage = page;
this.pageLinks = this.generatePageLinks();
}
setFirstPage() {
if (this.activePage > 1) {
this.setActivePage(1);
}
}
setLastPage() {
if (this.activePage < this.pages) {
this.setActivePage(this.pages);
}
}
setPreviousPage() {
if (this.activePage > 1) {
this.setActivePage(this.activePage - 1);
}
}
setNextPage() {
if (this.activePage < this.pages) {
this.setActivePage(this.activePage + 1);
}
}
generatePageLinks(): number[] {
const numberOfLinks = Math.min(this.visiblePages, this.pages);
const midPoint = Math.ceil(numberOfLinks / 2);
let start = Math.max(this.activePage - midPoint, 1);
// respect visible links
if (start + midPoint * 2 > this.pages) {
start = Math.max(this.pages + 1 - midPoint * 2, 1);
}
const end = Math.min(start + numberOfLinks - 1, this.pages);
const list: number[] = [];
for (let i = start; i <= end; i++) {
list.push(i);
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment