-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathnote.js
More file actions
103 lines (90 loc) · 2.96 KB
/
Copy pathnote.js
File metadata and controls
103 lines (90 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
OSM.Note = function (map) {
const content = $("#sidebar_content"),
page = {};
page.pushstate = page.popstate = function (path, id) {
OSM.loadSidebarContent(path, function () {
const data = $(".details").data();
if (!data) return;
const [lat, lng] = data.coordinates.split(",").map(parseFloat);
initialize(path, id, map.getBounds().contains({ lat, lng }));
});
};
page.load = function (path, id) {
initialize(path, id);
};
function initialize(path, id, skipMoveToNote) {
content.find("button[name]").on("click", function (e) {
e.preventDefault();
const { url, method } = $(e.target).data(),
name = $(e.target).attr("name"),
data = new URLSearchParams();
content.find("button[name]").prop("disabled", true);
if (name !== "subscribe" && name !== "unsubscribe") {
const textarea = content.find("textarea");
if (textarea.length) {
data.set("text", textarea.val());
}
}
fetch(url, {
method: method,
headers: { ...OSM.oauth },
body: data
})
.then(response => {
if (response.ok) return response;
return response.text().then(text => {
throw new Error(text);
});
})
.then(() => {
OSM.loadSidebarContent(path, () => {
initialize(path, id, false);
});
})
.catch(error => {
content.find("#comment-error")
.text(error.message)
.prop("hidden", false)
.get(0).scrollIntoView({ block: "nearest" });
updateButtons();
});
});
content.find("textarea").on("input", function (e) {
updateButtons(e.target.form);
});
content.find("textarea").val("").trigger("input");
const data = $(".details").data();
if (data) {
const hashParams = OSM.parseHash();
map.addObject({
type: "note",
id: parseInt(id, 10),
latLng: L.latLng(data.coordinates.split(",")),
icon: OSM.noteMarkers[data.status]
}, function () {
if (!hashParams.center && !skipMoveToNote) {
const latLng = L.latLng(data.coordinates.split(","));
OSM.router.withoutMoveListener(function () {
map.setView(latLng, 15, { reset: true });
});
}
});
}
}
function updateButtons() {
const resolveButton = content.find("button[name='close']");
const commentButton = content.find("button[name='comment']");
if (content.find("textarea").val() === "") {
resolveButton.text(resolveButton.data("defaultActionText"));
resolveButton.prop("disabled", !resolveButton.data("canResolveWithoutComment"));
commentButton.prop("disabled", true);
} else {
resolveButton.text(resolveButton.data("commentActionText"));
content.find("button[name]").prop("disabled", false);
}
}
page.unload = function () {
map.removeObject();
};
return page;
};