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 $.ajax($("#createnoteanchor").attr("href"), {
28 text: $(form.comment).val()
30 success: function (data) {
31 map.noteSelector.unselect(feature);
33 feature.attributes = data.properties;
35 map.noteLayer.drawFeature(feature);
37 map.noteMover.deactivate();
42 function updateNote(feature, form, close) {
43 var url = close ? feature.attributes.close_url : feature.attributes.comment_url;
48 text: $(form.text).val()
50 success: function (data) {
51 map.noteSelector.unselect(feature)
53 feature.attributes = data.properties;
55 map.noteSelector.select(feature)
60 function noteSelected(o) {
61 var feature = o.feature;
62 var location = feature.geometry.getBounds().getCenterLonLat();
66 if (feature.attributes.status === "new") {
67 content = JST["templates/notes/new"]();
69 onClose = function (e) {
70 feature.attributes.status = "cancelled";
72 map.noteSelector.unselect(feature);
73 map.noteLayer.removeFeatures(feature);
77 map.noteMover.deactivate();
80 content = JST["templates/notes/show"]({ note: feature.attributes });
82 onClose = function (e) {
83 map.noteSelector.unselect(feature)
87 feature.popup = new OpenLayers.Popup.FramedCloud(
88 feature.attributes.id, location, null, content, null, true, onClose
91 map.addPopup(feature.popup);
92 // feature.popup.show();
94 $(feature.popup.contentDiv).find("textarea").autoGrow();
96 $(feature.popup.contentDiv).find("textarea").on("input", function (e) {
97 var form = e.target.form;
99 if ($(e.target).val() == "") {
100 $(form.close).val(I18n.t("javascripts.notes.show.close"));
102 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
106 $(feature.popup.contentDiv).find("input#note-add").click(function (e) {
109 createNote(feature, e.target.form);
112 $(feature.popup.contentDiv).find("input#note-comment").click(function (e) {
115 updateNote(feature, e.target.form, false);
118 $(feature.popup.contentDiv).find("input#note-close").click(function (e) {
121 updateNote(feature, e.target.form, true);
124 feature.popup.updateSize();
127 function noteUnselected(o) {
128 var feature = o.feature;
130 map.removePopup(feature.popup);
134 var lonlat = map.getCenter();
135 var layer = map.noteLayer;
136 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
137 var feature = new OpenLayers.Feature.Vector(geometry, {
141 layer.addFeatures(feature);
142 map.noteSelector.unselectAll();
143 map.noteSelector.select(feature);
144 map.noteMover.activate();
145 map.noteLayer.setVisibility(true);
148 $("#map").on("initialised", function () {
149 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
150 visibility: params.notes,
151 displayInLayerSwitcher: false,
152 projection: new OpenLayers.Projection("EPSG:4326"),
153 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
161 new OpenLayers.Rule({
162 filter: new OpenLayers.Filter.Comparison({
163 type: OpenLayers.Filter.Comparison.EQUAL_TO,
168 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
171 new OpenLayers.Rule({
172 filter: new OpenLayers.Filter.Comparison({
173 type: OpenLayers.Filter.Comparison.EQUAL_TO,
178 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
181 new OpenLayers.Rule({
182 filter: new OpenLayers.Filter.Comparison({
183 type: OpenLayers.Filter.Comparison.EQUAL_TO,
188 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
194 new OpenLayers.Strategy.BBOX()
196 protocol: new OpenLayers.Protocol.HTTP({
197 url: $("#show_notes").attr("href"),
198 format: new OpenLayers.Format.GeoJSON()
202 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
203 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
204 map.noteLayer.events.register("featureselected", map, noteSelected);
205 map.noteLayer.events.register("featureunselected", map, noteUnselected);
207 map.addLayer(map.noteLayer);
209 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
213 map.addControl(map.noteSelector);
215 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
216 onDrag: function (feature, pixel) {
217 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
218 feature.popup.updatePosition();
221 over: function (feature) {
222 if (feature.attributes.status === "new") {
223 map.noteMover.overFeature.apply(map.noteMover, [feature]);
229 map.addControl(map.noteMover);
231 $("#show_notes").click(function (e) {
232 map.noteLayer.setVisibility(true);
237 $("#createnoteanchor").click(function (e) {
238 map.noteLayer.setVisibility(true);