]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/new_note.js
6cb8db215efdf8879b93f8a8505731fbd0927131
[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   function addNewNoteMarker(latlng) {
81     if (newNoteMarker) map.removeLayer(newNoteMarker);
82
83     newNoteMarker = L.marker(latlng, {
84       icon: noteIcons.new,
85       opacity: 0.9,
86       draggable: true
87     });
88
89     newNoteMarker.on("dragstart dragend", function (a) {
90       removeHalo();
91       if (a.type === "dragend") {
92         addHalo(newNoteMarker.getLatLng());
93       }
94     });
95
96     newNoteMarker.addTo(map);
97     addHalo(newNoteMarker.getLatLng());
98
99     newNoteMarker.on("dragend", function () {
100       content.find("textarea").focus();
101     });
102   }
103
104   function removeNewNoteMarker() {
105     removeHalo();
106     if (newNoteMarker) map.removeLayer(newNoteMarker);
107     newNoteMarker = null;
108   }
109
110   page.pushstate = page.popstate = function (path) {
111     OSM.loadSidebarContent(path, function () {
112       page.load(path);
113     });
114   };
115
116   page.load = function (path) {
117     if (addNoteButton.hasClass("disabled")) return;
118     if (addNoteButton.hasClass("active")) return;
119
120     addNoteButton.addClass("active");
121
122     map.addLayer(noteLayer);
123
124     var params = Qs.parse(path.substring(path.indexOf("?") + 1));
125     var markerLatlng;
126
127     if (params.lat && params.lon) {
128       markerLatlng = L.latLng(params.lat, params.lon);
129     } else {
130       markerLatlng = map.getCenter();
131     }
132
133     map.panInside(markerLatlng, {
134       padding: [50, 50]
135     });
136
137     addNewNoteMarker(markerLatlng);
138
139     content.find("textarea")
140       .on("input", disableWhenBlank)
141       .focus();
142
143     function disableWhenBlank(e) {
144       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
145     }
146
147     content.find("input[type=submit]").on("click", function (e) {
148       const location = newNoteMarker.getLatLng().wrap();
149       const text = content.find("textarea").val();
150
151       e.preventDefault();
152       $(this).prop("disabled", true);
153       newNoteMarker.options.draggable = false;
154       newNoteMarker.dragging.disable();
155
156       createNote(location, text, (feature) => {
157         content.find("textarea").val("");
158         addCreatedNoteMarker(feature);
159         OSM.router.route("/note/" + feature.properties.id);
160       });
161     });
162
163     return map.getState();
164   };
165
166   page.unload = function () {
167     removeNewNoteMarker();
168     addNoteButton.removeClass("active");
169   };
170
171   return page;
172 };