]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/note.js.erb
9afe3b5f887bf54e58d0299ae1f3e406c8dea592
[rails.git] / app / assets / javascripts / index / note.js.erb
1 OSM.Note = function(map) {
2     var noteLayer = map.noteLayer,
3         content = $('#sidebar_content'),
4         page = {},
5         marker;
6
7     var noteIcons = {
8         "new": L.icon({
9             iconUrl: "<%= image_path('new_note_marker.png') %>",
10             iconSize: [25, 40],
11             iconAnchor: [12, 40]
12         }),
13         "open": L.icon({
14             iconUrl: "<%= image_path('open_note_marker.png') %>",
15             iconSize: [25, 40],
16             iconAnchor: [12, 40]
17         }),
18         "closed": L.icon({
19             iconUrl: "<%= image_path('closed_note_marker.png') %>",
20             iconSize: [25, 40],
21             iconAnchor: [12, 40]
22         })
23     };
24
25     function updateNote(marker, form, method, url) {
26         $(form).find("input[type=submit]").prop("disabled", true);
27
28         $.ajax({
29             url: url,
30             type: method,
31             oauth: true,
32             data: {text: $(form.text).val()},
33             success: function (feature) {
34                 marker = noteLayer.getLayer(marker);
35                 if (feature.properties.status == "hidden") {
36                     noteLayer.removeLayer(marker);
37                 } else {
38                     marker.setIcon(noteIcons[feature.properties.status]);
39                     page.load();
40                 }
41             }
42         });
43     }
44
45     function bind() {
46         content.find("input[type=submit]").on("click", function (e) {
47             e.preventDefault();
48             var data = $(e.target).data();
49             updateNote(data.noteId, e.target.form, data.method, data.url);
50         });
51
52         content.find("textarea").on("input", function (e) {
53             var form = e.target.form;
54
55             if ($(e.target).val() == "") {
56                 $(form.close).val(I18n.t("javascripts.notes.show.resolve"));
57                 $(form.comment).prop("disabled", true);
58             } else {
59                 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_resolve"));
60                 $(form.comment).prop("disabled", false);
61             }
62         });
63
64         content.find("textarea").val('').trigger("input");
65     }
66
67     page.pushstate = page.popstate = function() {
68         page.load();
69     };
70
71     page.load = function(path, id) {
72         var loadTimer = setTimeout(setLoading, 250);
73         $('#sidebar_content').load(window.location.pathname + "?xhr=1", function(a, b, xhr) {
74             if (xhr.getResponseHeader('X-Page-Title')) {
75                 document.title = xhr.getResponseHeader('X-Page-Title');
76             }
77             bind();
78             clearTimeout(loadTimer);
79             clearLoading();
80
81             if (!map.hasLayer(noteLayer)) map.addLayer(noteLayer);
82             if (window.location.hash == "") map.panTo($('.details').data().coordinates.split(','));
83         });
84     };
85
86     page.unload = function() {
87         if (marker) map.removeLayer(marker);
88     };
89
90     function setLoading() {
91       if($('#browse_status').is(':empty')) {
92         $('#browse_status').append($('<p></p>').text(I18n.t('browse.start_rjs.loading')));
93       }
94     }
95
96     function clearLoading() {
97       $('#browse_status').empty();
98     }
99
100     return page;
101 };