From 383e3e1e50ea0788a8bd3ba0ceaeef37061e0fa4 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 3 Jan 2020 17:52:13 +0300 Subject: [PATCH] feat(theme-shared): create paginator component #2537 --- .../paginator/paginator.component.html | 38 ++++++++++++++ .../paginator/paginator.component.ts | 51 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.html create mode 100644 npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.ts diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.html b/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.html new file mode 100644 index 0000000000..56e1e5545f --- /dev/null +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.html @@ -0,0 +1,38 @@ +
+ {{ page }} +
diff --git a/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.ts b/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.ts new file mode 100644 index 0000000000..453b79193d --- /dev/null +++ b/npm/ng-packs/packages/theme-shared/src/lib/components/paginator/paginator.component.ts @@ -0,0 +1,51 @@ +import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'; + +@Component({ + selector: 'abp-paginator', + templateUrl: 'paginator.component.html', +}) +export class PaginatorComponent implements OnInit { + private _value = 1; + @Input() + get value(): number { + return this._value; + } + set value(newValue: number) { + if (newValue < 1) return; + else if (newValue > this.totalPages) return; + + this._value = newValue; + this.valueChange.emit(newValue); + } + + @Output() + readonly valueChange = new EventEmitter(); + + @Input() + totalPages = 0; + + get pageArray(): number[] { + const count = this.totalPages < 5 ? this.totalPages : 5; + + if (this.value === 1 || this.value === 2) { + return Array.from(new Array(count)).map((_, index) => index + 1); + } else if (this.value === this.totalPages || this.value === this.totalPages - 1) { + return Array.from(new Array(count)).map((_, index) => this.totalPages - count + 1 + index); + } else { + return [this.value - 2, this.value - 1, this.value, this.value + 1, this.value + 2]; + } + } + + ngOnInit() { + if (!this.value || this.value < 1 || this.value > this.totalPages) { + this.value = 1; + } + } + + changePage(page: number) { + if (page < 1) return; + else if (page > this.totalPages) return; + + this.value = page; + } +}