<commit_before># FIXME - fetch kml from here http://localhost:8080/api/v1/mission/10/messages.kml
class MapController
  @$inject: ['$scope', '$http']
  constructor: (@scope, @http) ->
    @initMap()

  initMap: =>
    # Collect various maps worth using
    mbox = (key) ->
      url: "https://a.tiles.mapbox.com/v3/" + key + "/{z}/{x}/{y}.png"
      options:
        attribution: '<a href="http://www.mapbox.com/about/maps/" target="_blank">Terms &amp; Feedback</a>'

    maps =
      openstreetmap:
        url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        options:
          attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      opencyclemap:
        url: "http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png",
        options:
          attribution: 'All maps &copy; <a href="http://www.opencyclemap.org">OpenCycleMap</a>, map data &copy; <a href="http://www.openstreetmap.org">OpenStreetMap</a> (<a href="http://www.openstreetmap.org/copyright">ODbL</a>'
      mapbox_bright: mbox("mapbox.world-bright")
      mapbox_example: mbox("examples.map-zr0njcqy")
      threedr_default: mbox("kevin3dr.hokdl9ko")

    @scope.defaults =
      scrollWheelZoom: true
      zoom: 10
    @scope.tiles = maps.threedr_default

class BoundsFactory
  constructor: () ->
    @bounds =
      northEast:
        lat: -Number.MAX_VALUE
        lng: -Number.MAX_VALUE
      southWest:
        lat: Number.MAX_VALUE
        lng: Number.MAX_VALUE

  # expand bounding box to include pt
  # returns true if we changed the bounding box
  expand: (lat, lon) ->
    # Updating bounds are _very_ expensive in leaflet, so we try to 'bucket' our bounds in .1 deg increments
    roundUp = (x) -> Math.ceil(x * 10) / 10.0
    roundDown = (x) -> Math.floor(x * 10) / 10.0

    dirty = false

    # We use the dirty flag to avoid changing scope object if nothing changed
    min = (newval, old) ->
      if newval < old
        dirty = true
        newval
      else
        old

    max = (newval, old) ->
      if newval > old
        dirty = true
        newval
      else
        old

    @bounds.southWest.lat = min(roundDown(lat),@bounds.southWest.lat)
    @bounds.southWest.lng = min(roundDown(lon), @bounds.southWest.lng)
    @bounds.northEast.lat = max(roundUp(lat), @bounds.northEast.lat)
    @bounds.northEast.lng = max(roundUp(lon), @bounds.northEast.lng)
    dirty

class LiveMapController extends MapController
  @$inject: ['$scope', '$log', 'leafletData', '$http', 'missionService']
  constructor: (scope, @log, leafletData, http, @missionService) ->
    scope.leafletData = leafletData
    @boundsFactory = new BoundsFactory

    @count = 0
    scope.vehicleMarkers = {}
    scope.vehiclePaths = {}
    scope.bounds = {} # Angular wants to see at least an empty object (not undefined)
    scope.center = {}

    # TODO: fake markers arn't workng
    #scope.vehicleMarkers['missionId_fake'] = {lat: scope.hawaii.lat, lng: scope.hawwai.lng, focus: false, draggable: false}

    super(scope, http)

    # FIXME - Ramon, why is service now one level nested in @missionService?
    s = @missionService.service

    listeners =
      "loc": @onLive
      "att": @onAttitude
      "start": @onMissionStart
      "end": @onMissionEnd
      "mode": @updateVehicleMessage
      "arm": @updateVehicleMessage
      "mystery": @updateVehicleMessage
      "text": @updateVehicleMessage

    listenerIds = s.atmosphere.on(name, callback) for name, callback of listeners
    @log.debug('live map now subscribed')

    # We bounce our atmosphere link, because the server will automatically resend vehicle messages on each new connection
    # (And our controller is not keeping a list of past connection messages - FIXME, it would be better to keep
    # a model object to preserve such state)
    s.atmosphere_connect()

    scope.$on("$destroy", () =>
        @log.debug('Unsubscribe for atmosphere notification')
        for id of listenerIds
          s.atmosphere.off(id)
        s.atmosphere_disconnect()
    )

  onLive: (data) =>
    # FIXME - not sure if I need apply... (or how to optimize it)
    # Apply should be called around the function to be guarded... -kevinh<commit_msg>fix indentation<commit_after># FIXME - fetch kml from here http://localhost:8080/api/v1/mission/10/messages.kml
class MapController
  @$inject: ['$scope', '$http']
  constructor: (@scope, @http) ->
    @initMap()

  initMap: =>
    # Collect various maps worth using
    mbox = (key) ->
      url: "https://a.tiles.mapbox.com/v3/" + key + "/{z}/{x}/{y}.png"
      options:
        attribution: '<a href="http://www.mapbox.com/about/maps/" target="_blank">Terms &amp; Feedback</a>'

    maps =
      openstreetmap:
        url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        options:
          attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
      opencyclemap:
        url: "http://{s}.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png",
        options:
          attribution: 'All maps &copy; <a href="http://www.opencyclemap.org">OpenCycleMap</a>, map data &copy; <a href="http://www.openstreetmap.org">OpenStreetMap</a> (<a href="http://www.openstreetmap.org/copyright">ODbL</a>'
      mapbox_bright: mbox("mapbox.world-bright")
      mapbox_example: mbox("examples.map-zr0njcqy")
      threedr_default: mbox("kevin3dr.hokdl9ko")

    @scope.defaults =
      scrollWheelZoom: true
      zoom: 10
    @scope.tiles = maps.threedr_default

class BoundsFactory
  constructor: () ->
    @bounds =
      northEast:
        lat: -Number.MAX_VALUE
        lng: -Number.MAX_VALUE
      southWest:
        lat: Number.MAX_VALUE
        lng: Number.MAX_VALUE

  # expand bounding box to include pt
  # returns true if we changed the bounding box
  expand: (lat, lon) ->
    # Updating bounds are _very_ expensive in leaflet, so we try to 'bucket' our bounds in .1 deg increments
    roundUp = (x) -> Math.ceil(x * 10) / 10.0
    roundDown = (x) -> Math.floor(x * 10) / 10.0

    dirty = false

    # We use the dirty flag to avoid changing scope object if nothing changed
    min = (newval, old) ->
      if newval < old
        dirty = true
        newval
      else
        old

    max = (newval, old) ->
      if newval > old
        dirty = true
        newval
      else
        old

    @bounds.southWest.lat = min(roundDown(lat),@bounds.southWest.lat)
    @bounds.southWest.lng = min(roundDown(lon), @bounds.southWest.lng)
    @bounds.northEast.lat = max(roundUp(lat), @bounds.northEast.lat)
    @bounds.northEast.lng = max(roundUp(lon), @bounds.northEast.lng)
    dirty

class LiveMapController extends MapController
  @$inject: ['$scope', '$log', 'leafletData', '$http', 'missionService']
  constructor: (scope, @log, leafletData, http, @missionService) ->
    scope.leafletData = leafletData
    @boundsFactory = new BoundsFactory

    @count = 0
    scope.vehicleMarkers = {}
    scope.vehiclePaths = {}
    scope.bounds = {} # Angular wants to see at least an empty object (not undefined)
    scope.center = {}

    # TODO: fake markers arn't workng
    #scope.vehicleMarkers['missionId_fake'] = {lat: scope.hawaii.lat, lng: scope.hawwai.lng, focus: false, draggable: false}

    super(scope, http)

    # FIXME - Ramon, why is service now one level nested in @missionService?
    s = @missionService.service

    listeners =
      "loc": @onLive
      "att": @onAttitude
      "start": @onMissionStart
      "end": @onMissionEnd
      "mode": @updateVehicleMessage
      "arm": @updateVehicleMessage
      "mystery": @updateVehicleMessage
      "text": @updateVehicleMessage

    listenerIds = s.atmosphere.on(name, callback) for name, callback of listeners
    @log.debug('live map now subscribed')

    # We bounce our atmosphere link, because the server will automatically resend vehicle messages on each new connection
    # (And our controller is not keeping a list of past connection messages - FIXME, it would be better to keep
    # a model object to preserve such state)
    s.atmosphere_connect()

    scope.$on("$destroy", () =>
      @log.debug('Unsubscribe for atmosphere notification')
      for id of listenerIds
        s.atmosphere.off(id)
      s.atmosphere_disconnect()
    )

  onLive: (data) =>
    # FIXME - not sure if I need apply... (or how to optimize it)
    # Apply should be called around the function to be guarded... -kevinh
{'fraction_non_alphanumeric': 0.13109323237103643, 'fraction_numerical': 0.005915759583530526, 'mean_word_length': 2.8775229357798167, 'pattern_counts': {'":': 16, '<': 25, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 10, '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': '3621687', 'n_tokens_mistral': 2821, 'n_tokens_neox': 2688, 'n_words': 718}