]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/new_note.js.erb
9990da7836c366830edb216b614a2a5e5ebf6634
[rails.git] / app / assets / javascripts / index / new_note.js.erb
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: "<%= image_path 'new_note_marker.png' %>",
12       iconSize: [25, 40],
13       iconAnchor: [12, 40]
14     }),
15     "open": L.icon({
16       iconUrl: "<%= image_path 'open_note_marker.png' %>",
17       iconSize: [25, 40],
18       iconAnchor: [12, 40]
19     }),
20     "closed": L.icon({
21       iconUrl: "<%= image_path 'closed_note_marker.png' %>",
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     if (mapSize.y > 800) {
112       markerPosition = [mapSize.x / 2, mapSize.y / 2];
113     } else if (mapSize.y > 400) {
114       markerPosition = [mapSize.x / 2, 400];
115     } else {
116       markerPosition = [mapSize.x / 2, mapSize.y];
117     }
118
119     newNote = L.marker(map.containerPointToLatLng(markerPosition), {
120       icon: noteIcons["new"],
121       opacity: 0.9,
122       draggable: true
123     });
124
125     newNote.on("dragstart dragend", function(a) {
126       newHalo(newNote.getLatLng(), a.type);
127     });
128
129     newNote.addTo(noteLayer);
130     newHalo(newNote.getLatLng());
131
132     newNote.on("remove", function () {
133       addNoteButton.removeClass("active");
134     }).on("dragstart",function () {
135       $(newNote).stopTime("removenote");
136     }).on("dragend", function () {
137       content.find("textarea").focus();
138     });
139
140     content.find("textarea")
141       .on("input", disableWhenBlank)
142       .focus();
143
144     function disableWhenBlank(e) {
145       $(e.target.form.add).prop("disabled", $(e.target).val() === "");
146     }
147
148     content.find('input[type=submit]').on('click', function (e) {
149       e.preventDefault();
150       createNote(newNote, e.target.form, '/api/0.6/notes.json');
151     });
152
153     return map.getState();
154   };
155
156   page.unload = function () {
157     noteLayer.removeLayer(newNote);
158     map.removeLayer(halo);
159     addNoteButton.removeClass("active");
160   };
161
162   return page;
163 };