1 $(document).ready(function () {
2 var params = OSM.mapParams();
4 // map.noteLayer = addNoteLayer(map, $("#show_notes").attr("href"), $("#createnoteanchor"), $("#new-note"), 11);
6 $("#show_notes").click(function () {
7 map.noteLayer.setVisibility(true);
11 map.noteLayer.setVisibility(true);
14 function addNoteLayer(map, notesUrl, newNoteControls, newNoteForm, minZoom) {
17 var saveNewNotes = function (o) {
19 newNotes = layer.getFeaturesByAttribute("status", "new")
20 layer.removeFeatures(newNotes, { silent: true });
23 var restoreNewNotes = function (o) {
25 layer.addFeatures(newNotes);
29 var describeNote = function (n) {
30 var description = "<h2>Note " + n.id + "</h2>";
32 n.comments.forEach(function (c) {
33 description += "<p><small class='deemphasize'>" + c.action + " by ";
34 description += c.user + " at " + c.date + "</small><br/>" + c.text + "</p>";
40 var noteSelected = function (o) {
41 var feature = o.feature;
42 var location = feature.geometry.getBounds().getCenterLonLat();
46 if (feature.attributes.status === "new") {
47 var form = newNoteForm.clone();
48 form.removeClass("hidden");
49 content = form.html();
52 content = describeNote(feature.attributes);
56 feature.popup = new OpenLayers.Popup.FramedCloud(
57 feature.attributes.id, location, null, content, null, close,
58 function (e) { map.noteSelector.unselect(feature) }
61 map.addPopup(feature.popup);
62 // feature.popup.show();
64 $(feature.popup.contentDiv).find("textarea").autoGrow();
66 $(feature.popup.contentDiv).find("input#note-submit").click(function (e) {
67 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
68 var form = $(e.target).parents("form").first();
70 $.ajax(form.prop("action"), {
71 type: form.prop("method"),
75 text: form.find("textarea#comment").val()
77 success: function (data) {
78 map.noteSelector.unselect(feature);
80 feature.attributes.status = "open";
81 feature.attributes.id = data;
83 map.noteLayer.drawFeature(feature);
85 map.noteMover.deactivate();
92 $(feature.popup.contentDiv).find("input#note-cancel").click(function (e) {
93 feature.attributes.status = "cancelled";
95 map.noteSelector.unselect(feature);
96 map.noteLayer.removeFeatures(feature);
100 map.noteMover.deactivate();
105 feature.popup.updateSize();
108 var noteUnselected = function (o) {
109 var feature = o.feature;
111 map.removePopup(feature.popup);
113 delete feature.popup;
116 var allowNoteReports = function () {
117 if (map.getZoom() > minZoom) {
118 newNoteControls.show();
120 newNoteControls.hide();
124 var addNote = function () {
125 var lonlat = map.getCenter();
126 var layer = map.noteLayer;
127 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
128 var feature = new OpenLayers.Feature.Vector(geometry, {
132 layer.addFeatures(feature);
133 map.noteSelector.unselectAll();
134 map.noteSelector.select(feature);
135 map.noteMover.activate();
136 map.noteLayer.setVisibility(true);
139 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
141 displayInLayerSwitcher: false,
142 projection: new OpenLayers.Projection("EPSG:4326"),
143 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
151 new OpenLayers.Rule({
152 filter: new OpenLayers.Filter.Comparison({
153 type: OpenLayers.Filter.Comparison.EQUAL_TO,
158 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
161 new OpenLayers.Rule({
162 filter: new OpenLayers.Filter.Comparison({
163 type: OpenLayers.Filter.Comparison.EQUAL_TO,
168 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
171 new OpenLayers.Rule({
172 filter: new OpenLayers.Filter.Comparison({
173 type: OpenLayers.Filter.Comparison.EQUAL_TO,
178 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
184 new OpenLayers.Strategy.BBOX()
186 protocol: new OpenLayers.Protocol.HTTP({
188 format: new OpenLayers.Format.GeoJSON()
192 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
193 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
194 map.noteLayer.events.register("featureselected", map, noteSelected);
195 map.noteLayer.events.register("featureunselected", map, noteUnselected);
197 map.addLayer(map.noteLayer);
199 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
203 map.addControl(map.noteSelector);
205 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
206 onDrag: function (feature, pixel) {
207 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
208 feature.popup.updatePosition();
211 over: function (feature) {
212 if (feature.attributes.status === "new") {
213 map.noteMover.overFeature.apply(map.noteMover, [feature]);
219 map.addControl(map.noteMover);
221 newNoteControls.click(addNote);
223 map.events.register("zoomend", map, allowNoteReports);
225 return map.noteLayer;