<commit_before>/*
    Login form using ajax
 */

// TODO: Would be nice to provide user with timeout error instead of a generic one
// https://github.com/Semantic-Org/Semantic-UI/issues/5121
var XHR_TIMEOUT = 5000;
var XHR_VERBOSE = true;
var XHR_DEBUG = true;

var formElement, checkBoxElement, passwordElement, logoutButton, logoutLink, errorElement;

var apiSettings = {
    login: {
        action: 'login',
        method: 'post',
        timeout: XHR_TIMEOUT,
        serializeForm: true,
        verbose: XHR_VERBOSE,
        debug: XHR_DEBUG,
        beforeXHR: setCsrfTokenHeader,
        onSuccess: redirectHome,
        successTest: isSuccess,
        onFailure: showFailure,
        onError: showError,
        onAbort: addShowError
    },
    logout: {
        action: 'logout',
        method: 'get',
        timeout: XHR_TIMEOUT,
        verbose: XHR_VERBOSE,
        debug: XHR_DEBUG,
        beforeXHR: setCsrfTokenHeader,
        onComplete: redirectHome
    }
};

var validation = {
    on: 'blur', // validate form when user changes field
    fields: {
        username: {
            identifier: 'username',
            rules: [{
                type: 'empty',
                prompt: 'Please enter a username'
            }, {
                type: 'minLength[3]',
                prompt: 'Your username needs at least {ruleValue} characters'
            }, {
                type: 'maxLength[30]',
                prompt: 'Your username can not have more than {ruleValue} characters'
            }]
        },
        password: {
            identifier: 'password',
            rules: [{
                type: 'empty',
                prompt: 'Please enter a password'
            }, {
                type: 'minLength[3]',
                prompt: 'Your password needs at least {ruleValue} characters'
            }, {
                type: 'maxLength[30]',
                prompt: 'Your password can not have more than {ruleValue} characters'
            }]
        }
    },
    onValid: updateUI,
    onInvalid: updateUI
};

$(document).ready(function() {
    grabDomElements();
    setupCheckBoxBehaviour();
    setupLoginFormBehaviour();
    setupLogoutButtonBehaviour();
});

function grabDomElements() {
    formElement = $('.ui.form');
    checkBoxElement = $('.ui.checkbox');
    passwordElement = $('#form-password');
    errorElement = $('.ui.message.error');
    logoutButton = $('.ui.logout.button');
    logoutLink = $('a.logout');
}

function setupCheckBoxBehaviour() {

    checkBoxElement.checkbox({
        onChecked: showPassword,
        onUnchecked: hidePassword
    });
}

function setupLoginFormBehaviour() {
    formElement.api(apiSettings.login).form(validation);
}

function setupLogoutButtonBehaviour() {<commit_msg>prevent double form submission on return<commit_after>/*
    Login form using ajax
 */

// TODO: Would be nice to provide user with timeout error instead of a generic one
// https://github.com/Semantic-Org/Semantic-UI/issues/5121
var XHR_TIMEOUT = 5000;
var XHR_VERBOSE = true;
var XHR_DEBUG = true;

var formElement, checkBoxElement, passwordElement, logoutButton, logoutLink, errorElement;

var apiSettings = {
    login: {
        action: 'login',
        method: 'post',
        timeout: XHR_TIMEOUT,
        serializeForm: true,
        verbose: XHR_VERBOSE,
        debug: XHR_DEBUG,
        beforeXHR: setCsrfTokenHeader,
        onSuccess: redirectHome,
        successTest: isSuccess,
        onFailure: showFailure,
        onError: showError,
        onAbort: addShowError
    },
    logout: {
        action: 'logout',
        method: 'get',
        timeout: XHR_TIMEOUT,
        verbose: XHR_VERBOSE,
        debug: XHR_DEBUG,
        beforeXHR: setCsrfTokenHeader,
        onComplete: redirectHome
    }
};

var validation = {
    on: 'blur', // validate form when user changes field
    fields: {
        username: {
            identifier: 'username',
            rules: [{
                type: 'empty',
                prompt: 'Please enter a username'
            }, {
                type: 'minLength[3]',
                prompt: 'Your username needs at least {ruleValue} characters'
            }, {
                type: 'maxLength[30]',
                prompt: 'Your username can not have more than {ruleValue} characters'
            }]
        },
        password: {
            identifier: 'password',
            rules: [{
                type: 'empty',
                prompt: 'Please enter a password'
            }, {
                type: 'minLength[3]',
                prompt: 'Your password needs at least {ruleValue} characters'
            }, {
                type: 'maxLength[30]',
                prompt: 'Your password can not have more than {ruleValue} characters'
            }]
        }
    },
    onValid: updateUI,
    onInvalid: updateUI
};

$(document).ready(function() {
    grabDomElements();
    setupCheckBoxBehaviour();
    setupLoginFormBehaviour();
    setupLogoutButtonBehaviour();
});

function grabDomElements() {
    formElement = $('.ui.form');
    checkBoxElement = $('.ui.checkbox');
    passwordElement = $('#form-password');
    errorElement = $('.ui.message.error');
    logoutButton = $('.ui.logout.button');
    logoutLink = $('a.logout');
}

function setupCheckBoxBehaviour() {

    checkBoxElement.checkbox({
        onChecked: showPassword,
        onUnchecked: hidePassword
    });
}

function setupLoginFormBehaviour() {
    formElement.form(validation).api(apiSettings.login);
    formElement.keypress(function(event) {
        if (event.which === 13) {
            event.preventDefault();
        }
    });
}

function setupLogoutButtonBehaviour() {
{'fraction_non_alphanumeric': 0.11286919831223628, 'fraction_numerical': 0.005274261603375527, 'mean_word_length': 2.100272479564033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4074248', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1562, 'n_words': 392}