<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
    CoreLabeledDOMWidgetModel,
} from './widget_core';

import {
    LabeledDOMWidgetView, unpack_models, ViewList
} from './widget';

import * as _ from 'underscore';
import * as utils from './utils';
import * as $ from 'jquery';

function scrollIfNeeded(area, elem) {
    var ar = area.getBoundingClientRect();
    var er = elem.getBoundingClientRect();
    if (er.top < ar.top) {
        area.scrollTop -= ar.top - er.top;
    } else if (er.bottom > ar.bottom) {
        area.scrollTop += er.bottom - ar.bottom;
    }
}

export
class SelectionModel extends CoreLabeledDOMWidgetModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'SelectionModel',
            value: '',
            _options_labels: [],
            disabled: false,
        });
    }
}

export
class SelectModel extends SelectionModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'SelectModel',
            _view_name: 'SelectView'
        });
    }
}

export
class SelectView extends LabeledDOMWidgetView {
    /**
     * Public constructor.
     */
    initialize(parameters) {
        super.initialize(parameters);
        this.listenTo(this.model, 'change:_options_labels', () => this._updateOptions());
    }

    /**
     * Called when view is rendered.
     */
    render() {
        super.render();

        this.el.classList.add('jupyter-widgets');
        this.el.classList.add('widget-inline-hbox');
        this.el.classList.add('widget-select');

        this.listbox = document.createElement('select');
        this.el.appendChild(this.listbox);
        this._updateOptions();
        this.update();
    }

    /**
     * Update the contents of this view
     */
    update() {
        // Disable listbox if needed
        this.listbox.disabled = this.model.get('disabled');

        // Select the correct element
        var value = this.model.get('value');
        this.listbox.selectedIndex = this.model.get('_options_labels').indexOf(value);
        return super.update();
    }

    _updateOptions() {
        this.listbox.textContent = '';
        let items = this.model.get('_options_labels');
        for (let i = 0; i < items.length; i++) {
            let item = items[i];
            let option = document.createElement('option');
            option.textContent = item.replace(/ /g, '\xa0'); // space -> &nbsp;
            option.setAttribute('data-value', encodeURIComponent(item));
            option.value = item;
            this.listbox.appendChild(option);
        }
    }

    events(): {[e: string]: string} {
        return {
            'change select': '_handle_change'
        }
    }

    /**
     * Handle when a new value is selected.
     */
    _handle_change() {
        let value = this.listbox.options[this.listbox.selectedIndex].value;
        this.model.set('value', value);
        this.touch();
    }

    listbox: HTMLSelectElement;
}


export
class DropdownModel extends SelectionModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'DropdownModel',
            _view_name: 'DropdownView',
            button_style: ''
        });
    }
}

// TODO: Make a phosphor dropdown control, wrapped in DropdownView. Also, fix
// bugs in keyboard handling. See
// https://github.com/ipython/ipywidgets/issues/1055 and
// https://github.com/ipython/ipywidgets/issues/1049
// For now, we subclass SelectView to provide DropdownView
export
class DropdownView extends SelectView {}
<commit_msg>add note about commit where the dropdown code was removed<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import {
    CoreLabeledDOMWidgetModel,
} from './widget_core';

import {
    LabeledDOMWidgetView, unpack_models, ViewList
} from './widget';

import * as _ from 'underscore';
import * as utils from './utils';
import * as $ from 'jquery';

function scrollIfNeeded(area, elem) {
    var ar = area.getBoundingClientRect();
    var er = elem.getBoundingClientRect();
    if (er.top < ar.top) {
        area.scrollTop -= ar.top - er.top;
    } else if (er.bottom > ar.bottom) {
        area.scrollTop += er.bottom - ar.bottom;
    }
}

export
class SelectionModel extends CoreLabeledDOMWidgetModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'SelectionModel',
            value: '',
            _options_labels: [],
            disabled: false,
        });
    }
}

export
class SelectModel extends SelectionModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'SelectModel',
            _view_name: 'SelectView'
        });
    }
}

export
class SelectView extends LabeledDOMWidgetView {
    /**
     * Public constructor.
     */
    initialize(parameters) {
        super.initialize(parameters);
        this.listenTo(this.model, 'change:_options_labels', () => this._updateOptions());
    }

    /**
     * Called when view is rendered.
     */
    render() {
        super.render();

        this.el.classList.add('jupyter-widgets');
        this.el.classList.add('widget-inline-hbox');
        this.el.classList.add('widget-select');

        this.listbox = document.createElement('select');
        this.el.appendChild(this.listbox);
        this._updateOptions();
        this.update();
    }

    /**
     * Update the contents of this view
     */
    update() {
        // Disable listbox if needed
        this.listbox.disabled = this.model.get('disabled');

        // Select the correct element
        var value = this.model.get('value');
        this.listbox.selectedIndex = this.model.get('_options_labels').indexOf(value);
        return super.update();
    }

    _updateOptions() {
        this.listbox.textContent = '';
        let items = this.model.get('_options_labels');
        for (let i = 0; i < items.length; i++) {
            let item = items[i];
            let option = document.createElement('option');
            option.textContent = item.replace(/ /g, '\xa0'); // space -> &nbsp;
            option.setAttribute('data-value', encodeURIComponent(item));
            option.value = item;
            this.listbox.appendChild(option);
        }
    }

    events(): {[e: string]: string} {
        return {
            'change select': '_handle_change'
        }
    }

    /**
     * Handle when a new value is selected.
     */
    _handle_change() {
        let value = this.listbox.options[this.listbox.selectedIndex].value;
        this.model.set('value', value);
        this.touch();
    }

    listbox: HTMLSelectElement;
}


export
class DropdownModel extends SelectionModel {
    defaults() {
        return _.extend(super.defaults(), {
            _model_name: 'DropdownModel',
            _view_name: 'DropdownView',
            button_style: ''
        });
    }
}

// TODO: Make a phosphor dropdown control, wrapped in DropdownView. Also, fix
// bugs in keyboard handling. See
// https://github.com/ipython/ipywidgets/issues/1055 and
// https://github.com/ipython/ipywidgets/issues/1049
// For now, we subclass SelectView to provide DropdownView
// For the old code, see commit f68bfbc566f3a78a8f3350b438db8ed523ce3642
export
class DropdownView extends SelectView {}

{'fraction_non_alphanumeric': 0.12858489295812575, 'fraction_numerical': 0.005924330146761815, 'mean_word_length': 2.319034852546917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3619387', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2118, 'n_words': 486}