]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js
Remove query result geometry when unloading the page
[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       });
57     });
58
59     content.find("textarea").on("input", function (e) {
60       var form = e.target.form;
61
62       if ($(e.target).val() === "") {
63         $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
64         $(form.comment).prop("disabled", true);
65       } else {
66         $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
67         $(form.comment).prop("disabled", false);
68       }
69     });
70
71     content.find("textarea").val("").trigger("input");
72
73     var data = $(".details").data();
74
75     if (data) {
76       map.addObject({
77         type: "note",
78         id: parseInt(id, 10),
79         latLng: L.latLng(data.coordinates.split(",")),
80         icon: noteIcons[data.status]
81       });
82     }
83
84     if (callback) callback();
85   }
86
87   function moveToNote() {
88     var data = $(".details").data();
89     if (!data) return;
90     var latLng = L.latLng(data.coordinates.split(","));
91
92     if (!window.location.hash || window.location.hash.match(/^#?c[0-9]+$/)) {
93       OSM.router.withoutMoveListener(function () {
94         map.setView(latLng, 15, { reset: true });
95       });
96     }
97   }
98
99   page.unload = function () {
100     map.removeObject();
101   };
102
103   return page;
104 };