<commit_before>/* global: zicht_admin_rc */
/**
 * zicht_admin_rc is a simple front end helper to do "rc" request (RC stands for Remote Control) to the back end.
 * This is typically suitable for flushing caches, triggering reindexes, etc. etc.
 *
 * This JS is included by the controls.html.twig template and runs stand-alone (vanilla), but it makes use of the
 * `glyphicons` class names to show a status indicator next to the button.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Zicht online <http://zicht.nl>
 */
var zicht_admin_rc = (function() {
    var doRequest, setState;

    /**
     * Send an XHR and call callback after the response was received.
     *
     * @param {String} method
     * @param {String} url
     * @param {function} callback
     */
    doRequest = function (method, url, callback) {
        var xhr = new XMLHttpRequest();

        xhr.open(method, url, true);
        xhr.onreadystatechange = function() {
            var data = null;

            if (xhr.readyState === 4) {
                data = false;

                if (xhr.getResponseHeader('Content-Type').indexOf('application/json') >= 0) {
                    data = JSON.parse(xhr.responseText);
                }

                callback(data, xhr.responseText);
            }
        };
        xhr.send();
    };


    /**
     * Set element state
     *
     * @param {HTMLElement} el
     * @param {String} state
     * @param {String} text
     */
    setState = function (el, state, text) {
        switch (state) {
            case 'error':
                el.setAttribute('class', 'glyphicon glyphicon-remove');
                el.style.color = 'red';

                break;
            case 'ok':
                el.setAttribute('class', 'glyphicon glyphicon-ok');
                el.style.color = 'green';

                break;
            case 'on':
                el.setAttribute('class', 'glyphicon glyphicon-ok');
                el.style.color = 'green';

                break;
            case 'off':
                el.setAttribute('className', 'glyphicon');

                break;
        }
        el.setAttribute('title', text);
    };


    handleErrorResponse = function (el, response, responseTxt) {
        var isError = false;

        if (!response) {
            setState(el, 'error', responseTxt);
            isError = true;
        } else if (typeof response.error !== 'undefined') {
            setState(el, 'error', response.error);
            isError = true;
        }
        return isError;
    };

    /**
     * Registers a form as being an RC form
     *
     * @param {HTMLFormElement} form
     */
    function registerForm(form) {
        var button = form.getElementsByTagName('button').item(0),
            state = form.ownerDocument.createElement('i');

        state.style.paddingLeft = '14px';
        button.parentNode.insertBefore(state, button.nextSibling);

        form.addEventListener('submit', function(e) {
            e.preventDefault();
            return false;
        });

        console.log(':)');

        if (form.getAttribute('data-mode') === 'toggle') {
            doRequest(
                'GET',
                form.getAttribute('action'),
                function (response, responseTxt) {
                    var toggleStatus,
                        updateStatus = function(response) {
                            toggleStatus = response;
                            if (toggleStatus.status) {
                                setState(state, 'ok');
                            } else {
                                setState(state, 'off');
                            }
                        };

                    if (handleErrorResponse(state, response, responseTxt)) {
                        return;
                    }
                    updateStatus(response);

                    button.addEventListener('click', function (e) {
                        button.setAttribute('disabled', 'disabled');

                        state.setAttribute('class', 'glyphicon glyphicon-hourglass');
                        state.style.color = 'grey';
<commit_msg>remove debug code<commit_after>/* global: zicht_admin_rc */
/**
 * zicht_admin_rc is a simple front end helper to do "rc" request (RC stands for Remote Control) to the back end.
 * This is typically suitable for flushing caches, triggering reindexes, etc. etc.
 *
 * This JS is included by the controls.html.twig template and runs stand-alone (vanilla), but it makes use of the
 * `glyphicons` class names to show a status indicator next to the button.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Zicht online <http://zicht.nl>
 */
var zicht_admin_rc = (function() {
    var doRequest, setState;

    /**
     * Send an XHR and call callback after the response was received.
     *
     * @param {String} method
     * @param {String} url
     * @param {function} callback
     */
    doRequest = function (method, url, callback) {
        var xhr = new XMLHttpRequest();

        xhr.open(method, url, true);
        xhr.onreadystatechange = function() {
            var data = null;

            if (xhr.readyState === 4) {
                data = false;

                if (xhr.getResponseHeader('Content-Type').indexOf('application/json') >= 0) {
                    data = JSON.parse(xhr.responseText);
                }

                callback(data, xhr.responseText);
            }
        };
        xhr.send();
    };


    /**
     * Set element state
     *
     * @param {HTMLElement} el
     * @param {String} state
     * @param {String} text
     */
    setState = function (el, state, text) {
        switch (state) {
            case 'error':
                el.setAttribute('class', 'glyphicon glyphicon-remove');
                el.style.color = 'red';

                break;
            case 'ok':
                el.setAttribute('class', 'glyphicon glyphicon-ok');
                el.style.color = 'green';

                break;
            case 'on':
                el.setAttribute('class', 'glyphicon glyphicon-ok');
                el.style.color = 'green';

                break;
            case 'off':
                el.setAttribute('className', 'glyphicon');

                break;
        }
        el.setAttribute('title', text);
    };


    handleErrorResponse = function (el, response, responseTxt) {
        var isError = false;

        if (!response) {
            setState(el, 'error', responseTxt);
            isError = true;
        } else if (typeof response.error !== 'undefined') {
            setState(el, 'error', response.error);
            isError = true;
        }
        return isError;
    };

    /**
     * Registers a form as being an RC form
     *
     * @param {HTMLFormElement} form
     */
    function registerForm(form) {
        var button = form.getElementsByTagName('button').item(0),
            state = form.ownerDocument.createElement('i');

        state.style.paddingLeft = '14px';
        button.parentNode.insertBefore(state, button.nextSibling);

        form.addEventListener('submit', function(e) {
            e.preventDefault();
            return false;
        });

        if (form.getAttribute('data-mode') === 'toggle') {
            doRequest(
                'GET',
                form.getAttribute('action'),
                function (response, responseTxt) {
                    var toggleStatus,
                        updateStatus = function(response) {
                            toggleStatus = response;
                            if (toggleStatus.status) {
                                setState(state, 'ok');
                            } else {
                                setState(state, 'off');
                            }
                        };

                    if (handleErrorResponse(state, response, responseTxt)) {
                        return;
                    }
                    updateStatus(response);

                    button.addEventListener('click', function (e) {
                        button.setAttribute('disabled', 'disabled');

                        state.setAttribute('class', 'glyphicon glyphicon-hourglass');
                        state.style.color = 'grey';

{'fraction_non_alphanumeric': 0.11423039690222653, 'fraction_numerical': 0.0012100677637947724, 'mean_word_length': 1.5091074681238617, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '4891836', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2074, 'n_words': 541}