<commit_before>(function (E, $) {
    const onKeyUpDelay = 1000;
    const namingFromatUrl = '/backend/naming_format_land_parcels/build.json';

    class SupportNamePreview {
        constructor(formElement) {
            this.formElement = formElement;
            this.previewElement = formElement.querySelector('[data-support-name-preview]');
            this.customNameInput = formElement.querySelector('#activity_production_custom_name');
            this.cultivableZoneSelect = formElement.querySelector('input#activity_production_cultivable_zone_id');
            this.namingBaseParams = JSON.parse(this.previewElement.dataset.supportNamePreview).activity_production;
            this.boundInput = new FreeFieldInput(this.customNameInput);
        }

        init() {
            this.boundInput.inputElement.addEventListener('keyup', () => this.enableLoader());

            this.boundInput.inputElement.addEventListener('keyup', _.debounce(this.update.bind(this), onKeyUpDelay));

            this.cultivableZoneSelect.addEventListener('unroll:selector:change', () => {
                this.enableLoader();
                this.update();
            });

            window.addEventListener('focus', () => this.update());
            this.update();
        }

        enableLoader() {
            if (this.previewElement.classList.contains('loading-error')) {
                this.previewElement.classList.remove('loading-error');
            }
            this.previewElement.classList.add('spinner');
            this.previewElement.textContent = '';
        }

        disableLoader() {
            this.previewElement.classList.remove('spinner');
        }

        setValue(value) {
            this.previewElement.textContent = value;
        }

        getParams() {
            const cultivableZoneId = this.formElement.querySelector('input#activity_production_cultivable_zone_id + input').value;
            let seasonId = '';
            if (this.formElement.querySelector('#activity_production_season') !== null) {
                seasonId = this.formElement.querySelector('input[id^=activity_production_season_id]:checked').value || '';
            }
            const freeFieldValue = this.formElement.querySelector('input#activity_production_custom_name').value;
            return {
                ...this.namingBaseParams,
                cultivable_zone_id: cultivableZoneId,
                season_id: seasonId,
                free_field: freeFieldValue,
            };
        }

        update() {
            $.ajax({
                url: namingFromatUrl,
                data: this.getParams(),
                context: this,
                success: function (data, status, request) {
                    this.disableLoader();
                    this.setValue(data.name);
                    this.boundInput.setVisibility(data.has_free_field);
                },
                error: function (err) {
                    _.delay(
                        function () {
                            this.previewElement.classList.add('loading-error');
                        }.bind(this),
                        1000
                    );
                },
            });
        }
    }

    class FreeFieldInput {
        constructor(inputElement) {
            this.inputElement = inputElement;
            this.addonElement = inputElement.previousElementSibling;
            this.hintElement = inputElement.closest('.controls').querySelector('.help-block');
        }

        setVisibility(value) {
            const display = value ? 'inline-block' : 'none';
            this.inputElement.disabled = !value;
            this.addonElement.style.display = display;
            this.inputElement.style.display = display;
            this.hintElement.style.display = value ? 'none' : 'inline-block';
        }
    }

    E.onDomReady(function () {
        const formElement = document.querySelector('form#new_activity_production,form[id^=edit_activity_production]');
        if (formElement !== null) {
            const previewElement = formElement.querySelector('[data-support-name-preview]');
            if (previewElement !== null) {
                new SupportNamePreview(formElement).init();
            }

            const cultivableZoneSelector = formElement.querySelector('#activity_production_cultivable_zone_id');
            cultivableZoneSelector.addEventListener('unroll:selector:change', function(_event, _value, is_initialization) {
                if (is_initialization) {
                    return;
                } else {
                    const element = $(this);
                    const id = element.selector('value');
                    const map = $('#activity_production_support_shape');
                    new E.cultivableZoneService().get(id).then((cultivable_zone, _status, _request) => {
                        map.mapeditor('edit', cultivable_zone.shape, true);
                    });
                }
            });
        }
    });
})(ekylibre, jQuery);<commit_msg>Fix error related to the handling of 'this' in javascript being different for arrow functions and anonymous/named functions<commit_after>(function (E, $) {
    const onKeyUpDelay = 1000;
    const namingFromatUrl = '/backend/naming_format_land_parcels/build.json';

    class SupportNamePreview {
        constructor(formElement) {
            this.formElement = formElement;
            this.previewElement = formElement.querySelector('[data-support-name-preview]');
            this.customNameInput = formElement.querySelector('#activity_production_custom_name');
            this.cultivableZoneSelect = formElement.querySelector('input#activity_production_cultivable_zone_id');
            this.namingBaseParams = JSON.parse(this.previewElement.dataset.supportNamePreview).activity_production;
            this.boundInput = new FreeFieldInput(this.customNameInput);
        }

        init() {
            this.boundInput.inputElement.addEventListener('keyup', () => this.enableLoader());

            this.boundInput.inputElement.addEventListener('keyup', _.debounce(this.update.bind(this), onKeyUpDelay));

            this.cultivableZoneSelect.addEventListener('unroll:selector:change', () => {
                this.enableLoader();
                this.update();
            });

            window.addEventListener('focus', () => this.update());
            this.update();
        }

        enableLoader() {
            if (this.previewElement.classList.contains('loading-error')) {
                this.previewElement.classList.remove('loading-error');
            }
            this.previewElement.classList.add('spinner');
            this.previewElement.textContent = '';
        }

        disableLoader() {
            this.previewElement.classList.remove('spinner');
        }

        setValue(value) {
            this.previewElement.textContent = value;
        }

        getParams() {
            const cultivableZoneId = this.formElement.querySelector('input#activity_production_cultivable_zone_id + input').value;
            let seasonId = '';
            if (this.formElement.querySelector('#activity_production_season') !== null) {
                seasonId = this.formElement.querySelector('input[id^=activity_production_season_id]:checked').value || '';
            }
            const freeFieldValue = this.formElement.querySelector('input#activity_production_custom_name').value;
            return {
                ...this.namingBaseParams,
                cultivable_zone_id: cultivableZoneId,
                season_id: seasonId,
                free_field: freeFieldValue
            };
        }

        update() {
            $.ajax({
                url: namingFromatUrl,
                data: this.getParams(),
                context: this,
                success: data => {
                    this.disableLoader();
                    this.setValue(data.name);
                    this.boundInput.setVisibility(data.has_free_field);
                },
                error: () => {
                    _.delay(() => this.previewElement.classList.add('loading-error'), 1000);
                }
            });
        }
    }

    class FreeFieldInput {
        constructor(inputElement) {
            this.inputElement = inputElement;
            this.addonElement = inputElement.previousElementSibling;
            this.hintElement = inputElement.closest('.controls').querySelector('.help-block');
        }

        setVisibility(value) {
            const display = value ? 'inline-block' : 'none';
            this.inputElement.disabled = !value;
            this.addonElement.style.display = display;
            this.inputElement.style.display = display;
            this.hintElement.style.display = value ? 'none' : 'inline-block';
        }
    }

    E.onDomReady(function () {
        const formElement = document.querySelector('form#new_activity_production,form[id^=edit_activity_production]');
        if (formElement !== null) {
            const previewElement = formElement.querySelector('[data-support-name-preview]');
            if (previewElement !== null) {
                new SupportNamePreview(formElement).init();
            }

            const cultivableZoneSelector = formElement.querySelector('#activity_production_cultivable_zone_id');
            cultivableZoneSelector.addEventListener('unroll:selector:change', function (_event, _value, is_initialization) {
                if (!is_initialization) {
                    const element = $(this);
                    const id = element.selector('value');
                    const map = $('#activity_production_support_shape');
                    new E.CultivableZoneService().get(id)
                                                 .then(cultivable_zone => map.mapeditor('edit', cultivable_zone.shape, true));
                }
            });
        }
    });
})(ekylibre, jQuery);
{'fraction_non_alphanumeric': 0.11183616102800924, 'fraction_numerical': 0.0016062644312819997, 'mean_word_length': 1.9853161522325442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6124869', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2373, 'n_words': 333}