<commit_before># Creates the links of the network. A link consists of a
# Polyline drawn between two steps in the route as well as
# an arrow head pointing in the appropriate direction if the
# Polyline is sufficiently long.
class window.beats.MapLinkView extends Backbone.View
  @LINK_COLOR: 'blue'
  @SELECTED_LINK_COLOR: 'red'

  @STROKE_WEIGHT_THIN: 2
  @STROKE_WEIGHT_THINNER: 1

  $a = window.beats
  model_events : {
    'remove': 'removeLink'
    'change:selected': 'toggleSelected'
    'change:lane_offset': 'drawLink'
    'change:lanes': 'setStrokeWeight'
    'change:hide': 'hideShowLink'
    'change:editor_show': 'editor'
    'change:show_demands': 'viewDemands'
  }
  
  broker_events: {
    'map:init': 'render'
  }

  initialize: (@model) ->
    @model.view = @
    @iWindow = new google.maps.InfoWindow()
    # Gets the encoded path line string if it is not already been set
    if(@model.position()? and @model.position().length > 0)
      @_createEncodedPath @model.position()
      @_saveEncodedPath()
    @drawLink()
    # only calculates and then updates link length if it not already set
    if !model.length()?
      @_saveLinkLength()
    @_contextMenu()
    @model.poly = @link
    @_publishEvents()
  
  render: ->
    @link.setMap($a.map) if @link?
    @

  # this method reads the path of MO points converting them into Google Points
  # and then encodes the using googles
  # geomtry package in order to save the path to models shape field
  _createEncodedPath: (pts) ->
    gPath = $a.Util.convertPointsToGoogleLatLng(pts)
    @encodedPath = google.maps.geometry.encoding.encodePath gPath

  # save the encoded path to the model
  _saveEncodedPath: ->
    @model.set_geometry @encodedPath
  
  # Creates the Polyline to rendered on the map
  # The Polyline map attribute is set immediately so the user can see that the
  # lines are being drawn
  # We set listeners up in drawLink because they need to be re-attached anytime
  # the link is re-drawn
  drawLink: ->
    @hideLink() if @link? #if you are applying offset to existing line
    linkGeom = @model.geometry()
    enc = google.maps.geometry.encoding
    pathVertices = @applyOffset(enc.decodePath(linkGeom))
    @link = new google.maps.Polyline({
      path: pathVertices
      map: $a.map
      strokeColor: @_getStrokeColor()
      icons: [{
          icon: { path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW }
          fillColor: 'blue'
          offset: '60%'
        }]
      strokeOpacity: 0.6
      strokeWeight: @getLinkStrokeWeight()
    })
    @_publishGoogleEvents()
    @_createInfoWindow()

  # publish polyline google events
  _publishGoogleEvents: ->
    gme = google.maps.event
    @dblclckHandler = gme.addListener(@link, 'dblclick', (evt) => 
      @model.set_editor_show(true)
      evt.stop()
    )
    @clickHandler = gme.addListener(@link, 'click', =>  
      $a.broker.trigger('map:clear_selected') # this could also go in the model?
      @model.toggle_selected()
    )
  
  _unpublishGoogleEvents: ->
    gme = google.maps.event
    gme.removeListener(@zoomListener)
    gme.removeListener(@dblclckHandler)
    gme.removeListener(@clickHandler)
  
  # this is the rollover window for the link
  _createInfoWindow: ->
    google.maps.event.addListener(@link, 'mouseover', (e) =>
      if(not @_isInfoWindowOpen())
        @iWindow.setContent(@getLinkRollOverInfo())
        @iWindow.setPosition(e.latLng)
        trackMouseoverTimeout = setTimeout((() => @iWindow.open($a.map)), 1000)
        google.maps.event.addListenerOnce(@link, 'mouseout', (e) =>
          window.clearTimeout(trackMouseoverTimeout);
          @iWindow.close()
        );
    );
  
  _isInfoWindowOpen : -><commit_msg>Add: Link editor opens when InfoWindow is displaying<commit_after># Creates the links of the network. A link consists of a
# Polyline drawn between two steps in the route as well as
# an arrow head pointing in the appropriate direction if the
# Polyline is sufficiently long.
class window.beats.MapLinkView extends Backbone.View
  @LINK_COLOR: 'blue'
  @SELECTED_LINK_COLOR: 'red'

  @STROKE_WEIGHT_THIN: 2
  @STROKE_WEIGHT_THINNER: 1

  $a = window.beats
  model_events : {
    'remove': 'removeLink'
    'change:selected': 'toggleSelected'
    'change:lane_offset': 'drawLink'
    'change:lanes': 'setStrokeWeight'
    'change:hide': 'hideShowLink'
    'change:editor_show': 'editor'
    'change:show_demands': 'viewDemands'
  }
  
  broker_events: {
    'map:init': 'render'
  }

  initialize: (@model) ->
    @model.view = @
    @iWindow = new google.maps.InfoWindow()
    # Gets the encoded path line string if it is not already been set
    if(@model.position()? and @model.position().length > 0)
      @_createEncodedPath @model.position()
      @_saveEncodedPath()
    @drawLink()
    # only calculates and then updates link length if it not already set
    if !model.length()?
      @_saveLinkLength()
    @_contextMenu()
    @model.poly = @link
    @_publishEvents()
  
  render: ->
    @link.setMap($a.map) if @link?
    @

  # this method reads the path of MO points converting them into Google Points
  # and then encodes the using googles
  # geomtry package in order to save the path to models shape field
  _createEncodedPath: (pts) ->
    gPath = $a.Util.convertPointsToGoogleLatLng(pts)
    @encodedPath = google.maps.geometry.encoding.encodePath gPath

  # save the encoded path to the model
  _saveEncodedPath: ->
    @model.set_geometry @encodedPath
  
  # Creates the Polyline to rendered on the map
  # The Polyline map attribute is set immediately so the user can see that the
  # lines are being drawn
  # We set listeners up in drawLink because they need to be re-attached anytime
  # the link is re-drawn
  drawLink: ->
    @hideLink() if @link? #if you are applying offset to existing line
    linkGeom = @model.geometry()
    enc = google.maps.geometry.encoding
    pathVertices = @applyOffset(enc.decodePath(linkGeom))
    @link = new google.maps.Polyline({
      path: pathVertices
      map: $a.map
      strokeColor: @_getStrokeColor()
      icons: [{
          icon: { path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW }
          fillColor: 'blue'
          offset: '60%'
        }]
      strokeOpacity: 0.6
      strokeWeight: @getLinkStrokeWeight()
    })
    @_publishGoogleEvents()
    @_createInfoWindow()

  # publish polyline google events
  _publishGoogleEvents: ->
    gme = google.maps.event
    @dblclckHandler = gme.addListener(@link, 'dblclick', (evt) =>
      @model.set_editor_show(true)
      evt.stop()
    )
    @clickHandler = gme.addListener(@link, 'click', =>  
      $a.broker.trigger('map:clear_selected') # this could also go in the model?
      @model.toggle_selected()
    )
  
  _unpublishGoogleEvents: ->
    gme = google.maps.event
    gme.removeListener(@zoomListener)
    gme.removeListener(@dblclckHandler)
    gme.removeListener(@clickHandler)
  
  # this is the rollover window for the link
  _createInfoWindow: ->
    gme = google.maps.event
    @mOHandler = gme.addListener(@link, 'mouseover', (e) =>
      if(not @_isInfoWindowOpen())
        @iWindow.setContent(@getLinkRollOverInfo())
        l = e.latLng
        @iWindow.setPosition(new google.maps.LatLng(l.lat(),l.lng()+0.0001))
        trackMouseoverTimeout = setTimeout(( => @iWindow.open($a.map)), 1000)
        gme.addListenerOnce(@link, 'mouseout', (e) =>
          window.clearTimeout(trackMouseoverTimeout);
          @iWindow.close()
        );
    );
  
  _isInfoWindowOpen : ->
{'fraction_non_alphanumeric': 0.11034299388460515, 'fraction_numerical': 0.0035894708854028183, 'mean_word_length': 3.2004466778336127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '243559', 'n_tokens_mistral': 2413, 'n_tokens_neox': 2373, 'n_words': 629}