<commit_before>var featuresAttributeListConfiguratorLabel = {
    'attribute': OpenLayers.i18n('Attributes'),
    'showInList': OpenLayers.i18n('Show in list'),
    'showInPopup': OpenLayers.i18n('Show in popup'),
    'apply': OpenLayers.i18n('Apply'),
    'noLayer': OpenLayers.i18n('No layer selected'),
    'noAttributes': OpenLayers.i18n('Layer have no attributes')
};



gbi.widgets = gbi.widgets || {};

gbi.widgets.FeatureAttributesListConfigurator = function(thematicalVector, options) {
    if(!(thematicalVector instanceof gbi.widgets.ThematicalVector)) {
        return;
    }
    var self = this;
    var defaults = {
        element: 'featureattributelistconfigurator',
        mode: 'exact',
        initOnly: false
    }
    this.options = $.extend({}, defaults, options);
    this.thematicalVector = thematicalVector
    this.editor = thematicalVector.editor;
    this.activeLayer = this.editor.layerManager.active();

    $(gbi).on('gbi.layermanager.layer.active', function(event, layer) {
        self.activeLayer = layer;
        if(self.activeLayer) {
            self._registerLayerEvents(self.activeLayer);
            self.attributes = self.activeLayer.featuresAttributes() || [];
        } else {
            self.attributes = [];
        }
        self.render();
    });
    if(this.activeLayer) {
        this._registerLayerEvents(this.activeLayer);
        this.attributes = this.activeLayer.featuresAttributes() || [];
    } else {
        this.attributes = [];
    }
    if(!this.options.initOnly) {
        self.render();
    }
};
gbi.widgets.FeatureAttributesListConfigurator.prototype = {
    render: function() {
        var self = this;
        var element = $('#' + this.options.element);
        element.empty();

        if(!this.activeLayer) {
            element.append($('<div class="text-center">' + featuresAttributeListConfiguratorLabel.noLayer + '</div>'));
            return;
        }

        element.append(tmpl(
            gbi.widgets.FeatureAttributesListConfigurator.template, {
                attributes: self.attributes || []
            }
        ));

        var listAttributes = self.activeLayer ? self.activeLayer.listAttributes() : [];
        var popupAttributes = self.activeLayer ? self.activeLayer.popupAttributes() : [];

        if(listAttributes) {
            element.find('.list-attribute').each(function(idx, elm) {
                elm = $(elm);
                elm.change(function() {
                    self._restrictAttributes(elm, '.list-attribute', 5)
                })
                if($.inArray(elm.val(), listAttributes) != -1) {
                    elm.attr('checked', 'checked');
                }

            })
        }

        if(popupAttributes) {
            element.find('.popup-attribute').each(function(idx, elm) {
                elm = $(elm);
                elm.change(function() {
                    self._restrictAttributes(elm, '.popup-attribute', 5)
                });
                if($.inArray(elm.val(), popupAttributes) != -1) {
                    elm.attr('checked', 'checked');
                }
            })
        }

        element.find('#sortable').sortable();

        $('#setListAttributes').click(function() {
            var listAttributes = [];
            var popupAttributes = [];
            $.each(self.element.find('.list-attribute:checked'), function(idx, checkbox) {
                listAttributes.push(checkbox.value);
            });
            $.each(self.element.find('.popup-attribute:checked'), function(idx, checkbox) {
                popupAttributes.push(checkbox.value);
            });
            self.activeLayer.listAttributes(listAttributes);
            self.activeLayer.popupAttributes(popupAttributes);
            if(self.activeLayer instanceof gbi.Layers.Couch) {
                self.activeLayer._saveGBIData();
            }
        })
    },
    _registerLayerEvents: function(layer) {
        var self = this;
        if(layer instanceof gbi.Layers.SaveableVector && !layer.loaded) {
            $(layer).on('gbi.layer.couch.loadFeaturesEnd', function() {
                self.attributes = layer.featuresAttributes() || [];
                self.render();
            });
        }
        $(layer).on('gbi.layer.vector.featureAttributeChanged', function() {
            self.attributes = layer.featuresAttributes() || [];
            self.render();
        });
    },
    _restrictAttributes: function(elm, selector, max) {
        var self = this;
        var count = self.element.find(selector + ':checked').length;
        if(count > max) {
            elm.removeAttr('checked');
            console.log('Only ' + max + ' or less attributes can be selected')
        }
    }
};

gbi.widgets.FeatureAttributesListConfigurator.template = '\
    <% if(attributes.length == 0) { %>\
        <div>' + featuresAttributeListConfiguratorLabel.noAttributes + '</div>\
    <% } else { %>\
        <table class="table">\
            <thead>\
                <tr>\
                    <th>&nbsp;</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.attribute + '</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.showInList + '</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.showInPopup + '</th>\
                </tr>\
            </thead>\
            <tbody id="sortable">\
                <% for(var a_key in attributes) { %>\<commit_msg>Fixed typo<commit_after>var featuresAttributeListConfiguratorLabel = {
    'attributes': OpenLayers.i18n('Attributes'),
    'showInList': OpenLayers.i18n('Show in list'),
    'showInPopup': OpenLayers.i18n('Show in popup'),
    'apply': OpenLayers.i18n('Apply'),
    'noLayer': OpenLayers.i18n('No layer selected'),
    'noAttributes': OpenLayers.i18n('Layer have no attributes')
};



gbi.widgets = gbi.widgets || {};

gbi.widgets.FeatureAttributesListConfigurator = function(thematicalVector, options) {
    if(!(thematicalVector instanceof gbi.widgets.ThematicalVector)) {
        return;
    }
    var self = this;
    var defaults = {
        element: 'featureattributelistconfigurator',
        mode: 'exact',
        initOnly: false
    }
    this.options = $.extend({}, defaults, options);
    this.thematicalVector = thematicalVector
    this.editor = thematicalVector.editor;
    this.activeLayer = this.editor.layerManager.active();

    $(gbi).on('gbi.layermanager.layer.active', function(event, layer) {
        self.activeLayer = layer;
        if(self.activeLayer) {
            self._registerLayerEvents(self.activeLayer);
            self.attributes = self.activeLayer.featuresAttributes() || [];
        } else {
            self.attributes = [];
        }
        self.render();
    });
    if(this.activeLayer) {
        this._registerLayerEvents(this.activeLayer);
        this.attributes = this.activeLayer.featuresAttributes() || [];
    } else {
        this.attributes = [];
    }
    if(!this.options.initOnly) {
        self.render();
    }
};
gbi.widgets.FeatureAttributesListConfigurator.prototype = {
    render: function() {
        var self = this;
        var element = $('#' + this.options.element);
        element.empty();

        if(!this.activeLayer) {
            element.append($('<div class="text-center">' + featuresAttributeListConfiguratorLabel.noLayer + '</div>'));
            return;
        }

        element.append(tmpl(
            gbi.widgets.FeatureAttributesListConfigurator.template, {
                attributes: self.attributes || []
            }
        ));

        var listAttributes = self.activeLayer ? self.activeLayer.listAttributes() : [];
        var popupAttributes = self.activeLayer ? self.activeLayer.popupAttributes() : [];

        if(listAttributes) {
            element.find('.list-attribute').each(function(idx, elm) {
                elm = $(elm);
                elm.change(function() {
                    self._restrictAttributes(elm, '.list-attribute', 5)
                })
                if($.inArray(elm.val(), listAttributes) != -1) {
                    elm.attr('checked', 'checked');
                }

            })
        }

        if(popupAttributes) {
            element.find('.popup-attribute').each(function(idx, elm) {
                elm = $(elm);
                elm.change(function() {
                    self._restrictAttributes(elm, '.popup-attribute', 5)
                });
                if($.inArray(elm.val(), popupAttributes) != -1) {
                    elm.attr('checked', 'checked');
                }
            })
        }

        element.find('#sortable').sortable();

        $('#setListAttributes').click(function() {
            var listAttributes = [];
            var popupAttributes = [];
            $.each(self.element.find('.list-attribute:checked'), function(idx, checkbox) {
                listAttributes.push(checkbox.value);
            });
            $.each(self.element.find('.popup-attribute:checked'), function(idx, checkbox) {
                popupAttributes.push(checkbox.value);
            });
            self.activeLayer.listAttributes(listAttributes);
            self.activeLayer.popupAttributes(popupAttributes);
            if(self.activeLayer instanceof gbi.Layers.Couch) {
                self.activeLayer._saveGBIData();
            }
        })
    },
    _registerLayerEvents: function(layer) {
        var self = this;
        if(layer instanceof gbi.Layers.SaveableVector && !layer.loaded) {
            $(layer).on('gbi.layer.couch.loadFeaturesEnd', function() {
                self.attributes = layer.featuresAttributes() || [];
                self.render();
            });
        }
        $(layer).on('gbi.layer.vector.featureAttributeChanged', function() {
            self.attributes = layer.featuresAttributes() || [];
            self.render();
        });
    },
    _restrictAttributes: function(elm, selector, max) {
        var self = this;
        var count = self.element.find(selector + ':checked').length;
        if(count > max) {
            elm.removeAttr('checked');
            console.log('Only ' + max + ' or less attributes can be selected')
        }
    }
};

gbi.widgets.FeatureAttributesListConfigurator.template = '\
    <% if(attributes.length == 0) { %>\
        <div>' + featuresAttributeListConfiguratorLabel.noAttributes + '</div>\
    <% } else { %>\
        <table class="table">\
            <thead>\
                <tr>\
                    <th>&nbsp;</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.attributes + '</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.showInList + '</th>\
                    <th>' + featuresAttributeListConfiguratorLabel.showInPopup + '</th>\
                </tr>\
            </thead>\
            <tbody id="sortable">\
                <% for(var a_key in attributes) { %>\
{'fraction_non_alphanumeric': 0.1428702262111915, 'fraction_numerical': 0.0031138382635772506, 'mean_word_length': 2.009922822491731, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, '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': '2762430', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2877, 'n_words': 439}