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("textarea").on("input", function (e) {
57 var form = e.target.form;
59 if ($(e.target).val() == "") {
60 $(form.close).val(I18n.t("javascripts.notes.show.close"));
62 $(form.close).val(I18n.t("javascripts.notes.show.comment_and_close"));
66 $(feature.popup.contentDiv).find("input#note-add").click(function (e) {
67 var location = unproj(feature.geometry.getBounds().getCenterLonLat());
68 var form = e.target.form;
72 $.ajax($("#createnoteanchor").attr("href"), {
77 text: $(form.comment).val()
79 success: function (data) {
80 map.noteSelector.unselect(feature);
82 feature.attributes.status = "open";
83 feature.attributes.id = data;
85 map.noteLayer.drawFeature(feature);
87 map.noteMover.deactivate();
92 $(feature.popup.contentDiv).find("input#note-comment").click(function (e) {
93 var form = e.target.form;
97 $.ajax(feature.attributes.comment_url, {
100 text: $(form.text).val()
102 success: function (data) {
103 map.noteSelector.unselect(feature)
105 feature.attributes = data.properties;
107 map.noteSelector.select(feature)
112 $(feature.popup.contentDiv).find("input#note-close").click(function (e) {
113 var form = e.target.form;
117 $.ajax(feature.attributes.close_url, {
120 text: $(form.text).val()
122 success: function (data) {
123 map.noteSelector.unselect(feature)
125 feature.attributes = data.properties;
127 map.noteSelector.select(feature)
132 feature.popup.updateSize();
135 function noteUnselected(o) {
136 var feature = o.feature;
138 map.removePopup(feature.popup);
142 var lonlat = map.getCenter();
143 var layer = map.noteLayer;
144 var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
145 var feature = new OpenLayers.Feature.Vector(geometry, {
149 layer.addFeatures(feature);
150 map.noteSelector.unselectAll();
151 map.noteSelector.select(feature);
152 map.noteMover.activate();
153 map.noteLayer.setVisibility(true);
156 $("#map").on("initialised", function () {
157 map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
158 visibility: params.notes,
159 displayInLayerSwitcher: false,
160 projection: new OpenLayers.Projection("EPSG:4326"),
161 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
169 new OpenLayers.Rule({
170 filter: new OpenLayers.Filter.Comparison({
171 type: OpenLayers.Filter.Comparison.EQUAL_TO,
176 externalGraphic: "<%= image_path 'new_note_marker.png' %>"
179 new OpenLayers.Rule({
180 filter: new OpenLayers.Filter.Comparison({
181 type: OpenLayers.Filter.Comparison.EQUAL_TO,
186 externalGraphic: "<%= image_path 'open_note_marker.png' %>"
189 new OpenLayers.Rule({
190 filter: new OpenLayers.Filter.Comparison({
191 type: OpenLayers.Filter.Comparison.EQUAL_TO,
196 externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
202 new OpenLayers.Strategy.BBOX()
204 protocol: new OpenLayers.Protocol.HTTP({
205 url: $("#show_notes").attr("href"),
206 format: new OpenLayers.Format.GeoJSON()
210 map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
211 map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
212 map.noteLayer.events.register("featureselected", map, noteSelected);
213 map.noteLayer.events.register("featureunselected", map, noteUnselected);
215 map.addLayer(map.noteLayer);
217 map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
221 map.addControl(map.noteSelector);
223 map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
224 onDrag: function (feature, pixel) {
225 feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
226 feature.popup.updatePosition();
229 over: function (feature) {
230 if (feature.attributes.status === "new") {
231 map.noteMover.overFeature.apply(map.noteMover, [feature]);
237 map.addControl(map.noteMover);
239 $("#show_notes").click(function (e) {
240 map.noteLayer.setVisibility(true);
245 $("#createnoteanchor").click(function (e) {
246 map.noteLayer.setVisibility(true);