]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js.erb
8c2ab5c5671f8d629e9a083eb3212f0e16c335e0
[rails.git] / app / assets / javascripts / index / note.js.erb
1 OSM.Note = function (map) {
2   var noteLayer = map.noteLayer,
3     content = $('#sidebar_content'),
4     page = {},
5     halo, currentNote;
6
7   var noteIcons = {
8     "new": L.icon({
9       iconUrl: "<%= image_path('new_note_marker.png') %>",
10       iconSize: [25, 40],
11       iconAnchor: [12, 40]
12     }),
13     "open": L.icon({
14       iconUrl: "<%= image_path('open_note_marker.png') %>",
15       iconSize: [25, 40],
16       iconAnchor: [12, 40]
17     }),
18     "closed": L.icon({
19       iconUrl: "<%= image_path('closed_note_marker.png') %>",
20       iconSize: [25, 40],
21       iconAnchor: [12, 40]
22     })
23   };
24
25   function updateNote(form, method, url) {
26     $(form).find("input[type=submit]").prop("disabled", true);
27
28     $.ajax({
29       url: url,
30       type: method,
31       oauth: true,
32       data: {text: $(form.text).val()},
33       success: function () {
34         OSM.loadSidebarContent(window.location.pathname, page.load);
35       }
36     });
37   }
38
39   page.pushstate = page.popstate = function (path) {
40     OSM.loadSidebarContent(path, page.load);
41   };
42
43   page.load = function () {
44     content.find("input[type=submit]").on("click", function (e) {
45       e.preventDefault();
46       var data = $(e.target).data();
47       updateNote(e.target.form, data.method, data.url);
48     });
49
50     content.find("textarea").on("input", function (e) {
51       var form = e.target.form;
52
53       if ($(e.target).val() == "") {
54         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
55         $(form.comment).prop("disabled", true);
56       } else {
57         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
58         $(form.comment).prop("disabled", false);
59       }
60     });
61
62     content.find("textarea").val('').trigger("input");
63
64     var data = $('.details').data();
65     if (!window.location.hash) {
66       var coords = data.coordinates.split(',');
67       OSM.route.moveListenerOff();
68       map.once('moveend', OSM.route.moveListenerOn);
69       map.getZoom() > 15 ? map.panTo(coords) : map.setView(coords, 16);
70     }
71
72     if (!map.hasLayer(halo)) {
73       halo = L.circleMarker(data.coordinates.split(','), {
74         weight: 2.5,
75         radius: 20,
76         fillOpacity: 0.5,
77         color: "#FF6200"
78       });
79       map.addLayer(halo);
80     }
81
82     if (map.hasLayer(currentNote)) map.removeLayer(currentNote);
83     currentNote = L.marker(data.coordinates.split(','), {
84       icon: noteIcons[data.status],
85       opacity: 1,
86       clickable: true
87     });
88     map.addLayer(currentNote);
89   };
90
91   page.unload = function () {
92     if (map.hasLayer(halo)) map.removeLayer(halo);
93     if (map.hasLayer(currentNote)) map.removeLayer(currentNote);
94   };
95
96   return page;
97 };