<commit_before>Kwf.onContentReady(function(el, param) {
    if (!param.newRender) return false;
    Ext.select('.kwcForm > form', true, el).each(function(form) {
        form = form.parent('.kwcForm', false);
        if (!form.kwcForm) {
            form.kwcForm = new Kwc.Form.Component(form);
        }
    });
});
Ext.ns('Kwc.Form');
Kwc.Form.findForm = function(el) {
    var formEl = el.child('.kwcForm > form');
    if (formEl) {
        formEl = formEl.parent('.kwcForm');
        return formEl.kwcForm;
    }
    return null;
};
Kwc.Form.formsByComponentId = {};

Kwc.Form.Component = function(form)
{
    this.addEvents('submitSuccess', 'fieldChange');
    this.el = form;
    var config = form.parent().down('.config', true);
    if (!config) return;
    config = Ext.decode(config.value);
    if (!config) return;
    this.config = config;

    Kwc.Form.formsByComponentId[this.config.componentId] = this;

    this.fields = [];
    form.select('.kwfField', true).each(function(fieldEl) {
        var classes = fieldEl.dom.className.split(' ');
        var fieldConstructor = false;
        classes.each(function (c) {
            if (Kwf.FrontendForm.fields[c]) {
                fieldConstructor = Kwf.FrontendForm.fields[c];
            }
        }, this);
        if (fieldConstructor) {
            var field = new fieldConstructor(fieldEl, this);
            this.fields.push(field);
        }
    }, this);

    if (this.config.hideForValue) {
        this.config.hideForValue.each(function(hideForValue) {
            var field = this.findField(hideForValue.field);
            field.on('change', function(v, f) {
                if (v == hideForValue.value) {
                    this.findField(hideForValue.hide).hide();
                } else {
                    this.findField(hideForValue.hide).show();
                }
            }, this);

            //initialize on startup
            if (field.getValue() == hideForValue.value) {
                this.findField(hideForValue.hide).hide();
            }
        }, this);
    }

    if (this.config.useAjaxRequest) {
        var button = form.child('form button.submit');
        if (button) {
            button.on('click', this.onSubmit, this);
        }
    }

    this.fields.forEach(function(f) {
        f.on('change', function() {
            this.fireEvent('fieldChange', f);
        }, this);
    }, this);

    this.errorStyle = new Kwf.FrontendForm.errorStyles[this.config.errorStyle](this);
};
Ext.extend(Kwc.Form.Component, Ext.util.Observable, {
    getFieldConfig: function(fieldName)
    {
        if (this.config.fieldConfig[fieldName]) {
            return this.config.fieldConfig[fieldName];
        }
        return {};
    },
    findField: function(fieldName) {
        var ret = null;
        this.fields.each(function(f) {
            if (f.getFieldName() == fieldName) {
                ret = f;
                return true;
            }
        }, this);
        return ret;
    },
    getValues: function() {
        var ret = {};
        this.fields.each(function(f) {
            ret[f.getFieldName()] = f.getValue();
        }, this);
        return ret;
    },
    //returns values including parameters required for processInput to be considered as posted
    getValuesIncludingPost: function() {
        var ret = this.getValues();
        ret[this.config.componentId] = true;
        ret[this.config.componentId+'-post'] = true;
        return ret;
    },
    clearValues: function() {
        this.fields.each(function(f) {
            f.clearValue();
        }, this);
    },
    setValues: function(values) {
        for(var i in values) {
            var f = this.findField(i);
            if (f) f.setValue(values[i]);
        }
    },
    onSubmit: function(e) {
        if (this.dontUseAjaxRequest) return;

        var button = this.el.child('.submitWrapper .button');
        button.down('.saving').show();
        button.down('.submit').hide();

        this.errorStyle.hideErrors();

        Ext.Ajax.request({
            url: this.config.controllerUrl + '/json-save',
            ignoreErrors: true,
            params: {
                componentId: this.config.componentId
            },
            form: this.el.down('form'),
            failure: function() {
                //on failure try a plain old post of the form
                this.dontUseAjaxRequest = true; //avoid endless recursion
                button.down('.submit').dom.click();
            },
            success: function(response, options, r) {<commit_msg>Kwc_Form: add beforeSubmit event that allows canceling the submit<commit_after>Kwf.onContentReady(function(el, param) {
    if (!param.newRender) return false;
    Ext.select('.kwcForm > form', true, el).each(function(form) {
        form = form.parent('.kwcForm', false);
        if (!form.kwcForm) {
            form.kwcForm = new Kwc.Form.Component(form);
        }
    });
});
Ext.ns('Kwc.Form');
Kwc.Form.findForm = function(el) {
    var formEl = el.child('.kwcForm > form');
    if (formEl) {
        formEl = formEl.parent('.kwcForm');
        return formEl.kwcForm;
    }
    return null;
};
Kwc.Form.formsByComponentId = {};

Kwc.Form.Component = function(form)
{
    this.addEvents('beforeSubmit', 'submitSuccess', 'fieldChange');
    this.el = form;
    var config = form.parent().down('.config', true);
    if (!config) return;
    config = Ext.decode(config.value);
    if (!config) return;
    this.config = config;

    Kwc.Form.formsByComponentId[this.config.componentId] = this;

    this.fields = [];
    form.select('.kwfField', true).each(function(fieldEl) {
        var classes = fieldEl.dom.className.split(' ');
        var fieldConstructor = false;
        classes.each(function (c) {
            if (Kwf.FrontendForm.fields[c]) {
                fieldConstructor = Kwf.FrontendForm.fields[c];
            }
        }, this);
        if (fieldConstructor) {
            var field = new fieldConstructor(fieldEl, this);
            this.fields.push(field);
        }
    }, this);

    if (this.config.hideForValue) {
        this.config.hideForValue.each(function(hideForValue) {
            var field = this.findField(hideForValue.field);
            field.on('change', function(v, f) {
                if (v == hideForValue.value) {
                    this.findField(hideForValue.hide).hide();
                } else {
                    this.findField(hideForValue.hide).show();
                }
            }, this);

            //initialize on startup
            if (field.getValue() == hideForValue.value) {
                this.findField(hideForValue.hide).hide();
            }
        }, this);
    }

    if (this.config.useAjaxRequest) {
        var button = form.child('form button.submit');
        if (button) {
            button.on('click', this.onSubmit, this);
        }
    }

    this.fields.forEach(function(f) {
        f.on('change', function() {
            this.fireEvent('fieldChange', f);
        }, this);
    }, this);

    this.errorStyle = new Kwf.FrontendForm.errorStyles[this.config.errorStyle](this);
};
Ext.extend(Kwc.Form.Component, Ext.util.Observable, {
    getFieldConfig: function(fieldName)
    {
        if (this.config.fieldConfig[fieldName]) {
            return this.config.fieldConfig[fieldName];
        }
        return {};
    },
    findField: function(fieldName) {
        var ret = null;
        this.fields.each(function(f) {
            if (f.getFieldName() == fieldName) {
                ret = f;
                return true;
            }
        }, this);
        return ret;
    },
    getValues: function() {
        var ret = {};
        this.fields.each(function(f) {
            ret[f.getFieldName()] = f.getValue();
        }, this);
        return ret;
    },
    //returns values including parameters required for processInput to be considered as posted
    getValuesIncludingPost: function() {
        var ret = this.getValues();
        ret[this.config.componentId] = true;
        ret[this.config.componentId+'-post'] = true;
        return ret;
    },
    clearValues: function() {
        this.fields.each(function(f) {
            f.clearValue();
        }, this);
    },
    setValues: function(values) {
        for(var i in values) {
            var f = this.findField(i);
            if (f) f.setValue(values[i]);
        }
    },

    onSubmit: function(e)
    {
        //return false to cancel submit
        if (this.fireEvent('beforeSubmit', this, e) === false) {
            e.stopEvent();
            return;
        }

        if (this.dontUseAjaxRequest) return;

        var button = this.el.child('.submitWrapper .button');
        button.down('.saving').show();
        button.down('.submit').hide();

        this.errorStyle.hideErrors();

        Ext.Ajax.request({
            url: this.config.controllerUrl + '/json-save',
            ignoreErrors: true,
            params: {
                componentId: this.config.componentId
            },
            form: this.el.down('form'),
            failure: function() {
                //on failure try a plain old post of the form
                this.dontUseAjaxRequest = true; //avoid endless recursion
                button.down('.submit').dom.click();
            },
            success: function(response, options, r) {
{'fraction_non_alphanumeric': 0.14030775806796325, 'fraction_numerical': 0.0, 'mean_word_length': 1.9002169197396963, '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': '1031376', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2698, 'n_words': 513}