<commit_before>ns = @edsc.map.L

ns.ShapefileLayer = do (L, Dropzone, config=@edsc.config, help=@edsc.help) ->

  MAX_POLYGON_SIZE = config.maxPolygonSize

  originalPropagate = L.FeatureGroup.prototype._propagateEvent
  L.FeatureGroup.prototype._propagateEvent = (e) ->
    e.chain = e.chain ? []
    e.chain.push e.target
    originalPropagate.call(this, e)

  Dropzone.autoDiscover = false

  dropzoneOptions =
    # Official Ogre web service
    # We likely want to use this, once they fix OPTIONS requests
    # See: https://github.com/wavded/ogre/pull/22
    #url: 'http://ogre.adc4gis.com/convert'

    # Ogre running locally on port 3001
    #url: 'http://localhost:3001/convert'

    # Ogre proxied through Rails
    url: '/convert'
    headers: {'X-CSRF-Token': config.csrfToken}

    # Test fallback behavior
    #forceFallback: true

    # Common options
    paramName: 'upload'
    clickable: '.geojson-dropzone-link'
    createImageThumbnails: false
    fallback: -> # If the browser can't support the necessary features
      $('.select-shapefile').parent().hide()
    parallelUploads: 1
    uploadMultiple: false
    addRemoveLinks: true

  class ShapefileLayer
    defaultOptions:
      selection: L.Draw.Polygon.prototype.options.shapeOptions

    constructor: (options={}) ->
      @options =
        selection: L.extend({}, @defaultOptions.selection, options.selection)

    onAdd: (map) ->
      @map = map
      container = map.getContainer()
      dropzone = new Dropzone(container, dropzoneOptions)
      dropzone.on 'success', @_geoJsonResponse
      dropzone.on 'removedfile', @_removeFile
      @_dropzone = dropzone
      L.DomUtil.addClass(container, 'dropzone')

    onRemove: (map) ->
      @remove()
      @map = null
      @_jsonLayer = null

    activate: (showHelp=true) ->
      @_isActive = true
      @show()
      @showHelp() if showHelp
      $('#datasets-overlay').masterOverlay('hide')

    deactivate: ->
      @_isActive = false
      @hide()
      @hideHelp()

    showHelp: ->
      L.DomUtil.addClass(@map.getContainer(), 'is-geojson-help-visible')

    hideHelp: ->
      L.DomUtil.removeClass(@map.getContainer(), 'is-geojson-help-visible')

    isActive: ->
      @_isActive

    hide: ->
      $('.dz-preview').hide()
      @map.removeLayer(@_jsonLayer) if @_jsonLayer?

    show: ->
      $('.dz-preview').show()
      @map.addLayer(@_jsonLayer) if @_jsonLayer?

    remove: ->
      @_dropzone.removeFile(@_file) if @_file?

    _removeFile: =>
      @map.removeLayer(@_jsonLayer) if @_jsonLayer?
      @_file = null

    _geoJsonResponse: (file, response) =>
      @activate(false) unless @isActive()
      @hideHelp()
      @remove()

      icon = new L.Icon.Default()
      icon.options.className = 'geojson-icon'
      jsonLayer = new L.geoJson response,
        className: 'geojson-svg'
        onEachFeature: (feature, featureLayer) ->
          addIconClasses = (layer) ->
            if layer.options?.icon?
              layer.options?.icon = icon

          addIconClasses(featureLayer)
          if featureLayer.getLayers?
            for layer in featureLayer.getLayers()
              addIconClasses(layer)

      jsonLayer.on 'click', @_clickLayer

      @_file = file
      @_jsonLayer = jsonLayer

      @map.addLayer(jsonLayer)
      @map.fitBounds(jsonLayer.getBounds())

      children = jsonLayer.getLayers()

      tourSteps = []
      if children.length > 1
        middleChild = children[Math.floor(children.length / 2)]
        el = middleChild._container ? middleChild._icon
        help.add('shapefile_multiple', element: el)
      else if children.length == 1
        @_setConstraint(children[0])

    _clickLayer: (e) =>
      @_setConstraint(e.chain[0])

    _setConstraint: (sourceLayer) ->
      help.next() if help.current()?.key == 'shapefile_multiple'

      if sourceLayer.getLatLngs?
        # Polygon
        originalLatLngs = sourceLayer.getLatLngs()
        latlngs = @_simplifyPoints(originalLatLngs)

        if latlngs.length != originalLatLngs.length
          help.add('shapefile_reduction', element: '.leaflet-draw-edit-edit')

        layer = L.sphericalPolygon(latlngs, @options.selection)
        layerType = 'polygon'

      else if sourceLayer.getLatLng?
        # Point
        layer = L.marker(sourceLayer.getLatLng())
        layerType = 'marker'

      if layer?
        map = @map
        map.fire 'draw:drawstart'
        map.fire 'draw:created',
          target: map
          layer: layer
          layerType: layerType

    _simplifyPoints: (latlngs) ->
      if latlngs.length > MAX_POLYGON_SIZE

        map = @map
        points = (map.latLngToLayerPoint(latlng) for latlng in latlngs)
        tolerance = 1<commit_msg>EDSC-263: Avoid conflicts between shapefile selection and granule selection.  Granule selection pauses while shapefile selection is available and resumes afterward.<commit_after>ns = @edsc.map.L

ns.ShapefileLayer = do (L, Dropzone, config=@edsc.config, help=@edsc.help) ->

  MAX_POLYGON_SIZE = config.maxPolygonSize

  originalPropagate = L.FeatureGroup.prototype._propagateEvent
  L.FeatureGroup.prototype._propagateEvent = (e) ->
    e.chain = e.chain ? []
    e.chain.push e.target
    originalPropagate.call(this, e)

  Dropzone.autoDiscover = false

  dropzoneOptions =
    # Official Ogre web service
    # We likely want to use this, once they fix OPTIONS requests
    # See: https://github.com/wavded/ogre/pull/22
    #url: 'http://ogre.adc4gis.com/convert'

    # Ogre running locally on port 3001
    #url: 'http://localhost:3001/convert'

    # Ogre proxied through Rails
    url: '/convert'
    headers: {'X-CSRF-Token': config.csrfToken}

    # Test fallback behavior
    #forceFallback: true

    # Common options
    paramName: 'upload'
    clickable: '.geojson-dropzone-link'
    createImageThumbnails: false
    fallback: -> # If the browser can't support the necessary features
      $('.select-shapefile').parent().hide()
    parallelUploads: 1
    uploadMultiple: false
    addRemoveLinks: true

  class ShapefileLayer
    defaultOptions:
      selection: L.Draw.Polygon.prototype.options.shapeOptions

    constructor: (options={}) ->
      @options =
        selection: L.extend({}, @defaultOptions.selection, options.selection)

    onAdd: (map) ->
      @map = map
      container = map.getContainer()
      dropzone = new Dropzone(container, dropzoneOptions)
      dropzone.on 'success', @_geoJsonResponse
      dropzone.on 'removedfile', @_removeFile
      @_dropzone = dropzone
      L.DomUtil.addClass(container, 'dropzone')

    onRemove: (map) ->
      @remove()
      @map = null
      @_jsonLayer = null

    activate: (showHelp=true) ->
      @_isActive = true
      @show()
      @showHelp() if showHelp
      $('#datasets-overlay').masterOverlay('hide')

    deactivate: ->
      @_isActive = false
      @hide()
      @hideHelp()

    showHelp: ->
      L.DomUtil.addClass(@map.getContainer(), 'is-geojson-help-visible')

    hideHelp: ->
      L.DomUtil.removeClass(@map.getContainer(), 'is-geojson-help-visible')

    isActive: ->
      @_isActive

    hide: ->
      $('.dz-preview').hide()
      @map.removeLayer(@_jsonLayer) if @_jsonLayer?
      @map.fire 'draw:drawstop'

    show: ->
      $('.dz-preview').show()
      @map.addLayer(@_jsonLayer) if @_jsonLayer?
      @map.fire 'draw:drawstart'

    remove: ->
      @_dropzone.removeFile(@_file) if @_file?

    _removeFile: =>
      @map.removeLayer(@_jsonLayer) if @_jsonLayer?
      @map.fire 'draw:drawstop'
      @_file = null

    _geoJsonResponse: (file, response) =>
      @activate(false) unless @isActive()
      @hideHelp()
      @remove()

      icon = new L.Icon.Default()
      icon.options.className = 'geojson-icon'
      jsonLayer = new L.geoJson response,
        className: 'geojson-svg'
        onEachFeature: (feature, featureLayer) ->
          addIconClasses = (layer) ->
            if layer.options?.icon?
              layer.options?.icon = icon

          addIconClasses(featureLayer)
          if featureLayer.getLayers?
            for layer in featureLayer.getLayers()
              addIconClasses(layer)

      jsonLayer.on 'click', @_clickLayer

      @_file = file
      @_jsonLayer = jsonLayer

      @map.addLayer(jsonLayer)
      @map.fire 'draw:drawstart'
      @map.fitBounds(jsonLayer.getBounds())

      children = jsonLayer.getLayers()

      tourSteps = []
      if children.length > 1
        middleChild = children[Math.floor(children.length / 2)]
        el = middleChild._container ? middleChild._icon
        help.add('shapefile_multiple', element: el)
      else if children.length == 1
        @_setConstraint(children[0])

    _clickLayer: (e) =>
      @_setConstraint(e.chain[0])

    _setConstraint: (sourceLayer) ->
      help.next() if help.current()?.key == 'shapefile_multiple'

      if sourceLayer.getLatLngs?
        # Polygon
        originalLatLngs = sourceLayer.getLatLngs()
        latlngs = @_simplifyPoints(originalLatLngs)

        if latlngs.length != originalLatLngs.length
          help.add('shapefile_reduction', element: '.leaflet-draw-edit-edit')

        layer = L.sphericalPolygon(latlngs, @options.selection)
        layerType = 'polygon'

      else if sourceLayer.getLatLng?
        # Point
        layer = L.marker(sourceLayer.getLatLng())
        layerType = 'marker'

      if layer?
        map = @map
        map.fire 'draw:created',
          target: map
          layer: layer
          layerType: layerType

    _simplifyPoints: (latlngs) ->
      if latlngs.length > MAX_POLYGON_SIZE

        map = @map
        points = (map.latLngToLayerPoint(latlng) for latlng in latlngs)
        tolerance = 1
{'fraction_non_alphanumeric': 0.12040753318925594, 'fraction_numerical': 0.004013584439641865, 'mean_word_length': 2.5714810731348767, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 49, 'https://': 2, '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': '3023056', 'n_tokens_mistral': 3155, 'n_tokens_neox': 3011, 'n_words': 620}