1 //= require templates/notes/show
2 //= require templates/notes/new
4 $(document).ready(function () {
5 var params = OSM.mapParams();
8 function saveNewNotes(o) {
10 newNotes = layer.getFeaturesByAttribute("status", "new")
11 layer.removeFeatures(newNotes, { silent: true });
14 function restoreNewNotes(o) {
16 layer.addFeatures(newNotes);
20 function noteSelected(o) {
21 var feature = o.feature;
22 var location = feature.geometry.getBounds().getCenterLonLat();
26 if (feature.attributes.status === "new") {
27 content = JST["templates/notes/new"]();
29 onClose = function (e) {
30 feature.attributes.status = "cancelled";
32 map.noteSelector.unselect(feature);
33 map.noteLayer.removeFeatures(feature);
37 map.noteMover.deactivate();
40 content = JST["templates/notes/show"]({ note: feature.attributes });
42 onClose = function (e) {
43 map.noteSelector.unselect(feature)
47 feature.popup = new OpenLayers.Popup.FramedCloud(
48 feature.attributes.id, location, null, content, null, true, onClose
51 map.addPopup(feature.popup);
52 // feature.popup.show();
54 $(feature.popup.contentDiv).find("textarea").autoGrow();
56 $(feature.popup.contentDiv).find("input#note-add").click(function (e) {
57 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
58 var form = e.target.form;
62 $.ajax($("#createnoteanchor").attr("href"), {
67 text: $(form.comment).val()
69 success: function (data) {
70 map.noteSelector.unselect(feature);
72 feature.attributes.status = "open";
73 feature.attributes.id = data;
75 map.noteLayer.drawFeature(feature);
77 map.noteMover.deactivate();
84 feature.popup.updateSize();
87 function noteUnselected(o) {
88 var feature = o.feature;
90 map.removePopup(feature.popup);
94 var lonlat = map.getCenter();
95 var layer = map.noteLayer;
96 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
97 var feature = new OpenLayers.Feature.Vector(geometry, {
101 layer.addFeatures(feature);
102 map.noteSelector.unselectAll();
103 map.noteSelector.select(feature);
104 map.noteMover.activate();
105 map.noteLayer.setVisibility(true);
108 $("#map").on("initialised", function () {
109 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
110 visibility: params.notes,
111 displayInLayerSwitcher: false,
112 projection: new OpenLayers.Projection("EPSG:4326"),
113 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
121 new OpenLayers.Rule({
122 filter: new OpenLayers.Filter.Comparison({
123 type: OpenLayers.Filter.Comparison.EQUAL_TO,
128 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
131 new OpenLayers.Rule({
132 filter: new OpenLayers.Filter.Comparison({
133 type: OpenLayers.Filter.Comparison.EQUAL_TO,
138 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
141 new OpenLayers.Rule({
142 filter: new OpenLayers.Filter.Comparison({
143 type: OpenLayers.Filter.Comparison.EQUAL_TO,
148 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
154 new OpenLayers.Strategy.BBOX()
156 protocol: new OpenLayers.Protocol.HTTP({
157 url: $("#show_notes").attr("href"),
158 format: new OpenLayers.Format.GeoJSON()
162 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
163 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
164 map.noteLayer.events.register("featureselected", map, noteSelected);
165 map.noteLayer.events.register("featureunselected", map, noteUnselected);
167 map.addLayer(map.noteLayer);
169 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
173 map.addControl(map.noteSelector);
175 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
176 onDrag: function (feature, pixel) {
177 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
178 feature.popup.updatePosition();
181 over: function (feature) {
182 if (feature.attributes.status === "new") {
183 map.noteMover.overFeature.apply(map.noteMover, [feature]);
189 map.addControl(map.noteMover);
191 $("#show_notes").click(function (e) {
192 map.noteLayer.setVisibility(true);
197 $("#createnoteanchor").click(function (e) {
198 map.noteLayer.setVisibility(true);