<commit_before>var categoryGroups = [];

$(document).ready(function() {
    loadTable();
    bindEditModal();
    //bindModalCancelCallback();
    bindFormSubmit();
    //bindDeleteButton();
    bindCreateModal();
    //bindSortButtons();
});


function loadTable() {
    $('#overlay').show();
    var heskUrl = $('p#hesk-path').text();
    var $tableBody = $('#table-body');

    $.ajax({
        method: 'GET',
        url: heskUrl + 'api/index.php/v1/category-groups',
        headers: { 'X-Internal-Call': true },
        success: function(data) {
            $tableBody.html('');

            if (data.length === 0) {
                $tableBody.append('<td colspan="4">' + mfhLang.text('no_category_groups_found') + '</td>');
            }

            var lastElement = undefined;
            var first = true;
            $.each(data, function() {
                var $template = $($('#category-group-row-template').html());

                $template.find('span[data-property="id"]').text(this.id).attr('data-value', this.id);
                var $nameField = $template.find('td[data-property="name"]');
                $nameField.text(this.names[$('input[name="hesk_lang"]').val()]);

                $template.find('[data-property="parent-name"]').text(this.parentId === null ?
                    mfhLang.text('none') :
                    this.parentId);

                $tableBody.append($template);

                categoryGroups[this.id] = this;

                lastElement = this;

                if (first) {
                    $template.find('[data-direction="up"]').css('visibility', 'hidden');
                    first = false;
                }
            });

            if (lastElement) {
                //-- Hide the down arrow on the last element
                $('[data-value="' + lastElement.id + '"]').parent().parent()
                    .find('[data-direction="down"]').css('visibility', 'hidden');
            }

            $.each(categoryGroups, function() {
                var catId = this.id;
                var parentId = this.parentId;

                console.log(catId);
                console.info(parentId);
            });
        },
        error: function(data) {
            mfhAlert.errorWithLog(mfhLang.text('error_retrieving_category_groups'), data.responseJSON);
            console.error(data);
        },
        complete: function() {
            $('#overlay').hide();
        }
    });
}

function bindEditModal() {
    $(document).on('click', '[data-action="edit"]', function() {
        var element = categoryGroups[$(this).parent().parent().find('[data-property="id"]').text()];
        var $modal = $('#category-modal');

        $modal.find('#edit-label').show();
        $modal.find('#create-label').hide();

        for (var key in element.names) {
            $modal.find('input[name="' + key + '"]').val(element.names[key]);
        }

        refreshParentCategoryGroups();
        $modal.find('select[name="parent-category-group"]').val(element.parentId).selectpicker('refresh');

        console.info(element);
        $modal.modal('show');
    });
}

function refreshParentCategoryGroups() {
    var $dropdown = $('#category-modal').find('select[name="parent-category-group"]');
    $dropdown.html('');
    $dropdown.append('<option value="">' + mfhLang.text('none') + '</option>');

    for (var key in categoryGroups) {
        var value = categoryGroups[key];

        $dropdown.append('<option value="' + value.id + '">' + mfhStrings.escape(value.names[$('input[name="hesk_lang"]').val()]) + '</option>');
    }
    $dropdown.selectpicker('refresh');
}

function bindCreateModal() {
    $('#create-button').click(function() {
        var $modal = $('#category-modal');
        $modal.find('#edit-label').hide();
        $modal.find('#create-label').show();

        $modal.find('input[data-type="name"]').val('');
        $modal.find('select[name="parent-category-group"]').val('');
        $modal.find('input[name="id"]').val('-1');
        refreshParentCategoryGroups();

        $modal.modal('show');
    });
}

function bindModalCancelCallback() {
    $('.cancel-callback').click();
}

function bindFormSubmit() {
    $('form#manage-category').submit(function(e) {
        e.preventDefault();
        var heskUrl = $('p#hesk-path').text();

        var $modal = $('#category-modal');
        var names = {};
        $.each($('input[data-type="name"]'), function() {
            names[$(this).attr('name')] = $(this).val();
        });

        var $parentCategoryGroupDropdown = $('select[name="parent-category-group"]');
        var data = {
            names: names,
            parentId: $parentCategoryGroupDropdown.val() !== '' ? parseInt($parentCategoryGroupDropdown.val()) : null
        };<commit_msg>#515 Working on preventing cyclic subcategories<commit_after>var categoryGroups = [];

$(document).ready(function() {
    loadTable();
    bindEditModal();
    //bindModalCancelCallback();
    bindFormSubmit();
    //bindDeleteButton();
    bindCreateModal();
    //bindSortButtons();
});


function loadTable() {
    $('#overlay').show();
    var heskUrl = $('p#hesk-path').text();
    var $tableBody = $('#table-body');

    $.ajax({
        method: 'GET',
        url: heskUrl + 'api/index.php/v1/category-groups',
        headers: { 'X-Internal-Call': true },
        success: function(data) {
            $tableBody.html('');

            if (data.length === 0) {
                $tableBody.append('<td colspan="4">' + mfhLang.text('no_category_groups_found') + '</td>');
            }

            var lastElement = undefined;
            var first = true;
            $.each(data, function() {
                var $template = $($('#category-group-row-template').html());

                $template.find('span[data-property="id"]').text(this.id).attr('data-value', this.id);
                var $nameField = $template.find('td[data-property="name"]');
                $nameField.text(this.names[$('input[name="hesk_lang"]').val()]);

                $template.find('[data-property="parent-name"]').text(this.parentId === null ?
                    mfhLang.text('none') :
                    this.parentId);

                $tableBody.append($template);

                categoryGroups[this.id] = this;

                lastElement = this;

                if (first) {
                    $template.find('[data-direction="up"]').css('visibility', 'hidden');
                    first = false;
                }
            });

            if (lastElement) {
                //-- Hide the down arrow on the last element
                $('[data-value="' + lastElement.id + '"]').parent().parent()
                    .find('[data-direction="down"]').css('visibility', 'hidden');
            }

            $.each(categoryGroups, function() {
                var catId = this.id;
                var parentId = this.parentId;

                console.log(catId);
                console.info(parentId);
            });
        },
        error: function(data) {
            mfhAlert.errorWithLog(mfhLang.text('error_retrieving_category_groups'), data.responseJSON);
            console.error(data);
        },
        complete: function() {
            $('#overlay').hide();
        }
    });
}

function bindEditModal() {
    $(document).on('click', '[data-action="edit"]', function() {
        var element = categoryGroups[$(this).parent().parent().find('[data-property="id"]').text()];
        var $modal = $('#category-modal');

        $modal.find('#edit-label').show();
        $modal.find('#create-label').hide();

        for (var key in element.names) {
            $modal.find('input[name="' + key + '"]').val(element.names[key]);
        }

        refreshParentCategoryGroups(element.id);
        $modal.find('select[name="parent-category-group"]').val(element.parentId).selectpicker('refresh');

        console.info(element);
        $modal.modal('show');
    });
}

function refreshParentCategoryGroups(ignoreId) {
    var $dropdown = $('#category-modal').find('select[name="parent-category-group"]');
    $dropdown.html('');
    $dropdown.append('<option value="">' + mfhLang.text('none') + '</option>');

    // Remove itself and any parents
    var exclude = [];
    if (ignoreId !== undefined) {
        var element = categoryGroups[ignoreId];
        exclude.push(element.id);

        while (element.parentId !== null) {
            element = categoryGroups[element.parentId];
            exclude.push(element.id);
        }
    }

    for (var key in categoryGroups) {
        if (exclude.indexOf(key) > -1) {
            continue;
        }

        var value = categoryGroups[key];

        $dropdown.append('<option value="' + value.id + '">' + mfhStrings.escape(value.names[$('input[name="hesk_lang"]').val()]) + '</option>');
    }
    $dropdown.selectpicker('refresh');
}

function bindCreateModal() {
    $('#create-button').click(function() {
        var $modal = $('#category-modal');
        $modal.find('#edit-label').hide();
        $modal.find('#create-label').show();

        $modal.find('input[data-type="name"]').val('');
        $modal.find('select[name="parent-category-group"]').val('');
        $modal.find('input[name="id"]').val('-1');
        refreshParentCategoryGroups(undefined);

        $modal.modal('show');
    });
}

function bindModalCancelCallback() {
    $('.cancel-callback').click();
}

function bindFormSubmit() {
    $('form#manage-category').submit(function(e) {
        e.preventDefault();
        var heskUrl = $('p#hesk-path').text();

        var $modal = $('#category-modal');
        var names = {};
        $.each($('input[data-type="name"]'), function() {
            names[$(this).attr('name')] = $(this).val();
        });

        var $parentCategoryGroupDropdown = $('select[name="parent-category-group"]');
        var data = {
            names: names,
            parentId: $parentCategoryGroupDropdown.val() !== '' ? parseInt($parentCategoryGroupDropdown.val()) : null
        };
{'fraction_non_alphanumeric': 0.19423459244532804, 'fraction_numerical': 0.0011928429423459245, 'mean_word_length': 2.2750651041666665, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '3820190', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2791, 'n_words': 419}