<commit_before>import jQuery from 'jquery';

class ConfirmLeave {
    // object of form ids which require confirmation for leaving the page
    // store changed fields to pass through submit state
    forms = {};
    // store currently submitted form. If it'll fail to validate then we
    // have list of changed fields
    forms_freezed = {};
    message = 'Leave page?';
    //checks if leaving the page is approved
    check = () => {
        //remove non-existent forms from the array
        [...Object.keys(this.forms), ...Object.keys(this.forms_freezed)].filter(item => (document.getElementById(item) === null)).forEach(item => Epesi.confirmLeave.deactivate(item));

        //check if there is any not freezed form with changed values to confirm leave
        let requires_confirmation = Object.keys(this.forms).reduce((previous, form_id) => previous || document.querySelector(`#${form_id} .changed-input`) !== null, false);

        if (requires_confirmation) {
            //take care if user disabled alert messages
            let openTime = new Date();
            let confirmed = false;
            try {
                confirmed = confirm(this.message);
            } catch(e) {
                confirmed = true;
            }
            let closeTime = new Date();
            if ((closeTime - openTime) < 350) confirmed = true;

            if(!confirmed) return false;
            else this.deactivate();
        }
        return true;
    };

    activate = (form_id, message) => {
        this.message = message;
        // add form or restore from freezed state - form is freezed for submit
        if (!this.forms.hasOwnProperty(form_id)) {
            if (this.forms_freezed.hasOwnProperty(form_id)) {
                this.forms[form_id] = this.forms_freezed[form_id];
                delete this.forms_freezed[form_id];
            } else {
                this.forms[form_id] = {};
            }
        }
        // apply class to all changed inputs - required for validation failure
        for (var key in this.forms[form_id]) {
            jQuery('#' + form_id + ' [name="' + key + '"]').addClass('changed-input');
        }
        // on change add changed-input class
        jQuery('#' + form_id).on('change', 'input, textarea, select', function (e) {
            if (e.originalEvent === undefined) return;
            var el = jQuery(this);
            el.addClass('changed-input');
            var form = form_id in Epesi.confirmLeave.forms ? Epesi.confirmLeave.forms[form_id] : Epesi.confirmLeave.forms_freezed[form_id];
            form[el.attr('name')] = true;
        });
        //take care if user refreshing or going to another page
        jQuery(window).unbind('beforeunload').on('beforeunload', function () {
            if (jQuery('.changed-input').length) {
                return Epesi.confirmLeave.message;
            }
        });
    };

    deactivate = (form_id = null) => {
        if (form_id !== null) {
            delete this.forms[form_id];
            delete this.forms_freezed[form_id];
        } else {
            this.forms = {};
            this.forms_freezed = {};
        }

        if (!Object.keys(this.forms).length) jQuery(window).unbind('beforeunload');
    };

    freeze = (form_id) => {
        if (form_id in this.forms) {
            this.forms_freezed[form_id] = this.forms[form_id];
            delete this.forms[form_id];
        }
    }
}

export default ConfirmLeave;<commit_msg>refactor activate<commit_after>import jQuery from 'jquery';

class ConfirmLeave {
    // object of form ids which require confirmation for leaving the page
    // store changed fields to pass through submit state
    forms = {};
    // store currently submitted form. If it'll fail to validate then we
    // have list of changed fields
    forms_freezed = {};
    message = 'Leave page?';
    //checks if leaving the page is approved
    check = () => {
        //remove non-existent forms from the array
        [...Object.keys(this.forms), ...Object.keys(this.forms_freezed)].filter(item => (document.getElementById(item) === null)).forEach(item => Epesi.confirmLeave.deactivate(item));

        //check if there is any not freezed form with changed values to confirm leave
        let requires_confirmation = Object.keys(this.forms).reduce((previous, form_id) => previous || document.querySelector(`#${form_id} .changed-input`) !== null, false);

        if (requires_confirmation) {
            //take care if user disabled alert messages
            let openTime = new Date();
            let confirmed = false;
            try {
                confirmed = confirm(this.message);
            } catch(e) {
                confirmed = true;
            }
            let closeTime = new Date();
            if ((closeTime - openTime) < 350) confirmed = true;

            if(!confirmed) return false;
            else this.deactivate();
        }
        return true;
    };

    activate = (form_id, message) => {
        this.message = message;
        // add form or restore from freezed state - form is freezed for submit
        if (!this.forms.hasOwnProperty(form_id)) {
            if (this.forms_freezed.hasOwnProperty(form_id)) {
                this.forms[form_id] = this.forms_freezed[form_id];
                delete this.forms_freezed[form_id];
            } else {
                this.forms[form_id] = [];
            }
        }

        let form_element = document.getElementById(form_id);

        // apply class to all changed inputs - required for validation failure
        this.forms[form_id].forEach(input_name => jQuery(form_element).find(`[name="${input_name}"]`).addClass('changed-input'));

        // on change add changed-input class
        jQuery(form_element).on('change', 'input, textarea, select', function (e) {
            if (e.originalEvent === undefined) return;
            let input = jQuery(this);
            input.addClass('changed-input');
            if(Epesi.confirmLeave.forms.hasOwnProperty(form_id)) Epesi.confirmLeave.forms[form_id].push(input.attr('name'));
            if(Epesi.confirmLeave.forms_freezed.hasOwnProperty(form_id)) Epesi.confirmLeave.forms_freezed[form_id].push(input.attr('name'));
        });
        //take care if user refreshing or going to another page
        jQuery(window).unbind('beforeunload').on('beforeunload', function () {
            if (jQuery('.changed-input').length) {
                return Epesi.confirmLeave.message;
            }
        });
    };

    deactivate = (form_id = null) => {
        if (form_id !== null) {
            delete this.forms[form_id];
            delete this.forms_freezed[form_id];
        } else {
            this.forms = {};
            this.forms_freezed = {};
        }

        if (!Object.keys(this.forms).length) jQuery(window).unbind('beforeunload');
    };

    freeze = (form_id) => {
        if (this.forms.hasOwnProperty(form_id)) {
            this.forms_freezed[form_id] = this.forms[form_id];
            delete this.forms[form_id];
        }
    }
}

export default ConfirmLeave;
{'fraction_non_alphanumeric': 0.12351778656126482, 'fraction_numerical': 0.0008469791078486731, 'mean_word_length': 2.130799823243482, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '3667338', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1902, 'n_words': 483}