<commit_before>import Utils from './utils/utils';
import Scene from './scene';

// Exports must appear outside a function, but will only be defined in main thread (below)
export var LeafletLayer;
export function leafletLayer(options) {
    return new LeafletLayer(options);
}

// Leaflet layer functionality is only defined in main thread
if (Utils.isMainThread) {

    // Determine if we are extending the leaflet 0.7.x TileLayer class, or the newer
    // leaflet 1.x GridLayer class.
    let layerBaseClass = L.GridLayer ? L.GridLayer : L.TileLayer;
    let leafletVersion = layerBaseClass === L.GridLayer ? '1.x' : '0.7.x';
    let layerClassConfig = {};

    // If extending leaflet 0.7.x TileLayer, make add/remove tile no ops
    if (layerBaseClass === L.TileLayer) {
        layerClassConfig._addTile = function(){};
        layerClassConfig._removeTile = function(){};
    }

    // Define custom layer methods
    Object.assign(layerClassConfig, {

        initialize: function (options) {
            // Defaults
            options.showDebug = (!options.showDebug ? false : true);

            L.setOptions(this, options);
            this.createScene();
            this.hooks = {};
            this._updating_tangram = false;

            // Force leaflet zoom animations off
            this._zoomAnimated = false;
        },

        createScene: function () {
            this.scene = Scene.create(
                this.options.scene,
                {
                    numWorkers: this.options.numWorkers,
                    preUpdate: this.options.preUpdate,
                    postUpdate: this.options.postUpdate,
                    continuousZoom: (LeafletLayer.leafletVersion === '1.x'),
                    highDensityDisplay: this.options.highDensityDisplay,
                    logLevel: this.options.logLevel,
                    // advanced option, app will have to manually called scene.update() per frame
                    disableRenderLoop: this.options.disableRenderLoop,
                    // advanced option, will require library to be served as same host as page
                    allowCrossDomainWorkers: this.options.allowCrossDomainWorkers
                });
        },

        // Finish initializing scene and setup events when layer is added to map
        onAdd: function (map) {
            if (!this.scene) {
                this.createScene();
            }

            layerBaseClass.prototype.onAdd.apply(this, arguments);

            this.hooks.resize = () => {
                this._updating_tangram = true;
                var size = map.getSize();
                this.scene.resizeMap(size.x, size.y);
                this._updating_tangram = false;
            };
            map.on('resize', this.hooks.resize);

            this.hooks.move = () => {
                if (this._updating_tangram) {
                    return;
                }

                this._updating_tangram = true;
                var view = map.getCenter();
                view.zoom = map.getZoom();

                this.scene.setView(view);
                this.scene.immediateRedraw();
                this._updating_tangram = false;
            };
            map.on('move', this.hooks.move);

            this.hooks.zoomstart = () => {
                if (this._updating_tangram) {
                    return;
                }

                this._updating_tangram = true;
                this.scene.startZoom();
                this._updating_tangram = false;
            };
            map.on('zoomstart', this.hooks.zoomstart);

            this.hooks.dragstart = () => {
                this.scene.panning = true;
            };
            map.on('dragstart', this.hooks.dragstart);

            this.hooks.dragend = () => {
                this.scene.panning = false;
            };
            map.on('dragend', this.hooks.dragend);

            // Force leaflet zoom animations off
            map._zoomAnimated = false;

            // Modify default leaflet scroll wheel behavior
            this.modifyScrollWheelBehavior(map);

            // Canvas element will be inserted after map container (leaflet transforms shouldn't be applied to the GL canvas)
            // TODO: find a better way to deal with this? right now GL map only renders correctly as the bottom layer
            this.scene.container = map.getContainer();

            // Initial view
            var view = map.getCenter();
            view.zoom = map.getZoom();
            this.scene.setView(view);

            // Subscribe to tangram events<commit_msg>leaflet layer: add fallback for max zoom observed large zoom values (1015) in leaflet 0.7.x when map had no explicit maxZoom set<commit_after>import Utils from './utils/utils';
import Scene from './scene';
import Geo from './geo';

// Exports must appear outside a function, but will only be defined in main thread (below)
export var LeafletLayer;
export function leafletLayer(options) {
    return new LeafletLayer(options);
}

// Leaflet layer functionality is only defined in main thread
if (Utils.isMainThread) {

    // Determine if we are extending the leaflet 0.7.x TileLayer class, or the newer
    // leaflet 1.x GridLayer class.
    let layerBaseClass = L.GridLayer ? L.GridLayer : L.TileLayer;
    let leafletVersion = layerBaseClass === L.GridLayer ? '1.x' : '0.7.x';
    let layerClassConfig = {};

    // If extending leaflet 0.7.x TileLayer, make add/remove tile no ops
    if (layerBaseClass === L.TileLayer) {
        layerClassConfig._addTile = function(){};
        layerClassConfig._removeTile = function(){};
    }

    // Define custom layer methods
    Object.assign(layerClassConfig, {

        initialize: function (options) {
            // Defaults
            options.showDebug = (!options.showDebug ? false : true);

            L.setOptions(this, options);
            this.createScene();
            this.hooks = {};
            this._updating_tangram = false;

            // Force leaflet zoom animations off
            this._zoomAnimated = false;
        },

        createScene: function () {
            this.scene = Scene.create(
                this.options.scene,
                {
                    numWorkers: this.options.numWorkers,
                    preUpdate: this.options.preUpdate,
                    postUpdate: this.options.postUpdate,
                    continuousZoom: (LeafletLayer.leafletVersion === '1.x'),
                    highDensityDisplay: this.options.highDensityDisplay,
                    logLevel: this.options.logLevel,
                    // advanced option, app will have to manually called scene.update() per frame
                    disableRenderLoop: this.options.disableRenderLoop,
                    // advanced option, will require library to be served as same host as page
                    allowCrossDomainWorkers: this.options.allowCrossDomainWorkers
                });
        },

        // Finish initializing scene and setup events when layer is added to map
        onAdd: function (map) {
            if (!this.scene) {
                this.createScene();
            }

            layerBaseClass.prototype.onAdd.apply(this, arguments);

            this.hooks.resize = () => {
                this._updating_tangram = true;
                var size = map.getSize();
                this.scene.resizeMap(size.x, size.y);
                this._updating_tangram = false;
            };
            map.on('resize', this.hooks.resize);

            this.hooks.move = () => {
                if (this._updating_tangram) {
                    return;
                }

                this._updating_tangram = true;
                var view = map.getCenter();
                view.zoom = Math.min(map.getZoom(), map.getMaxZoom(), Geo.max_zoom);

                this.scene.setView(view);
                this.scene.immediateRedraw();
                this._updating_tangram = false;
            };
            map.on('move', this.hooks.move);

            this.hooks.zoomstart = () => {
                if (this._updating_tangram) {
                    return;
                }

                this._updating_tangram = true;
                this.scene.startZoom();
                this._updating_tangram = false;
            };
            map.on('zoomstart', this.hooks.zoomstart);

            this.hooks.dragstart = () => {
                this.scene.panning = true;
            };
            map.on('dragstart', this.hooks.dragstart);

            this.hooks.dragend = () => {
                this.scene.panning = false;
            };
            map.on('dragend', this.hooks.dragend);

            // Force leaflet zoom animations off
            map._zoomAnimated = false;

            // Modify default leaflet scroll wheel behavior
            this.modifyScrollWheelBehavior(map);

            // Canvas element will be inserted after map container (leaflet transforms shouldn't be applied to the GL canvas)
            // TODO: find a better way to deal with this? right now GL map only renders correctly as the bottom layer
            this.scene.container = map.getContainer();

            // Initial view
            var view = map.getCenter();
            view.zoom = Math.min(map.getZoom(), map.getMaxZoom(), Geo.max_zoom);
            this.scene.setView(view);

            // Subscribe to tangram events
{'fraction_non_alphanumeric': 0.09800170068027211, 'fraction_numerical': 0.002551020408163265, 'mean_word_length': 1.6519165727170237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '4079569', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2370, 'n_words': 622}