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