]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
Merge remote-tracking branch 'upstream/pull/5936'
[rails.git] / app / assets / javascripts / index / note.js
1 OSM.Note = function (map) {
2   const content = $("#sidebar_content"),
3         page = {};
4
5   page.pushstate = page.popstate = function (path, id) {
6     OSM.loadSidebarContent(path, function () {
7       const data = $(".details").data();
8       if (!data) return;
9       const latLng = L.latLng(data.coordinates.split(","));
10       initialize(path, id, map.getBounds().contains(latLng));
11     });
12   };
13
14   page.load = function (path, id) {
15     initialize(path, id);
16   };
17
18   function initialize(path, id, skipMoveToNote) {
19     content.find("button[name]").on("click", function (e) {
20       e.preventDefault();
21       const { url, method } = $(e.target).data(),
22             name = $(e.target).attr("name"),
23             data = new URLSearchParams();
24       content.find("button[name]").prop("disabled", true);
25
26       if (name !== "subscribe" && name !== "unsubscribe") {
27         const textarea = content.find("textarea");
28         if (textarea.length) {
29           data.set("text", textarea.val());
30         }
31       }
32
33       fetch(url, {
34         method: method,
35         headers: { ...OSM.oauth },
36         body: data
37       })
38         .then(response => {
39           if (response.ok) return response;
40           return response.text().then(text => {
41             throw new Error(text);
42           });
43         })
44         .then(() => {
45           OSM.loadSidebarContent(path, () => {
46             initialize(path, id, false);
47           });
48         })
49         .catch(error => {
50           content.find("#comment-error")
51             .text(error.message)
52             .prop("hidden", false)
53             .get(0).scrollIntoView({ block: "nearest" });
54           updateButtons();
55         });
56     });
57
58     content.find("textarea").on("input", function (e) {
59       updateButtons(e.target.form);
60     });
61
62     content.find("textarea").val("").trigger("input");
63
64     const data = $(".details").data();
65
66     if (data) {
67       const hashParams = OSM.parseHash();
68       map.addObject({
69         type: "note",
70         id: parseInt(id, 10),
71         latLng: L.latLng(data.coordinates.split(",")),
72         icon: OSM.getMarker({ icon: `${data.status}_NOTE_MARKER`, shadow: false, height: 40 })
73       }, function () {
74         if (!hashParams.center && !skipMoveToNote) {
75           const latLng = L.latLng(data.coordinates.split(","));
76           OSM.router.withoutMoveListener(function () {
77             map.setView(latLng, 15, { reset: true });
78           });
79         }
80       });
81     }
82   }
83
84   function updateButtons() {
85     const resolveButton = content.find("button[name='close']");
86     const commentButton = content.find("button[name='comment']");
87
88     content.find("button[name]").prop("disabled", false);
89     if (content.find("textarea").val() === "") {
90       resolveButton.text(resolveButton.data("defaultActionText"));
91       commentButton.prop("disabled", true);
92     } else {
93       resolveButton.text(resolveButton.data("commentActionText"));
94     }
95   }
96
97   page.unload = function () {
98     map.removeObject();
99   };
100
101   return page;
102 };