]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/notes.js.erb
36eabeaee115c3ab83fbe3f99c0274c5ed18be9b
[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         projection: new OpenLayers.Projection("EPSG:4326"),
72         styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
73             graphicWidth: 22,
74             graphicHeight: 22,
75             graphicOpacity: 0.7,
76             graphicXOffset: -11,
77             graphicYOffset: -11
78         }, {
79             rules: [
80                 new OpenLayers.Rule({
81                     filter: new OpenLayers.Filter.Comparison({
82                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
83                         property: "status",
84                         value: "new"
85                     }),
86                     symbolizer: {
87                         externalGraphic: "<%= image_path 'new_note_marker.png' %>"
88                     }
89                 }),
90                 new OpenLayers.Rule({
91                     filter: new OpenLayers.Filter.Comparison({
92                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
93                         property: "status",
94                         value: "open"
95                     }),
96                     symbolizer: {
97                         externalGraphic: "<%= image_path 'open_note_marker.png' %>"
98                     }
99                 }),
100                 new OpenLayers.Rule({
101                     filter: new OpenLayers.Filter.Comparison({
102                         type: OpenLayers.Filter.Comparison.EQUAL_TO,
103                         property: "status",
104                         value: "closed"
105                     }),
106                     symbolizer: {
107                         externalGraphic: "<%= image_path 'closed_note_marker.png' %>"
108                     }
109                 })
110             ]
111         })),
112         strategies: [
113             new OpenLayers.Strategy.BBOX()
114         ],
115         protocol: new OpenLayers.Protocol.HTTP({
116             url: notesUrl,
117             format: new OpenLayers.Format.GeoJSON(),
118             callback: noteCallback
119         })
120     });
121
122     map.noteLayer.events.register("beforefeaturesremoved", map, saveNewNotes);
123     map.noteLayer.events.register("featuresremoved", map, restoreNewNotes);
124     map.noteLayer.events.register("featureselected", map, noteSelected);
125     map.noteLayer.events.register("featureunselected", map, noteUnselected);
126
127     map.addLayer(map.noteLayer);
128
129     map.noteSelector = new OpenLayers.Control.SelectFeature(map.noteLayer, {
130         autoActivate: true
131     });
132
133     map.addControl(map.noteSelector);
134
135     map.noteMover = new OpenLayers.Control.DragFeature(map.noteLayer, {
136         onDrag: function (feature, pixel) {
137             feature.popup.lonlat = feature.geometry.getBounds().getCenterLonLat();
138             feature.popup.updatePosition();
139         },
140         featureCallbacks: {
141             over: function (feature) {
142                 if (feature.attributes.status === "new") {
143                     map.noteMover.overFeature.apply(map.noteMover, [feature]);
144                 }
145             }
146         }
147     });
148
149     map.addControl(map.noteMover);
150
151     newNoteControls.click(addNote);
152
153     map.events.register("zoomend", map, allowNoteReports);
154
155     return map.noteLayer;
156 }