1 //= require templates/notes/show
2 //= require templates/notes/new
4 $(document).ready(function () {
5 var params = OSM.mapParams();
8 function saveNewNotes(o) {
10 newNotes = layer.getFeaturesByAttribute("status", "new")
11 layer.removeFeatures(newNotes, { silent: true });
14 function restoreNewNotes(o) {
16 layer.addFeatures(newNotes);
20 function createNote(feature, form) {
21 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
23 $(form).find("input[type=submit]").prop("disabled", true);
25 $.ajax($("#createnoteanchor").attr("href"), {
30 text: $(form.text).val()
32 success: function (data) {
33 map.noteSelector.unselect(feature);
35 feature.attributes = data.properties;
37 map.noteLayer.drawFeature(feature);
39 map.noteMover.deactivate();
44 function updateNote(feature, form, close) {
45 var url = close ? feature.attributes.close_url : feature.attributes.comment_url;
47 $(form).find("input[type=submit]").prop("disabled", true);
52 text: $(form.text).val()
54 success: function (data) {
55 map.noteSelector.unselect(feature)
57 feature.attributes = data.properties;
59 map.noteSelector.select(feature)
64 function noteSelected(o) {
65 var feature = o.feature;
66 var location = feature.geometry.getBounds().getCenterLonLat();
70 if (feature.attributes.status === "new") {
71 content = JST["templates/notes/new"]();
73 onClose = function (e) {
74 feature.attributes.status = "cancelled";
76 map.noteSelector.unselect(feature);
77 map.noteLayer.removeFeatures(feature);
81 map.noteMover.deactivate();
84 content = JST["templates/notes/show"]({ note: feature.attributes });
86 onClose = function (e) {
87 map.noteSelector.unselect(feature)
91 feature.popup = new OpenLayers.Popup.FramedCloud(
92 feature.attributes.id, location, null, content, null, true, onClose
95 map.addPopup(feature.popup);
96 // feature.popup.show();
98 $(feature.popup.contentDiv).find("textarea").autoGrow();
100 $(feature.popup.contentDiv).find("textarea").on("input", function (e) {
101 var form = e.target.form;
103 if ($(e.target).val() == "") {
104 $(form.close).val(I18n.t("javascripts.notes.show.close"));
106 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
110 $(feature.popup.contentDiv).find("input#note-add").click(function (e) {
113 createNote(feature, e.target.form);
116 $(feature.popup.contentDiv).find("input#note-comment").click(function (e) {
119 updateNote(feature, e.target.form, false);
122 $(feature.popup.contentDiv).find("input#note-close").click(function (e) {
125 updateNote(feature, e.target.form, true);
128 feature.popup.updateSize();
131 function noteUnselected(o) {
132 var feature = o.feature;
134 map.removePopup(feature.popup);
138 var lonlat = map.getCenter();
139 var layer = map.noteLayer;
140 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
141 var feature = new OpenLayers.Feature.Vector(geometry, {
145 layer.addFeatures(feature);
146 map.noteSelector.unselectAll();
147 map.noteSelector.select(feature);
148 map.noteMover.activate();
149 map.noteLayer.setVisibility(true);
152 $("#map").on("initialised", function () {
153 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
154 visibility: params.notes,
155 displayInLayerSwitcher: false,
156 projection: new OpenLayers.Projection("EPSG:4326"),
157 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
165 new OpenLayers.Rule({
166 filter: new OpenLayers.Filter.Comparison({
167 type: OpenLayers.Filter.Comparison.EQUAL_TO,
172 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
175 new OpenLayers.Rule({
176 filter: new OpenLayers.Filter.Comparison({
177 type: OpenLayers.Filter.Comparison.EQUAL_TO,
182 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
185 new OpenLayers.Rule({
186 filter: new OpenLayers.Filter.Comparison({
187 type: OpenLayers.Filter.Comparison.EQUAL_TO,
192 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
198 new OpenLayers.Strategy.BBOX()
200 protocol: new OpenLayers.Protocol.HTTP({
201 url: $("#show_notes").attr("href"),
202 format: new OpenLayers.Format.GeoJSON()
206 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
207 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
208 map.noteLayer.events.register("featureselected", map, noteSelected);
209 map.noteLayer.events.register("featureunselected", map, noteUnselected);
211 map.addLayer(map.noteLayer);
213 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
217 map.addControl(map.noteSelector);
219 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
220 onDrag: function (feature, pixel) {
221 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
222 feature.popup.updatePosition();
225 over: function (feature) {
226 if (feature.attributes.status === "new") {
227 map.noteMover.overFeature.apply(map.noteMover, [feature]);
233 map.addControl(map.noteMover);
235 $("#show_notes").click(function (e) {
236 map.noteLayer.setVisibility(true);
241 $("#createnoteanchor").click(function (e) {
242 map.noteLayer.setVisibility(true);