alphasusu/alphasusu

View on GitHub
app/assets/javascripts/places.js.coffee

Summary

Maintainability
Test Coverage
#= require leaflet

buildings = [
  {
    name: "SUSU Building 42",
    points: [
      [50.9345967,-1.3978175],
      [50.9343885,-1.3979678],
      [50.9343698,-1.3979024],
      [50.9343131,-1.3976974],
      [50.9343028,-1.3976593],
      [50.9342917,-1.3976179],
      [50.9342638,-1.3976345],
      [50.9341943,-1.3976822],
      [50.9341991,-1.3976985],
      [50.9341733,-1.3977217],
      [50.9342216,-1.397889],
      [50.9343217,-1.3978158],
      [50.9343508,-1.3979158],
      [50.9342614,-1.3979804],
      [50.9342489,-1.3979367],
      [50.9342441,-1.3979401],
      [50.9341279,-1.398024],
      [50.934116,-1.3979827],
      [50.9339285,-1.3981181],
      [50.9338592,-1.3978764],
      [50.934018,-1.3977617],
      [50.9340089,-1.3977297],
      [50.9339959,-1.3976843],
      [50.933893,-1.3977586],
      [50.9337733,-1.3973458],
      [50.9339185,-1.3972373],
      [50.9339515,-1.3972126],
      [50.9340006,-1.3973843],
      [50.9341138,-1.3973011],
      [50.9341541,-1.3972722],
      [50.9341653,-1.3972647],
      [50.9341902,-1.397248],
      [50.9341321,-1.3970431],
      [50.9342522,-1.3969589],
      [50.9343214,-1.3969075],
      [50.9343334,-1.3968995],
      [50.9343356,-1.3969078],
      [50.9343502,-1.3969629],
      [50.9343658,-1.3970219],
      [50.9344206,-1.397207],
      [50.9344589,-1.3973368],
      [50.9344179,-1.3973658],
      [50.9344281,-1.397407],
      [50.9344401,-1.3974475],
      [50.9344817,-1.3974175],
      [50.9344827,-1.3974211],
      [50.9344915,-1.3974545],
      [50.9345002,-1.3974837],
      [50.9345202,-1.3975513],
      [50.9345967,-1.3978175]
    ]
  },{
    name: "The Shop",
    points: [
      [50.9345267,-1.3968889],
      [50.9345187,-1.3968951],
      [50.9345016,-1.3969083],
      [50.9344921,-1.3969154],
      [50.9344517,-1.3969454],
      [50.9344304,-1.3969615],
      [50.9343933,-1.3968334],
      [50.934311,-1.3965497],
      [50.9343734,-1.3965049],
      [50.9343993,-1.3964863],
      [50.9344107,-1.3964781],
      [50.934416,-1.3964968],
      [50.9345267,-1.3968889]
    ]
  },{
    name: "SUSU Building 40",
    points: [
      [50.9348699,-1.3975707],
      [50.9345783,-1.3974006],
      [50.9345911,-1.3973427],
      [50.9346257,-1.3971858],
      [50.9346499,-1.3970764],
      [50.934723,-1.3971239],
      [50.9347187,-1.3971428],
      [50.9347355,-1.3971541],
      [50.9347947,-1.3971912],
      [50.9348559,-1.3972276],
      [50.9348699,-1.3972353],
      [50.9348746,-1.3972157],
      [50.9349321,-1.397251],
      [50.93492,-1.3973133],
      [50.9349632,-1.3973343],
      [50.9351041,-1.3972276],
      [50.9351091,-1.397245],
      [50.9351392,-1.3973487],
      [50.9351597,-1.397336],
      [50.9351772,-1.397392],
      [50.9351564,-1.397408],
      [50.9351723,-1.3974626],
      [50.9351957,-1.3975425],
      [50.9352228,-1.397633],
      [50.9350896,-1.397735],
      [50.9350956,-1.3977537],
      [50.9351535,-1.3979556],
      [50.9350475,-1.3980329],
      [50.9350091,-1.3980616],
      [50.9349507,-1.3978558],
      [50.9348699,-1.3975707]
    ]
  }]
    
addBuildings = (buildings, map) ->
  $.each buildings, (index) ->
    map.addLayer(
      L.polygon(this.points, {
        smoothFactor: 0.0,
        color: '#333',
        opacity: 1.0,
        fillOpacity: 0.8
      })
    )

mappedMarkers = {}
addMarkers = (markers, map) ->
  $.each markers, ->
    mappedMarkers[this.id] = new L.Marker(
      [this["latlng"][0], this["latlng"][1]]
    ).addTo(map)

highlightMarker = (id, map) ->
  $.each mappedMarkers, (i, marker) ->
    map.removeLayer(marker)
  mappedMarkers[id].addTo(map)

unhighlightMarker = (id, map) ->
  map.removeLayer(mappedMarkers[id])
  $.each mappedMarkers, (i, marker) ->
    marker.addTo(map)

$ ->
  if window.map
    addBuildings buildings, window.map
    if window.markers
      addMarkers window.markers, window.map
    $('.place').hover ->
      highlightMarker $(this).data('id'), window.map
    , ->
      unhighlightMarker $(this).data('id'), window.map