]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index_modules/changeset.js
Make loadSidebarContent use promises
[rails.git] / app / assets / javascripts / index_modules / changeset.js
1 export default function (map) {
2   const page = {},
3         content = $("#sidebar_content");
4
5   content.on("turbo:before-frame-render", "turbo-frame", function () {
6     $(this).find(".numbered_pagination").trigger("numbered_pagination:disable");
7   });
8
9   content.on("turbo:frame-render", "turbo-frame", function () {
10     $(this).find(".numbered_pagination").trigger("numbered_pagination:enable");
11   });
12
13   page.pushstate = page.popstate = function (path) {
14     OSM.loadSidebarContent(path)
15       .then(() => page.load());
16   };
17
18   page.load = function () {
19     const changesetData = content.find("[data-changeset]").data("changeset");
20     changesetData.type = "changeset";
21
22     const hashParams = OSM.parseHash();
23     initialize();
24     map.addObject(changesetData, function (bounds) {
25       if (!hashParams.center && bounds.isValid()) {
26         OSM.router.withoutMoveListener(function () {
27           map.fitBounds(bounds);
28         });
29       }
30     });
31     $(".numbered_pagination").trigger("numbered_pagination:enable");
32   };
33
34   function updateChangeset(method, url, include_data) {
35     const data = new URLSearchParams();
36
37     content.find("#comment-error").prop("hidden", true);
38     content.find("button[data-method][data-url]").prop("disabled", true);
39
40     if (include_data) {
41       data.set("text", content.find("textarea").val());
42     }
43
44     fetch(url, {
45       method: method,
46       headers: { ...OSM.oauth },
47       body: data
48     })
49       .then(response => {
50         if (response.ok) return response;
51         return response.text().then(text => {
52           throw new Error(text);
53         });
54       })
55       .then(() => {
56         OSM.loadSidebarContent(location.pathname)
57           .then(page.load);
58       })
59       .catch(error => {
60         content.find("button[data-method][data-url]").prop("disabled", false);
61         content.find("#comment-error")
62           .text(error.message)
63           .prop("hidden", false)
64           .get(0).scrollIntoView({ block: "nearest" });
65       });
66   }
67
68   function initialize() {
69     content.find("button[data-method][data-url]").on("click", function (e) {
70       e.preventDefault();
71       const data = $(e.target).data();
72       const include_data = e.target.name === "comment";
73       updateChangeset(data.method, data.url, include_data);
74     });
75
76     content.find("textarea").on("input", function (e) {
77       const form = e.target.form,
78             disabled = $(e.target).val() === "";
79       form.comment.disabled = disabled;
80     });
81
82     content.find("textarea").val("").trigger("input");
83   }
84
85   page.unload = function () {
86     map.removeObject();
87     $(".numbered_pagination").trigger("numbered_pagination:disable");
88   };
89
90   return page;
91 }