<commit_before>var config = require('config');
var debug = require('debug')(config.name() + ':map');
var page = require('page');
var plugins = require('./leaflet-plugins');

/**
 * Expose `map`
 */

module.exports = function(el, opts) {
  opts = opts || {};
  opts.tileLayer = opts.tileLayer || {
    detectRetina: true
  };

  // create a map in the el with given options
  if (config.map_provider && config.map_provider() === 'AmigoCloud') {
    return new Map(L.amigo.map(el, opts));
  } else {
    return new Map(L.mapbox.map(el, config.mapbox_map_id(), opts));
  }
};

/**
 * Expose `createMarker`
 */

module.exports.createMarker = function(opts) {
  debug('creating marker %s', opts);

  var marker;

  if (config.map_provider && config.map_provider() === 'AmigoCloud') {
    marker = L.marker(new L.LatLng(opts.coordinate[1], opts.coordinate[0]), {
      icon: L.amigo.marker.icon({
        'marker-size': opts.size || 'medium',
        'marker-color': opts.color || '#ccc',
        'marker-symbol': opts.icon || ''
      }),
      title: opts.title || ''
    });
  } else {
    marker = L.marker(new L.LatLng(opts.coordinate[1], opts.coordinate[0]), {
      icon: L.mapbox.marker.icon({
        'marker-size': opts.size || 'medium',
        'marker-color': opts.color || '#ccc',
        'marker-symbol': opts.icon || ''
      }),
      title: opts.title || ''
    });
  }

  if (opts.url) {
    marker.on('click', function() {
      page(opts.url);
    });
  }
  return marker;
};

/**
 * Expose `realtime`
 */


if (config.map_provider() === 'AmigoCloud') {
    module.exports.realtime = function() {
        debug('setting up socket connection');

        L.amigo.realtime.setAccessToken(config.realtime_access_token());
        L.amigo.realtime.connectDatasetByUrl(config.realtime_dataset_url());
    };
}

module.exports.getRealtimeVehicles = function (validVehicles) {
  var currRoutes = module.exports.currRoutes,
      requests = [],
      endpoint = '/api/transitime/vehiclesDetails';

  $.each(currRoutes, function (routeId, direction) {
    // currRoutes is on obj containing current route legs stored as:
    // key: routeId, value: direction of travel (1 or 0)
    requests.push($.get(endpoint, {
      r: routeId,
      format: 'json'
    }).then(function (data) {
      // find vehicles in route heading direction that matches legs in currRoutes
      var vehicle = {};

      for (var j = 0; j < data.vehicles.length; j++) {
        vehicle = data.vehicles[j];
        if (vehicle.direction === currRoutes[vehicle.routeId]) {
          validVehicles.push(vehicle);
        }
      }
    }));
  });

  return requests;
};

/**
 * Toggle realtime
 */
module.exports.toggleRealtime = function(viewMap) {
  var map = viewMap;
  debug('toggling realtime');

  var loadRealtime = function () {
      map = module.exports.realtimeMap = viewMap;

      if (!map.realtime) {
        map.realtime = {
          points: []
        };
      }

      map.realtime.active = true;

      var pollForVehicles = function () {
        var validVehicles = [];
        getValidVehicles = module.exports.getRealtimeVehicles(validVehicles);
        $.when.apply($, getValidVehicles).done(function () {
          if (map.realtime.active && validVehicles.length) {
            for (var i = 0; i < validVehicles.length; i++) {
              var existingPoint = module.exports.findPoint(map, validVehicles[i]);
              if (existingPoint === -1) {
                module.exports.addPoint(map, validVehicles[i]);
              } else if (module.exports.hasVehicleMoved(map.realtime.points[existingPoint], validVehicles[i])) {
                module.exports.movePoint(map, validVehicles[i]);
              }
            }
          }
          module.exports.vehiclePoller = setTimeout(function () {
            pollForVehicles();
          }, 3000);
        });
      };

      pollForVehicles(); // recursive, fires at least 3 seconds after it last completed
  };

  var clearRealtime = function () {
      clearTimeout(module.exports.vehiclePoller);
      map.realtime.active = false;
      L.amigo.realtime.socket.removeAllListeners('realtime'); // I left this in for reduncency when switching to polling - Luke
      for (var i = 0; i < map.realtime.points.length; i++) {
        map.removeLayer(map.realtime.points[i].marker);
      }<commit_msg>Fixed #84 - stops initiating websocket<commit_after>var config = require('config');
var debug = require('debug')(config.name() + ':map');
var page = require('page');
var plugins = require('./leaflet-plugins');

/**
 * Expose `map`
 */

module.exports = function(el, opts) {
  opts = opts || {};
  opts.tileLayer = opts.tileLayer || {
    detectRetina: true
  };

  // create a map in the el with given options
  if (config.map_provider && config.map_provider() === 'AmigoCloud') {
    return new Map(L.amigo.map(el, opts));
  } else {
    return new Map(L.mapbox.map(el, config.mapbox_map_id(), opts));
  }
};

/**
 * Expose `createMarker`
 */

module.exports.createMarker = function(opts) {
  debug('creating marker %s', opts);

  var marker;

  if (config.map_provider && config.map_provider() === 'AmigoCloud') {
    marker = L.marker(new L.LatLng(opts.coordinate[1], opts.coordinate[0]), {
      icon: L.amigo.marker.icon({
        'marker-size': opts.size || 'medium',
        'marker-color': opts.color || '#ccc',
        'marker-symbol': opts.icon || ''
      }),
      title: opts.title || ''
    });
  } else {
    marker = L.marker(new L.LatLng(opts.coordinate[1], opts.coordinate[0]), {
      icon: L.mapbox.marker.icon({
        'marker-size': opts.size || 'medium',
        'marker-color': opts.color || '#ccc',
        'marker-symbol': opts.icon || ''
      }),
      title: opts.title || ''
    });
  }

  if (opts.url) {
    marker.on('click', function() {
      page(opts.url);
    });
  }
  return marker;
};

/**
 * Expose `realtime`
 */


if (config.map_provider() === 'AmigoCloud') {
    module.exports.realtime = function() {
        debug('setting up empty socket connection');

        // L.amigo.realtime.setAccessToken(config.realtime_access_token());
        // L.amigo.realtime.connectDatasetByUrl(config.realtime_dataset_url());
    };
}

module.exports.getRealtimeVehicles = function (validVehicles) {
  var currRoutes = module.exports.currRoutes,
      requests = [],
      endpoint = '/api/transitime/vehiclesDetails';

  $.each(currRoutes, function (routeId, direction) {
    // currRoutes is on obj containing current route legs stored as:
    // key: routeId, value: direction of travel (1 or 0)
    requests.push($.get(endpoint, {
      r: routeId,
      format: 'json'
    }).then(function (data) {
      // find vehicles in route heading direction that matches legs in currRoutes
      var vehicle = {};

      for (var j = 0; j < data.vehicles.length; j++) {
        vehicle = data.vehicles[j];
        if (vehicle.direction === currRoutes[vehicle.routeId]) {
          validVehicles.push(vehicle);
        }
      }
    }));
  });

  return requests;
};

/**
 * Toggle realtime
 */
module.exports.toggleRealtime = function(viewMap) {
  var map = viewMap;
  debug('toggling realtime');

  var loadRealtime = function () {
      map = module.exports.realtimeMap = viewMap;

      if (!map.realtime) {
        map.realtime = {
          points: []
        };
      }

      map.realtime.active = true;

      var pollForVehicles = function () {
        var validVehicles = [];
        getValidVehicles = module.exports.getRealtimeVehicles(validVehicles);
        $.when.apply($, getValidVehicles).done(function () {
          if (map.realtime.active && validVehicles.length) {
            for (var i = 0; i < validVehicles.length; i++) {
              var existingPoint = module.exports.findPoint(map, validVehicles[i]);
              if (existingPoint === -1) {
                module.exports.addPoint(map, validVehicles[i]);
              } else if (module.exports.hasVehicleMoved(map.realtime.points[existingPoint], validVehicles[i])) {
                module.exports.movePoint(map, validVehicles[i]);
              }
            }
          }
          module.exports.vehiclePoller = setTimeout(function () {
            pollForVehicles();
          }, 3000);
        });
      };

      pollForVehicles(); // recursive, fires at least 3 seconds after it last completed
  };

  var clearRealtime = function () {
      clearTimeout(module.exports.vehiclePoller);
      map.realtime.active = false;
      // L.amigo.realtime.socket.removeAllListeners('realtime'); // I left this in for reduncency when switching to polling - Luke
      for (var i = 0; i < map.realtime.points.length; i++) {
        map.removeLayer(map.realtime.points[i].marker);
      }
{'fraction_non_alphanumeric': 0.15367024925680312, 'fraction_numerical': 0.003658815458495312, 'mean_word_length': 2.5833674723473985, 'pattern_counts': {'":': 0, '<': 9, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7348344', 'n_tokens_mistral': 2784, 'n_tokens_neox': 2714, 'n_words': 559}