1 function addNoteLayer(map, notesUrl, newNoteControls, newNoteForm, minZoom) {
4 var noteCallback = function (scope, response) {
5 for (var f = 0; f < response.features.length; f++) {
6 var feature = response.features[f];
10 var saveNewNotes = function (o) {
12 newNotes = layer.getFeaturesByAttribute("status", "new")
13 layer.removeFeatures(newNotes, { silent: true });
16 var restoreNewNotes = function (o) {
18 layer.addFeatures(newNotes);
22 var noteSelected = function (o) {
23 var feature = o.feature;
24 var location = feature.geometry.getBounds().getCenterLonLat();
28 if (feature.attributes.status === "new") {
29 var form = newNoteForm.clone();
30 form.removeClass("hidden");
31 content = form.html();
34 content = "<p>" + feature.attributes.id + "</p>";
38 feature.popup = new OpenLayers.Popup.FramedCloud(
39 feature.attributes.id, location, null, content, null, close,
40 function (e) { map.noteSelector.unselect(feature) }
43 map.addPopup(feature.popup);
44 // feature.popup.show();
46 $(feature.popup.contentDiv).find("textarea").autoGrow();
48 $(feature.popup.contentDiv).find("input#note-submit").click(function (e) {
49 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
50 var form = $(e.target).parents("form").first();
52 $.ajax(form.prop("action"), {
53 type: form.prop("method"),
57 text: form.find("textarea#comment").val()
59 success: function (data) {
60 map.noteSelector.unselect(feature);
62 feature.attributes.status = "open";
63 feature.attributes.id = data;
65 map.noteLayer.drawFeature(feature);
67 map.noteMover.deactivate();
74 $(feature.popup.contentDiv).find("input#note-cancel").click(function (e) {
75 feature.attributes.status = "cancelled";
77 map.noteSelector.unselect(feature);
78 map.noteLayer.removeFeatures(feature);
82 map.noteMover.deactivate();
87 feature.popup.updateSize();
90 var noteUnselected = function (o) {
91 var feature = o.feature;
93 map.removePopup(feature.popup);
98 var allowNoteReports = function () {
99 if (map.getZoom() > minZoom) {
100 newNoteControls.show();
102 newNoteControls.hide();
106 var addNote = function () {
107 var lonlat = map.getCenter();
108 var layer = map.noteLayer;
109 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
110 var feature = new OpenLayers.Feature.Vector(geometry, {
114 layer.addFeatures(feature);
115 map.noteSelector.unselectAll();
116 map.noteSelector.select(feature);
117 map.noteMover.activate();
118 map.noteLayer.setVisibility(true);
121 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
123 displayInLayerSwitcher: false,
124 projection: new OpenLayers.Projection("EPSG:4326"),
125 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
133 new OpenLayers.Rule({
134 filter: new OpenLayers.Filter.Comparison({
135 type: OpenLayers.Filter.Comparison.EQUAL_TO,
140 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
143 new OpenLayers.Rule({
144 filter: new OpenLayers.Filter.Comparison({
145 type: OpenLayers.Filter.Comparison.EQUAL_TO,
150 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
153 new OpenLayers.Rule({
154 filter: new OpenLayers.Filter.Comparison({
155 type: OpenLayers.Filter.Comparison.EQUAL_TO,
160 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
166 new OpenLayers.Strategy.BBOX()
168 protocol: new OpenLayers.Protocol.HTTP({
170 format: new OpenLayers.Format.GeoJSON(),
171 callback: noteCallback
175 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
176 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
177 map.noteLayer.events.register("featureselected", map, noteSelected);
178 map.noteLayer.events.register("featureunselected", map, noteUnselected);
180 map.addLayer(map.noteLayer);
182 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
186 map.addControl(map.noteSelector);
188 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
189 onDrag: function (feature, pixel) {
190 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
191 feature.popup.updatePosition();
194 over: function (feature) {
195 if (feature.attributes.status === "new") {
196 map.noteMover.overFeature.apply(map.noteMover, [feature]);
202 map.addControl(map.noteMover);
204 newNoteControls.click(addNote);
206 map.events.register("zoomend", map, allowNoteReports);
208 return map.noteLayer;