aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/route_section.js.coffee
blob: f2e494c0ce2ac4d301ff4ae91488b33f57f2c299 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class @RouteSectionMap
  @onSelectedFeature: (feature) ->
    route_section_id = feature.data.id

    routeSectionOption = $("option[value=#{route_section_id}]")
    routeSectionOption.parent().val route_section_id

    $('#map-selection').show()
    $('#empty-map-selection').hide()

    selectionUrl = location.pathname.replace /edit$/, "selection"
    $.ajax(url: selectionUrl, method: 'POST', data: { route_section_id: route_section_id }, dataType: 'html').done (data) ->
      $('#map-selection div').replaceWith(data)

  @onUnselectedFeature: (feature) ->
    $('#map-selection').hide()
    $('#empty-map-selection').show()

jQuery ->
  if $("#map.route_section").length > 0 and user_geometry?
    projWGS84 = new OpenLayers.Projection("EPSG:4326")
    proj900913 = new OpenLayers.Projection("EPSG:900913")
    wtk_format = new OpenLayers.Format.WKT()

    user_geometry.events.on({
      afterfeaturemodified: (event) ->
        wgs84_geometry = event.feature.geometry.transform(proj900913, projWGS84)
        wgs84_feature = new OpenLayers.Feature.Vector(wgs84_geometry)
        ewtk = "SRID=4326;#{wtk_format.write(wgs84_feature)}"

        $('#route_section_editable_geometry').val(ewtk)
        return
    })

  $('#new_route_sections_selector select').on 'change', ->
    new_route_section_id = $(this).val()

    edit_link = $(this).closest("tr").find("a.edit-route-section")

    # Save edit link to play with it
    unless edit_link.data("href-pattern")?
      edit_link.data "href-pattern", edit_link.attr('href').replace(new RegExp("/route_sections/([0-9]+)/edit"), "/route_sections/:id/edit")

    if !!new_route_section_id
      edit_link.removeClass "disabled"
      edit_link.attr 'href', edit_link.data("href-pattern").replace(/:id/, new_route_section_id)
    else
      edit_link.addClass "disabled"
      edit_link.attr 'href', '#'

  $('form.route_section').find('button[type="submit"]').on 'click', (e) ->
    e.preventDefault();
    if typeof modify_feature != 'undefined'
      modify_feature.deactivate()
    $('form.route_section').submit()
    return

  return