<commit_before>class Bus.Views.Navigation extends Backbone.View
  template: JST['navigation']
  el: 'div#navigation'

  events:
    'submit': 'plan'
    'click #from-location': 'from_current_location'
    'click #to-location': 'to_current_location'

  initialize: =>
    Bus.events.on 'plan:complete', @add_segments
    Bus.events.on 'plan:complete', @get_real_time
    Bus.events.on 'plan:clear', @render
    Bus.events.on 'real_time:complete', @render_real_time

  render: =>
    $(@el).html(@template())
    this

  plan: (event) ->
    event.preventDefault()
    from_query = @$('#from_query').val()
    to_query = @$('#to_query').val()
    from_data = {
      format: 'json', q: from_query, countrycodes: 'US'
    }
    to_data = {
      format: 'json', q: to_query, countrycodes: 'US'
    }
    # TODO: This is fugly, need to batch it
    $.get '/nominatim/v1/search', from_data, (from_response) =>
      $.get '/nominatim/v1/search', to_data, (to_response) =>
        Bus.events.trigger 'geocode:complete', from_response[0], to_response[0]


  _first_transit_leg: (segments) ->
    _.find segments, (segment) ->
      segment.mode != 'WALK'

  add_segments: (plan) =>
    @$('.trip').html('')
    segments = plan.itineraries[0].legs
    first_transit_leg = @_first_transit_leg(segments)
    for leg in segments
      if first_transit_leg.tripId == leg.tripId
        leg.real_time = true
      view = new Bus.Views.Segment(segment: leg)
      @$('.trip').append(view.render().el)

  # TODO: Use leaflet's map.locate()
  from_current_location: =>
    navigator.geolocation.getCurrentPosition (position) ->
      @$('#from_query').val("#{position.coords.latitude},#{position.coords.longitude}")

  to_current_location: =>
    navigator.geolocation.getCurrentPosition (position) ->
      @$('#to_query').val("#{position.coords.latitude},#{position.coords.longitude}")

  get_real_time: (plan) =>
    segments = plan.itineraries[0].legs
    first_transit_leg = @_first_transit_leg(segments)
    # TODO: get the onebusaway agency id from the agency name, cache this!
    $.get "/workshop/agencies/otp/#{first_transit_leg.agencyId}.json", (response) =>
      # OBA gives a response with an invalid content type, force it to json
      $.ajax
        url: "/oba/where/arrivals-and-departures-for-stop/#{response.oba_id}_#{first_transit_leg.from.stopId.id}.json"
        data: { key: 'TEST' }
        success: (real_time) ->
          Bus.events.trigger 'real_time:complete', real_time.data.arrivalsAndDepartures
        dataType: 'json'


  render_real_time: (data) =>
    # FIXME: Match the trip using the data.tripId and OTP trip id.
    # _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; })
    trip = data[0]
    readable_time_delta = '<span class=\"label label-warning\">unknown</span>'

    if trip?
      time_delta = Math.round((trip.scheduledArrivalTime - trip.predictedArrivalTime)/60000)

      # TODO: Noticed several outliers, figure out what's causing them
      if Math.abs(time_delta) < 360
        # TODO: Move tags stuff into templates<commit_msg>left a reminder to set a date for a real time data<commit_after>class Bus.Views.Navigation extends Backbone.View
  template: JST['navigation']
  el: 'div#navigation'

  events:
    'submit': 'plan'
    'click #from-location': 'from_current_location'
    'click #to-location': 'to_current_location'

  initialize: =>
    Bus.events.on 'plan:complete', @add_segments
    Bus.events.on 'plan:complete', @get_real_time
    Bus.events.on 'plan:clear', @render
    Bus.events.on 'real_time:complete', @render_real_time

  render: =>
    $(@el).html(@template())
    this

  plan: (event) ->
    event.preventDefault()
    from_query = @$('#from_query').val()
    to_query = @$('#to_query').val()
    from_data = {
      format: 'json', q: from_query, countrycodes: 'US'
    }
    to_data = {
      format: 'json', q: to_query, countrycodes: 'US'
    }
    # TODO: This is fugly, need to batch it
    $.get '/nominatim/v1/search', from_data, (from_response) =>
      $.get '/nominatim/v1/search', to_data, (to_response) =>
        Bus.events.trigger 'geocode:complete', from_response[0], to_response[0]


  _first_transit_leg: (segments) ->
    _.find segments, (segment) ->
      segment.mode != 'WALK'

  add_segments: (plan) =>
    @$('.trip').html('')
    segments = plan.itineraries[0].legs
    first_transit_leg = @_first_transit_leg(segments)
    for leg in segments
      if first_transit_leg.tripId == leg.tripId
        leg.real_time = true
      view = new Bus.Views.Segment(segment: leg)
      @$('.trip').append(view.render().el)

  # TODO: Use leaflet's map.locate()
  from_current_location: =>
    navigator.geolocation.getCurrentPosition (position) ->
      @$('#from_query').val("#{position.coords.latitude},#{position.coords.longitude}")

  to_current_location: =>
    navigator.geolocation.getCurrentPosition (position) ->
      @$('#to_query').val("#{position.coords.latitude},#{position.coords.longitude}")

  get_real_time: (plan) =>
    segments = plan.itineraries[0].legs
    first_transit_leg = @_first_transit_leg(segments)
    # TODO: get the onebusaway agency id from the agency name, cache this!
    $.get "/workshop/agencies/otp/#{first_transit_leg.agencyId}.json", (response) =>
      # OBA gives a response with an invalid content type, force it to json
      $.ajax
        url: "/oba/where/arrivals-and-departures-for-stop/#{response.oba_id}_#{first_transit_leg.from.stopId.id}.json"
        # TODO: put the actual time in here, defaults to NOW
        data: { key: 'TEST' }
        success: (real_time) ->
          Bus.events.trigger 'real_time:complete', real_time.data.arrivalsAndDepartures
        dataType: 'json'


  render_real_time: (data) =>
    # FIXME: Match the trip using the data.tripId and OTP trip id.
    # _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; })
    trip = data[0]
    readable_time_delta = '<span class=\"label label-warning\">unknown</span>'

    if trip?
      time_delta = Math.round((trip.scheduledArrivalTime - trip.predictedArrivalTime)/60000)

      # TODO: Noticed several outliers, figure out what's causing them
      if Math.abs(time_delta) < 360
        # TODO: Move tags stuff into templates
{'fraction_non_alphanumeric': 0.14817774936061381, 'fraction_numerical': 0.007352941176470588, 'mean_word_length': 3.4219081272084804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 39, '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': '244914', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2102, 'n_words': 448}