<commit_before>'use strict';

(function(){
    var ExcerptViewer = function(mapElementID, selectElementExistingExcerpts, excerptApiUrl) {
        this.excerptApiUrl = excerptApiUrl;
        this.selectElementExistingExcerpts = selectElementExistingExcerpts;
        this.currentCountryLayer = null;

        this.map = L.map(mapElementID).setView([0, 0], 2);
        // add an OpenStreetMap tile layer
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(this.map);

        this.locationFilter = new L.LocationFilter({
            enable: false,
            enableButton: false
        }).addTo(this.map);


        // update excerpt on map on selection of existing excerpt in list
        var getExcerptOptionAndUpdateFilter = function() {
            // we have single select, if it would happen to have two selected, ignore it.
            var excerptOption = this.selectElementExistingExcerpts.querySelector('option:checked');
            if(excerptOption) {
                this._setLocationFilterFromExcerptID(excerptOption.value);
            }
        }.bind(this);
        this.selectElementExistingExcerpts.addEventListener('click', getExcerptOptionAndUpdateFilter);
        this.selectElementExistingExcerpts.addEventListener('keyup', function(event) {
            // enter, arrow up or down
            if (event.keyCode == 13 || event.keyCode == 38 || event.keyCode == 40) {
                getExcerptOptionAndUpdateFilter();
            }
        });

        this._handleCountry = function (country){
            if (this.currentCountryLayer !== null) {
                this.map.removeLayer(this.currentCountryLayer);
            }

            this.locationFilter.disable();
            this.currentCountryLayer = country;
            this.map.addLayer(country);
            this.map.fitBounds(country.getBounds());
        }.bind(this);

        this._handleBBoxBoundingGeometry = function (geometry){
            if (this.currentCountryLayer !== null) {
                this.map.removeLayer(this.currentCountryLayer);
            }

            // If location filter is enabled, it will collide with fitBounds and you will get some strange behaviour
            this.locationFilter.disable();
            this.locationFilter.setBounds(geometry.getBounds());
            this.map.fitBounds(geometry.getBounds());
            this.locationFilter.enable();
        }.bind(this);

        this._setLocationFilterFromExcerptID = function(ID) {
            L.geoJson.ajax(this.excerptApiUrl.replace('{ID}', ID)).on('data:loaded', function(event) {
                // We are certain that there is only one layer on this feature, because our API provides it so.
                var feature_type = event.target.getLayers()[0].feature.properties.type_of_geometry;
                switch(feature_type) {
                    case 'BBoxBoundingGeometry':
                        this._handleBBoxBoundingGeometry(event.target);
                        break;
                    case 'Country':
                        this._handleCountry(event.target);
                        break;<commit_msg>Replace leaflet location filter by colorized square<commit_after>'use strict';

(function(){
    var ExcerptViewer = function(mapElementID, selectElementExistingExcerpts, excerptApiUrl) {
        this.excerptApiUrl = excerptApiUrl;
        this.selectElementExistingExcerpts = selectElementExistingExcerpts;
        this.currentLayer = null;

        this.map = L.map(mapElementID).setView([0, 0], 2);
        // add an OpenStreetMap tile layer
        L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(this.map);

        // update excerpt on map on selection of existing excerpt in list
        var getExcerptOptionAndUpdateFilter = function() {
            // we have single select, if it would happen to have two selected, ignore it.
            var excerptOption = this.selectElementExistingExcerpts.querySelector('option:checked');
            if(excerptOption) {
                this._showExcerptOrCountryOnMap(excerptOption.value);
            }
        }.bind(this);
        this.selectElementExistingExcerpts.addEventListener('click', getExcerptOptionAndUpdateFilter);
        this.selectElementExistingExcerpts.addEventListener('keyup', function(event) {
            // enter, arrow up or down
            if (event.keyCode == 13 || event.keyCode == 38 || event.keyCode == 40) {
                getExcerptOptionAndUpdateFilter();
            }
        });

        this._handleCountryOrBBox = function (layer){
            if (this.currentLayer !== null) {
                this.map.removeLayer(this.currentLayer);
            }

            this.currentLayer = layer;
            this.map.addLayer(layer);
        }.bind(this);

        this._extendBounds = function(bounds, marginFactor) {
            var latDiff = bounds._northEast.lat - bounds._southWest.lat;
            var lonDiff = bounds._northEast.lon - bounds._southWest.lon;
            bounds._northEast.lat += latDiff*marginFactor;
            bounds._southWest.lat -= latDiff*marginFactor;
            bounds._northEast.lon += lonDiff*marginFactor;
            bounds._southWest.lon -= lonDiff*marginFactor;
            return bounds;
        };

        this._showExcerptOrCountryOnMap = function(ID) {
            L.geoJson.ajax(this.excerptApiUrl.replace('{ID}', ID)).on('data:loaded', function(event) {
                // We are certain that there is only one layer on this feature, because our API provides it so.
                var feature_type = event.target.getLayers()[0].feature.properties.type_of_geometry;
                var layer = event.target;
                this._handleCountryOrBBox(layer);
                console.log(layer);
                switch(feature_type) {
                    case 'BBoxBoundingGeometry':
                        layer.setStyle({
                            color: 'black',
                            fillOpacity: 0.15
                        });
                        this.map.fitBounds(this._extendBounds(layer.getBounds(), 0.2));
                        break;
                    case 'Country':
                        layer.setStyle({
                            color: 'navy',
                            fillOpacity: 0.1
                        });
                        this.map.fitBounds(layer.getBounds());
                        break;
{'fraction_non_alphanumeric': 0.10499321164579876, 'fraction_numerical': 0.004073012520742193, 'mean_word_length': 1.985141828005403, 'pattern_counts': {'":': 0, '<': 7, '<?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': '7155382', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1657, 'n_words': 335}