]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/changeset.js
060c5c8be909ecf2763f4efdd6c9b04c83794119
[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     map.addObject({type: 'changeset', id: parseInt(id, 10)}, 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     var data;
32
33     $(form).find("input[type=submit]").prop("disabled", true);
34
35     if (include_data) {
36       data = {text: $(form.text).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     });
50   }
51
52   function initialize() {
53     content.find("input[name=comment]").on("click", function (e) {
54       e.preventDefault();
55       var data = $(e.target).data();
56       updateChangeset(e.target.form, data.method, data.url, true);
57     });
58
59     content.find(".action-button").on("click", function (e) {
60       e.preventDefault();
61       var data = $(e.target).data();
62       updateChangeset(e.target.form, data.method, data.url);
63     });
64
65     content.find("textarea").on("input", function (e) {
66       var form = e.target.form;
67
68       if ($(e.target).val() === "") {
69         $(form.comment).prop("disabled", true);
70       } else {
71         $(form.comment).prop("disabled", false);
72       }
73     });
74
75     content.find("textarea").val('').trigger("input");
76   }
77
78   page.unload = function() {
79     map.removeObject();
80   };
81
82   return page;
83 };