]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/new_note.js
Merge remote-tracking branch 'openstreetmap/pull/1004'
[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       clickable: 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, page.load);
81   };
82
83   function newHalo(loc, a) {
84     if (a === 'dragstart' && map.hasLayer(halo)) {
85       map.removeLayer(halo);
86     } else {
87       if (map.hasLayer(halo)) map.removeLayer(halo);
88
89       halo = L.circleMarker(loc, {
90         weight: 2.5,
91         radius: 20,
92         fillOpacity: 0.5,
93         color: "#FF6200"
94       });
95
96       map.addLayer(halo);
97     }
98   }
99
100   page.load = function () {
101     if (addNoteButton.hasClass("disabled")) return;
102     if (addNoteButton.hasClass("active")) return;
103
104     addNoteButton.addClass("active");
105
106     map.addLayer(noteLayer);
107
108     var mapSize = map.getSize();
109     var markerPosition;
110
111     markerPosition = [mapSize.x / 2, mapSize.y / 2];
112
113     newNote = L.marker(map.containerPointToLatLng(markerPosition), {
114       icon: noteIcons["new"],
115       opacity: 0.9,
116       draggable: true
117     });
118
119     newNote.on("dragstart dragend", function(a) {
120       newHalo(newNote.getLatLng(), a.type);
121     });
122
123     newNote.addTo(noteLayer);
124     newHalo(newNote.getLatLng());
125
126     newNote.on("remove", function () {
127       addNoteButton.removeClass("active");
128     }).on("dragstart",function () {
129       $(newNote).stopTime("removenote");
130     }).on("dragend", function () {
131       content.find("textarea").focus();
132     });
133
134     content.find("textarea")
135       .on("input", disableWhenBlank)
136       .focus();
137
138     function disableWhenBlank(e) {
139       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
140     }
141
142     content.find('input[type=submit]').on('click', function (e) {
143       e.preventDefault();
144       createNote(newNote, e.target.form, '/api/0.6/notes.json');
145     });
146
147     return map.getState();
148   };
149
150   page.unload = function () {
151     noteLayer.removeLayer(newNote);
152     map.removeLayer(halo);
153     addNoteButton.removeClass("active");
154   };
155
156   return page;
157 };