]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/notes.js.erb
6c598120a109f9d5e8f3fae6e099a893e816bb0c
[rails.git] / app / assets / javascripts / notes.js.erb
1 function addNoteLayer(map, notesUrl, newNoteControls, minZoom) {
2     var newNotes;
3
4     var noteCallback = function (scope, response) {
5         for (var f = 0; f < response.features.length; f++) {
6             var feature = response.features[f];
7         }
8     };
9
10     var saveNewNotes = function (o) {
11         var layer = o.object;
12         newNotes = layer.getFeaturesByAttribute("status", "new")
13         layer.removeFeatures(newNotes, { silent: true });
14     };
15
16     var restoreNewNotes = function (o) {
17         var layer = o.object;
18         layer.addFeatures(newNotes);
19         newNotes = undefined;
20     };
21
22     var noteSelected = function (o) {
23         var feature = o.feature;
24         var location = feature.geometry.getBounds().getCenterLonLat();
25
26         feature.popup = new OpenLayers.Popup.FramedCloud(
27             feature.attributes.id, location, null,
28             "<p>" + feature.attributes.id + "</p>",
29             null, 
30             feature.attributes.status !== "new",
31             function (e) { map.noteSelector.unselect(feature) }
32         );
33
34         map.addPopup(feature.popup);
35         //    feature.popup.show();
36     };
37
38     var noteUnselected = function (o) {
39         var feature = o.feature;
40
41         map.removePopup(feature.popup);
42
43         delete feature.popup;
44     };
45
46     var allowNoteReports = function () { 
47         if (map.getZoom() > minZoom) {
48             newNoteControls.show();
49         } else {
50             newNoteControls.hide();
51         }
52     };
53
54     var addNote = function () {
55         var lonlat = map.getCenter();
56         var layer = map.noteLayer;
57         var geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
58         var feature = new OpenLayers.Feature.Vector(geometry, {
59             status: "new"
60         });
61
62         layer.addFeatures(feature);
63         map.noteSelector.unselectAll();
64         map.noteSelector.select(feature);
65         map.noteMover.activate();
66         map.noteLayer.setVisibility(true);
67     };
68
69     map.noteLayer = new OpenLayers.Layer.Vector("Notes", {
70         visibility: false,
71         displayInLayerSwitcher: false,
72         projection: new OpenLayers.Projection("EPSG:4326"),
73         styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
74             graphicWidth: 22,
75             graphicHeight: 22,
76             graphicOpacity: 0.7,
77             graphicXOffset: -11,
78             graphicYOffset: -11
79         }, {
80             rules: [
81                 new OpenLayers.Rule({
82                     filter: new OpenLayers.Filter.Comparison({
83                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
84                         property: "status",
85                         value: "new"
86                     }),
87                     symbolizer: {
88                         externalGraphic: "<%= image_path 'new_note_marker.png' %>"
89                     }
90                 }),
91                 new OpenLayers.Rule({
92                     filter: new OpenLayers.Filter.Comparison({
93                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
94                         property: "status",
95                         value: "open"
96                     }),
97                     symbolizer: {
98                         externalGraphic: "<%= image_path 'open_note_marker.png' %>"
99                     }
100                 }),
101                 new OpenLayers.Rule({
102                     filter: new OpenLayers.Filter.Comparison({
103                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
104                         property: "status",
105                         value: "closed"
106                     }),
107                     symbolizer: {
108                         externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
109                     }
110                 })
111             ]
112         })),
113         strategies: [
114             new OpenLayers.Strategy.BBOX()
115         ],
116         protocol: new OpenLayers.Protocol.HTTP({
117             url: notesUrl,
118             format: new OpenLayers.Format.GeoJSON(),
119             callback: noteCallback
120         })
121     });
122
123     map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
124     map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
125     map.noteLayer.events.register("featureselected", map, noteSelected);
126     map.noteLayer.events.register("featureunselected", map, noteUnselected);
127
128     map.addLayer(map.noteLayer);
129
130     map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
131         autoActivate: true
132     });
133
134     map.addControl(map.noteSelector);
135
136     map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
137         onDrag: function (feature, pixel) {
138             feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
139             feature.popup.updatePosition();
140         },
141         featureCallbacks: {
142             over: function (feature) {
143                 if (feature.attributes.status === "new") {
144                     map.noteMover.overFeature.apply(map.noteMover, [feature]);
145                 }
146             }
147         }
148     });
149
150     map.addControl(map.noteMover);
151
152     newNoteControls.click(addNote);
153
154     map.events.register("zoomend", map, allowNoteReports);
155
156     return map.noteLayer;
157 }