]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/changeset.js
Merge pull request #5677 from tomhughes/user-list-enhancements
[rails.git] / app / assets / javascripts / index / changeset.js
1 OSM.Changeset = function (map) {
2   var page = {},
3       content = $("#sidebar_content");
4
5   page.pushstate = page.popstate = function (path) {
6     OSM.loadSidebarContent(path, function () {
7       page.load();
8     });
9   };
10
11   page.load = function () {
12     const changesetData = content.find("[data-changeset]").data("changeset");
13     changesetData.type = "changeset";
14
15     var hashParams = OSM.parseHash(window.location.hash);
16     initialize();
17     map.addObject(changesetData, function (bounds) {
18       if (!hashParams.center && bounds.isValid()) {
19         OSM.router.withoutMoveListener(function () {
20           map.fitBounds(bounds);
21         });
22       }
23     });
24   };
25
26   function updateChangeset(method, url, include_data) {
27     var data;
28
29     content.find("#comment-error").prop("hidden", true);
30     content.find("button[data-method][data-url]").prop("disabled", true);
31
32     if (include_data) {
33       data = { text: content.find("textarea").val() };
34     } else {
35       data = {};
36     }
37
38     $.ajax({
39       url: url,
40       type: method,
41       oauth: true,
42       data: data,
43       success: function () {
44         OSM.loadSidebarContent(window.location.pathname, page.load);
45       },
46       error: function (xhr) {
47         content.find("button[data-method][data-url]").prop("disabled", false);
48         content.find("#comment-error")
49           .text(xhr.responseText)
50           .prop("hidden", false)
51           .get(0).scrollIntoView({ block: "nearest" });
52       }
53     });
54   }
55
56   function initialize() {
57     content.find("button[data-method][data-url]").on("click", function (e) {
58       e.preventDefault();
59       var data = $(e.target).data();
60       var include_data = e.target.name === "comment";
61       updateChangeset(data.method, data.url, include_data);
62     });
63
64     content.find("textarea").on("input", function (e) {
65       const form = e.target.form,
66             disabled = $(e.target).val() === "";
67       form.comment.disabled = disabled;
68     });
69
70     content.find("textarea").val("").trigger("input");
71   }
72
73   page.unload = function () {
74     map.removeObject();
75   };
76
77   return page;
78 };