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