<commit_before>import { map } from 'lodash';
import {
  BaseModel, identifiedBy, belongsTo, hasMany, observable, computed, action,
} from 'shared/model';
import Exercise from './exercises/exercise';


@identifiedBy('search/clause')
class Clause extends BaseModel {

  @observable filter = 'uid';
  @observable value = '';
  @belongsTo({ model: 'search' }) search;

  @computed get description() {
    return `Search by ${this.filter}`;
  }

  @action.bound onKey(e) {
    if(e.keyCode == 13 && e.shiftKey == false) {
      this.search.perform();
    }
  }

  @action.bound onSelect(evKey) {
    this.filter = evKey;
  }

  @action.bound setValue({ target: { value } }) {
    this.value = value;
  }

  get asQuery() {
    return `${this.filter}:"${this.value}"`;
  }
}

export default
@identifiedBy('search')
class Search extends BaseModel {

  @hasMany({ model: Clause, inverseOf: 'search' }) clauses;
  @hasMany({ model: Exercise }) exercises;
  @observable total_count = 0;
  @observable perPageSize = 25;
  @observable currentPage = 1;

  constructor() {
    super();
    if (!this.clauses.length) { this.clauses.push({}); }
  }

  @action.bound execute() {
    this.perform();
  }

  @action.bound setPerPageSize(size) {
    this.perPageSize = Number(size);
    this.perform();
  }


  @action.bound onPageChange(pg) {
    this.currentPage = pg;
    this.perform();
  }

  onComplete({ data: { total_count, items } }) {
    this.total_count = total_count;
    this.exercises = items;
  }

  @computed get pagination() {
    if (!this.total_count) {
      return null;
    }

    return {
      currentPage: this.currentPage ,
      totalPages: Math.floor(this.total_count / this.perPageSize) + 1,
      onChange: this.onPageChange,
    };
  }


  //called by api
  perform() {
    return {
      query: {
        q: map(this.clauses, 'asQuery').join(' '),
        per_page: this.perPageSize,
        page: this.currentPage,
      },
    };
  }

}
<commit_msg>adjust current page when page size changes<commit_after>import { map } from 'lodash';
import {
  BaseModel, identifiedBy, belongsTo, hasMany, observable, computed, action,
} from 'shared/model';
import Exercise from './exercises/exercise';


@identifiedBy('search/clause')
class Clause extends BaseModel {

  @observable filter = 'uid';
  @observable value = '';
  @belongsTo({ model: 'search' }) search;

  @computed get description() {
    return `Search by ${this.filter}`;
  }

  @action.bound onKey(e) {
    if(e.keyCode == 13 && e.shiftKey == false) {
      this.search.perform();
    }
  }

  @action.bound onSelect(evKey) {
    this.filter = evKey;
  }

  @action.bound setValue({ target: { value } }) {
    this.value = value;
  }

  get asQuery() {
    return `${this.filter}:"${this.value}"`;
  }
}

export default
@identifiedBy('search')
class Search extends BaseModel {

  @hasMany({ model: Clause, inverseOf: 'search' }) clauses;
  @hasMany({ model: Exercise }) exercises;
  @observable total_count = 0;
  @observable perPageSize = 25;
  @observable currentPage = 1;

  constructor() {
    super();
    if (!this.clauses.length) { this.clauses.push({}); }
  }

  @action.bound execute() {
    this.perform();
  }

  @action.bound setPerPageSize(size) {
    const firstExercise = this.perPageSize * this.currentPage;
    this.perPageSize = Number(size);
    // keep cursor showing roughly the same exercise
    this.currentPage = Math.floor(firstExercise / this.perPageSize);
    this.perform();
  }


  @action.bound onPageChange(pg) {
    this.currentPage = pg;
    this.perform();
  }

  onComplete({ data: { total_count, items } }) {
    this.total_count = total_count;
    this.exercises = items;
  }

  @computed get pagination() {
    if (!this.total_count) {
      return null;
    }

    return {
      currentPage: this.currentPage ,
      totalPages: Math.floor(this.total_count / this.perPageSize) + 1,
      onChange: this.onPageChange,
    };
  }


  //called by api
  perform() {
    return {
      query: {
        q: map(this.clauses, 'asQuery').join(' '),
        per_page: this.perPageSize,
        page: this.currentPage,
      },
    };
  }

}

{'fraction_non_alphanumeric': 0.14896185417672622, 'fraction_numerical': 0.003380009657170449, 'mean_word_length': 2.991329479768786, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5424757', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1369, 'n_words': 302}