<commit_before>﻿import {
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter,
  ChangeDetectionStrategy,
} from '@angular/core';

import { MdSelectChange } from '@angular/material';

import { BaseComponent } from './helpers';
import { IDatatablePaginationEvent } from './md-datatable.interfaces';

@Component({
  selector: 'ng2-md-datatable-pagination',
  template: `
    <span>Rows per page:</span>
    <md-select [ngModel]="itemsPerPage" (change)="onSelectChange($event)" class="pagination__itemsPerPage">
      <md-option *ngFor="let choice of itemsPerPageChoices"
        [value]="choice">{{ choice }}</md-option>
    </md-select>
    <span class="pagination__range">{{firstIndexOfPage}}-{{lastIndexOfPage}} of {{itemsCount}}</span>
    <div class="pagination__controls">
      <button md-icon-button
        (click)="onClickFirst()"
        aria-label="First">
        <md-icon>first_page</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isPreviousButtonEnabled"
        (click)="onClickPrevious()"
        aria-label="Previous">
        <md-icon>navigate_before</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isNextOrLastButtonEnabled"
        (click)="onClickNext()"
        aria-label="Next">
        <md-icon>navigate_next</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isNextOrLastButtonEnabled"
        (click)="onClickLast()"
        aria-label="Last">
        <md-icon>last_page</md-icon>
      </button>
    </div>
  `,
  styleUrls: ['md-datatable-pagination.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdDataTablePaginationComponent extends BaseComponent implements OnInit {
  @Input() currentPage: number;
  @Input() itemsPerPage: number;
  @Input() itemsCount: number;
  @Input() itemsPerPageChoices: Array<number> = [5, 10, 20, 50];
  @Input() itemsPerPageFirstChoice = 10;

  @Output() paginationChange: EventEmitter<IDatatablePaginationEvent>;

  get firstIndexOfPage() {
    return this.currentPage * this.itemsPerPage - this.itemsPerPage + 1;
  }

  get lastIndexOfPage() {
      const maxLastIndexOnPage = this.currentPage * this.itemsPerPage;
      return maxLastIndexOnPage >= this.itemsCount ? this.itemsCount : maxLastIndexOnPage;
  }

  get isPreviousButtonEnabled() {
    return this.firstIndexOfPage === 1;
  }

  get isNextOrLastButtonEnabled() {
    return this.lastIndexOfPage >= this.itemsCount;
  }

  constructor() {
    super();
    this.paginationChange = new EventEmitter<IDatatablePaginationEvent>(true);
  }

  ngOnInit() {
    // set defaults values if not provided
    if (!this.currentPage) {
      this.currentPage = 1;
    }

    if (!this.itemsPerPage) {
      this.itemsPerPage = this.itemsPerPageFirstChoice;
    }

    if (!this.itemsCount) {
      this.itemsCount = 0;
    }
  }

  onSelectChange(event: MdSelectChange) {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: 1,
      itemsPerPage: Number(event.value),
    });
  }

  onClickFirst() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: 1,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickLast() {
    const lastPage = Math.round(this.itemsCount / this.itemsPerPage);

    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: lastPage,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickPrevious() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: this.currentPage - 1,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickNext() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: this.currentPage + 1,
      itemsPerPage: this.itemsPerPage,
    });
  }
}<commit_msg>Fixing bug where last page button isn't returning the correct last page<commit_after>﻿import {
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter,
  ChangeDetectionStrategy,
} from '@angular/core';

import { MdSelectChange } from '@angular/material';

import { BaseComponent } from './helpers';
import { IDatatablePaginationEvent } from './md-datatable.interfaces';

@Component({
  selector: 'ng2-md-datatable-pagination',
  template: `
    <span>Rows per page:</span>
    <md-select [ngModel]="itemsPerPage" (change)="onSelectChange($event)" class="pagination__itemsPerPage">
      <md-option *ngFor="let choice of itemsPerPageChoices"
        [value]="choice">{{ choice }}</md-option>
    </md-select>
    <span class="pagination__range">{{firstIndexOfPage}}-{{lastIndexOfPage}} of {{itemsCount}}</span>
    <div class="pagination__controls">
      <button md-icon-button
        (click)="onClickFirst()"
        aria-label="First">
        <md-icon>first_page</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isPreviousButtonEnabled"
        (click)="onClickPrevious()"
        aria-label="Previous">
        <md-icon>navigate_before</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isNextOrLastButtonEnabled"
        (click)="onClickNext()"
        aria-label="Next">
        <md-icon>navigate_next</md-icon>
      </button>
      <button md-icon-button
        [disabled]="isNextOrLastButtonEnabled"
        (click)="onClickLast()"
        aria-label="Last">
        <md-icon>last_page</md-icon>
      </button>
    </div>
  `,
  styleUrls: ['md-datatable-pagination.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdDataTablePaginationComponent extends BaseComponent implements OnInit {
  @Input() currentPage: number;
  @Input() itemsPerPage: number;
  @Input() itemsCount: number;
  @Input() itemsPerPageChoices: Array<number> = [5, 10, 20, 50];
  @Input() itemsPerPageFirstChoice = 10;

  @Output() paginationChange: EventEmitter<IDatatablePaginationEvent>;

  get firstIndexOfPage() {
    return this.currentPage * this.itemsPerPage - this.itemsPerPage + 1;
  }

  get lastIndexOfPage() {
      const maxLastIndexOnPage = this.currentPage * this.itemsPerPage;
      return maxLastIndexOnPage >= this.itemsCount ? this.itemsCount : maxLastIndexOnPage;
  }

  get isPreviousButtonEnabled() {
    return this.firstIndexOfPage === 1;
  }

  get isNextOrLastButtonEnabled() {
    return this.lastIndexOfPage >= this.itemsCount;
  }

  constructor() {
    super();
    this.paginationChange = new EventEmitter<IDatatablePaginationEvent>(true);
  }

  ngOnInit() {
    // set defaults values if not provided
    if (!this.currentPage) {
      this.currentPage = 1;
    }

    if (!this.itemsPerPage) {
      this.itemsPerPage = this.itemsPerPageFirstChoice;
    }

    if (!this.itemsCount) {
      this.itemsCount = 0;
    }
  }

  onSelectChange(event: MdSelectChange) {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: 1,
      itemsPerPage: Number(event.value),
    });
  }

  onClickFirst() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: 1,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickLast() {
    const lastPage = Math.ceil(this.itemsCount / this.itemsPerPage);

    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: lastPage,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickPrevious() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: this.currentPage - 1,
      itemsPerPage: this.itemsPerPage,
    });
  }

  onClickNext() {
    this.paginationChange.emit(<IDatatablePaginationEvent>{
      page: this.currentPage + 1,
      itemsPerPage: this.itemsPerPage,
    });
  }
}
{'fraction_non_alphanumeric': 0.1332361129524062, 'fraction_numerical': 0.004772636881877237, 'mean_word_length': 3.4376470588235293, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6335722', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2382, 'n_words': 405}