<commit_before>var attributeEditorLabel = {
    'noAttributes': OpenLayers.i18n('No attributes'),
    'key': OpenLayers.i18n('Key'),
    'val': OpenLayers.i18n('Value'),
    'add': OpenLayers.i18n('Add'),
    'formTitle': OpenLayers.i18n('Add attribute'),
    'addAttributesNotPossible': OpenLayers.i18n('No new attributes posible'),
    'sameKeyDifferentValue': OpenLayers.i18n('Different values for same attribute'),
    'featuresWithInvalidAttributes': OpenLayers.i18n('Features with non valid attributes present'),
    'invalidFeaturesLeft': OpenLayers.i18n('features with invalid attributes left'),
    'next': OpenLayers.i18n('Next'),
    'prev': OpenLayers.i18n('Previous'),
    'additionalProperties': OpenLayers.i18n('Additional attributes'),
    'schemaViolatingAttribute': OpenLayers.i18n('This attribute is not defined in given schema. Remove it!'),
    'addJsonSchemaUrl': OpenLayers.i18n('Add JSONSchema URL')
}

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

gbi.widgets.AttributeEditor = function(editor, options) {
    var self = this;
    var defaults = {
        element: 'attributeeditor',
        alpacaSchemaElement: 'alpaca_schema',
        alpacaNonSchemaElement: 'alpaca_non_schema',
        allowNewAttributes: true,
        editable: true,
        allowSchemaLoad: true
    };
    this.layerManager = editor.layerManager;
    this.options = $.extend({}, defaults, options);
    this.element = $('#' + this.options.element);
    this.selectedFeatures = [];
    this.featureChanges = {};
    this.invalidFeatures = [];
    this.selectedInvalidFeature = false;
    this.changed = false;
    this.labelValue = undefined;
    this.renderAttributes = false;
    this.jsonSchema = this.options.jsonSchema || false;

    $.alpaca.registerView(gbi.widgets.AttributeEditor.alpacaViews.edit)
    $.alpaca.registerView(gbi.widgets.AttributeEditor.alpacaViews.display)

    var activeLayer = this.layerManager.active();
    var listenOn = activeLayer instanceof gbi.Layers.Couch ? 'gbi.layer.couch.loadFeaturesEnd' : 'gbi.layer.saveableVector.loadFeaturesEnd';
    if(!activeLayer.loaded) {
        $(activeLayer).on(listenOn, function() {
            self.render();
            $(activeLayer).off(listenOn, this);
        });
    }

    this.registerEvents();

    $(gbi).on('gbi.layermanager.layer.add', function(event, layer) {
       self.registerEvents();
    });

    self.render();
};

gbi.widgets.AttributeEditor.prototype = {
    CLASS_NAME: 'gbi.widgets.AttributeEditor',

    registerEvents: function() {
        var self = this;
        $.each(self.layerManager.vectorLayers, function(idx, layer) {
            layer.registerEvent('featureselected', self, function(f) {
                if(!(f.feature.id in self.featureChanges)) {
                    self.featureChanges[f.feature.id] = {'added': {}, 'edited': {}, 'removed': []};
                }
                self.jsonSchema = layer.jsonSchema || this.options.jsonSchema || false;
                if(self.invalidFeatures) {
                    var id = self._isInvalidFeature(f.feature);
                    if(id != -1) {
                        self.selectedInvalidFeature = self.invalidFeatures[id];
                    }
                }
                self.selectedFeatures.push(f.feature);
                self.render();
            });
            layer.registerEvent('featureunselected', self, function(f) {
                if(self.selectedInvalidFeature && self.selectedInvalidFeature.feature.id == f.feature.id) {
                    self.selectedInvalidFeature = false;
                }
                var idx = $.inArray(f.feature, self.selectedFeatures);
                if(idx != -1) {
                    self.selectedFeatures.splice(idx, 1);
                    self.render();
                }
            });
        });
    },
    render: function() {
        var self = this;
        var activeLayer = this.layerManager.active();
        self.invalidFeatures = $.isFunction(activeLayer.validateFeaturesAttributes) ? activeLayer.validateFeaturesAttributes() : [];
        var attributes = self.jsonSchema ? activeLayer.schemaAttributes() : this.renderAttributes || activeLayer.featuresAttributes();
        this.element.empty();

        if(self.options.allowSchemaLoad && !self.jsonSchema) {
            this.element.append(tmpl(gbi.widgets.AttributeEditor.addSchemaTemplate));
            $('#add_json_schema_url').click(function() {
                $(activeLayer).on('gbi.layer.vector.schemaLoaded', function(event, schema) {
                    self.setJsonSchema(schema);
                });
                var schemaURL = $('#json_schema_url').val();
                activeLayer.addSchemaFromUrl(schemaURL);
            })
        }
        if(self.invalidFeatures && self.invalidFeatures.length > 0) {
            self.renderInvalidFeatures(activeLayer);
        } else {
            self.selectedInvalidFeature = false;
        }

        if(self.selectedFeatures.length > 0) {
            self.renderInputMask(attributes, activeLayer);
        }

        //prepare list of all possible rendered attributes
        var renderedAttributes = [];
        if(self.jsonSchema) {
            renderedAttributes = activeLayer.schemaAttributes();
        }
        if(this.renderAttributes) {
            $.each(this.renderAttributes, function(idx, attribute) {
                if($.inArray(attribute, renderedAttributes) == -1) {
                    renderedAttributes.push(attribute);
                }
            });
        }
        $.each(activeLayer.featuresAttributes(), function(idx, attribute) {
            if($.inArray(attribute, renderedAttributes) == -1) {
                renderedAttributes.push(attribute);
            }
        });

        //bind events
        $.each(renderedAttributes, function(idx, key) {
            $('#'+key).change(function() {
                var newVal = $('#'+key).val();
                self.edit(key, newVal);
            });
            $('#_'+key+'_remove').click(function() {
                self.remove(key);
                return false;
            });
            $('#_'+key+'_label').click(function() {<commit_msg>Fixed attribute editor widget when no layer is active<commit_after>var attributeEditorLabel = {
    'noAttributes': OpenLayers.i18n('No attributes'),
    'key': OpenLayers.i18n('Key'),
    'val': OpenLayers.i18n('Value'),
    'add': OpenLayers.i18n('Add'),
    'formTitle': OpenLayers.i18n('Add attribute'),
    'addAttributesNotPossible': OpenLayers.i18n('No new attributes posible'),
    'sameKeyDifferentValue': OpenLayers.i18n('Different values for same attribute'),
    'featuresWithInvalidAttributes': OpenLayers.i18n('Features with non valid attributes present'),
    'invalidFeaturesLeft': OpenLayers.i18n('features with invalid attributes left'),
    'next': OpenLayers.i18n('Next'),
    'prev': OpenLayers.i18n('Previous'),
    'additionalProperties': OpenLayers.i18n('Additional attributes'),
    'schemaViolatingAttribute': OpenLayers.i18n('This attribute is not defined in given schema. Remove it!'),
    'addJsonSchemaUrl': OpenLayers.i18n('Add JSONSchema URL')
}

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

gbi.widgets.AttributeEditor = function(editor, options) {
    var self = this;
    var defaults = {
        element: 'attributeeditor',
        alpacaSchemaElement: 'alpaca_schema',
        alpacaNonSchemaElement: 'alpaca_non_schema',
        allowNewAttributes: true,
        editable: true,
        allowSchemaLoad: true
    };
    this.layerManager = editor.layerManager;
    this.options = $.extend({}, defaults, options);
    this.element = $('#' + this.options.element);
    this.selectedFeatures = [];
    this.featureChanges = {};
    this.invalidFeatures = [];
    this.selectedInvalidFeature = false;
    this.changed = false;
    this.labelValue = undefined;
    this.renderAttributes = false;
    this.jsonSchema = this.options.jsonSchema || false;

    $.alpaca.registerView(gbi.widgets.AttributeEditor.alpacaViews.edit)
    $.alpaca.registerView(gbi.widgets.AttributeEditor.alpacaViews.display)

    var activeLayer = this.layerManager.active();
    var listenOn = activeLayer instanceof gbi.Layers.Couch ? 'gbi.layer.couch.loadFeaturesEnd' : 'gbi.layer.saveableVector.loadFeaturesEnd';
    if(!activeLayer.loaded) {
        $(activeLayer).on(listenOn, function() {
            self.render();
            $(activeLayer).off(listenOn, this);
        });
    }

    this.registerEvents();

    $(gbi).on('gbi.layermanager.layer.add', function(event, layer) {
       self.registerEvents();
    });

    self.render();
};

gbi.widgets.AttributeEditor.prototype = {
    CLASS_NAME: 'gbi.widgets.AttributeEditor',

    registerEvents: function() {
        var self = this;
        $.each(self.layerManager.vectorLayers, function(idx, layer) {
            layer.registerEvent('featureselected', self, function(f) {
                if(!(f.feature.id in self.featureChanges)) {
                    self.featureChanges[f.feature.id] = {'added': {}, 'edited': {}, 'removed': []};
                }
                self.jsonSchema = layer.jsonSchema || this.options.jsonSchema || false;
                if(self.invalidFeatures) {
                    var id = self._isInvalidFeature(f.feature);
                    if(id != -1) {
                        self.selectedInvalidFeature = self.invalidFeatures[id];
                    }
                }
                self.selectedFeatures.push(f.feature);
                self.render();
            });
            layer.registerEvent('featureunselected', self, function(f) {
                if(self.selectedInvalidFeature && self.selectedInvalidFeature.feature.id == f.feature.id) {
                    self.selectedInvalidFeature = false;
                }
                var idx = $.inArray(f.feature, self.selectedFeatures);
                if(idx != -1) {
                    self.selectedFeatures.splice(idx, 1);
                    self.render();
                }
            });
        });
    },
    render: function() {
        var self = this;
        var attributes = []
        var activeLayer = this.layerManager.active();
        self.invalidFeatures = $.isFunction(activeLayer.validateFeaturesAttributes) ? activeLayer.validateFeaturesAttributes() : [];
        if(activeLayer) {
            attributes = self.jsonSchema ? activeLayer.schemaAttributes() : this.renderAttributes || activeLayer.featuresAttributes();
        }
        this.element.empty();

        if(self.options.allowSchemaLoad && !self.jsonSchema) {
            this.element.append(tmpl(gbi.widgets.AttributeEditor.addSchemaTemplate));
            $('#add_json_schema_url').click(function() {
                $(activeLayer).on('gbi.layer.vector.schemaLoaded', function(event, schema) {
                    self.setJsonSchema(schema);
                });
                var schemaURL = $('#json_schema_url').val();
                activeLayer.addSchemaFromUrl(schemaURL);
            })
        }
        if(self.invalidFeatures && self.invalidFeatures.length > 0) {
            self.renderInvalidFeatures(activeLayer);
        } else {
            self.selectedInvalidFeature = false;
        }

        if(self.selectedFeatures.length > 0) {
            self.renderInputMask(attributes, activeLayer);
        }

        //prepare list of all possible rendered attributes
        var renderedAttributes = [];
        if(self.jsonSchema) {
            renderedAttributes = activeLayer.schemaAttributes();
        }
        if(this.renderAttributes) {
            $.each(this.renderAttributes, function(idx, attribute) {
                if($.inArray(attribute, renderedAttributes) == -1) {
                    renderedAttributes.push(attribute);
                }
            });
        }
        if(activeLayer) {
            $.each(activeLayer.featuresAttributes(), function(idx, attribute) {
                if($.inArray(attribute, renderedAttributes) == -1) {
                    renderedAttributes.push(attribute);
                }
            });
        }

        //bind events
        $.each(renderedAttributes, function(idx, key) {
            $('#'+key).change(function() {
                var newVal = $('#'+key).val();
                self.edit(key, newVal);
            });
            $('#_'+key+'_remove').click(function() {
                self.remove(key);
                return false;
            });
            $('#_'+key+'_label').click(function() {
{'fraction_non_alphanumeric': 0.13204676489429854, 'fraction_numerical': 0.005605381165919282, 'mean_word_length': 2.2969904963041183, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1766810', 'n_tokens_mistral': 3460, 'n_tokens_neox': 3285, 'n_words': 539}