|
| 1 | +import { Component, forwardRef, Inject, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { PageEvent } from '@angular/material/paginator'; |
| 3 | +import { BaseWidget, NgAisInstantSearch } from 'angular-instantsearch'; |
| 4 | +import { connectPagination } from 'instantsearch.js/es/connectors'; |
| 5 | + |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'mat-algolia-pagination', |
| 9 | + templateUrl: './mat-algolia-pagination.component.html', |
| 10 | + styleUrls: ['./mat-algolia-pagination.component.scss'] |
| 11 | +}) |
| 12 | +export class MatAlgoliaPaginationComponent extends BaseWidget implements OnInit { |
| 13 | + |
| 14 | + @ViewChild('hpp') |
| 15 | + hitsPerPage: any; |
| 16 | + |
| 17 | + items = [ |
| 18 | + { label: '1 hits per page', value: 10, default: true }, |
| 19 | + { label: '2 hits per page', value: 15 }, |
| 20 | + { label: '2 hits per page', value: 25 }, |
| 21 | + { label: '2 hits per page', value: 50 } |
| 22 | + ]; |
| 23 | + |
| 24 | + pageSize: number; |
| 25 | + |
| 26 | + public state: { |
| 27 | + pages: number[]; |
| 28 | + currentRefinement: number; |
| 29 | + nbHits: number; |
| 30 | + nbPages: number; |
| 31 | + isFirstPage: boolean; |
| 32 | + isLastPage: boolean; |
| 33 | + refine: Function; |
| 34 | + createURL: Function; |
| 35 | + widgetParams: Function; |
| 36 | + }; |
| 37 | + |
| 38 | + constructor( |
| 39 | + @Inject(forwardRef(() => NgAisInstantSearch)) |
| 40 | + public instantSearchParent: NgAisInstantSearch |
| 41 | + ) { |
| 42 | + super('MatAlgoliaPaginationComponent'); |
| 43 | + } |
| 44 | + |
| 45 | + ngOnInit() { |
| 46 | + this.createWidget(connectPagination, { |
| 47 | + // instance options |
| 48 | + }); |
| 49 | + super.ngOnInit(); |
| 50 | + } |
| 51 | + |
| 52 | + onPageChanged(pageEvent: PageEvent, hpp) { |
| 53 | + console.log('onPageChanged', pageEvent, this.pageSize, pageEvent.pageSize); |
| 54 | + if (this.pageSize !== pageEvent.pageSize) { |
| 55 | + console.log('refining', this.pageSize, pageEvent.pageSize); |
| 56 | + this.pageSize = pageEvent.pageSize; |
| 57 | + hpp.state.refine(this.pageSize); |
| 58 | + } |
| 59 | + if (pageEvent.pageIndex !== pageEvent.previousPageIndex) { |
| 60 | + this.state.refine(this.state.currentRefinement + (pageEvent.pageIndex > pageEvent.previousPageIndex ? 1 : -1)); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + getDefaultPageSize(hpp) { |
| 65 | + // this.pageSize = hpp.state.items.filter(item => item.default === true).map(item => item.value)[0]; |
| 66 | + // return this.pageSize; |
| 67 | + return hpp.state.items.filter(item => item.default === true).map(item => item.value)[0]; |
| 68 | + } |
| 69 | + |
| 70 | + getPageSizeOptions(hpp) { |
| 71 | + if (hpp && hpp.state.items) { |
| 72 | + // console.log('page size options', hpp.state.items.map(item => item.value)); |
| 73 | + return hpp.state.items.map(item => item.value); |
| 74 | + } else { |
| 75 | + return 0; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments