1 $(document).ready(function () {
2 var params = OSM.mapParams();
5 function saveNewNotes(o) {
7 newNotes = layer.getFeaturesByAttribute("status", "new")
8 layer.removeFeatures(newNotes, { silent: true });
11 function restoreNewNotes(o) {
13 layer.addFeatures(newNotes);
17 function describeNote(n) {
18 var description = "<h2>Note " + n.id + "</h2>";
20 n.comments.forEach(function (c) {
21 description += "<p><small class='deemphasize'>" + c.action + " by ";
22 description += c.user + " at " + c.date + "</small><br/>" + c.text + "</p>";
28 function noteSelected(o) {
29 var feature = o.feature;
30 var location = feature.geometry.getBounds().getCenterLonLat();
34 if (feature.attributes.status === "new") {
35 var form = $("#new-note").clone();
36 form.removeClass("hidden");
37 content = form.html();
40 content = describeNote(feature.attributes);
44 feature.popup = new OpenLayers.Popup.FramedCloud(
45 feature.attributes.id, location, null, content, null, close,
46 function (e) { map.noteSelector.unselect(feature) }
49 map.addPopup(feature.popup);
50 // feature.popup.show();
52 $(feature.popup.contentDiv).find("textarea").autoGrow();
54 $(feature.popup.contentDiv).find("input#note-submit").click(function (e) {
55 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
56 var form = $(e.target).parents("form").first();
58 $.ajax(form.prop("action"), {
59 type: form.prop("method"),
63 text: form.find("textarea#comment").val()
65 success: function (data) {
66 map.noteSelector.unselect(feature);
68 feature.attributes.status = "open";
69 feature.attributes.id = data;
71 map.noteLayer.drawFeature(feature);
73 map.noteMover.deactivate();
80 $(feature.popup.contentDiv).find("input#note-cancel").click(function (e) {
81 feature.attributes.status = "cancelled";
83 map.noteSelector.unselect(feature);
84 map.noteLayer.removeFeatures(feature);
88 map.noteMover.deactivate();
93 feature.popup.updateSize();
96 function noteUnselected(o) {
97 var feature = o.feature;
99 map.removePopup(feature.popup);
103 var lonlat = map.getCenter();
104 var layer = map.noteLayer;
105 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
106 var feature = new OpenLayers.Feature.Vector(geometry, {
110 layer.addFeatures(feature);
111 map.noteSelector.unselectAll();
112 map.noteSelector.select(feature);
113 map.noteMover.activate();
114 map.noteLayer.setVisibility(true);
117 $("#map").on("initialised", function () {
118 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
119 visibility: params.notes,
120 displayInLayerSwitcher: false,
121 projection: new OpenLayers.Projection("EPSG:4326"),
122 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
130 new OpenLayers.Rule({
131 filter: new OpenLayers.Filter.Comparison({
132 type: OpenLayers.Filter.Comparison.EQUAL_TO,
137 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
140 new OpenLayers.Rule({
141 filter: new OpenLayers.Filter.Comparison({
142 type: OpenLayers.Filter.Comparison.EQUAL_TO,
147 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
150 new OpenLayers.Rule({
151 filter: new OpenLayers.Filter.Comparison({
152 type: OpenLayers.Filter.Comparison.EQUAL_TO,
157 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
163 new OpenLayers.Strategy.BBOX()
165 protocol: new OpenLayers.Protocol.HTTP({
166 url: $("#show_notes").attr("href"),
167 format: new OpenLayers.Format.GeoJSON()
171 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
172 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
173 map.noteLayer.events.register("featureselected", map, noteSelected);
174 map.noteLayer.events.register("featureunselected", map, noteUnselected);
176 map.addLayer(map.noteLayer);
178 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
182 map.addControl(map.noteSelector);
184 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
185 onDrag: function (feature, pixel) {
186 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
187 feature.popup.updatePosition();
190 over: function (feature) {
191 if (feature.attributes.status === "new") {
192 map.noteMover.overFeature.apply(map.noteMover, [feature]);
198 map.addControl(map.noteMover);
200 $("#show_notes").click(function (e) {
201 map.noteLayer.setVisibility(true);
206 $("#createnoteanchor").click(function (e) {
207 map.noteLayer.setVisibility(true);