<commit_before>/* $AMPERSAND_VERSION */
var domify = require('domify');
var dom = require('ampersand-dom');
var matches = require('matches-selector');
var View = require('ampersand-view');

// Replaceable with anything with label, message-container, message-text data-hooks and a <select multiple="multiple"s>
var defaultTemplate = [
    '<label class="select">',
        '<span data-hook="label"></span>',
        '<select multiple="multiple"></select>',
        '<span data-hook="message-container" class="message message-below message-error">',
            '<p data-hook="message-text"></p>',
        '</span>',
    '</label>'
].join('\n');

var createOption = function(value, text, disabled) {
    var node = document.createElement('option');

    // Set to empty-string if undefined or null, but not if 0, false, etc
    if (value === null || value === undefined) {
        value = '';
    }

    if (disabled) {
        node.disabled = true;
    }
    node.textContent = text;
    node.value = value;

    return node;
};

module.exports = View.extend({
    initialize: function(opts) {
        opts = opts || {};

        if (typeof opts.name !== 'string') {
            throw new Error('MultiSelectView requires a name property.');
        }
        this.name = opts.name;

        if (!Array.isArray(opts.options) && !opts.options.isCollection) {
            throw new Error('MultiSelectView requires select options.');
        }
        this.options = opts.options;

        if (this.options.isCollection) {
            this.idAttribute = opts.idAttribute || this.options.mainIndex || 'id';
            this.textAttribute = opts.textAttribute || 'text';
            this.disabledAttribute = opts.disabledAttribute;
        }

        this.el = opts.el;
        if (opts.label === undefined) {
            this.label = this.name;
        } else {
            this.label = opts.label;
        }
        this.parent = opts.parent || this.parent;
        this.template = opts.template || defaultTemplate;
        this.startingValues = opts.value;
        this.yieldModels = (opts.yieldModels === false) ? false : true;

        this.eagerValidate = opts.eagerValidate;
        this.required = opts.required || false;
        this.validClass = opts.validClass || 'input-valid';
        this.invalidClass = opts.invalidClass || 'input-invalid';
        if (opts.requiredMessage === undefined) {
            this.requiredMessage = 'Selection required';
        } else {
            this.requiredMessage = opts.requiredMessage;
        }

        this.onChange = this.onChange.bind(this);

        this.startingValues = this.setValue(opts.values, this.eagerValidate ? false : true, true);

        if (opts.beforeSubmit) {
            this.beforeSubmit = opts.beforeSubmit;
        }
        if (opts.autoRender) {
            this.autoRender = opts.autoRender;
        }<commit_msg>Fixed prefill bug<commit_after>/* $AMPERSAND_VERSION */
var domify = require('domify');
var dom = require('ampersand-dom');
var matches = require('matches-selector');
var View = require('ampersand-view');

// Replaceable with anything with label, message-container, message-text data-hooks and a <select multiple="multiple"s>
var defaultTemplate = [
    '<label class="select">',
        '<span data-hook="label"></span>',
        '<select multiple="multiple"></select>',
        '<span data-hook="message-container" class="message message-below message-error">',
            '<p data-hook="message-text"></p>',
        '</span>',
    '</label>'
].join('\n');

var createOption = function(value, text, disabled) {
    var node = document.createElement('option');

    // Set to empty-string if undefined or null, but not if 0, false, etc
    if (value === null || value === undefined) {
        value = '';
    }

    if (disabled) {
        node.disabled = true;
    }
    node.textContent = text;
    node.value = value;

    return node;
};

module.exports = View.extend({
    initialize: function(opts) {
        opts = opts || {};

        if (typeof opts.name !== 'string') {
            throw new Error('MultiSelectView requires a name property.');
        }
        this.name = opts.name;

        if (!Array.isArray(opts.options) && !opts.options.isCollection) {
            throw new Error('MultiSelectView requires select options.');
        }
        this.options = opts.options;

        if (this.options.isCollection) {
            this.idAttribute = opts.idAttribute || this.options.mainIndex || 'id';
            this.textAttribute = opts.textAttribute || 'text';
            this.disabledAttribute = opts.disabledAttribute;
        }

        this.el = opts.el;
        if (opts.label === undefined) {
            this.label = this.name;
        } else {
            this.label = opts.label;
        }
        this.parent = opts.parent || this.parent;
        this.template = opts.template || defaultTemplate;
        this.startingValues = opts.value;
        this.yieldModels = (opts.yieldModels === false) ? false : true;

        this.eagerValidate = opts.eagerValidate;
        this.required = opts.required || false;
        this.validClass = opts.validClass || 'input-valid';
        this.invalidClass = opts.invalidClass || 'input-invalid';
        if (opts.requiredMessage === undefined) {
            this.requiredMessage = 'Selection required';
        } else {
            this.requiredMessage = opts.requiredMessage;
        }

        this.onChange = this.onChange.bind(this);

        this.startingValues = this.setValue(opts.value, this.eagerValidate ? false : true, true);

        if (opts.beforeSubmit) {
            this.beforeSubmit = opts.beforeSubmit;
        }
        if (opts.autoRender) {
            this.autoRender = opts.autoRender;
        }
{'fraction_non_alphanumeric': 0.13892257058721635, 'fraction_numerical': 0.00034644032565390613, 'mean_word_length': 2.3589296102385107, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '7222066', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1584, 'n_words': 364}