]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
Merge remote-tracking branch 'upstream/pull/3746'
[rails.git] / app / assets / javascripts / index / note.js
1 OSM.Note = function (map) {
2   var content = $("#sidebar_content"),
3       page = {};
4
5   var noteIcons = {
6     "new": L.icon({
7       iconUrl: OSM.NEW_NOTE_MARKER,
8       iconSize: [25, 40],
9       iconAnchor: [12, 40]
10     }),
11     "open": L.icon({
12       iconUrl: OSM.OPEN_NOTE_MARKER,
13       iconSize: [25, 40],
14       iconAnchor: [12, 40]
15     }),
16     "closed": L.icon({
17       iconUrl: OSM.CLOSED_NOTE_MARKER,
18       iconSize: [25, 40],
19       iconAnchor: [12, 40]
20     })
21   };
22
23   page.pushstate = page.popstate = function (path, id) {
24     OSM.loadSidebarContent(path, function () {
25       initialize(path, id, function () {
26         var data = $(".details").data(),
27             latLng = L.latLng(data.coordinates.split(","));
28         if (!map.getBounds().contains(latLng)) moveToNote();
29       });
30     });
31   };
32
33   page.load = function (path, id) {
34     initialize(path, id, moveToNote);
35   };
36
37   function initialize(path, id, callback) {
38     content.find("input[type=submit]").on("click", function (e) {
39       e.preventDefault();
40       var data = $(e.target).data();
41       var form = e.target.form;
42
43       $(form).find("input[type=submit]").prop("disabled", true);
44
45       $.ajax({
46         url: data.url,
47         type: data.method,
48         oauth: true,
49         data: { text: $(form.text).val() },
50         success: function () {
51           OSM.loadSidebarContent(path, function () {
52             initialize(path, id, moveToNote);
53           });
54         }
55       });
56     });
57
58     content.find("textarea").on("input", function (e) {
59       var form = e.target.form;
60
61       if ($(e.target).val() === "") {
62         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
63         $(form.comment).prop("disabled", true);
64       } else {
65         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
66         $(form.comment).prop("disabled", false);
67       }
68     });
69
70     content.find("textarea").val("").trigger("input");
71
72     var data = $(".details").data();
73
74     map.addObject({
75       type: "note",
76       id: parseInt(id, 10),
77       latLng: L.latLng(data.coordinates.split(",")),
78       icon: noteIcons[data.status]
79     });
80
81     if (callback) callback();
82   }
83
84   function moveToNote() {
85     var data = $(".details").data(),
86         latLng = L.latLng(data.coordinates.split(","));
87
88     if (!window.location.hash || window.location.hash.match(/^#?c[0-9]+$/)) {
89       OSM.router.withoutMoveListener(function () {
90         map.setView(latLng, 15, { reset: true });
91       });
92     }
93   }
94
95   page.unload = function () {
96     map.removeObject();
97   };
98
99   return page;
100 };