<commit_before>// Copyright (c) Microsoft Corporation. All rights reserved.
// Based in part on code from Apache Ripple (https://github.com/apache/incubator-ripple)

/*global OpenLayers: false */
var telemetry = require('telemetry-helper');

var baseProps = {
    plugin: 'cordova-plugin-geolocation',
    panel: 'geolocation'
};

// For telemetry about interacting with the map using the mouse, we "batch" the mouse events to prevent sending too many messages.
var mouseEventHoldDelay = 1000; // The inactivity delay to wait before sending a telemetry event when the user interacts with the map.
var pendingMouseEvents = 0; // The number of mouse events that are "on hold".

module.exports = function (messages) {
    var constants = require('sim-constants'),
        geo = require('./geo-model'),
        db = require('db'),
        event = require('event'),
        utils = require('utils'),
        navUtils = utils.navHelper(),
        _gpsMapZoomLevel;

    geo.initialize(messages);

    function _updateGpsMap() {
        var positionInfo = geo.getPositionInfo(),
            mapContainer = document.getElementById(constants.GEO.OPTIONS.MAP_CONTAINER),
            geoZoomValue = document.getElementById(constants.GEO.MAP_ZOOM_LEVEL_CONTAINER);

        if (mapContainer) {
            geo.map.setCenter(new OpenLayers.LonLat(positionInfo.longitude, positionInfo.latitude) // Center of the map
                .transform(
                new OpenLayers.Projection('EPSG:4326'), // transform from WGS 1984
                new OpenLayers.Projection('EPSG:900913') // to Spherical Mercator Projection
                ),
                _gpsMapZoomLevel,
                true // don't trigger dragging events
            );
        }

        if (geoZoomValue) {
            geoZoomValue.innerHTML = _gpsMapZoomLevel;
        }
    }

    function _updateGpsMapZoom(goUp) {
        var inc = goUp ? 1 : -1;
        _gpsMapZoomSet(_gpsMapZoomLevel + inc);
        _updateGpsMap();
    }

    function _gpsMapZoomSet(value) {
        _gpsMapZoomLevel = Math.max(Math.min(value, constants.GEO.MAP_ZOOM_MAX), constants.GEO.MAP_ZOOM_MIN);
        document.getElementById(constants.GEO.MAP_ZOOM_LEVEL_CONTAINER).innerHTML = _gpsMapZoomLevel;
        db.save(constants.GEO.MAP_ZOOM_KEY, _gpsMapZoomLevel);
    }

    function mapEventTelemetryHandler() {
        pendingMouseEvents++;
        setTimeout(function () {
            --pendingMouseEvents;

            if (pendingMouseEvents === 0) {
                telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-map-container' }));
            }
        }, mouseEventHoldDelay);
    }

    function registerTelemetryEvents() {
        // Register the simple events (onclick / onchange / etc -> send the control ID).
        var basicTelemetryEvents = [
            { control: 'geo-latitude' },
            { control: 'geo-longitude' },
            { control: 'geo-altitude' },
            { control: 'geo-accuracy' },
            { control: 'geo-altitude-accuracy' },
            { control: 'geo-heading' },
            { control: 'geo-speed' },
            { control: 'geo-delay' },
            { control: 'geo-gpxfile-button', event: 'click' },
            { control: 'geo-map-zoom-decrease', event: 'click' },
            { control: 'geo-map-zoom-increase', event: 'click' }
        ];

        basicTelemetryEvents.forEach(function (controlEvent) {
            registerTelemetryForControl(controlEvent.control, controlEvent.event);
        });

        // Register the event for the timeout checkbox.
        // Clicking the checkbox's label fires the click event twice, so keep track of the previous state. Note that we can't use the change event because the component seems to swallow it.
        var previousTimeoutState = false;
        var geoTimeoutCheckbox = document.querySelector('#geo-timeout');

        geoTimeoutCheckbox.onclick = function () {
            if (geoTimeoutCheckbox.checked !== previousTimeoutState) {
                previousTimeoutState = geoTimeoutCheckbox.checked;
                telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-timeout' }));
            }
        };

        // Register the event for the Go button.
        document.getElementById('geo-gpx-go').onclick = function () {
            var rateList = document.getElementById('geo-gpxmultiplier-select');
            var option = rateList.options[rateList.selectedIndex];

            telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-gpx-go', value: option.value }));
        };

        // Register the event for zooming with the mouse wheel on the map.
        document.getElementById('geo-map-container').onwheel = mapEventTelemetryHandler;
    }

    function registerTelemetryForControl(controlId, event) {
        event = event || 'change';
        document.getElementById(controlId).addEventListener(event, telemetry.sendUITelemetry.bind(this, Object.assign({}, baseProps, { control: controlId })));
    }

    return {
        panel: {
            domId: 'gps-container',
            collapsed: true,
            pane: 'right'
        },

        initialize: function () {
            var GEO_OPTIONS = constants.GEO.OPTIONS,
                positionInfo = geo.getPositionInfo(),
                positionUpdatedMessage = 'position-info-updated',
                latitude = document.getElementById(GEO_OPTIONS.LATITUDE),
                longitude = document.getElementById(GEO_OPTIONS.LONGITUDE),
                altitude = document.getElementById(GEO_OPTIONS.ALTITUDE),
                accuracy = document.getElementById(GEO_OPTIONS.ACCURACY),
                altitudeAccuracy = document.getElementById(GEO_OPTIONS.ALTITUDE_ACCURACY),
                heading = document.getElementById(GEO_OPTIONS.HEADING),
                speed = document.getElementById(GEO_OPTIONS.SPEED),
                delay = document.getElementById(GEO_OPTIONS.DELAY),
                delayLabel = document.getElementById(GEO_OPTIONS.DELAY_LABEL),
                headingLabel = document.getElementById(GEO_OPTIONS.HEADING_LABEL),
                headingMapLabel = document.getElementById(GEO_OPTIONS.HEADING_MAP_LABEL),
                timeout = document.getElementById(GEO_OPTIONS.TIMEOUT),
                gpxMultiplier = document.getElementById(GEO_OPTIONS.GPXMULTIPLIER),
                gpxReplayStatus = document.getElementById(GEO_OPTIONS.GPXREPLAYSTATUS),
                gpxGo = document.getElementById(GEO_OPTIONS.GPXGO),
                mapMarker = document.getElementById(GEO_OPTIONS.MAP_MARKER),
                mapContainer = document.getElementById(GEO_OPTIONS.MAP_CONTAINER),
                map = null,
                track = [],
                _replayingGpxFile = false,
                _haltGpxReplay = false;

            var updateGeoPending = false;
            function updateGeo() {
                if (!updateGeoPending) {
                    updateGeoPending = true;
                    window.setTimeout(function () {
                        geo.updatePositionInfo({
                            latitude: parseFloat(latitude.value),
                            longitude: parseFloat(longitude.value),
                            altitude: parseInt(altitude.value, 10),
                            accuracy: parseInt(accuracy.value, 10),
                            altitudeAccuracy: parseInt(altitudeAccuracy.value, 10),
                            heading: heading.value ? parseFloat(heading.value) : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                            speed: speed.value ? parseInt(speed.value, 10) : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                            timeStamp: new Date()
                        },
                            delay.value,
                            timeout.checked);
                        updateGeoPending = false;
                    }, 0);
                }
            }

            function onHeadingValueUpdated(value) {
                heading.value = value;

                var headingDeg  = parseFloat(heading.value),
                    headingText = navUtils.getDirection(headingDeg);
<commit_msg>Heading on map shows too many decimal places - make it an int (#166)<commit_after>// Copyright (c) Microsoft Corporation. All rights reserved.
// Based in part on code from Apache Ripple (https://github.com/apache/incubator-ripple)

/*global OpenLayers: false */
var telemetry = require('telemetry-helper');

var baseProps = {
    plugin: 'cordova-plugin-geolocation',
    panel: 'geolocation'
};

// For telemetry about interacting with the map using the mouse, we "batch" the mouse events to prevent sending too many messages.
var mouseEventHoldDelay = 1000; // The inactivity delay to wait before sending a telemetry event when the user interacts with the map.
var pendingMouseEvents = 0; // The number of mouse events that are "on hold".

module.exports = function (messages) {
    var constants = require('sim-constants'),
        geo = require('./geo-model'),
        db = require('db'),
        event = require('event'),
        utils = require('utils'),
        navUtils = utils.navHelper(),
        _gpsMapZoomLevel;

    geo.initialize(messages);

    function _updateGpsMap() {
        var positionInfo = geo.getPositionInfo(),
            mapContainer = document.getElementById(constants.GEO.OPTIONS.MAP_CONTAINER),
            geoZoomValue = document.getElementById(constants.GEO.MAP_ZOOM_LEVEL_CONTAINER);

        if (mapContainer) {
            geo.map.setCenter(new OpenLayers.LonLat(positionInfo.longitude, positionInfo.latitude) // Center of the map
                .transform(
                new OpenLayers.Projection('EPSG:4326'), // transform from WGS 1984
                new OpenLayers.Projection('EPSG:900913') // to Spherical Mercator Projection
                ),
                _gpsMapZoomLevel,
                true // don't trigger dragging events
            );
        }

        if (geoZoomValue) {
            geoZoomValue.innerHTML = _gpsMapZoomLevel;
        }
    }

    function _updateGpsMapZoom(goUp) {
        var inc = goUp ? 1 : -1;
        _gpsMapZoomSet(_gpsMapZoomLevel + inc);
        _updateGpsMap();
    }

    function _gpsMapZoomSet(value) {
        _gpsMapZoomLevel = Math.max(Math.min(value, constants.GEO.MAP_ZOOM_MAX), constants.GEO.MAP_ZOOM_MIN);
        document.getElementById(constants.GEO.MAP_ZOOM_LEVEL_CONTAINER).innerHTML = _gpsMapZoomLevel;
        db.save(constants.GEO.MAP_ZOOM_KEY, _gpsMapZoomLevel);
    }

    function mapEventTelemetryHandler() {
        pendingMouseEvents++;
        setTimeout(function () {
            --pendingMouseEvents;

            if (pendingMouseEvents === 0) {
                telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-map-container' }));
            }
        }, mouseEventHoldDelay);
    }

    function registerTelemetryEvents() {
        // Register the simple events (onclick / onchange / etc -> send the control ID).
        var basicTelemetryEvents = [
            { control: 'geo-latitude' },
            { control: 'geo-longitude' },
            { control: 'geo-altitude' },
            { control: 'geo-accuracy' },
            { control: 'geo-altitude-accuracy' },
            { control: 'geo-heading' },
            { control: 'geo-speed' },
            { control: 'geo-delay' },
            { control: 'geo-gpxfile-button', event: 'click' },
            { control: 'geo-map-zoom-decrease', event: 'click' },
            { control: 'geo-map-zoom-increase', event: 'click' }
        ];

        basicTelemetryEvents.forEach(function (controlEvent) {
            registerTelemetryForControl(controlEvent.control, controlEvent.event);
        });

        // Register the event for the timeout checkbox.
        // Clicking the checkbox's label fires the click event twice, so keep track of the previous state. Note that we can't use the change event because the component seems to swallow it.
        var previousTimeoutState = false;
        var geoTimeoutCheckbox = document.querySelector('#geo-timeout');

        geoTimeoutCheckbox.onclick = function () {
            if (geoTimeoutCheckbox.checked !== previousTimeoutState) {
                previousTimeoutState = geoTimeoutCheckbox.checked;
                telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-timeout' }));
            }
        };

        // Register the event for the Go button.
        document.getElementById('geo-gpx-go').onclick = function () {
            var rateList = document.getElementById('geo-gpxmultiplier-select');
            var option = rateList.options[rateList.selectedIndex];

            telemetry.sendUITelemetry(Object.assign({}, baseProps, { control: 'geo-gpx-go', value: option.value }));
        };

        // Register the event for zooming with the mouse wheel on the map.
        document.getElementById('geo-map-container').onwheel = mapEventTelemetryHandler;
    }

    function registerTelemetryForControl(controlId, event) {
        event = event || 'change';
        document.getElementById(controlId).addEventListener(event, telemetry.sendUITelemetry.bind(this, Object.assign({}, baseProps, { control: controlId })));
    }

    return {
        panel: {
            domId: 'gps-container',
            collapsed: true,
            pane: 'right'
        },

        initialize: function () {
            var GEO_OPTIONS = constants.GEO.OPTIONS,
                positionInfo = geo.getPositionInfo(),
                positionUpdatedMessage = 'position-info-updated',
                latitude = document.getElementById(GEO_OPTIONS.LATITUDE),
                longitude = document.getElementById(GEO_OPTIONS.LONGITUDE),
                altitude = document.getElementById(GEO_OPTIONS.ALTITUDE),
                accuracy = document.getElementById(GEO_OPTIONS.ACCURACY),
                altitudeAccuracy = document.getElementById(GEO_OPTIONS.ALTITUDE_ACCURACY),
                heading = document.getElementById(GEO_OPTIONS.HEADING),
                speed = document.getElementById(GEO_OPTIONS.SPEED),
                delay = document.getElementById(GEO_OPTIONS.DELAY),
                delayLabel = document.getElementById(GEO_OPTIONS.DELAY_LABEL),
                headingLabel = document.getElementById(GEO_OPTIONS.HEADING_LABEL),
                headingMapLabel = document.getElementById(GEO_OPTIONS.HEADING_MAP_LABEL),
                timeout = document.getElementById(GEO_OPTIONS.TIMEOUT),
                gpxMultiplier = document.getElementById(GEO_OPTIONS.GPXMULTIPLIER),
                gpxReplayStatus = document.getElementById(GEO_OPTIONS.GPXREPLAYSTATUS),
                gpxGo = document.getElementById(GEO_OPTIONS.GPXGO),
                mapMarker = document.getElementById(GEO_OPTIONS.MAP_MARKER),
                mapContainer = document.getElementById(GEO_OPTIONS.MAP_CONTAINER),
                map = null,
                track = [],
                _replayingGpxFile = false,
                _haltGpxReplay = false;

            var updateGeoPending = false;
            function updateGeo() {
                if (!updateGeoPending) {
                    updateGeoPending = true;
                    window.setTimeout(function () {
                        geo.updatePositionInfo({
                            latitude: parseFloat(latitude.value),
                            longitude: parseFloat(longitude.value),
                            altitude: parseInt(altitude.value, 10),
                            accuracy: parseInt(accuracy.value, 10),
                            altitudeAccuracy: parseInt(altitudeAccuracy.value, 10),
                            heading: heading.value ? parseFloat(heading.value) : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                            speed: speed.value ? parseInt(speed.value, 10) : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                            timeStamp: new Date()
                        },
                            delay.value,
                            timeout.checked);
                        updateGeoPending = false;
                    }, 0);
                }
            }

            function onHeadingValueUpdated(value) {
                heading.value = value;

                var headingDeg  = parseInt(heading.value),
                    headingText = navUtils.getDirection(headingDeg);

{'fraction_non_alphanumeric': 0.09969223341982983, 'fraction_numerical': 0.005853599662060225, 'mean_word_length': 2.1097766935635205, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6534973', 'n_tokens_mistral': 4569, 'n_tokens_neox': 4317, 'n_words': 890}