1 function addNoteLayer(map, notesUrl, newNoteControls, newNoteForm, minZoom) {
4 var saveNewNotes = function (o) {
6 newNotes = layer.getFeaturesByAttribute("status", "new")
7 layer.removeFeatures(newNotes, { silent: true });
10 var restoreNewNotes = function (o) {
12 layer.addFeatures(newNotes);
16 var describeNote = function (n) {
17 var description = "<h2>Note " + n.id + "</h2>";
19 n.comments.forEach(function (c) {
20 description += "<p><small class='deemphasize'>" + c.action + " by ";
21 description += c.user + " at " + c.date + "</small><br/>" + c.text + "</p>";
27 var noteSelected = function (o) {
28 var feature = o.feature;
29 var location = feature.geometry.getBounds().getCenterLonLat();
33 if (feature.attributes.status === "new") {
34 var form = newNoteForm.clone();
35 form.removeClass("hidden");
36 content = form.html();
39 content = describeNote(feature.attributes);
43 feature.popup = new OpenLayers.Popup.FramedCloud(
44 feature.attributes.id, location, null, content, null, close,
45 function (e) { map.noteSelector.unselect(feature) }
48 map.addPopup(feature.popup);
49 // feature.popup.show();
51 $(feature.popup.contentDiv).find("textarea").autoGrow();
53 $(feature.popup.contentDiv).find("input#note-submit").click(function (e) {
54 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
55 var form = $(e.target).parents("form").first();
57 $.ajax(form.prop("action"), {
58 type: form.prop("method"),
62 text: form.find("textarea#comment").val()
64 success: function (data) {
65 map.noteSelector.unselect(feature);
67 feature.attributes.status = "open";
68 feature.attributes.id = data;
70 map.noteLayer.drawFeature(feature);
72 map.noteMover.deactivate();
79 $(feature.popup.contentDiv).find("input#note-cancel").click(function (e) {
80 feature.attributes.status = "cancelled";
82 map.noteSelector.unselect(feature);
83 map.noteLayer.removeFeatures(feature);
87 map.noteMover.deactivate();
92 feature.popup.updateSize();
95 var noteUnselected = function (o) {
96 var feature = o.feature;
98 map.removePopup(feature.popup);
100 delete feature.popup;
103 var allowNoteReports = function () {
104 if (map.getZoom() > minZoom) {
105 newNoteControls.show();
107 newNoteControls.hide();
111 var addNote = function () {
112 var lonlat = map.getCenter();
113 var layer = map.noteLayer;
114 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
115 var feature = new OpenLayers.Feature.Vector(geometry, {
119 layer.addFeatures(feature);
120 map.noteSelector.unselectAll();
121 map.noteSelector.select(feature);
122 map.noteMover.activate();
123 map.noteLayer.setVisibility(true);
126 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
128 displayInLayerSwitcher: false,
129 projection: new OpenLayers.Projection("EPSG:4326"),
130 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
138 new OpenLayers.Rule({
139 filter: new OpenLayers.Filter.Comparison({
140 type: OpenLayers.Filter.Comparison.EQUAL_TO,
145 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
148 new OpenLayers.Rule({
149 filter: new OpenLayers.Filter.Comparison({
150 type: OpenLayers.Filter.Comparison.EQUAL_TO,
155 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
158 new OpenLayers.Rule({
159 filter: new OpenLayers.Filter.Comparison({
160 type: OpenLayers.Filter.Comparison.EQUAL_TO,
165 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
171 new OpenLayers.Strategy.BBOX()
173 protocol: new OpenLayers.Protocol.HTTP({
175 format: new OpenLayers.Format.GeoJSON()
179 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
180 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
181 map.noteLayer.events.register("featureselected", map, noteSelected);
182 map.noteLayer.events.register("featureunselected", map, noteUnselected);
184 map.addLayer(map.noteLayer);
186 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
190 map.addControl(map.noteSelector);
192 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
193 onDrag: function (feature, pixel) {
194 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
195 feature.popup.updatePosition();
198 over: function (feature) {
199 if (feature.attributes.status === "new") {
200 map.noteMover.overFeature.apply(map.noteMover, [feature]);
206 map.addControl(map.noteMover);
208 newNoteControls.click(addNote);
210 map.events.register("zoomend", map, allowNoteReports);
212 return map.noteLayer;