<commit_before>'use strict';
import { Button, FormInput, InputGroup, Modal, Pagination } from 'elemental';
import qs from 'qs';
import xhr from 'xhr';
import React from 'react';


let SelectorMixin = (superclass) => class extends superclass {
    constructor(props) {
        super(props);

        // input in the search field
        this._searchInput = '';
        this.PAGE_SIZE = 10;
        this.API = '/api/';

        this.state = {
            currentPage: 1,
            error: null,
            isSelectionOpen: props.isSelectionOpen,
            items: [],
            selectedItems: [],
            total: 0,
        };

        // method binding
        this.searchFilterChange = this._searchFilterChange.bind(this);
        this.updateSelection = this._updateSelection.bind(this);
        this.handlePageSelect = this._handlePageSelect.bind(this);
        this.searchByInput = this._searchByInput.bind(this);
        this.handleCancel = this._handleCancel.bind(this);
        this.handleSave = this._handleSave.bind(this);
    }

    componentWillMount () {
        this._getItems();
    }

    componentWillUnmount () {
        this._searchInput = '';
    }

    componentWillReceiveProps (nextProps) {
        this.setState({
            isSelectionOpen: nextProps.isSelectionOpen,
            selectedItems: nextProps.selectedItems
        });
    }

    _handleCancel () {
        this.toggleSelect(false);
        this.props.onFinish();
    }
    _handleSave () {
        this.toggleSelect(false);
        this.props.onChange(this.state.selectedItems);
        this.props.onFinish();
    }

    /** build query string for keystone api
     * @param {number} [page=0] - Page we used to calculate how many items we want to skip
     * @param {limit} [limit=10] - The number of items we want to get
     * @param {string[]} [filters=[]] - keywords for filtering
     * @return {Promise}
     */
    _buildQueryString (page=0, limit=10, filters=[]) {
        return Promise.resolve(this._buildFilters(filters, page, this.PAGE_SIZE));
    }

    /** build query string filtered by description for keystone api
     * @param {string[]} [filters=[]] - keywords for filtering
     * @param {number} [page=0] - Page we used to calculate how many items we want to skip
     * @param {limit} [limit=10] - The number of items we want to get
     * @return {string} a query string
     */
    _buildFilters (filters=[], page=0, limit=10) {
        let filterQuery = {
            description: {
                value: filters
            }
        };
        let queryString = {
            filters: JSON.stringify(filterQuery),
            select: 'image,description,keywords',
            limit: limit,
            skip: page === 0 ? 0 : (page-1) * limit
        };
        return qs.stringify(queryString);
    }

    /** load images from keystone api
     * @param {string} [queryString=] - Query string for keystone api
     * @return {Promise}
     */
	loadItems (queryString = '') {
        return new Promise((resolve, reject) => {
            xhr({
                url: Keystone.adminPath + this.API + this.props.apiPath + '?' + queryString,
                responseType: 'json',<commit_msg>Update SelectorMixin.js<commit_after>'use strict';
import { Button, FormInput, InputGroup, Modal, Pagination } from 'elemental';
import qs from 'qs';
import xhr from 'xhr';
import React from 'react';


let SelectorMixin = (superclass) => class extends superclass {
    constructor(props) {
        super(props);

        // input in the search field
        this._searchInput = '';
        this.PAGE_SIZE = 10;
        this.API = '/api/';

        this.state = {
            currentPage: 1,
            error: null,
            isSelectionOpen: props.isSelectionOpen,
            items: [],
            selectedItems: [],
            total: 0,
        };

        // method binding
        this.searchFilterChange = this._searchFilterChange.bind(this);
        this.updateSelection = this._updateSelection.bind(this);
        this.handlePageSelect = this._handlePageSelect.bind(this);
        this.searchByInput = this._searchByInput.bind(this);
        this.handleCancel = this._handleCancel.bind(this);
        this.handleSave = this._handleSave.bind(this);
    }

    componentWillMount () {
        this._getItems();
    }

    componentWillUnmount () {
        this._searchInput = '';
    }

    componentWillReceiveProps (nextProps) {
        this.setState({
            isSelectionOpen: nextProps.isSelectionOpen,
            selectedItems: nextProps.selectedItems
        });
    }

    _handleCancel () {
        this.toggleSelect(false);
        this.props.onFinish();
    }
    _handleSave () {
        this.toggleSelect(false);
        this.props.onChange(this.state.selectedItems);
        this.props.onFinish();
    }

    /** build query string for keystone api
     * @param {number} [page=0] - Page we used to calculate how many items we want to skip
     * @param {limit} [limit=10] - The number of items we want to get
     * @param {string[]} [filters=[]] - keywords for filtering
     * @return {Promise}
     */
    _buildQueryString (page=0, limit=10, filters=[]) {
        return Promise.resolve(this._buildFilters(filters, page, this.PAGE_SIZE));
    }

    /** build query string for keystone api
     * @param {string[]} [filters=[]] - keywords for filtering
     * @param {number} [page=0] - Page we used to calculate how many items we want to skip
     * @param {limit} [limit=10] - The number of items we want to get
     * @return {string} a query string
     */
    _buildFilters (filters=[], page=0, limit=10) {
        let queryString = {
            limit: limit,
            skip: page === 0 ? 0 : (page-1) * limit
        };
        return qs.stringify(queryString);
    }

    /** load items from keystone api
     * @param {string} [queryString=] - Query string for keystone api
     * @return {Promise}
     */
	loadItems (queryString = '') {
        return new Promise((resolve, reject) => {
            xhr({
                url: Keystone.adminPath + this.API + this.props.apiPath + '?' + queryString,
                responseType: 'json',
{'fraction_non_alphanumeric': 0.1195862960568843, 'fraction_numerical': 0.006140917905623788, 'mean_word_length': 2.218408736349454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3130207', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1716, 'n_words': 450}