<commit_before>//
// Emapic main map related code
//

var emapic = emapic || {};

(function(emapic) {

    var allCountriesDataBboxDfd = null,
        votedCountriesDataNoGeomDfd = null,
        votedProvincesDataBboxDfd = null;

    emapic.map = null;
    emapic.position = null;
    emapic.precision = null;
    emapic.indivVotesLayer = null;
    emapic.indivVotesLayerData = null;
    emapic.surveyId = null;
    emapic.surveyResults = null;
    emapic.resultsAfterVote = true;
    emapic.redirectUrl = "/";
    emapic.legend = {};
    emapic.fullLegend = null;
    emapic.allCountriesData = {};
    emapic.votedCountriesData = {};
    emapic.votedProvincesData = {};
    // We'll use this color, for example, in ties
    emapic.neutralColor = 'grey';
    emapic.fallbackColor = 'black';
    emapic.allLayersLoadedPromise = $.Deferred();

    emapic.oldResponses = {};
    emapic.userLoggedIn = false;
    emapic.logicAlreadyStarted = false;

    emapic.getCountriesJsonBboxUrl = function() {
        return "/api/baselayers/countries/bbox";
    };

    emapic.getResultsJsonUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/results";
    };

    emapic.getLegendJsonUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/legend";
    };

    emapic.getStatsCountriesJsonNoGeomUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/totals/countries/nogeom";
    };

    emapic.getStatsProvincesJsonBboxUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/totals/provinces/bbox";
    };

    // Methods for loading additional JSON data. If we want to preload them, we
    // can simply call the methods and ignore the returned values.
    emapic.getAllCountriesDataBbox = function() {
        if (allCountriesDataBboxDfd === null) {
            allCountriesDataBboxDfd = emapic.utils.getJsonAlertError(
                emapic.getCountriesJsonBboxUrl()
            ).done(function(data) {
                $.each(data.features, function(i, country) {
                    emapic.allCountriesData[country.properties.iso_code] = {};
                    emapic.allCountriesData[country.properties.iso_code].properties = country.properties;
                    emapic.allCountriesData[country.properties.iso_code].bbox = [[country.geometry.coordinates[0][0][1], country.geometry.coordinates[0][0][0]],
                        [country.geometry.coordinates[0][2][1], country.geometry.coordinates[0][2][0]]];
                });
            });
        }
        return allCountriesDataBboxDfd;
    };

    emapic.getVotedCountriesDataNoGeom = function() {
        if (votedCountriesDataNoGeomDfd === null) {
            votedCountriesDataNoGeomDfd = emapic.utils.getJsonAlertError(
                emapic.getStatsCountriesJsonNoGeomUrl()
            ).done(function(data) {
                emapic.votedCountriesData = data;
            });
        }
        return votedCountriesDataNoGeomDfd;
    };

    emapic.getVotedProvincesDataBbox = function() {
        if (votedProvincesDataBboxDfd === null) {
            votedProvincesDataBboxDfd = emapic.utils.getJsonAlertError(
                emapic.getStatsProvincesJsonBboxUrl()
            ).done(function(data) {
                $.each(data.features, function(i, province) {
                    emapic.votedProvincesData[province.properties.adm_code] = {};
                    emapic.votedProvincesData[province.properties.adm_code].properties = province.properties;
                    emapic.votedProvincesData[province.properties.adm_code].bbox = [[province.geometry.coordinates[0][0][1], province.geometry.coordinates[0][0][0]],
                        [province.geometry.coordinates[0][2][1], province.geometry.coordinates[0][2][0]]];
                });
            });
        }
        return votedProvincesDataBboxDfd;
    };

    emapic.preinitEmapic = function() {
        // If we have a legend, we load it. Otherwise, we init the map.
        if (emapic.getLegendJsonUrl() !== null) {
            emapic.loadLegend();
        } else {
            emapic.initEmapic();
        }
    };

    emapic.initEmapic = function() {
        if (emapic.map === null) {
            emapic.initializeMap();
        }
        emapic.startMapLogic();
    };

    emapic.initializeMap = function() {
        var osmAttrib = 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>';
        var mapboxAttrib = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>&copy; Mapbox &copy; OpenStreetMap</a> <a class='mapbox-improve-map' href='https://www.mapbox.com/map-feedback/' target='_blank'>Improve this map</a>";

        // Can't use "https://{s}.tiles.wmflabs.org" because their ssl cert is wrong
        var osmMapnikBW = new L.TileLayer('https://tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
            minZoom : 1,
            maxZoom : 18,
            attribution : osmAttrib
        });
        var osmMapnik = new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            minZoom : 1,
            maxZoom : 18,
            attribution : osmAttrib
        });
        var mapboxSatellite = new L.TileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets-satellite/{z}/{x}/{y}.png?access_token=<KEY>', {
            minZoom : 1,
            maxZoom : 18,
            attribution : mapboxAttrib
        });

        emapic.map = L.map('map', {
            attributionControl: false,
            zoomControl: false
        });
        emapic.map.addControl(L.control.zoom({
            zoomInTitle: emapic.utils.getI18n('js_zoom_in', 'Zoom más'),
            zoomOutTitle: emapic.utils.getI18n('js_zoom_out', 'Zoom menos'),
        }));
        emapic.map.addControl(L.control.attribution({
            prefix: '<a href="/legal/terms" title="' + emapic.utils.getI18n('js_open_legal_terms_another_tab', 'Abrir cláusulas legales en otra pestaña') + '" target="_blank">' + emapic.utils.getI18n('js_emapic_legal_terms', 'Cláusulas legales de emapic') + '</a> | <a title="A JS library for interactive maps" href="http://leafletjs.com">Leaflet</a>'
        }));

        emapic.map.setMaxBounds(
            L.latLngBounds(
                L.latLng(85, -180),
                L.latLng(-85, 180)
            )
        );
        emapic.map.addLayer(osmMapnikBW);

        var baseMaps = {
            "Escala de grises (OSM)" : osmMapnikBW,
            "Color (OSM)" : osmMapnik,
            "Satélite (Mapbox)" : mapboxSatellite
        };
        L.control.layers(baseMaps, null, {position: 'bottomright'}).addTo(emapic.map);
        emapic.addTooltips();
    };

    emapic.startMapLogic = function() {
        // We use this flag in order to prevent a strange problem where
        // layers and controls are loaded twice
        if (emapic.logicAlreadyStarted) {
            return false;
        }
        emapic.logicAlreadyStarted = true;
        if (emapic.resultsAfterVote) {
            emapic.loadData();
        } else {
            window.location.href = emapic.redirectUrl;
        }
        return true;
    };

    emapic.loadData = function() {
        emapic.utils.disableMapInteraction(true);
        emapic.addAllMarkers().done(function() {
            emapic.allLayersLoadedPromise.resolve();
        }).fail(function() {
            emapic.allLayersLoadedPromise.reject();
        }).always(function() {
            emapic.utils.enableMapInteraction();
        });
        emapic.addViewsControls();
        // If we have more than one set of legend, we display a question selector
        if (emapic.fullLegend && emapic.fullLegend.color && emapic.fullLegend.color.length > 1) {<commit_msg>Added a promise for checking if base layer is loaded<commit_after>//
// Emapic main map related code
//

var emapic = emapic || {};

(function(emapic) {

    var allCountriesDataBboxDfd = null,
        votedCountriesDataNoGeomDfd = null,
        votedProvincesDataBboxDfd = null;

    emapic.map = null;
    emapic.currentBaseLayer = null;
    emapic.position = null;
    emapic.precision = null;
    emapic.indivVotesLayer = null;
    emapic.indivVotesLayerData = null;
    emapic.surveyId = null;
    emapic.surveyResults = null;
    emapic.resultsAfterVote = true;
    emapic.redirectUrl = "/";
    emapic.legend = {};
    emapic.fullLegend = null;
    emapic.allCountriesData = {};
    emapic.votedCountriesData = {};
    emapic.votedProvincesData = {};
    // We'll use this color, for example, in ties
    emapic.neutralColor = 'grey';
    emapic.fallbackColor = 'black';
    emapic.allLayersLoadedPromise = $.Deferred();
    // Take care when using this promise as any pan/zoom made by the user
    // would restart it completely. It will prove more useful when changing the
    // map programmatically.
    emapic.baseLayerLoadedPromise = $.Deferred();

    emapic.oldResponses = {};
    emapic.userLoggedIn = false;
    emapic.logicAlreadyStarted = false;

    emapic.getCountriesJsonBboxUrl = function() {
        return "/api/baselayers/countries/bbox";
    };

    emapic.getResultsJsonUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/results";
    };

    emapic.getLegendJsonUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/legend";
    };

    emapic.getStatsCountriesJsonNoGeomUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/totals/countries/nogeom";
    };

    emapic.getStatsProvincesJsonBboxUrl = function() {
        return "/api/survey/" + emapic.surveyId + "/totals/provinces/bbox";
    };

    // Methods for loading additional JSON data. If we want to preload them, we
    // can simply call the methods and ignore the returned values.
    emapic.getAllCountriesDataBbox = function() {
        if (allCountriesDataBboxDfd === null) {
            allCountriesDataBboxDfd = emapic.utils.getJsonAlertError(
                emapic.getCountriesJsonBboxUrl()
            ).done(function(data) {
                $.each(data.features, function(i, country) {
                    emapic.allCountriesData[country.properties.iso_code] = {};
                    emapic.allCountriesData[country.properties.iso_code].properties = country.properties;
                    emapic.allCountriesData[country.properties.iso_code].bbox = [[country.geometry.coordinates[0][0][1], country.geometry.coordinates[0][0][0]],
                        [country.geometry.coordinates[0][2][1], country.geometry.coordinates[0][2][0]]];
                });
            });
        }
        return allCountriesDataBboxDfd;
    };

    emapic.getVotedCountriesDataNoGeom = function() {
        if (votedCountriesDataNoGeomDfd === null) {
            votedCountriesDataNoGeomDfd = emapic.utils.getJsonAlertError(
                emapic.getStatsCountriesJsonNoGeomUrl()
            ).done(function(data) {
                emapic.votedCountriesData = data;
            });
        }
        return votedCountriesDataNoGeomDfd;
    };

    emapic.getVotedProvincesDataBbox = function() {
        if (votedProvincesDataBboxDfd === null) {
            votedProvincesDataBboxDfd = emapic.utils.getJsonAlertError(
                emapic.getStatsProvincesJsonBboxUrl()
            ).done(function(data) {
                $.each(data.features, function(i, province) {
                    emapic.votedProvincesData[province.properties.adm_code] = {};
                    emapic.votedProvincesData[province.properties.adm_code].properties = province.properties;
                    emapic.votedProvincesData[province.properties.adm_code].bbox = [[province.geometry.coordinates[0][0][1], province.geometry.coordinates[0][0][0]],
                        [province.geometry.coordinates[0][2][1], province.geometry.coordinates[0][2][0]]];
                });
            });
        }
        return votedProvincesDataBboxDfd;
    };

    emapic.preinitEmapic = function() {
        // If we have a legend, we load it. Otherwise, we init the map.
        if (emapic.getLegendJsonUrl() !== null) {
            emapic.loadLegend();
        } else {
            emapic.initEmapic();
        }
    };

    emapic.initEmapic = function() {
        if (emapic.map === null) {
            emapic.initializeMap();
        }
        emapic.startMapLogic();
    };

    emapic.initializeMap = function() {
        var osmAttrib = 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>';
        var mapboxAttrib = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>&copy; Mapbox &copy; OpenStreetMap</a> <a class='mapbox-improve-map' href='https://www.mapbox.com/map-feedback/' target='_blank'>Improve this map</a>";

        // Can't use "https://{s}.tiles.wmflabs.org" because their ssl cert is wrong
        var osmMapnikBW = new L.TileLayer('https://tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
            minZoom : 1,
            maxZoom : 18,
            attribution : osmAttrib
        });
        var osmMapnik = new L.TileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            minZoom : 1,
            maxZoom : 18,
            attribution : osmAttrib
        });
        var mapboxSatellite = new L.TileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets-satellite/{z}/{x}/{y}.png?access_token=<KEY>', {
            minZoom : 1,
            maxZoom : 18,
            attribution : mapboxAttrib
        });

        emapic.map = L.map('map', {
            attributionControl: false,
            zoomControl: false
        });
        emapic.map.addControl(L.control.zoom({
            zoomInTitle: emapic.utils.getI18n('js_zoom_in', 'Zoom más'),
            zoomOutTitle: emapic.utils.getI18n('js_zoom_out', 'Zoom menos'),
        }));
        emapic.map.addControl(L.control.attribution({
            prefix: '<a href="/legal/terms" title="' + emapic.utils.getI18n('js_open_legal_terms_another_tab', 'Abrir cláusulas legales en otra pestaña') + '" target="_blank">' + emapic.utils.getI18n('js_emapic_legal_terms', 'Cláusulas legales de emapic') + '</a> | <a title="A JS library for interactive maps" href="http://leafletjs.com">Leaflet</a>'
        }));

        emapic.map.setMaxBounds(
            L.latLngBounds(
                L.latLng(85, -180),
                L.latLng(-85, 180)
            )
        );
        emapic.currentBaseLayer = osmMapnikBW;
        emapic.map.addLayer(osmMapnikBW);

        var baseMaps = {
            "Escala de grises (OSM)" : osmMapnikBW,
            "Color (OSM)" : osmMapnik,
            "Satélite (Mapbox)" : mapboxSatellite
        };
        $.each(baseMaps, function(index, value) {
            value.on('loading', function() {
                if (emapic.baseLayerLoadedPromise === null) {
                    emapic.baseLayerLoadedPromise = $.Deferred();
                }
            });
            value.on('load', function() {
                if (emapic.baseLayerLoadedPromise !== null) {
                    emapic.baseLayerLoadedPromise.resolve();
                }
            });
        });
        L.control.layers(baseMaps, null, {position: 'bottomright'}).addTo(emapic.map);
        emapic.map.on('baselayerchange', function(layer) {
            if (emapic.currentBaseLayer != layer.layer) {
                emapic.baseLayerLoadedPromise = null;
                emapic.currentBaseLayer = layer;
            }
        });
        emapic.addTooltips();
    };

    emapic.startMapLogic = function() {
        // We use this flag in order to prevent a strange problem where
        // layers and controls are loaded twice
        if (emapic.logicAlreadyStarted) {
            return false;
        }
        emapic.logicAlreadyStarted = true;
        if (emapic.resultsAfterVote) {
            emapic.loadData();
        } else {
            window.location.href = emapic.redirectUrl;
        }
        return true;
    };

    emapic.loadData = function() {
        emapic.utils.disableMapInteraction(true);
        emapic.addAllMarkers().done(function() {
            emapic.allLayersLoadedPromise.resolve();
        }).fail(function() {
            emapic.allLayersLoadedPromise.reject();
        }).always(function() {
            emapic.utils.enableMapInteraction();
        });
        emapic.addViewsControls();
        // If we have more than one set of legend, we display a question selector
        if (emapic.fullLegend && emapic.fullLegend.color && emapic.fullLegend.color.length > 1) {
{'fraction_non_alphanumeric': 0.13076505697232774, 'fraction_numerical': 0.0066316995237233976, 'mean_word_length': 2.2602201257861636, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 12, 'lorem ipsum': 0, 'www.': 4, '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': '7537114', 'n_tokens_mistral': 5453, 'n_tokens_neox': 5235, 'n_words': 847}