]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
Really remove login.live.com from CSP allow list
[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         if (!data) return;
28         var latLng = L.latLng(data.coordinates.split(","));
29         if (!map.getBounds().contains(latLng)) moveToNote();
30       });
31     });
32   };
33
34   page.load = function (path, id) {
35     initialize(path, id, moveToNote);
36   };
37
38   function initialize(path, id, callback) {
39     content.find("input[type=submit]").on("click", function (e) {
40       e.preventDefault();
41       var data = $(e.target).data();
42       var form = e.target.form;
43
44       $(form).find("input[type=submit]").prop("disabled", true);
45
46       $.ajax({
47         url: data.url,
48         type: data.method,
49         oauth: true,
50         data: { text: $(form.text).val() },
51         success: function () {
52           OSM.loadSidebarContent(path, function () {
53             initialize(path, id, moveToNote);
54           });
55         },
56         error: function (xhr) {
57           $(form).find("#comment-error")
58             .text(xhr.responseText)
59             .prop("hidden", false);
60           updateButtons(form);
61         }
62       });
63     });
64
65     content.find("textarea").on("input", function (e) {
66       updateButtons(e.target.form);
67     });
68
69     content.find("textarea").val("").trigger("input");
70
71     var data = $(".details").data();
72
73     if (data) {
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
82     if (callback) callback();
83   }
84
85   function updateButtons(form) {
86     $(form).find("input[type=submit]").prop("disabled", false);
87     if ($(form.text).val() === "") {
88       $(form.close).val($(form.close).data("defaultActionText"));
89       $(form.comment).prop("disabled", true);
90     } else {
91       $(form.close).val($(form.close).data("commentActionText"));
92       $(form.comment).prop("disabled", false);
93     }
94   }
95
96   function moveToNote() {
97     var data = $(".details").data();
98     if (!data) return;
99     var latLng = L.latLng(data.coordinates.split(","));
100
101     if (!window.location.hash || window.location.hash.match(/^#?c[0-9]+$/)) {
102       OSM.router.withoutMoveListener(function () {
103         map.setView(latLng, 15, { reset: true });
104       });
105     }
106   }
107
108   page.unload = function () {
109     map.removeObject();
110   };
111
112   return page;
113 };