]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/new_note.js
712d03dc50f780a6f2f740dc51e3ac9a94ba68d1
[rails.git] / app / assets / javascripts / index / new_note.js
1 //= require qs/dist/qs
2
3 OSM.NewNote = function (map) {
4   var noteLayer = map.noteLayer,
5       content = $("#sidebar_content"),
6       page = {},
7       addNoteButton = $(".control-note .control-button"),
8       newNoteMarker,
9       halo;
10
11   var noteIcons = {
12     "new": L.icon({
13       iconUrl: OSM.NEW_NOTE_MARKER,
14       iconSize: [25, 40],
15       iconAnchor: [12, 40]
16     }),
17     "open": L.icon({
18       iconUrl: OSM.OPEN_NOTE_MARKER,
19       iconSize: [25, 40],
20       iconAnchor: [12, 40]
21     }),
22     "closed": L.icon({
23       iconUrl: OSM.CLOSED_NOTE_MARKER,
24       iconSize: [25, 40],
25       iconAnchor: [12, 40]
26     })
27   };
28
29   addNoteButton.on("click", function (e) {
30     e.preventDefault();
31     e.stopPropagation();
32
33     if ($(this).hasClass("disabled")) return;
34
35     OSM.router.route("/note/new");
36   });
37
38   function createNote(location, text, callback) {
39     $.ajax({
40       url: "/api/0.6/notes.json",
41       type: "POST",
42       oauth: true,
43       data: {
44         lat: location.lat,
45         lon: location.lng,
46         text
47       },
48       success: callback
49     });
50   }
51
52   function addCreatedNoteMarker(feature) {
53     var marker = L.marker(feature.geometry.coordinates.reverse(), {
54       icon: noteIcons[feature.properties.status],
55       opacity: 0.9,
56       interactive: true
57     });
58     marker.id = feature.properties.id;
59     marker.addTo(noteLayer);
60   }
61
62   function addHalo(latlng) {
63     if (halo) map.removeLayer(halo);
64
65     halo = L.circleMarker(latlng, {
66       weight: 2.5,
67       radius: 20,
68       fillOpacity: 0.5,
69       color: "#FF6200"
70     });
71
72     map.addLayer(halo);
73   }
74
75   function removeHalo() {
76     if (halo) map.removeLayer(halo);
77     halo = null;
78   }
79
80   page.pushstate = page.popstate = function (path) {
81     OSM.loadSidebarContent(path, function () {
82       page.load(path);
83     });
84   };
85
86   page.load = function (path) {
87     if (addNoteButton.hasClass("disabled")) return;
88     if (addNoteButton.hasClass("active")) return;
89
90     addNoteButton.addClass("active");
91
92     map.addLayer(noteLayer);
93
94     var params = Qs.parse(path.substring(path.indexOf("?") + 1));
95     var markerLatlng;
96
97     if (params.lat && params.lon) {
98       markerLatlng = L.latLng(params.lat, params.lon);
99     } else {
100       markerLatlng = map.getCenter();
101     }
102
103     map.panInside(markerLatlng, {
104       padding: [50, 50]
105     });
106
107     newNoteMarker = L.marker(markerLatlng, {
108       icon: noteIcons.new,
109       opacity: 0.9,
110       draggable: true
111     });
112
113     newNoteMarker.on("dragstart dragend", function (a) {
114       removeHalo();
115       if (a.type !== "dragstart") {
116         addHalo(newNoteMarker.getLatLng());
117       }
118     });
119
120     newNoteMarker.addTo(map);
121     addHalo(newNoteMarker.getLatLng());
122
123     newNoteMarker.on("remove", function () {
124       addNoteButton.removeClass("active");
125     }).on("dragend", function () {
126       content.find("textarea").focus();
127     });
128
129     content.find("textarea")
130       .on("input", disableWhenBlank)
131       .focus();
132
133     function disableWhenBlank(e) {
134       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
135     }
136
137     content.find("input[type=submit]").on("click", function (e) {
138       const location = newNoteMarker.getLatLng().wrap();
139       const text = content.find("textarea").val();
140
141       e.preventDefault();
142       $(this).prop("disabled", true);
143       newNoteMarker.options.draggable = false;
144       newNoteMarker.dragging.disable();
145
146       createNote(location, text, (feature) => {
147         content.find("textarea").val("");
148         addCreatedNoteMarker(feature);
149         map.removeLayer(newNoteMarker);
150         newNoteMarker = null;
151         addNoteButton.removeClass("active");
152         OSM.router.route("/note/" + feature.properties.id);
153       });
154     });
155
156     return map.getState();
157   };
158
159   page.unload = function () {
160     if (newNoteMarker) map.removeLayer(newNoteMarker);
161     removeHalo();
162     addNoteButton.removeClass("active");
163   };
164
165   return page;
166 };