<commit_before>
(function(angular) {
    var app = angular.module('SearchMap', ['ng-mapasculturais', 'FindOneService']);
    app.controller('SearchMapController', ['$window', '$scope', '$rootScope', 'FindOneService', function($window, $scope, $rootScope, FindOneService) {

        $scope.init = function (){

            if($scope.data.global.map && $scope.data.global.map.zoom){
                MapasCulturais.mapCenter = $scope.data.global.map.center;
            }else{
                MapasCulturais.mapCenter = null;
            }

            $scope.map = null;
            $scope.resultLayer = null;
            $scope.markers = [];

            angular.element(document).ready(function(){
                $scope.map = $window.leaflet.map;
                $scope.map.removeLayer($window.leaflet.marker);

                $scope.map.on('load', function(){
                    $scope.setMapView();
                });

                $scope.map.on('zoomend moveend', function(){
                    $scope.data.global.map = {
                        center : $window.leaflet.map.getCenter(),
                        zoom : $window.leaflet.map.getZoom()
                    };
                    $scope.$apply();
                });

                $scope.updateMap();
                $scope.setMapView();
                if($scope.data.global.openEntity && $scope.data.global.openEntity.id){
                    FindOneService($scope.data);
                }
            });



            $rootScope.$on('searchResultsReady', function(ev, results){
                console.log('ON searchResultsReady', results);

                delete $scope.markers;
                $scope.markers = [];
                if(results.agent) $scope.createMarkers('agent', results.agent);
                if(results.event) $scope.createMarkers('event', results.event);
                if(results.space) $scope.createMarkers('space', results.space);

                $scope.updateMap();
            });

            $rootScope.$on('searchDataChange', function(ev, data){
                $scope.setMapView();
            });

        };

        $scope.setMapView = function(){
            console.log('SET MAP VIEW', $scope.data.global.map);
            if($scope.map && $scope.data.global.map && $scope.data.global.map.zoom) {
                $scope.map.setZoom($scope.data.global.map.zoom);
                $scope.map.panTo($scope.data.global.map.center);
            }
        };

        $scope.createMarkers = function(entity, results) {
            results.forEach(function(item) {
                var marker;
                //TEMPORARY PATCH FOR EVENTS... WITHOUT LOCATION
                if(!item.location) return;

                marker = new L.marker(
                    new L.LatLng(item.location.latitude, item.location.longitude),
                    $window.leaflet.iconOptions[entity]
                ).bindLabel(
                    item.name
                ).on('click', function() {

                    var infobox = document.querySelector('#infobox');
                    var infoboxContent = infobox.querySelector('article');

                    $scope.data.global.openEntity = {
                        id: item.id,
                        type: entity
                    };

                    $scope.openEntity = {};
                    $scope.openEntity[entity] = {name: item.name};

                    $scope.$apply();

                    FindOneService($scope.data);
                });

                if (item.location && (item.location.latitude !== 0 && item.location.longitude !== 0)) {
                    marker.entityType = entity;
                    $scope.markers.push(marker);

                }
            });
        };

        $scope.updateMap = function(){

            if($scope.resultLayer){
                $scope.resultLayer.clearLayers();

                //remove drawing if more than one
                if($scope.map.drawnItems){
                    if(!$scope.data.global.locationFilters || !('enabled' in $scope.data.global.locationFilters) )  {
                        console.log('CLEARING DRAWING LAYER');
                        $scope.map.drawnItems.clearLayers();
                        if(window.leaflet.locationMarker) { $scope.map.removeLayer(window.leaflet.locationMarker);}
                    }else if(Object.keys($scope.map.drawnItems._layers).length > 1) {
                        console.log('KEEPING THE LAST DRAWN ITEM');
                        var lastLayer = $scope.map.drawnItems.getLayers().pop();
                        $scope.map.drawnItems.clearLayers();
                        $scope.map.drawnItems.addLayer(lastLayer);
                    }
                }

            }
            $scope.resultLayer = new L.markerClusterGroup({
                spiderfyOnMaxZoom: true,
                showCoverageOnHover: false,
                zoomToBoundsOnClick: false,
                spiderfyDistanceMultiplier:1.3,<commit_msg>fix clearing map drawings if no data locationFilters enabled<commit_after>
(function(angular) {
    var app = angular.module('SearchMap', ['ng-mapasculturais', 'FindOneService']);
    app.controller('SearchMapController', ['$window', '$scope', '$rootScope', 'FindOneService', function($window, $scope, $rootScope, FindOneService) {

        $scope.init = function (){

            if($scope.data.global.map && $scope.data.global.map.zoom){
                MapasCulturais.mapCenter = $scope.data.global.map.center;
            }else{
                MapasCulturais.mapCenter = null;
            }

            $scope.map = null;
            $scope.resultLayer = null;
            $scope.markers = [];

            angular.element(document).ready(function(){
                $scope.map = $window.leaflet.map;
                $scope.map.removeLayer($window.leaflet.marker);
                $scope.map.invalidateSize();

                $scope.map.on('load', function(){
                    $scope.setMapView();
                });

                $scope.map.on('zoomend moveend', function(){
                    $scope.data.global.map = {
                        center : $window.leaflet.map.getCenter(),
                        zoom : $window.leaflet.map.getZoom()
                    };
                    $scope.$apply();
                });

                $scope.updateMap();
                $scope.setMapView();
                if($scope.data.global.openEntity && $scope.data.global.openEntity.id){
                    FindOneService($scope.data);
                }
            });



            $rootScope.$on('searchResultsReady', function(ev, results){
                delete $scope.markers;
                $scope.markers = [];
                if(results.agent) $scope.createMarkers('agent', results.agent);
                if(results.event) $scope.createMarkers('event', results.event);
                if(results.space) $scope.createMarkers('space', results.space);

                $scope.updateMap();
            });

            $rootScope.$on('searchDataChange', function(ev, data){
                $scope.setMapView();
            });

        };

        $scope.setMapView = function(){
            if($scope.map && $scope.data.global.map && $scope.data.global.map.zoom) {
                $scope.map.setZoom($scope.data.global.map.zoom);
                $scope.map.panTo($scope.data.global.map.center);
            }
        };

        $scope.createMarkers = function(entity, results) {
            results.forEach(function(item) {
                var marker;
                //TEMPORARY PATCH FOR EVENTS... WITHOUT LOCATION
                if(!item.location) return;

                marker = new L.marker(
                    new L.LatLng(item.location.latitude, item.location.longitude),
                    $window.leaflet.iconOptions[entity]
                ).bindLabel(
                    item.name
                ).on('click', function() {

                    var infobox = document.querySelector('#infobox');
                    var infoboxContent = infobox.querySelector('article');

                    $scope.data.global.openEntity = {
                        id: item.id,
                        type: entity
                    };

                    $scope.openEntity = {};
                    $scope.openEntity[entity] = {name: item.name};

                    $scope.$apply();

                    FindOneService($scope.data);
                });

                if (item.location && (item.location.latitude !== 0 && item.location.longitude !== 0)) {
                    marker.entityType = entity;
                    $scope.markers.push(marker);

                }
            });
        };

        $scope.updateMap = function(){
            if($scope.resultLayer){
                $scope.resultLayer.clearLayers();

                //remove drawing if more than one
                if($scope.map.drawnItems){
                    if(!$scope.data.global.locationFilters.enabled)  {
                        $scope.map.drawnItems.clearLayers();
                        if(window.leaflet.locationMarker) { $scope.map.removeLayer(window.leaflet.locationMarker);}
                    }else if(Object.keys($scope.map.drawnItems._layers).length > 1) {
                        var lastLayer = $scope.map.drawnItems.getLayers().pop();
                        $scope.map.drawnItems.clearLayers();
                        $scope.map.drawnItems.addLayer(lastLayer);
                    }
                }

            }
            $scope.resultLayer = new L.markerClusterGroup({
                spiderfyOnMaxZoom: true,
                showCoverageOnHover: false,
                zoomToBoundsOnClick: false,
                spiderfyDistanceMultiplier:1.3,
{'fraction_non_alphanumeric': 0.1288544006534613, 'fraction_numerical': 0.0010210332856851133, 'mean_word_length': 1.4167283493708365, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '416672', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2423, 'n_words': 350}