<commit_before>define([
    'uijet_dir/uijet',
    'explorer'
], function (uijet, explorer) {

    uijet.Adapter('LegendsList', {
        createItemModel : function (state) {
            var model = new explorer.LegendItemModel(state || {
                title       : gettext('Insert title'),
                muni        : '',
                nodes       : []
            });
            this.resource.add(model);
            return model;
        },
        createItemWidget: function (model, index) {
            index = typeof index == 'number' ? index : this.resource.length - 1;
            uijet.start({
                factory : 'LegendItem',
                config  : {
                    element : uijet.$('<li>', {
                        id          : this.id + '_item_' + model.cid
                    }).appendTo(this.$element),
                    resource: model,
                    index   : index,
                    signals : {
                        post_full_render: '-legend_item_added'
                    },
                    color   : this.resource.colors[index * 2]
                }
            }, true);
            return this;
        },
        createItem      : function (model_index) {
            var state = typeof model_index == 'number' ? this.resource.at(model_index).attributes : model_index,
                model = this.createItemModel(state);

            return this.createItemWidget(model);
        },
        addItem         : function (model_index) {
            this.createItem(model_index)
                .selectItem(this.resource.length - 1);
            return this;
        },
        setEntity       : function (id) {
            this.resource.at(this.current_index).set({
                muni: uijet.Resource('Munis').get(id)
            });
            return this;
        },
        selectItem      : function (index) {
            var model, muni;
            if ( index !== this.current_index ) {
                model = this.resource.at(index);
                muni = model.get('muni');
                this.current_index = index;
                if ( muni ) {
                    this.updateState(model, muni);
                }
            }
        },
        updateState     : function (model, muni) {
            if ( ! model ) {
                model = this.resource.at(this.current_index);
            }
            if ( ! muni ) {
                muni = model.get('muni');
            }
            this.publish('change_state', {
                entity_id   : muni.get('id'),
                selection   : model.get('state')
            });
        },
        deleteItem      : function (index) {
            var is_current_index = index === this.current_index,
                new_length;
            this.resource.remove(this.resource.at(index));
            // if the user is currently viewing the item s/he's deleting
            if ( is_current_index ) {
                new_length = this.resource.length;
                // if we still have other legend items left
                // and it was the last item in the list
                if ( new_length && new_length === index ) {
                    index--;
                }
            }
            // if current selected item is below the deleted one then need to shift the index by 1
            else if ( index < this.current_index ) {
                this.current_index--;
            }
        },
        removeItem      : function (index) {
            this.deleteItem(index);
            if ( this.resource.length ) {
                if ( this.picking ) {
                    uijet.publish('legends_list.selected', this.current_index);
                }<commit_msg>Fixed current_index setting after item deletion<commit_after>define([
    'uijet_dir/uijet',
    'explorer'
], function (uijet, explorer) {

    uijet.Adapter('LegendsList', {
        createItemModel : function (state) {
            var model = new explorer.LegendItemModel(state || {
                title       : gettext('Insert title'),
                muni        : '',
                nodes       : []
            });
            this.resource.add(model);
            return model;
        },
        createItemWidget: function (model, index) {
            index = typeof index == 'number' ? index : this.resource.length - 1;
            uijet.start({
                factory : 'LegendItem',
                config  : {
                    element : uijet.$('<li>', {
                        id          : this.id + '_item_' + model.cid
                    }).appendTo(this.$element),
                    resource: model,
                    index   : index,
                    signals : {
                        post_full_render: '-legend_item_added'
                    },
                    color   : this.resource.colors[index * 2]
                }
            }, true);
            return this;
        },
        createItem      : function (model_index) {
            var state = typeof model_index == 'number' ? this.resource.at(model_index).attributes : model_index,
                model = this.createItemModel(state);

            return this.createItemWidget(model);
        },
        addItem         : function (model_index) {
            this.createItem(model_index)
                .selectItem(this.resource.length - 1);
            return this;
        },
        setEntity       : function (id) {
            this.resource.at(this.current_index).set({
                muni: uijet.Resource('Munis').get(id)
            });
            return this;
        },
        selectItem      : function (index) {
            var model, muni;
            if ( index !== this.current_index ) {
                model = this.resource.at(index);
                muni = model.get('muni');
                this.current_index = index;
                if ( muni ) {
                    this.updateState(model, muni);
                }
            }
        },
        updateState     : function (model, muni) {
            if ( ! model ) {
                model = this.resource.at(this.current_index);
            }
            if ( ! muni ) {
                muni = model.get('muni');
            }
            this.publish('change_state', {
                entity_id   : muni.get('id'),
                selection   : model.get('state')
            });
        },
        deleteItem      : function (index) {
            var is_current_index = index === this.current_index,
                new_length;
            this.resource.remove(this.resource.at(index));
            // if the user is currently viewing the item s/he's deleting
            if ( is_current_index ) {
                new_length = this.resource.length;
                // if we still have other legend items left
                // and it was the last item in the list
                if ( new_length && new_length === index ) {
                    this.current_index = index - 1;
                }
            }
            // if current selected item is below the deleted one then need to shift the index by 1
            else if ( index < this.current_index ) {
                this.current_index--;
            }
        },
        removeItem      : function (index) {
            this.deleteItem(index);
            if ( this.resource.length ) {
                if ( this.picking ) {
                    uijet.publish('legends_list.selected', this.current_index);
                }
{'fraction_non_alphanumeric': 0.10400214161424173, 'fraction_numerical': 0.001204658010975773, 'mean_word_length': 1.1796966161026838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '4557974', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1844, 'n_words': 407}