1 //= require templates/notes/show
 
   2 //= require templates/notes/new
 
   4 function initializeNotes(map, params) {
 
   5   var noteLayer = map.noteLayer,
 
  11       iconUrl: "<%= image_path 'new_note_marker.png' %>",
 
  16       iconUrl: "<%= image_path 'open_note_marker.png' %>",
 
  21       iconUrl: "<%= image_path 'closed_note_marker.png' %>",
 
  27   map.on("layeradd", function (e) {
 
  28     if (e.layer == noteLayer) {
 
  30       map.on("moveend", loadNotes);
 
  32   }).on("layerremove", function (e) {
 
  33     if (e.layer == noteLayer) {
 
  34       map.off("moveend", loadNotes);
 
  35       noteLayer.clearLayers();
 
  38   }).on("popupclose", function (e) {
 
  39     if (newNote && e.popup == newNote._popup) {
 
  40       $(newNote).oneTime(10, "removenote", function () {
 
  41         map.removeLayer(newNote);
 
  45   }).on("popupopen", function (e) {
 
  46     if (!('ontouchstart' in document.documentElement)) {
 
  47       $(e.popup._container).find(".comment").focus();
 
  51   if (OSM.STATUS != 'api_offline' && OSM.STATUS != 'database_offline') {
 
  54         url: "/api/" + OSM.API_VERSION + "/notes/" + params.note + ".json",
 
  55         success: function (feature) {
 
  56           var marker = updateMarker(notes[feature.properties.id], feature);
 
  57           notes[feature.properties.id] = marker;
 
  58           map.addLayer(noteLayer);
 
  65   function updateMarker(marker, feature) {
 
  67       marker.setIcon(noteIcons[feature.properties.status]);
 
  68       marker.setPopupContent(createPopupContent(
 
  69         marker, feature.properties,
 
  70         $(marker._popup._content).find("textarea").val()
 
  73       marker = L.marker(feature.geometry.coordinates.reverse(), {
 
  74         icon: noteIcons[feature.properties.status],
 
  77       marker.addTo(noteLayer).bindPopup(
 
  78         createPopupContent(marker, feature.properties),
 
  87   function loadNotes() {
 
  88     var bounds = map.getBounds();
 
  89     var size = bounds.getSize();
 
  91     if (size <= OSM.MAX_NOTE_REQUEST_AREA) {
 
  92       var url = "/api/" + OSM.API_VERSION + "/notes.json?bbox=" + bounds.toBBoxString();
 
  94       if (noteLoader) noteLoader.abort();
 
 102     function success(json) {
 
 103       var oldNotes = notes;
 
 105       json.features.forEach(updateMarkers);
 
 107       function updateMarkers(feature) {
 
 108         var marker = oldNotes[feature.properties.id];
 
 109         delete oldNotes[feature.properties.id];
 
 110         notes[feature.properties.id] = updateMarker(marker, feature);
 
 113       for (id in oldNotes) {
 
 114         noteLayer.removeLayer(oldNotes[id]);
 
 121   function popupOptions() {
 
 122     var mapSize = map.getSize();
 
 126       maxWidth: mapSize.y * 1 / 3,
 
 127       maxHeight: mapSize.y * 2 / 3,
 
 128       offset: new L.Point(0, -40),
 
 129       autoPanPadding: new L.Point(60, 40)
 
 133   function createPopupContent(marker, properties, comment) {
 
 134     var content = $(JST["templates/notes/show"]({ note: properties }));
 
 136     content.find("textarea").on("input", function (e) {
 
 137       var form = e.target.form;
 
 139       if ($(e.target).val() == "") {
 
 140         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
 
 141         $(form.comment).prop("disabled", true);
 
 143         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
 
 144         $(form.comment).prop("disabled", false);
 
 148     content.find("input[type=submit]").on("click", function (e) {
 
 150       var data = $(e.target).data();
 
 151       updateNote(marker, e.target.form, data.method, data.url);
 
 155       content.find("textarea").val(comment).trigger("input");
 
 161   var addNoteButton = $(".control-note .control-button");
 
 163   function createNote(marker, form, url) {
 
 164     var location = marker.getLatLng();
 
 166     marker.options.draggable = false;
 
 167     marker.dragging.disable();
 
 169     $(form).find("input[type=submit]").prop("disabled", true);
 
 178         text: $(form.text).val()
 
 183     function noteCreated(feature) {
 
 184       $(marker._popup._content).find("textarea").val("");
 
 186       notes[feature.properties.id] = updateMarker(marker, feature);
 
 189       addNoteButton.removeClass("active");
 
 193   function updateNote(marker, form, method, url) {
 
 194     $(form).find("input[type=submit]").prop("disabled", true);
 
 201         text: $(form.text).val()
 
 203       success: function (feature) {
 
 204         if (feature.properties.status == "hidden") {
 
 205           noteLayer.removeLayer(marker);
 
 207           delete notes[feature.properties.id];
 
 209           var popupContent = createPopupContent(marker, feature.properties);
 
 211           marker.setIcon(noteIcons[feature.properties.status]);
 
 212           marker.setPopupContent(popupContent);
 
 218   addNoteButton.on("click", function (e) {
 
 222     if (addNoteButton.hasClass("disabled")) return;
 
 223     if (addNoteButton.hasClass("active")) return;
 
 225     addNoteButton.addClass("active");
 
 227     map.addLayer(noteLayer);
 
 229     var mapSize = map.getSize();
 
 232     if (mapSize.y > 800) {
 
 233       markerPosition = [mapSize.x / 2, mapSize.y / 2];
 
 234     } else if (mapSize.y > 400) {
 
 235       markerPosition = [mapSize.x / 2, 400];
 
 237       markerPosition = [mapSize.x / 2, mapSize.y];
 
 240     newNote = L.marker(map.containerPointToLatLng(markerPosition), {
 
 241       icon: noteIcons["new"],
 
 246     var popupContent = $(JST["templates/notes/new"]());
 
 248     popupContent.find("textarea").on("input", disableWhenBlank);
 
 250     function disableWhenBlank(e) {
 
 251       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
 
 254     popupContent.find("input[type=submit]").on("click", function (e) {
 
 256       createNote(newNote, e.target.form, '/api/0.6/notes.json');
 
 259     newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
 
 261     newNote.on("remove", function (e) {
 
 262       addNoteButton.removeClass("active");
 
 263     }).on("dragstart", function (e) {
 
 264       $(newNote).stopTime("removenote");
 
 265     }).on("dragend", function (e) {
 
 266       e.target.openPopup();