<commit_before>
(function ($) {

    const colorMap = { red: '#D63E2A', orange: '#F59630', green: '#72B026', blue: '#38AADD', purple: '#D252B9',
        darkred: '#A23336', darkblue: '#0067A3', darkgreen: '#728224', darkpurple: '#5B396B', cadetblue: '#436978',
        lightred: '#FF8E7F', beige: '#FFCB92', lightgreen: '#BBF970', lightblue: '#8ADAFF', pink: '#FF91EA',
        white: '#FBFBFB', lightgray: '#A3A3A3', gray: '#575757', black: '#303030', };
    const colors = ['blue', 'green', 'orange', 'purple', 'red', 'darkblue', 'darkpurple', 'lightblue', 'lightgreen', 'beige', 'pink', 'lightred'];

    function findRoute(map, start, end, index, mode = 'auto') {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            if (mode == 'straight') {
                // Use a deferred instead of returning it so we don't miss notifications
                _findRouteStraight(map, start, end, index)
                    .progress(deferred.notify)
                    .done(deferred.resolve)
                    .fail(deferred.reject);
            } else {
                _findRouteAuto(map, start, end, index)
                    .progress(deferred.notify)
                    .done(deferred.resolve)
                    .fail(function () {
                        console.log(this.error);
                        console.log('Trying straight line...');

                        const autoFailDrop = new Drop({
                            target: $('.awesome-marker').eq(index + 1)[0],
                            classes: 'drop-theme-arrows',
                            position: 'right middle',
                            constrainToWindow: false,
                            constrainToScrollParent: false,
                            openOn: null,
                            content: 'Impossible d\'obtenir le tracé en mode automatique. Le mode ligne droite va être utilisé.',
                        });
                        autoFailDrop.open();
                        $(autoFailDrop.content).on('click', function () {
                            autoFailDrop.destroy();
                        });

                        _findRouteStraight(map, start, end, index)
                            .progress(deferred.notify)
                            .done(deferred.resolve)
                            .fail(deferred.reject);
                    });
            }
        });
    }

    function _addRoute(map, geojson, start, end, index, mode) {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            geojson.prepareForMap(map, start, end);
            geojson.computeStats().progress(deferred.notify).then(function () {
                geojson.addTo(map);
                geojson.bindPopup('Calculs en cours...');
                geojson.on('popupopen', function (event) {
                    $('.marker-add-button:visible').click(function () {
                        const marker = L.Marker.routed(event.popup.getLatLng().roundE8(), {
                            riseOnHover: true,
                            draggable: true,
                            opacity: 0.5,
                            color: start.getColorIndex(),
                            type: 'waypoint',
                        });

                        marker.insert(geojson)
                            .done(function () {
                                marker.setOpacity(1);
                            });
                    });
                });

                geojson.snakeIn();
                start.setOpacity(1);
                end.setOpacity(1);

                deferred.resolveWith({ route: geojson });
            }).fail(function () {
                deferred.rejectWith({ error: 'Impossible d\'obtenir les données de la route' });
            });
        });
    }

    function _findRouteAuto(map, start, end, index) {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            const startLatLng = start.getLatLng();
            const endLatLng = end.getLatLng();

            const options = {
                distanceUnit: 'm',
                endPoint: {
                    x: endLatLng.lng,
                    y: endLatLng.lat,
                },
                exclusions: [],
                geometryInInstructions: true,
                graph: 'Pieton',
                routePreferences: 'fastest',
                startPoint: {
                    x: startLatLng.lng,
                    y: startLatLng.lat,
                },
                viaPoints: [],
                apiKey: keyIgn,
                onSuccess: function (results) {
                    if (results) {
                        const geojson = L.geoJSON([], {
                            color: start.getColorRgb(),
                            weight: 5,
                            opacity: 0.75,
                            snakingPause: 0,
                            snakingSpeed: 1000,
                        });

                        const _geometry = {
                            type: 'FeatureCollection',
                            features: [],
                        };
                        var counter = 1;
                        $.each(results.routeInstructions, function (idx, instructions) {
                            counter++;
                            _geometry.features.push({
                                id: counter,
                                type: 'Feature',
                                geometry: instructions.geometry,
                            });
                        });

                        geojson.addData(_geometry);

                        _addRoute(map, geojson, start, end, index, 'auto')
                            .progress(deferred.notify)
                            .done(deferred.resolve)
                            .fail(deferred.reject);

                        deferred.notify({ step: 'Route calculée' });
                    } else {
                        deferred.rejectWith({ error: 'Impossible d\'obtenir la route: pas de résultats fournis' });
                    }
                },
                onFailure: function (error) {    // seems to never be called
                    deferred.rejectWith({ error: 'Impossible d\'obtenir la route: ' + error.message });
                },
            };
            deferred.notify({ start: true, total: 1, status: 'Calcul de la route...' });
            Gp.Services.route(options);
        });
    }

    function _findRouteStraight(map, start, end, index) {
        const c1 = start.getLatLng().roundE8();
        const c2 = end.getLatLng().roundE8();
        const d = c1.distanceTo(c2);<commit_msg>Using L.Polyline instead of L.GeoJSON for routes<commit_after>
(function ($) {

    const colorMap = { red: '#D63E2A', orange: '#F59630', green: '#72B026', blue: '#38AADD', purple: '#D252B9',
        darkred: '#A23336', darkblue: '#0067A3', darkgreen: '#728224', darkpurple: '#5B396B', cadetblue: '#436978',
        lightred: '#FF8E7F', beige: '#FFCB92', lightgreen: '#BBF970', lightblue: '#8ADAFF', pink: '#FF91EA',
        white: '#FBFBFB', lightgray: '#A3A3A3', gray: '#575757', black: '#303030', };
    const colors = ['blue', 'green', 'orange', 'purple', 'red', 'darkblue', 'darkpurple', 'lightblue', 'lightgreen', 'beige', 'pink', 'lightred'];

    function findRoute(map, start, end, index, mode = 'auto') {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            if (mode == 'straight') {
                // Use a deferred instead of returning it so we don't miss notifications
                _findRouteStraight(map, start, end, index)
                    .progress(deferred.notify)
                    .done(deferred.resolve)
                    .fail(deferred.reject);
            } else {
                _findRouteAuto(map, start, end, index)
                    .progress(deferred.notify)
                    .done(deferred.resolve)
                    .fail(function () {
                        console.log(this.error);
                        console.log('Trying straight line...');

                        const autoFailDrop = new Drop({
                            target: $('.awesome-marker').eq(index + 1)[0],
                            classes: 'drop-theme-arrows',
                            position: 'right middle',
                            constrainToWindow: false,
                            constrainToScrollParent: false,
                            openOn: null,
                            content: 'Impossible d\'obtenir le tracé en mode automatique. Le mode ligne droite va être utilisé.',
                        });
                        autoFailDrop.open();
                        $(autoFailDrop.content).on('click', function () {
                            autoFailDrop.destroy();
                        });

                        _findRouteStraight(map, start, end, index)
                            .progress(deferred.notify)
                            .done(deferred.resolve)
                            .fail(deferred.reject);
                    });
            }
        });
    }

    function _addRoute(map, geojson, start, end, index, mode) {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            geojson.prepareForMap(map, start, end);
            geojson.computeStats().progress(deferred.notify).then(function () {
                geojson.addTo(map);
                geojson.bindPopup('Calculs en cours...');
                geojson.on('popupopen', function (event) {
                    $('.marker-add-button:visible').click(function () {
                        const marker = L.Marker.routed(event.popup.getLatLng().roundE8(), {
                            riseOnHover: true,
                            draggable: true,
                            opacity: 0.5,
                            color: start.getColorIndex(),
                            type: 'waypoint',
                        });

                        marker.insert(geojson)
                            .done(function () {
                                marker.setOpacity(1);
                            });
                    });
                });

                geojson.snakeIn();
                start.setOpacity(1);
                end.setOpacity(1);

                deferred.resolveWith({ route: geojson });
            }).fail(function () {
                deferred.rejectWith({ error: 'Impossible d\'obtenir les données de la route' });
            });
        });
    }

    function _findRouteAuto(map, start, end, index) {
        return $.Deferred(function () {
            const deferred = this;  // jscs:ignore safeContextKeyword

            const startLatLng = start.getLatLng();
            const endLatLng = end.getLatLng();

            const options = {
                distanceUnit: 'm',
                endPoint: {
                    x: endLatLng.lng,
                    y: endLatLng.lat,
                },
                exclusions: [],
                geometryInInstructions: true,
                graph: 'Pieton',
                routePreferences: 'fastest',
                startPoint: {
                    x: startLatLng.lng,
                    y: startLatLng.lat,
                },
                viaPoints: [],
                apiKey: keyIgn,
                onSuccess: function (results) {
                    if (results) {
                        const latlngs = [];
                        $.each(results.routeInstructions, function (idx, instructions) {
                            $.each(instructions.geometry.coordinates, function (j, coords) {
                                latlngs.push(L.latLng(coords[1], coords[0]));
                            });
                        });

                        const geojson = L.polyline(latlngs, {
                            color: start.getColorRgb(),
                            weight: 5,
                            opacity: 0.75,
                            snakingPause: 0,
                            snakingSpeed: 1000,
                        });

                        _addRoute(map, geojson, start, end, index, 'auto')
                            .progress(deferred.notify)
                            .done(deferred.resolve)
                            .fail(deferred.reject);

                        deferred.notify({ step: 'Route calculée' });
                    } else {
                        deferred.rejectWith({ error: 'Impossible d\'obtenir la route: pas de résultats fournis' });
                    }
                },
                onFailure: function (error) {    // seems to never be called
                    deferred.rejectWith({ error: 'Impossible d\'obtenir la route: ' + error.message });
                },
            };
            deferred.notify({ start: true, total: 1, status: 'Calcul de la route...' });
            Gp.Services.route(options);
        });
    }

    function _findRouteStraight(map, start, end, index) {
        const c1 = start.getLatLng().roundE8();
        const c2 = end.getLatLng().roundE8();
        const d = c1.distanceTo(c2);
{'fraction_non_alphanumeric': 0.11423025235179932, 'fraction_numerical': 0.01426011647006122, 'mean_word_length': 1.1583951015146632, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3776682', 'n_tokens_mistral': 3556, 'n_tokens_neox': 3259, 'n_words': 698}