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 noteSelected = function (o) {
17 var feature = o.feature;
18 var location = feature.geometry.getBounds().getCenterLonLat();
22 if (feature.attributes.status === "new") {
23 var form = newNoteForm.clone();
24 form.removeClass("hidden");
25 content = form.html();
28 content = "<p>" + feature.attributes.id + "</p>";
32 feature.popup = new OpenLayers.Popup.FramedCloud(
33 feature.attributes.id, location, null, content, null, close,
34 function (e) { map.noteSelector.unselect(feature) }
37 map.addPopup(feature.popup);
38 // feature.popup.show();
40 $(feature.popup.contentDiv).find("textarea").autoGrow();
42 $(feature.popup.contentDiv).find("input#note-submit").click(function (e) {
43 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
44 var form = $(e.target).parents("form").first();
46 $.ajax(form.prop("action"), {
47 type: form.prop("method"),
51 text: form.find("textarea#comment").val()
53 success: function (data) {
54 map.noteSelector.unselect(feature);
56 feature.attributes.status = "open";
57 feature.attributes.id = data;
59 map.noteLayer.drawFeature(feature);
61 map.noteMover.deactivate();
68 $(feature.popup.contentDiv).find("input#note-cancel").click(function (e) {
69 feature.attributes.status = "cancelled";
71 map.noteSelector.unselect(feature);
72 map.noteLayer.removeFeatures(feature);
76 map.noteMover.deactivate();
81 feature.popup.updateSize();
84 var noteUnselected = function (o) {
85 var feature = o.feature;
87 map.removePopup(feature.popup);
92 var allowNoteReports = function () {
93 if (map.getZoom() > minZoom) {
94 newNoteControls.show();
96 newNoteControls.hide();
100 var addNote = function () {
101 var lonlat = map.getCenter();
102 var layer = map.noteLayer;
103 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
104 var feature = new OpenLayers.Feature.Vector(geometry, {
108 layer.addFeatures(feature);
109 map.noteSelector.unselectAll();
110 map.noteSelector.select(feature);
111 map.noteMover.activate();
112 map.noteLayer.setVisibility(true);
115 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
117 displayInLayerSwitcher: false,
118 projection: new OpenLayers.Projection("EPSG:4326"),
119 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
127 new OpenLayers.Rule({
128 filter: new OpenLayers.Filter.Comparison({
129 type: OpenLayers.Filter.Comparison.EQUAL_TO,
134 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
137 new OpenLayers.Rule({
138 filter: new OpenLayers.Filter.Comparison({
139 type: OpenLayers.Filter.Comparison.EQUAL_TO,
144 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
147 new OpenLayers.Rule({
148 filter: new OpenLayers.Filter.Comparison({
149 type: OpenLayers.Filter.Comparison.EQUAL_TO,
154 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
160 new OpenLayers.Strategy.BBOX()
162 protocol: new OpenLayers.Protocol.HTTP({
164 format: new OpenLayers.Format.GeoJSON()
168 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
169 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
170 map.noteLayer.events.register("featureselected", map, noteSelected);
171 map.noteLayer.events.register("featureunselected", map, noteUnselected);
173 map.addLayer(map.noteLayer);
175 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
179 map.addControl(map.noteSelector);
181 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
182 onDrag: function (feature, pixel) {
183 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
184 feature.popup.updatePosition();
187 over: function (feature) {
188 if (feature.attributes.status === "new") {
189 map.noteMover.overFeature.apply(map.noteMover, [feature]);
195 map.addControl(map.noteMover);
197 newNoteControls.click(addNote);
199 map.events.register("zoomend", map, allowNoteReports);
201 return map.noteLayer;