]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js.erb
aa746265cb409ed595228b97cc040bb88c3f5577
[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       latLng = data.coordinates.split(',');
66
67     if (!window.location.hash) {
68       OSM.router.moveListenerOff();
69       map.once('moveend', OSM.router.moveListenerOn);
70       map.getZoom() > 15 ? map.panTo(latLng) : map.setView(latLng, 16);
71     }
72
73     if (!map.hasLayer(halo)) {
74       halo = L.circleMarker(latLng, {
75         weight: 2.5,
76         radius: 20,
77         fillOpacity: 0.5,
78         color: "#FF6200"
79       });
80       map.addLayer(halo);
81     }
82
83     if (map.hasLayer(currentNote)) map.removeLayer(currentNote);
84     currentNote = L.marker(latLng, {
85       icon: noteIcons[data.status],
86       opacity: 1,
87       clickable: true
88     });
89     map.addLayer(currentNote);
90   };
91
92   page.unload = function () {
93     if (map.hasLayer(halo)) map.removeLayer(halo);
94     if (map.hasLayer(currentNote)) map.removeLayer(currentNote);
95   };
96
97   return page;
98 };