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


interface PageNumber {
  number: number,
  text: string,
  active: boolean
}

@Component({
  selector: 'ui-pagination',
  template: `
    <div class="ui pagination menu">
      <a class="item" *ngFor="let page of pageNumberList" [ngClass]="{disabled: page === '...', active: page === currentPage}" (click)="onClickPage(page)">
        {{page.text}}
      </a>
    </div>
`
})
export class UIPagination {

  _currentPageNumber: number;
  private _total: number;
  private _countPerPage: number;
  private _max: number = Number.MAX_SAFE_INTEGER;

  pageNumberList: PageNumber[] = [];

  @Output()
  pageChange = new EventEmitter<number>();

  @Input()
  set currentPage(page: number) {
    if(page !== this._currentPageNumber) {
      this._currentPageNumber = page;
      this.pageNumberList = this.updatePageNumberList();
    }
  }

  get currentPage(): number {
    return this._currentPageNumber;
  }

  @Input()
  set total(total: number) {
    this._total = total;
    if(!this.isUndefined(this._total) && !this.isUndefined(this._currentPageNumber) && !this.isUndefined(this._countPerPage)) {
      this.pageNumberList = this.updatePageNumberList();
    }
  }

  @Input()
  set countPerPage(count: number) {
    this._countPerPage = count;
    this.pageNumberList = this.updatePageNumberList();
  }

  @Input()
  set max(max: number) {
    this._max = max;
  }

  onClickPage(page: PageNumber) {
    if(page.text === '...') {
      return;
    }
    if(page.number !== this._currentPageNumber) {
      this.currentPage = page.number;
      this.pageChange.emit(page.number);
    }
  }

  private isUndefined(obj: any) {
    return typeof obj === 'undefined';
  }

  private makePage(number: number, text: string, isActive: boolean): PageNumber {
    return {
      number: number,
      text: text,
      active: isActive
    }
  }

  private updatePageNumberList(): PageNumber[] {
    console.log('rebuild pages');
    let totalPages = Math.ceil(this._total / this._countPerPage);
    let pages: PageNumber [] = [];
    let startPage = 1, endPage = totalPages;
    if(totalPages > this._max) {
      // Visible pages are paginated with maxSize
      startPage = (Math.ceil(this._currentPageNumber / this._max) - 1) * this._max + 1;
      // Adjust last page if limit is exceeded
      endPage = Math.min(startPage + this._max - 1, totalPages);
    }
    for(let i = startPage; i <= endPage; i++) {
      pages.push(this.makePage(i, i + '', i === this._currentPageNumber));
    }

    if(totalPages > this._max) {
      if(startPage > 1) {
        if(startPage > 3) {
          let previousPageSet = this.makePage(startPage - 1, '...', false);
          pages.unshift(previousPageSet);
        }
        if(startPage === 3) {
          let secondPageLink = this.makePage(2, '2', false);
          pages.unshift(secondPageLink);
        }

        var firstPageLink = this.makePage(1, '1', false);
        pages.unshift(firstPageLink);
      }

      if(endPage < totalPages) {
        if(endPage < totalPages - 2) {
          let nextPageSet = this.makePage(endPage + 1, '...', false);
          pages.push(nextPageSet);
        }
        if(endPage === totalPages -2) {
          let secondToLastPageLink = this.makePage(totalPages -1 , (totalPages -1) + '', false);
          pages.push(secondToLastPageLink);
        }
        let lastPageList = this.makePage(totalPages, totalPages + '', false);
        pages.push(lastPageList);
      }
    }
    return pages;
  }
}<commit_msg>fix the bug in pagination<commit_after>import {Component, Input, Output, EventEmitter} from '@angular/core';


interface PageNumber {
  number: number,
  text: string,
  active: boolean
}

@Component({
  selector: 'ui-pagination',
  template: `
    <div class="ui pagination menu">
      <a class="item" *ngFor="let page of pageNumberList" [ngClass]="{disabled: page.text === '...', active: page.active}" (click)="onClickPage(page)">
        {{page.text}}
      </a>
    </div>
`
})
export class UIPagination {

  _currentPageNumber: number;
  private _total: number;
  private _countPerPage: number;
  private _max: number = Number.MAX_SAFE_INTEGER;

  pageNumberList: PageNumber[] = [];

  @Output()
  pageChange = new EventEmitter<number>();

  @Input()
  set currentPage(page: number) {
    if(page !== this._currentPageNumber) {
      this._currentPageNumber = page;
      this.pageNumberList = this.updatePageNumberList();
    }
  }

  get currentPage(): number {
    return this._currentPageNumber;
  }

  @Input()
  set total(total: number) {
    this._total = total;
    if(!this.isUndefined(this._total) && !this.isUndefined(this._currentPageNumber) && !this.isUndefined(this._countPerPage)) {
      this.pageNumberList = this.updatePageNumberList();
    }
  }

  @Input()
  set countPerPage(count: number) {
    this._countPerPage = count;
    this.pageNumberList = this.updatePageNumberList();
  }

  @Input()
  set max(max: number) {
    this._max = max;
    this.pageNumberList = this.updatePageNumberList();
  }

  onClickPage(page: PageNumber) {
    if(page.text === '...') {
      return;
    }
    if(page.number !== this._currentPageNumber) {
      this.currentPage = page.number;
      this.pageChange.emit(page.number);
    }
  }

  private isUndefined(obj: any) {
    return typeof obj === 'undefined';
  }

  private makePage(number: number, text: string, isActive: boolean): PageNumber {
    return {
      number: number,
      text: text,
      active: isActive
    }
  }

  private updatePageNumberList(): PageNumber[] {
    console.log('rebuild pages');
    let maxSize = this._max;
    let currentPage = this.currentPage;
    let totalPages = Math.ceil(this._total / this._countPerPage);
    let pages: PageNumber [] = [];
    let startPage = 1, endPage = totalPages;
    let isMaxSized = maxSize < totalPages;


    if (isMaxSized) {
        startPage = Math.max(currentPage - Math.floor(maxSize / 2), 1);
        endPage = startPage + maxSize - 1;

        if (endPage > totalPages) {
            endPage = totalPages;
            startPage = endPage - maxSize + 1;
        }
    }
    for (let number = startPage; number < endPage; number++) {
        pages.push(this.makePage(number, number + '', number === currentPage));
    }

    if (isMaxSized && maxSize > 0) {
        if (startPage > 1) {
            if (startPage > 3) {
                pages.unshift(this.makePage(startPage - 1, '...', false));
            }
            if (startPage === 3) {
                pages.unshift(this.makePage(2, '2', false));
            }
            // add the first page
            pages.unshift(this.makePage(1, '1', false));
        }
        if (endPage < totalPages) {
            if (endPage < totalPages -2) {
                pages.push(this.makePage(endPage + 1, '...', false));
            }
            if (endPage === totalPages -2) {
                pages.push(this.makePage(totalPages - 1, (totalPages - 1) + '', false));
            }
            pages.push(this.makePage(totalPages, totalPages + '', false));
        }
    }
    return pages;
  }
}
{'fraction_non_alphanumeric': 0.14160033514872225, 'fraction_numerical': 0.005027230833682447, 'mean_word_length': 2.6135216952573157, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 21, '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': '1527346', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2240, 'n_words': 482}