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