<commit_before>/**
A QuestionnaireItem that has a predefined set of answer and one of these can be selected.
A HTML select-element is used.

@class QuestionnaireItemDefinedSelector
@augments UIElement
@augments UIElementInteractive
@augments QuestionnaireItem
@augments QuestionnaireItemDefined

@param {string} [className] CSS class
@param {string} question question
@param {boolean} [required=false]
@param {array} optionList
*/
function QuestionnaireItemDefinedSelector(className, question, required, optionList) {
    QuestionnaireItemDefined.call(this, className, question, required, optionList);
}
QuestionnaireItemDefinedSelector.prototype = Object.create(QuestionnaireItemDefined.prototype);
QuestionnaireItemDefinedSelector.prototype.constructor = QuestionnaireItemDefinedSelector;

QuestionnaireItemDefinedSelector.prototype._createAnswerNode = function() {
    var node = document.createElement("div");

    this.select = document.createElement("select");
    this.select.addEventListener("change", this._handleChange.bind(this));

    var optionNull = document.createElement("option");
    optionNull.value = "";
    optionNull.disabled = true;
    optionNull.selected = true;
    optionNull.innerHTML = "";
    this.select.appendChild(optionNull);

    for (var i in this.optionList) {
        var option = document.createElement("option");
        option.value = this.optionList[i];
        option.innerHTML = this.optionList[i];
        this.select.appendChild(option);
    }

    node.appendChild(this.select);

    this._applyAnswerToUI();
    return node;
};
QuestionnaireItemDefinedSelector.prototype._handleChange = function(event) {
    this.answer = this.select.value;
    this._sendReadyStateChanged();
};

QuestionnaireItemDefinedSelector.prototype._applyAnswerToUI = function() {
    if (!this.isUIcreated()) {
        return;
    }

    if (this.isAnswered()) {
        this.select.value = this.getAnswer();
    }
};

QuestionnaireItemDefinedSelector.prototype.setAnswer = function(answer) {
    if (answer === null) {
        this.answer = null;
        this._applyAnswerToUI();
        return true;
    }

    var answerIndex = this.optionList.indexOf(answer);
    if (answerIndex === -1) {
        TheFragebogen.logger.error(this.constructor.name + ".setAnswer()", "Provided answer is not an option " + answer + ".");
        return false;
    }

    this.answer = this.optionList[answerIndex];
    this._applyAnswerToUI();
<commit_msg>QuestionnaireItemDefinedSelector: bug circumventing mandatory questions<commit_after>/**
A QuestionnaireItem that has a predefined set of answer and one of these can be selected.
A HTML select-element is used.

@class QuestionnaireItemDefinedSelector
@augments UIElement
@augments UIElementInteractive
@augments QuestionnaireItem
@augments QuestionnaireItemDefined

@param {string} [className] CSS class
@param {string} question question
@param {boolean} [required=false]
@param {array} optionList
*/
function QuestionnaireItemDefinedSelector(className, question, required, optionList) {
    QuestionnaireItemDefined.call(this, className, question, required, optionList);
}
QuestionnaireItemDefinedSelector.prototype = Object.create(QuestionnaireItemDefined.prototype);
QuestionnaireItemDefinedSelector.prototype.constructor = QuestionnaireItemDefinedSelector;

QuestionnaireItemDefinedSelector.prototype._createAnswerNode = function() {
    var node = document.createElement("div");

    this.select = document.createElement("select");
    this.select.addEventListener("change", this._handleChange.bind(this));

    var optionNull = document.createElement("option");
    optionNull.value = "";
    optionNull.disabled = true;
    optionNull.selected = true;
    optionNull.innerHTML = "";
    this.select.appendChild(optionNull);

    for (var i in this.optionList) {
        var option = document.createElement("option");
        option.value = this.optionList[i];
        option.innerHTML = this.optionList[i];
        this.select.appendChild(option);
    }

    node.appendChild(this.select);

    this._applyAnswerToUI();
    return node;
};
QuestionnaireItemDefinedSelector.prototype._handleChange = function(event) {
    // It is possible to select the optionNull field again to circumvent mandatory questions.
    this.answer = this.select.value === "" ? null : this.select.value;
    this._sendReadyStateChanged();
};

QuestionnaireItemDefinedSelector.prototype._applyAnswerToUI = function() {
    if (!this.isUIcreated()) {
        return;
    }

    if (this.isAnswered()) {
        this.select.value = this.getAnswer();
    }
};

QuestionnaireItemDefinedSelector.prototype.setAnswer = function(answer) {
    if (answer === null) {
        this.answer = null;
        this._applyAnswerToUI();
        return true;
    }

    var answerIndex = this.optionList.indexOf(answer);
    if (answerIndex === -1) {
        TheFragebogen.logger.error(this.constructor.name + ".setAnswer()", "Provided answer is not an option " + answer + ".");
        return false;
    }

    this.answer = this.optionList[answerIndex];
    this._applyAnswerToUI();

{'fraction_non_alphanumeric': 0.11043784206411258, 'fraction_numerical': 0.00039093041438623924, 'mean_word_length': 4.242827868852459, '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': '2127684', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1355, 'n_words': 301}