]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/changeset.js
Merge branch 'comments'
[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)
14       currentChangesetId = id;
15     initialize();
16     addChangeset(currentChangesetId, true);
17   };
18
19   function addChangeset(id, center) {
20     var bounds = map.addObject({type: 'changeset', id: parseInt(id)}, function(bounds) {
21       if (!window.location.hash && bounds.isValid() &&
22           (center || !map.getBounds().contains(bounds))) {
23         OSM.router.withoutMoveListener(function () {
24           map.fitBounds(bounds);
25         });
26       }
27     });
28   }
29
30   function updateChangeset(form, method, url, include_data) {
31     $(form).find("input[type=submit]").prop("disabled", true);
32     if(include_data) {
33       data = {text: $(form.text).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     });
47   }
48
49   function initialize() {
50     content.find("input[name=comment]").on("click", function (e) {
51       e.preventDefault();
52       var data = $(e.target).data();
53       updateChangeset(e.target.form, data.method, data.url, true);
54     });
55
56     content.find(".action-button").on("click", function (e) {
57       e.preventDefault();
58       var data = $(e.target).data();
59       updateChangeset(e.target.form, data.method, data.url);
60     });
61
62     content.find("textarea").on("input", function (e) {
63       var form = e.target.form;
64
65       if ($(e.target).val() == "") {
66         $(form.comment).prop("disabled", true);
67       } else {
68         $(form.comment).prop("disabled", false);
69       }
70     });
71
72     content.find("textarea").val('').trigger("input");
73   };
74
75   page.unload = function() {
76     map.removeObject();
77   };
78
79   return page;
80 };