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 createNote(feature, form) {
21 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
23 $.ajax($("#createnoteanchor").attr("href"), {
28 text: $(form.comment).val()
30 success: function (data) {
31 map.noteSelector.unselect(feature);
33 feature.attributes.status = "open";
34 feature.attributes.id = data;
36 map.noteLayer.drawFeature(feature);
38 map.noteMover.deactivate();
43 function updateNote(feature, form, close) {
44 var url = close ? feature.attributes.close_url : feature.attributes.comment_url;
49 text: $(form.text).val()
51 success: function (data) {
52 map.noteSelector.unselect(feature)
54 feature.attributes = data.properties;
56 map.noteSelector.select(feature)
61 function noteSelected(o) {
62 var feature = o.feature;
63 var location = feature.geometry.getBounds().getCenterLonLat();
67 if (feature.attributes.status === "new") {
68 content = JST["templates/notes/new"]();
70 onClose = function (e) {
71 feature.attributes.status = "cancelled";
73 map.noteSelector.unselect(feature);
74 map.noteLayer.removeFeatures(feature);
78 map.noteMover.deactivate();
81 content = JST["templates/notes/show"]({ note: feature.attributes });
83 onClose = function (e) {
84 map.noteSelector.unselect(feature)
88 feature.popup = new OpenLayers.Popup.FramedCloud(
89 feature.attributes.id, location, null, content, null, true, onClose
92 map.addPopup(feature.popup);
93 // feature.popup.show();
95 $(feature.popup.contentDiv).find("textarea").autoGrow();
97 $(feature.popup.contentDiv).find("textarea").on("input", function (e) {
98 var form = e.target.form;
100 if ($(e.target).val() == "") {
101 $(form.close).val(I18n.t("javascripts.notes.show.close"));
103 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
107 $(feature.popup.contentDiv).find("input#note-add").click(function (e) {
110 createNote(feature, e.target.form);
113 $(feature.popup.contentDiv).find("input#note-comment").click(function (e) {
116 updateNote(feature, e.target.form, false);
119 $(feature.popup.contentDiv).find("input#note-close").click(function (e) {
122 updateNote(feature, e.target.form, true);
125 feature.popup.updateSize();
128 function noteUnselected(o) {
129 var feature = o.feature;
131 map.removePopup(feature.popup);
135 var lonlat = map.getCenter();
136 var layer = map.noteLayer;
137 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
138 var feature = new OpenLayers.Feature.Vector(geometry, {
142 layer.addFeatures(feature);
143 map.noteSelector.unselectAll();
144 map.noteSelector.select(feature);
145 map.noteMover.activate();
146 map.noteLayer.setVisibility(true);
149 $("#map").on("initialised", function () {
150 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
151 visibility: params.notes,
152 displayInLayerSwitcher: false,
153 projection: new OpenLayers.Projection("EPSG:4326"),
154 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
162 new OpenLayers.Rule({
163 filter: new OpenLayers.Filter.Comparison({
164 type: OpenLayers.Filter.Comparison.EQUAL_TO,
169 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
172 new OpenLayers.Rule({
173 filter: new OpenLayers.Filter.Comparison({
174 type: OpenLayers.Filter.Comparison.EQUAL_TO,
179 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
182 new OpenLayers.Rule({
183 filter: new OpenLayers.Filter.Comparison({
184 type: OpenLayers.Filter.Comparison.EQUAL_TO,
189 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
195 new OpenLayers.Strategy.BBOX()
197 protocol: new OpenLayers.Protocol.HTTP({
198 url: $("#show_notes").attr("href"),
199 format: new OpenLayers.Format.GeoJSON()
203 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
204 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
205 map.noteLayer.events.register("featureselected", map, noteSelected);
206 map.noteLayer.events.register("featureunselected", map, noteUnselected);
208 map.addLayer(map.noteLayer);
210 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
214 map.addControl(map.noteSelector);
216 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
217 onDrag: function (feature, pixel) {
218 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
219 feature.popup.updatePosition();
222 over: function (feature) {
223 if (feature.attributes.status === "new") {
224 map.noteMover.overFeature.apply(map.noteMover, [feature]);
230 map.addControl(map.noteMover);
232 $("#show_notes").click(function (e) {
233 map.noteLayer.setVisibility(true);
238 $("#createnoteanchor").click(function (e) {
239 map.noteLayer.setVisibility(true);