]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
don't re-pan the map when selecting another note that is already on the screen
[rails.git] / app / assets / javascripts / index / note.js
1 OSM.Note = function (map) {
2   var content = $("#sidebar_content"),
3       page = {};
4
5   var noteIcons = {
6     "new": L.icon({
7       iconUrl: OSM.NEW_NOTE_MARKER,
8       iconSize: [25, 40],
9       iconAnchor: [12, 40]
10     }),
11     "open": L.icon({
12       iconUrl: OSM.OPEN_NOTE_MARKER,
13       iconSize: [25, 40],
14       iconAnchor: [12, 40]
15     }),
16     "closed": L.icon({
17       iconUrl: OSM.CLOSED_NOTE_MARKER,
18       iconSize: [25, 40],
19       iconAnchor: [12, 40]
20     })
21   };
22
23   page.pushstate = page.popstate = function (path, id) {
24     OSM.loadSidebarContent(path, function () {
25       var data = $(".details").data();
26       if (!data) return;
27       var latLng = L.latLng(data.coordinates.split(","));
28       initialize(path, id, map.getBounds().contains(latLng));
29     });
30   };
31
32   page.load = function (path, id) {
33     initialize(path, id);
34   };
35
36   function initialize(path, id, skipMoveToNote) {
37     content.find("button[name]").on("click", function (e) {
38       e.preventDefault();
39       var data = $(e.target).data();
40       var name = $(e.target).attr("name");
41       var ajaxSettings = {
42         url: data.url,
43         type: data.method,
44         oauth: true,
45         success: () => {
46           OSM.loadSidebarContent(path, () => {
47             initialize(path, id);
48           });
49         },
50         error: (xhr) => {
51           content.find("#comment-error")
52             .text(xhr.responseText)
53             .prop("hidden", false)
54             .get(0).scrollIntoView({ block: "nearest" });
55           updateButtons();
56         }
57       };
58
59       if (name !== "subscribe" && name !== "unsubscribe" && name !== "reopen") {
60         ajaxSettings.data = { text: content.find("textarea").val() };
61       }
62
63       content.find("button[name]").prop("disabled", true);
64       $.ajax(ajaxSettings);
65     });
66
67     content.find("textarea").on("input", function (e) {
68       updateButtons(e.target.form);
69     });
70
71     content.find("textarea").val("").trigger("input");
72
73     var data = $(".details").data();
74
75     if (data) {
76       var hashParams = OSM.parseHash(window.location.hash);
77       map.addObject({
78         type: "note",
79         id: parseInt(id, 10),
80         latLng: L.latLng(data.coordinates.split(",")),
81         icon: noteIcons[data.status]
82       }, function () {
83         if (!hashParams.center && !skipMoveToNote) {
84           var latLng = L.latLng(data.coordinates.split(","));
85           OSM.router.withoutMoveListener(function () {
86             map.setView(latLng, 15, { reset: true });
87           });
88         }
89       });
90     }
91   }
92
93   function updateButtons() {
94     var resolveButton = content.find("button[name='close']");
95     var commentButton = content.find("button[name='comment']");
96
97     content.find("button[name]").prop("disabled", false);
98     if (content.find("textarea").val() === "") {
99       resolveButton.text(resolveButton.data("defaultActionText"));
100       commentButton.prop("disabled", true);
101     } else {
102       resolveButton.text(resolveButton.data("commentActionText"));
103     }
104   }
105
106   page.unload = function () {
107     map.removeObject();
108   };
109
110   return page;
111 };