1 //= require templates/notes/show
 
   2 //= require templates/notes/new
 
   4 $(document).ready(function () {
 
   5   var params = OSM.mapParams();
 
   9       iconUrl: "<%= image_path 'new_note_marker.png' %>",
 
  14       iconUrl: "<%= image_path 'open_note_marker.png' %>",
 
  19       iconUrl: "<%= image_path 'closed_note_marker.png' %>",
 
  25   var noteLayer = new L.LayerGroup();
 
  34   map.on("layeradd", function (e) {
 
  35     if (e.layer == noteLayer) {
 
  37       map.on("moveend", loadNotes);
 
  41   map.on("layerremove", function (e) {
 
  42     if (e.layer == noteLayer) {
 
  43       map.off("moveend", loadNotes);
 
  44       noteLayer.clearLayers();
 
  49   map.on("popupopen", function (e) {
 
  50     $(e.popup._container).find(".comment").focus();
 
  53   map.on("popupclose", function (e) {
 
  54     if (newNote && e.popup == newNote._popup) {
 
  55       $(newNote).oneTime(10, "removenote", function () {
 
  56         map.removeLayer(newNote);
 
  62   if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
 
  63     map.layersControl.addOverlay(noteLayer, I18n.t("browse.start_rjs.notes_layer_name"));
 
  65     if (params.layers) setMapLayers(params.layers);
 
  66     if (params.notes) map.addLayer(noteLayer);
 
  70         url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
 
  71         success: function (feature) {
 
  72           var marker = updateMarker(notes[feature.properties.id], feature);
 
  74           notes[feature.properties.id] = marker;
 
  76           map.addLayer(noteLayer);
 
  83   function updateMarker(marker, feature) {
 
  86       marker.setIcon(noteIcons[feature.properties.status]);
 
  87       marker._popup.setContent(createPopupContent(marker, feature.properties));
 
  91       marker = L.marker(feature.geometry.coordinates.reverse(), {
 
  92         icon: noteIcons[feature.properties.status],
 
  96       marker.addTo(noteLayer).bindPopup(
 
  97         createPopupContent(marker, feature.properties),
 
 107   function loadNotes() {
 
 108     var bounds = map.getBounds();
 
 109     var size = bounds.getSize();
 
 111     if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
 
 112       var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBOX();
 
 114       if (noteLoader) noteLoader.abort();
 
 116       noteLoader = $.ajax({
 
 118         success: function (json) {
 
 119           var oldNotes = notes;
 
 123           json.features.forEach(function (feature) {
 
 124             var marker = oldNotes[feature.properties.id];
 
 126             delete oldNotes[feature.properties.id];
 
 128             notes[feature.properties.id] = updateMarker(marker, feature);
 
 131           for (id in oldNotes) {
 
 132             noteLayer.removeLayer(oldNotes[id]);
 
 141   function popupOptions() {
 
 142     var mapSize = map.getSize();
 
 146       maxWidth: mapSize.y * 1 / 3,
 
 147       maxHeight: mapSize.y * 2 / 3,
 
 148       offset: new L.Point(0, -3),
 
 149       autoPanPadding: new L.Point(60, 40)
 
 153   function createPopupContent(marker, properties) {
 
 154     var content = $(JST["templates/notes/show"]({ note: properties }));
 
 156     content.find("textarea").on("input", function (e) {
 
 157       var form = e.target.form;
 
 159       if ($(e.target).val() == "") {
 
 160         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
 
 161         $(form.comment).prop("disabled", true);
 
 163         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
 
 164         $(form.comment).prop("disabled", false);
 
 168     content.find("input[type=submit]").on("click", function (e) {
 
 170       var data = $(e.target).data();
 
 171       updateNote(marker, e.target.form, data.method, data.url);
 
 177   function createNote(marker, form, url) {
 
 178     var location = marker.getLatLng();
 
 180     marker.options.draggable = false;
 
 181     marker.dragging.disable();
 
 183     $(form).find("input[type=submit]").prop("disabled", true);
 
 192         text: $(form.text).val()
 
 194       success: function (feature) {
 
 195         notes[feature.properties.id] = updateMarker(marker, feature);
 
 198         $("#createnoteanchor").removeClass("disabled").addClass("geolink");
 
 203   function updateNote(marker, form, method, url) {
 
 204     $(form).find("input[type=submit]").prop("disabled", true);
 
 211         text: $(form.text).val()
 
 213       success: function (feature) {
 
 214         if (feature.properties.status == "hidden") {
 
 215           noteLayer.removeLayer(marker);
 
 217           delete notes[feature.properties.id];
 
 219           var popupContent = createPopupContent(marker, feature.properties);
 
 221           marker.setIcon(noteIcons[feature.properties.status]);
 
 222           marker._popup.setContent(popupContent);
 
 228   $(".leaflet-control-attribution").on("click", "#createnoteanchor", function (e) {
 
 231     if ($(e.target).hasClass("disabled")) return;
 
 233     $(e.target).removeClass("geolink").addClass("disabled");
 
 235     map.addLayer(noteLayer);
 
 237     var mapSize = map.getSize();
 
 242       markerPosition = [mapSize.x / 2, mapSize.y / 2];
 
 244     else if (mapSize.y > 400)
 
 246       markerPosition = [mapSize.x / 2, 400];
 
 250       markerPosition = [mapSize.x / 2, mapSize.y];
 
 253     newNote = L.marker(map.containerPointToLatLng(markerPosition), {
 
 254       icon: noteIcons["new"],
 
 259     var popupContent = $(JST["templates/notes/new"]({ create_url: $(e.target).attr("href") }));
 
 261     popupContent.find("textarea").on("input", function (e) {
 
 262       var form = e.target.form;
 
 264       if ($(e.target).val() == "") {
 
 265         $(form.add).prop("disabled", true);
 
 267         $(form.add).prop("disabled", false);
 
 271     popupContent.find("input[type=submit]").on("click", function (e) {
 
 273       createNote(newNote, e.target.form, $(e.target).data("url"));
 
 276     newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
 
 278     newNote.on("remove", function (e) {
 
 279       $("#createnoteanchor").removeClass("disabled").addClass("geolink");
 
 282     newNote.on("dragstart", function (e) {
 
 283       $(newNote).stopTime("removenote");
 
 286     newNote.on("dragend", function (e) {
 
 287       e.target.openPopup();