<commit_before>/*globals $, AdaptiveMap, ProjectionDiagram, WebGLDebugUtils, resizeCanvasElement, formatLatitude, formatLongitude */

var map;

$(window).load(function() {"use strict";

    var slippyMap, diagram;

    function updateSlippyMap() {
        var w, h, centerLon, centerLat, webMapScale;

        if ( typeof map === 'undefined' || typeof slippyMap === 'undefined') {
            return;
        }

        // adjust the size of the slippy map to the adaptive map
        w = map.getCanvasWidth();
        h = map.getCanvasHeight();
        resizeCanvasElement(document.getElementById('slippyMap'), w, h);

        // allow pre-loading at smaller scales
        if (map.getMapScale() < MERCATOR_LIMIT_WEB_MAP_SCALE - 2) {
            return;
        }

        centerLon = map.getCentralLongitude() / Math.PI * 180;
        centerLat = map.getCentralLatitude() / Math.PI * 180;
        slippyMap.setCenter({
            lat : centerLat,
            lon : centerLon
        });

        webMapScale = MERCATOR_LIMIT_WEB_MAP_SCALE + Math.floor(map.getMapScale() - MERCATOR_LIMIT_2);
        webMapScale = Math.max(webMapScale, MERCATOR_LIMIT_WEB_MAP_SCALE);
        // FIXME
        //var webMapScale = toWebMapScale(canvasHeight, mapScale);
        webMapScale = Math.floor(webMapScale);
        if (webMapScale !== slippyMap.getZoom()) {
            slippyMap.setZoom(webMapScale);
        }
    }

    function writeProjectionInfo() {
        var projection, infoText, ratio, latText, lonText;

        if ( typeof map === 'undefined') {
            return;
        }

        projection = map.updateProjection();
        infoText = "<B>";
        infoText += projection.toString();
        infoText += "</B><br>";
        infoText += projection.isEqualArea() ? "Equal area" : "Not equal area";
        infoText += "<br><br>";
        infoText += "<B>Zoom Factor</B><br> " + map.getMapScale().toFixed(2);
        infoText += "<br>";

        ratio = map.conf.canvasHeight / map.conf.canvasWidth;

        infoText += "<br><B>Canvas Height-to-width Ratio</B><br>" + ratio.toFixed(2);
        if (ratio > 1 / formatRatioLimit) {
            infoText += " (portrait)";
        } else if (ratio < formatRatioLimit) {
            infoText += " (landscape)";
        } else {
            infoText += " (square)";
        }
        infoText += "<br>";

        latText = formatLatitude(map.conf.lat0);
        lonText = formatLongitude(map.conf.lon0);
        infoText += "<br><B>Center of the Map</B>";
        infoText += "<br>Longitude: " + lonText + "<BR>Latitude: " + latText + "<br>";

        document.getElementById('infoText').innerHTML = infoText;
    }

    function adjustVisibility() {
        if ( typeof map === 'undefined' || typeof slippyMap === 'undefined') {
            return;
        }

        var lon0, lat0, slippyMapParent, vectorMapParent;
        slippyMapParent = document.getElementById("slippyMap");
        vectorMapParent = document.getElementById("adaptiveMap");
        if (map.getMapScale() > MERCATOR_LIMIT_2) {
            vectorMapParent.style.visibility = 'hidden';
            slippyMapParent.style.visibility = 'visible';
            updateSlippyMap();
        } else {
            if (vectorMapParent.style.visibility === 'hidden') {
                vectorMapParent.style.visibility = 'visible';
                lon0 = slippyMap.getCenter().lon / 180 * Math.PI;
                lat0 = slippyMap.getCenter().lat / 180 * Math.PI;
                map.setCenter(lon0, lat0);
                slippyMapParent.style.visibility = 'hidden';
            }
        }
    }

    function renderMap_Diagram_InfoText(renderDiagramBackground) {
        map.render();
        diagram.renderButton(map.getMapScale(), map.getCentralLatitude());
        if (renderDiagramBackground) {
            diagram.render(map.conf);
        }
        //plot projection characteristics on screen
        writeProjectionInfo();
    }

    function updateZoomSlider() {
        try {
            var mapScale = Math.round(map.getMapScale() * 100);
            if ($("#zoom-slider").slider('value') !== mapScale) {
                $("#zoom-slider").slider({
                    value : mapScale
                });
            }
        } catch (ignore) {
            // FIXME is called before the slider is initialized
        }
    }

    function diagramChangeListener(lat0, mapScale) {
        if (map.getCentralLatitude() < 0) {
            lat0 = -lat0;
        }
        map.setCentralLatitudeAndScale(lat0, mapScale);
        // adjust the zoom slider
        updateZoomSlider();
    }

    function projectionChangeListener(map) {
        var mapScale, lat0;

        mapScale = map.getMapScale();
        lat0 = map.getCentralLatitude();
        if (diagram) {
            diagram.renderButton(mapScale, lat0);
        }

        adjustVisibility();
        updateSlippyMap();
        writeProjectionInfo();
        updateZoomSlider();
    }

    function initSlippyMap() {
        var slippyMap, tilejson, parent = document.getElementById('slippyMap');
        if (parent !== null) {

            /*
            // xyz schema
            var template = 'http://spaceclaw.stamen.com/toner/{Z}/{X}/{Y}.png';
            var provider = new MM.TemplatedLayer(template);
            slippyMap = new MM.Map(parent, provider);
            */

            // tms schema not natively supported by Modestmaps, use wax by mapbox
            tilejson = {
                "version" : "1.0.0",
                "scheme" : "tms",
                "tiles" : ['http://cartography.geo.oregonstate.edu/naturalearth/{z}/{x}/{y}.png']
            };
            slippyMap = new MM.Map(parent, new wax.mm.connector(tilejson));

            slippyMap.setCenter({
                lat : 0,
                lon : 0
            }).setZoom(0);
        }

        // add event handler to slippy map that handles the transition from slippy map to the adaptive map
        slippyMap.addCallback('zoomed', function(m) {
            var slippyMapParent, vectorMapParent, lon0, lat0;

            slippyMapParent = document.getElementById("slippyMap");
            vectorMapParent = document.getElementById("adaptiveMap");

            if (m.getZoom() <= MERCATOR_LIMIT_WEB_MAP_SCALE && vectorMapParent.style.visibility === 'hidden') {
                lon0 = slippyMap.getCenter().lon / 180 * Math.PI;
                lat0 = slippyMap.getCenter().lat / 180 * Math.PI;
                map.setCenter(lon0, lat0);
                map.setMapScale(MERCATOR_LIMIT_2);
                vectorMapParent.style.visibility = 'visible';
                slippyMapParent.style.visibility = 'hidden';
                map.render();
                diagram.renderButton(MERCATOR_LIMIT_2, lat0);
            }
        });<commit_msg>Corrected link to NaturalEarth Mercator tiles<commit_after>/*globals $, AdaptiveMap, ProjectionDiagram, WebGLDebugUtils, resizeCanvasElement, formatLatitude, formatLongitude */

var map;

$(window).load(function() {"use strict";

    var slippyMap, diagram;

    function updateSlippyMap() {
        var w, h, centerLon, centerLat, webMapScale;

        if ( typeof map === 'undefined' || typeof slippyMap === 'undefined') {
            return;
        }

        // adjust the size of the slippy map to the adaptive map
        w = map.getCanvasWidth();
        h = map.getCanvasHeight();
        resizeCanvasElement(document.getElementById('slippyMap'), w, h);

        // allow pre-loading at smaller scales
        if (map.getMapScale() < MERCATOR_LIMIT_WEB_MAP_SCALE - 2) {
            return;
        }

        centerLon = map.getCentralLongitude() / Math.PI * 180;
        centerLat = map.getCentralLatitude() / Math.PI * 180;
        slippyMap.setCenter({
            lat : centerLat,
            lon : centerLon
        });

        webMapScale = MERCATOR_LIMIT_WEB_MAP_SCALE + Math.floor(map.getMapScale() - MERCATOR_LIMIT_2);
        webMapScale = Math.max(webMapScale, MERCATOR_LIMIT_WEB_MAP_SCALE);
        // FIXME
        //var webMapScale = toWebMapScale(canvasHeight, mapScale);
        webMapScale = Math.floor(webMapScale);
        if (webMapScale !== slippyMap.getZoom()) {
            slippyMap.setZoom(webMapScale);
        }
    }

    function writeProjectionInfo() {
        var projection, infoText, ratio, latText, lonText;

        if ( typeof map === 'undefined') {
            return;
        }

        projection = map.updateProjection();
        infoText = "<B>";
        infoText += projection.toString();
        infoText += "</B><br>";
        infoText += projection.isEqualArea() ? "Equal area" : "Not equal area";
        infoText += "<br><br>";
        infoText += "<B>Zoom Factor</B><br> " + map.getMapScale().toFixed(2);
        infoText += "<br>";

        ratio = map.conf.canvasHeight / map.conf.canvasWidth;

        infoText += "<br><B>Canvas Height-to-width Ratio</B><br>" + ratio.toFixed(2);
        if (ratio > 1 / formatRatioLimit) {
            infoText += " (portrait)";
        } else if (ratio < formatRatioLimit) {
            infoText += " (landscape)";
        } else {
            infoText += " (square)";
        }
        infoText += "<br>";

        latText = formatLatitude(map.conf.lat0);
        lonText = formatLongitude(map.conf.lon0);
        infoText += "<br><B>Center of the Map</B>";
        infoText += "<br>Longitude: " + lonText + "<BR>Latitude: " + latText + "<br>";

        document.getElementById('infoText').innerHTML = infoText;
    }

    function adjustVisibility() {
        if ( typeof map === 'undefined' || typeof slippyMap === 'undefined') {
            return;
        }

        var lon0, lat0, slippyMapParent, vectorMapParent;
        slippyMapParent = document.getElementById("slippyMap");
        vectorMapParent = document.getElementById("adaptiveMap");
        if (map.getMapScale() > MERCATOR_LIMIT_2) {
            vectorMapParent.style.visibility = 'hidden';
            slippyMapParent.style.visibility = 'visible';
            updateSlippyMap();
        } else {
            if (vectorMapParent.style.visibility === 'hidden') {
                vectorMapParent.style.visibility = 'visible';
                lon0 = slippyMap.getCenter().lon / 180 * Math.PI;
                lat0 = slippyMap.getCenter().lat / 180 * Math.PI;
                map.setCenter(lon0, lat0);
                slippyMapParent.style.visibility = 'hidden';
            }
        }
    }

    function renderMap_Diagram_InfoText(renderDiagramBackground) {
        map.render();
        diagram.renderButton(map.getMapScale(), map.getCentralLatitude());
        if (renderDiagramBackground) {
            diagram.render(map.conf);
        }
        //plot projection characteristics on screen
        writeProjectionInfo();
    }

    function updateZoomSlider() {
        try {
            var mapScale = Math.round(map.getMapScale() * 100);
            if ($("#zoom-slider").slider('value') !== mapScale) {
                $("#zoom-slider").slider({
                    value : mapScale
                });
            }
        } catch (ignore) {
            // FIXME is called before the slider is initialized
        }
    }

    function diagramChangeListener(lat0, mapScale) {
        if (map.getCentralLatitude() < 0) {
            lat0 = -lat0;
        }
        map.setCentralLatitudeAndScale(lat0, mapScale);
        // adjust the zoom slider
        updateZoomSlider();
    }

    function projectionChangeListener(map) {
        var mapScale, lat0;

        mapScale = map.getMapScale();
        lat0 = map.getCentralLatitude();
        if (diagram) {
            diagram.renderButton(mapScale, lat0);
        }

        adjustVisibility();
        updateSlippyMap();
        writeProjectionInfo();
        updateZoomSlider();
    }

    function initSlippyMap() {
        var slippyMap, tilejson, parent = document.getElementById('slippyMap');
        if (parent !== null) {

            /*
            // xyz schema
            var template = 'http://spaceclaw.stamen.com/toner/{Z}/{X}/{Y}.png';
            var provider = new MM.TemplatedLayer(template);
            slippyMap = new MM.Map(parent, provider);
            */

            // tms schema not natively supported by Modestmaps, use wax by mapbox
            tilejson = {
                "version" : "1.0.0",
                "scheme" : "tms",
                "tiles" : ['http://cartography.oregonstate.edu/tiles/naturalearth/{z}/{x}/{y}.png']
            };
            slippyMap = new MM.Map(parent, new wax.mm.connector(tilejson));

            slippyMap.setCenter({
                lat : 0,
                lon : 0
            }).setZoom(0);
        }

        // add event handler to slippy map that handles the transition from slippy map to the adaptive map
        slippyMap.addCallback('zoomed', function(m) {
            var slippyMapParent, vectorMapParent, lon0, lat0;

            slippyMapParent = document.getElementById("slippyMap");
            vectorMapParent = document.getElementById("adaptiveMap");

            if (m.getZoom() <= MERCATOR_LIMIT_WEB_MAP_SCALE && vectorMapParent.style.visibility === 'hidden') {
                lon0 = slippyMap.getCenter().lon / 180 * Math.PI;
                lat0 = slippyMap.getCenter().lat / 180 * Math.PI;
                map.setCenter(lon0, lat0);
                map.setMapScale(MERCATOR_LIMIT_2);
                vectorMapParent.style.visibility = 'visible';
                slippyMapParent.style.visibility = 'hidden';
                map.render();
                diagram.renderButton(MERCATOR_LIMIT_2, lat0);
            }
        });
{'fraction_non_alphanumeric': 0.124871757291514, 'fraction_numerical': 0.008500659533929356, 'mean_word_length': 1.9901402278702893, 'pattern_counts': {'":': 0, '<': 51, '<?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': '4746739', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3841, 'n_words': 775}