<commit_before>const path = require('path');
const Choice = require(path.resolve('electron/easy/choice'));
const Const = require(path.resolve('electron/easy/constants'));
const Form = require(path.resolve('electron/easy/form'));
const Field = require(path.resolve('electron/easy/field'));
const Util = require(path.resolve('electron/easy/util'));
const ValidationResult = require(path.resolve('electron/easy/validation_result'));


module.exports = class TagDefinition {
    constructor(tagFile, tagName) {
        this.id = Util.uuid4();
        this.tagFile = tagFile;
        this.tagName = tagName;
        this.required = false;
        this.emptyOk = true;
        this.values = [];
        this.defaultValue = "";
        this.userValue = "";
        this.isBuiltIn = false;
    }
    objectType() {
        return 'TagDefinition';
    }
    validate() {
        var result = new ValidationResult();
        if (this.values.length > 0 && !Util.listContains(this.values, this.defaultValue)) {
            result.errors['defaultValue'] = "The default value must be one of the allowed values.";
        }
        return result
    }
    toForm() {
        var form = new Form("");
        form.fields['tagFile'] = new Field('tagFile', 'tagFile', 'Tag File', this.tagFile)
        form.fields['tagName'] = new Field('tagName', 'tagName', 'Tag Name', this.tagName)
        form.fields['required'] = new Field('required', 'required', 'Presence Required?', this.required)
        form.fields['required'].choices = Choice.makeList(Const.YesNo, this.required, false);
        form.fields['required'].help = "Does this tag have to be present in the tag file?";
        form.fields['emptyOk'] = new Field('emptyOk', 'emptyOk', 'Can tag value be empty?', this.emptyOk)
        form.fields['emptyOk'].choices = Choice.makeList(Const.YesNo, this.emptyOk, false);
        form.fields['emptyOk'].help = "Is it valid for this tag to be present but empty?";
        form.fields['values'] = new Field('values', 'values', 'Allowed Values (one per line)', this.values.join("\n"))
        form.fields['values'].help = "List the legal values for this tag, one per line. Leave this field empty to allow any value.";
        form.fields['defaultValue'] = new Field('defaultValue', 'defaultValue', 'Default Value', this.defaultValue)
        form.fields['defaultValue'].help = "Optional default value for this field. If this tag has a list of allowed values, the default value must be one of the allowed values.";
        if (this.values != null && this.values.length > 0) {
            var defaultVal = "";
            if (this.values.length == 1) {
                defaultVal = this.values[0];
            }
            form.fields['defaultValue'].choices = Choice.makeList(this.values, defaultVal, true)
        }
        form.fields['tagDefinitionId'] = new Field('tagDefinitionId', 'tagDefinitionId', 'tagDefinitionId', this.id)

        // Don't let users edit anything but default value if this
        // tag definition is a native part of a built-in form.
        var readOnly = ['tagFile',
                        'tagName',
                        'required',
                        'emptyOk',
                        'values'];
        if (this.isBuiltIn) {
            for (var name of readOnly) {
                form.fields[name].attrs['disabled'] = true;
            }
        }
        if (this.systemMustSet()) {
            form.fields['defaultValue'].attrs['disabled'] = true;
            form.fields['defaultValue'].help = "You cannot set this tag value manually. The system will set it internally each time it generates a bag.";
        }

        return form;
    }
    static fromForm() {
        var tagName = $('#tagName').val().trim();
        var tagFile = $('#tagFile').val().trim();
        var tagDef = new TagDefinition(tagFile, tagName);
        tagDef.required = $('#required').val().trim();
        tagDef.emptyOk = $('#emptyOk').val().trim();
        tagDef.values = Util.filterEmpties($('#values').val().split("\n"));
        tagDef.defaultValue = $('#defaultValue').val().trim();
        tagDef.id = $('#tagDefinitionId').val().trim();
        return tagDef;
    }
    toFieldForJobForm() {
        var field = new Field(this.id, this.tagName, this.tagName, this.getValue());
        if (this.values) {
            field.choices = Choice.makeList(this.values, this.getValue(), true);
        }
        if (this.systemMustSet()) {
            field.help = "The system will set this field's value when it creates the bag.";
            field.attrs['disabled'] = true;
        }
        return field;
    }
    systemMustSet() {
        return (this.tagName == 'Bagging-Date' || this.tagName == 'Bag-Size' || this.tagName == 'Payload-Oxum');
    }
    getValue() {
        return this.userValue || this.defaultValue;
    }
    looksLikeDescriptionTag() {
        return this.tagName.toLowerCase().includes("description");
    }<commit_msg>Add required attribute to inputs where necessary.<commit_after>const path = require('path');
const Choice = require(path.resolve('electron/easy/choice'));
const Const = require(path.resolve('electron/easy/constants'));
const Form = require(path.resolve('electron/easy/form'));
const Field = require(path.resolve('electron/easy/field'));
const Util = require(path.resolve('electron/easy/util'));
const ValidationResult = require(path.resolve('electron/easy/validation_result'));


module.exports = class TagDefinition {
    constructor(tagFile, tagName) {
        this.id = Util.uuid4();
        this.tagFile = tagFile;
        this.tagName = tagName;
        this.required = false;
        this.emptyOk = false;
        this.values = [];
        this.defaultValue = "";
        this.userValue = "";
        this.isBuiltIn = false;
    }
    objectType() {
        return 'TagDefinition';
    }
    validate() {
        var result = new ValidationResult();
        if (this.values.length > 0 && !Util.listContains(this.values, this.defaultValue)) {
            result.errors['defaultValue'] = "The default value must be one of the allowed values.";
        }
        return result
    }
    toForm() {
        var form = new Form("");
        form.fields['tagFile'] = new Field('tagFile', 'tagFile', 'Tag File', this.tagFile)
        form.fields['tagName'] = new Field('tagName', 'tagName', 'Tag Name', this.tagName)
        form.fields['required'] = new Field('required', 'required', 'Presence Required?', this.required)
        form.fields['required'].choices = Choice.makeList(Const.YesNo, this.required, false);
        form.fields['required'].help = "Does this tag have to be present in the tag file?";
        form.fields['emptyOk'] = new Field('emptyOk', 'emptyOk', 'Can tag value be empty?', this.emptyOk)
        form.fields['emptyOk'].choices = Choice.makeList(Const.YesNo, this.emptyOk, false);
        form.fields['emptyOk'].help = "Is it valid for this tag to be present but empty?";
        form.fields['values'] = new Field('values', 'values', 'Allowed Values (one per line)', this.values.join("\n"))
        form.fields['values'].help = "List the legal values for this tag, one per line. Leave this field empty to allow any value.";
        form.fields['defaultValue'] = new Field('defaultValue', 'defaultValue', 'Default Value', this.defaultValue)
        form.fields['defaultValue'].help = "Optional default value for this field. If this tag has a list of allowed values, the default value must be one of the allowed values.";
        if (this.values != null && this.values.length > 0) {
            var defaultVal = "";
            if (this.values.length == 1) {
                defaultVal = this.values[0];
            }
            form.fields['defaultValue'].choices = Choice.makeList(this.values, defaultVal, true)
        }
        form.fields['tagDefinitionId'] = new Field('tagDefinitionId', 'tagDefinitionId', 'tagDefinitionId', this.id)

        // Don't let users edit anything but default value if this
        // tag definition is a native part of a built-in form.
        var readOnly = ['tagFile',
                        'tagName',
                        'required',
                        'emptyOk',
                        'values'];
        if (this.isBuiltIn) {
            for (var name of readOnly) {
                form.fields[name].attrs['disabled'] = true;
            }
        }
        if (this.systemMustSet()) {
            form.fields['defaultValue'].attrs['disabled'] = true;
            form.fields['defaultValue'].help = "You cannot set this tag value manually. The system will set it internally each time it generates a bag.";
        }

        return form;
    }
    static fromForm() {
        var tagName = $('#tagName').val().trim();
        var tagFile = $('#tagFile').val().trim();
        var tagDef = new TagDefinition(tagFile, tagName);
        tagDef.required = $('#required').val().trim();
        tagDef.emptyOk = $('#emptyOk').val().trim();
        tagDef.values = Util.filterEmpties($('#values').val().split("\n"));
        tagDef.defaultValue = $('#defaultValue').val().trim();
        tagDef.id = $('#tagDefinitionId').val().trim();
        return tagDef;
    }
    toFieldForJobForm() {
        var field = new Field(this.id, this.tagName, this.tagName, this.getValue());
        if (this.values) {
            field.choices = Choice.makeList(this.values, this.getValue(), true);
        }
        if (this.systemMustSet()) {
            field.help = "The system will set this field's value when it creates the bag.";
            field.attrs['disabled'] = true;
        }
        if (this.required && !this.emptyOk) {
            field.attrs['required'] = true;
            field.cssClasses.push('required');
        }
        return field;
    }
    systemMustSet() {
        return (this.tagName == 'Bagging-Date' || this.tagName == 'Bag-Size' || this.tagName == 'Payload-Oxum');
    }
    getValue() {
        return this.userValue || this.defaultValue;
    }
    looksLikeDescriptionTag() {
        return this.tagName.toLowerCase().includes("description");
    }
{'fraction_non_alphanumeric': 0.14679080824088747, 'fraction_numerical': 0.0009904912836767037, 'mean_word_length': 2.529185599440755, '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': '4309130', 'n_tokens_mistral': 2775, 'n_tokens_neox': 2721, 'n_words': 722}