<commit_before>const Validation       = require('./form_validation');
const showLoadingImage = require('../base/utility').showLoadingImage;

const FormManager = (function() {
    'use strict';

    const forms = {};

    const initForm = (form_selector, fields) => {
        const $form = $(`${form_selector}:visible`);
        if ($form.length && Array.isArray(fields) && fields.length) {
            forms[form_selector] = {
                fields     : fields,
                $btn_submit: $form.find('button[type="submit"]'),
            };
            fields.forEach((field) => {
                if (field.selector) {
                    field.$ = $form.find(field.selector);
                    if (!field.$.length) return;
                }

                field.form = form_selector;
            });
        }
        Validation.init(form_selector, fields);
    };

    const getFormData = (form_selector) => {
        const data = {};
        const fields = forms[form_selector].fields;
        let key,
            $selector,
            current_field,
            val,
            value;

        Object.keys(fields).forEach(function(field) {
            current_field = fields[field];
            if (!current_field.exclude_request) {
                $selector = $(current_field.form).find(current_field.selector);
                if ($selector.is(':visible') || current_field.value) {
                    val = $selector.val();
                    key = current_field.request_field || current_field.selector;

                    // prioritise data-value
                    // if label, take the text
                    // if checkbox, take checked value
                    // otherwise take the value
                    value = current_field.value ? current_field.value :
                        $selector.attr('data-value') || (/lbl_/.test(key) ? (current_field.value || $selector.text()) :
                            $selector.is(':checkbox') ? ($selector.is(':checked') ? 1 : 0) :
                                Array.isArray(val) ? val.join(',') : (val || ''));

                    key = key.replace(/lbl_|#|\./g, '');
                    if (current_field.parent_node) {
                        if (!data[current_field.parent_node]) {
                            data[current_field.parent_node] = {};
                        }
                        data[current_field.parent_node][key] = value;
                    } else {
                        data[key] = value;
                    }
                }
            }
        });
        return data;
    };

    const disableButton = ($btn) => {
        if ($btn.length && !$btn.find('.barspinner').length) {
            $btn.attr('disabled', 'disabled');
            const $btn_text = $('<span/>', { text: $btn.text(), class: 'invisible' });
            showLoadingImage($btn, 'white');
            $btn.append($btn_text);
        }
    };

    const enableButton = ($btn) => {
        if ($btn.length && $btn.find('.barspinner').length) {
            $btn.removeAttr('disabled').html($btn.find('span').text());
        }
    };

    const handleSubmit = (form_selector, obj_request, fnc_response_handler, fnc_additional_check) => {
        $(form_selector).off('submit').on('submit', function(evt) {
            evt.preventDefault();
            const $btn_submit = forms[form_selector].$btn_submit;
            if (Validation.validate(form_selector)) {
                const req = $.extend(obj_request, getFormData(form_selector));
                if (typeof fnc_additional_check === 'function' && !fnc_additional_check(req)) {
                    return;
                }
                disableButton($btn_submit);
                BinarySocket.send(req).then((response) => {
                    if (typeof fnc_response_handler === 'function') {
                        enableButton($btn_submit);
                        fnc_response_handler(response);
                    }
                });
            }
        });
    };

    return {
        init        : initForm,
        handleSubmit: handleSubmit,
    };
})();

module.exports = FormManager;
<commit_msg>only submit if button enabled<commit_after>const Validation       = require('./form_validation');
const showLoadingImage = require('../base/utility').showLoadingImage;

const FormManager = (function() {
    'use strict';

    const forms = {};
    let can_submit = true;

    const initForm = (form_selector, fields) => {
        const $form = $(`${form_selector}:visible`);
        if ($form.length && Array.isArray(fields) && fields.length) {
            forms[form_selector] = {
                fields     : fields,
                $btn_submit: $form.find('button[type="submit"]'),
            };
            fields.forEach((field) => {
                if (field.selector) {
                    field.$ = $form.find(field.selector);
                    if (!field.$.length) return;
                }

                field.form = form_selector;
            });
        }
        Validation.init(form_selector, fields);
    };

    const getFormData = (form_selector) => {
        const data = {};
        const fields = forms[form_selector].fields;
        let key,
            $selector,
            current_field,
            val,
            value;

        Object.keys(fields).forEach(function(field) {
            current_field = fields[field];
            if (!current_field.exclude_request) {
                $selector = $(current_field.form).find(current_field.selector);
                if ($selector.is(':visible') || current_field.value) {
                    val = $selector.val();
                    key = current_field.request_field || current_field.selector;

                    // prioritise data-value
                    // if label, take the text
                    // if checkbox, take checked value
                    // otherwise take the value
                    value = current_field.value ? current_field.value :
                        $selector.attr('data-value') || (/lbl_/.test(key) ? (current_field.value || $selector.text()) :
                            $selector.is(':checkbox') ? ($selector.is(':checked') ? 1 : 0) :
                                Array.isArray(val) ? val.join(',') : (val || ''));

                    key = key.replace(/lbl_|#|\./g, '');
                    if (current_field.parent_node) {
                        if (!data[current_field.parent_node]) {
                            data[current_field.parent_node] = {};
                        }
                        data[current_field.parent_node][key] = value;
                    } else {
                        data[key] = value;
                    }
                }
            }
        });
        return data;
    };

    const disableButton = ($btn) => {
        if ($btn.length && !$btn.find('.barspinner').length) {
            $btn.attr('disabled', 'disabled');
            const $btn_text = $('<span/>', { text: $btn.text(), class: 'invisible' });
            showLoadingImage($btn, 'white');
            $btn.append($btn_text);
        }
        can_submit = false;
    };

    const enableButton = ($btn) => {
        if ($btn.length && $btn.find('.barspinner').length) {
            $btn.removeAttr('disabled').html($btn.find('span').text());
        }
        can_submit = true;
    };

    const handleSubmit = (form_selector, obj_request, fnc_response_handler, fnc_additional_check) => {
        $(form_selector).off('submit').on('submit', function(evt) {
            evt.preventDefault();
            if (!can_submit) return;
            const $btn_submit = forms[form_selector].$btn_submit;
            if (Validation.validate(form_selector)) {
                const req = $.extend(obj_request, getFormData(form_selector));
                if (typeof fnc_additional_check === 'function' && !fnc_additional_check(req)) {
                    return;
                }
                disableButton($btn_submit);
                BinarySocket.send(req).then((response) => {
                    if (typeof fnc_response_handler === 'function') {
                        enableButton($btn_submit);
                        fnc_response_handler(response);
                    }
                });
            }
        });
    };

    return {
        init        : initForm,
        handleSubmit: handleSubmit,
    };
})();

module.exports = FormManager;

{'fraction_non_alphanumeric': 0.13786568755181808, 'fraction_numerical': 0.00047376524931896246, 'mean_word_length': 1.5145920190589637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '2364852', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2141, 'n_words': 370}