]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
Keep id passed to note page for subsequent use in map current object
[rails.git] / app / assets / javascripts / index / note.js
1 OSM.Note = function (map) {
2   var content = $("#sidebar_content"),
3       page = {},
4       halo, currentNote;
5
6   var noteIcons = {
7     "new": L.icon({
8       iconUrl: OSM.NEW_NOTE_MARKER,
9       iconSize: [25, 40],
10       iconAnchor: [12, 40]
11     }),
12     "open": L.icon({
13       iconUrl: OSM.OPEN_NOTE_MARKER,
14       iconSize: [25, 40],
15       iconAnchor: [12, 40]
16     }),
17     "closed": L.icon({
18       iconUrl: OSM.CLOSED_NOTE_MARKER,
19       iconSize: [25, 40],
20       iconAnchor: [12, 40]
21     })
22   };
23
24   page.pushstate = page.popstate = function (path, id) {
25     OSM.loadSidebarContent(path, function () {
26       initialize(path, id, function () {
27         var data = $(".details").data(),
28             latLng = L.latLng(data.coordinates.split(","));
29         if (!map.getBounds().contains(latLng)) moveToNote();
30       });
31     });
32   };
33
34   page.load = function (path, id) {
35     initialize(path, id, moveToNote);
36   };
37
38   function initialize(path, id, callback) {
39     content.find("input[type=submit]").on("click", function (e) {
40       e.preventDefault();
41       var data = $(e.target).data();
42       var form = e.target.form;
43
44       $(form).find("input[type=submit]").prop("disabled", true);
45
46       $.ajax({
47         url: data.url,
48         type: data.method,
49         oauth: true,
50         data: { text: $(form.text).val() },
51         success: function () {
52           OSM.loadSidebarContent(path, function () {
53             initialize(path, id, moveToNote);
54           });
55         }
56       });
57     });
58
59     content.find("textarea").on("input", function (e) {
60       var form = e.target.form;
61
62       if ($(e.target).val() === "") {
63         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
64         $(form.comment).prop("disabled", true);
65       } else {
66         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
67         $(form.comment).prop("disabled", false);
68       }
69     });
70
71     content.find("textarea").val("").trigger("input");
72
73     var data = $(".details").data(),
74         latLng = L.latLng(data.coordinates.split(","));
75
76     if (!halo || !map.hasLayer(halo)) {
77       halo = L.circleMarker(latLng, {
78         weight: 2.5,
79         radius: 20,
80         fillOpacity: 0.5,
81         color: "#FF6200"
82       });
83       map.addLayer(halo);
84     }
85
86     if (currentNote && map.hasLayer(currentNote)) map.removeLayer(currentNote);
87
88     currentNote = L.marker(latLng, {
89       icon: noteIcons[data.status],
90       opacity: 1,
91       interactive: true
92     });
93
94     map.addLayer(currentNote);
95
96     if (callback) callback();
97   }
98
99   function moveToNote() {
100     var data = $(".details").data(),
101         latLng = L.latLng(data.coordinates.split(","));
102
103     if (!window.location.hash || window.location.hash.match(/^#?c[0-9]+$/)) {
104       OSM.router.withoutMoveListener(function () {
105         map.setView(latLng, 15, { reset: true });
106       });
107     }
108   }
109
110   page.unload = function () {
111     if (map.hasLayer(halo)) map.removeLayer(halo);
112     if (map.hasLayer(currentNote)) map.removeLayer(currentNote);
113   };
114
115   return page;
116 };