]> dev.renevier.net Git - syp.git/blob - js/syp.js
rename marker files
[syp.git] / js / syp.js
1 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
2  * license. */
3
4 OpenLayers.Control.SypAttribution = OpenLayers.Class (OpenLayers.Control.Attribution, {
5     updateAttribution: function() {
6         var attributions = [SypStrings.propulsedByLink];
7         if (this.map && this.map.layers) {
8             for(var i=0, len=this.map.layers.length; i<len; i++) {
9                 var layer = this.map.layers[i];
10                 if (layer.attribution && layer.getVisibility()) {
11                     attributions.push( layer.attribution );
12                 }
13             }  
14             this.div.innerHTML = attributions.join(this.separator);
15         }
16     }
17 });
18
19 var SYP = {
20     Settings: {
21         MARKER_ICON: "media/marker-normal.png",
22         MARKER_SELECT_ICON: "media/marker-selected.png",
23         MARKER_HEIGHT: 25
24     },
25
26     map: null,
27     baseLayer: null,
28     dataLayer: null,
29     selectControl: null,
30
31     init: function() {
32         this.map = new OpenLayers.Map("map", {
33             controls:[
34                 new OpenLayers.Control.SypAttribution(),
35                 new OpenLayers.Control.Navigation(),
36                 new OpenLayers.Control.PanZoom(),
37                 new OpenLayers.Control.Permalink()
38             ],
39             projection: new OpenLayers.Projection("EPSG:900913"),
40             displayProjection: new OpenLayers.Projection("EPSG:4326")
41         } );
42
43         this.baseLayer = this.createBaseLayer();
44         this.dataLayer = this.createDataLayer();
45         this.map.addLayers([this.baseLayer, this.dataLayer]);
46
47         this.selectControl = this.createSelectControl();
48         this.map.addControl(this.selectControl);
49         this.selectControl.activate();
50
51         if (!this.map.getCenter()) {
52             var centerBounds = new OpenLayers.Bounds();
53
54             var mapProj = this.map.getProjectionObject();
55             var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
56
57             var bottomLeft = new OpenLayers.LonLat(sypOrig[0],sypOrig[1]);
58             bottomLeft = bottomLeft.transform(sypOrigProj, mapProj);
59             var topRight = new OpenLayers.LonLat(sypOrig[2],sypOrig[3])
60             topRight = topRight.transform(sypOrigProj, mapProj);
61
62             centerBounds.extend(bottomLeft);
63             centerBounds.extend(topRight);
64             this.map.zoomToExtent(centerBounds);
65         }
66     },
67
68     createBaseLayer: function() {
69         return new OpenLayers.Layer.OSM("OSM");
70     },
71
72     createDataLayer: function(map) {
73         var defaultStyle = new OpenLayers.Style({
74             externalGraphic: this.Settings.MARKER_ICON,
75             graphicHeight: "${height}"
76         }, {
77             context: {
78                 height: function(feature) {
79                     var defaultHeight = SYP.Settings.MARKER_HEIGHT || 32;
80                     var increase = 4 * (feature.attributes.count - 1);
81                     return Math.min(defaultHeight + increase, 50);
82                 }
83             }
84         });
85         var selectStyle = new OpenLayers.Style({
86             externalGraphic: this.Settings.MARKER_SELECT_ICON,
87             graphicHeight: this.Settings.MARKER_HEIGHT || 32 
88         });
89         var styleMap = new OpenLayers.StyleMap (
90                         {"default": defaultStyle,
91                          "select": selectStyle});
92
93         var layer = new OpenLayers.Layer.GML("KML", "items.php", 
94            {
95                strategies: [
96                 new OpenLayers.Strategy.Cluster()
97                 ],
98             styleMap: styleMap,
99             format: OpenLayers.Format.KML, 
100             projection: this.map.displayProjection,
101             eventListeners: { scope: this,
102                               loadend: this.checkForFeatures
103                             }
104            });
105
106         return layer;
107     },
108
109     createSelectControl: function() {
110         var control = new OpenLayers.Control.SelectFeature(
111                                             this.dataLayer, {
112                                                onSelect: this.onFeatureSelect,
113                                                onUnselect: this.onFeatureUnselect,
114                                                toggle: true,
115                                                clickout: false
116                                                             });
117         return control;
118     },
119
120     checkForFeatures: function() {
121         var features = this.dataLayer.features;
122         if (features.length == 0) {
123             var message = SypStrings.noImageRegistered;
124             this.Utils.displayUserMessage(message, "warn");
125         }
126     },
127
128     createPopup: function(position, contentHTML) {
129         var popup = new OpenLayers.Popup.Anchored("popup",
130                                                   position,
131                                                   null,
132                                                   contentHTML,
133                                                   null,
134                                                   true);
135         popup.autoSize = true;
136         popup.backgroundColor = ""; // deal with it in css
137         popup.border = ""; // deal with it in css
138         popup.closeOnMove = true;
139         return popup;
140     },
141
142     onFeatureUnselect: function (feature) {
143         var map = feature.layer.map;
144         var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
145         if (permaControl[0]) {
146             permaControl[0].div.style.display = "";
147         }
148         if (!feature.popup) {
149             this.map.events.unregister("movestart", this, this._unselect);
150             return;
151         }
152         var popup = feature.popup;
153         if (popup.visible()) {
154             popup.hide();
155         }
156     },
157
158     onFeatureSelect: function(feature) {
159         var map = feature.layer.map;
160         if (feature.attributes.count > 1) {
161             this.unselect(feature);
162             var lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y);
163             map.setCenter(lonlat, map.zoom + 1);
164             return;
165         }
166         var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
167         if (permaControl[0]) {
168             permaControl[0].div.style.display = "none";
169         }
170         var popup = feature.popup;
171
172         var brCorner = SYP.Utils.brCorner(map, 8);
173
174         // we cannot reuse popup; we need to recreate it in order for IE
175         // expressions to work. Otherwise, we get a 0x0 image on second view.
176         if (popup) {
177             popup.destroy();
178         }
179         var contentHTML;
180         if (feature.cluster[0].attributes.name) {
181             contentHTML = "<h2>" +
182                           feature.cluster[0].attributes.name + 
183                           "</h2>" + 
184                           feature.cluster[0].attributes.description;
185         } else {
186             contentHTML = feature.cluster[0].attributes.description;
187         }
188         if (!contentHTML || !contentHTML.length) {
189             this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
190             return;
191         }
192         popup = SYP.createPopup(brCorner, contentHTML);
193         var control = this;
194         popup.hide = function () {
195             OpenLayers.Element.hide(this.div);
196             control.unselectAll();
197         };
198         map.addPopup(popup);
199         feature.popup = popup;
200         var anchor = popup.div.getElementsByTagName("a")[0];
201         if (anchor) {
202             anchor.onclick = function() { 
203                 SYP.showBigImage(this.href);
204                 return false;
205             }
206         }
207     },
208
209     showBigImage: function (href) {
210         try {
211             document.getElementById('bigimg_container').style.display = "table";
212         } catch(e) {
213             document.getElementById('bigimg_container').style.display = "block";
214         }
215         var maxHeight = document.body.clientHeight * 0.9;
216         var maxWidth = document.body.clientWidth * 0.9;
217         document.getElementById('bigimg').style.height = "";
218         document.getElementById('bigimg').style.width = "";
219         document.getElementById('bigimg').style.maxHeight = maxHeight + "px";
220         document.getElementById('bigimg').style.maxWidth = maxWidth + "px";
221         document.getElementById('bigimg').onload = function () {
222             var icon = document.getElementById('bigimg_close');
223             icon.style.top = this.offsetTop;
224             icon.style.left = this.offsetLeft + this.clientWidth - icon.clientWidth;
225
226             var heightRatio = this.clientHeight / parseInt(this.style.maxHeight);
227             var widthRatio = this.clientWidth / parseInt(this.style.maxWidth);
228             if (heightRatio > 1 || widthRatio > 1) {
229                 if (heightRatio > widthRatio) {
230                     this.style.height = this.style.maxHeight;
231                 } else {
232                     this.style.width = this.style.maxWidth;
233                 }
234             }
235
236         };
237         document.getElementById('bigimg').src = href;
238     },
239
240     closeBigImage: function() {
241         document.getElementById('bigimg').src = "";
242         document.getElementById('bigimg').parentNode.innerHTML = document.getElementById('bigimg').parentNode.innerHTML;
243         document.getElementById('bigimg_container').style.display = "none";
244     },
245
246     Utils: {
247         brCorner: function(map, margin) {
248             var bounds = map.calculateBounds();
249             var corner = new OpenLayers.LonLat(bounds.right, bounds.bottom);
250             var cornerAsPx = map.getPixelFromLonLat(corner);
251             cornerAsPx = cornerAsPx.add( -margin, -margin);
252             corner = map.getLonLatFromPixel(cornerAsPx);
253             return corner;
254         },
255         displayUserMessage: function(message, status) {
256             var div = document.getElementById('message');
257             while (div.firstChild)
258                 div.removeChild(div.firstChild);
259             var textNode = document.createTextNode(message);
260             switch (status) {
261                 case "error":
262                     div.style.color = "red";
263                     break;
264                 case "warn":
265                     div.style.color = "#FF8C00";
266                     break;
267                 case "success":
268                     div.style.color = "green";
269                     break;
270                 default:
271                     div.style.color = "black";
272                     break;
273             }
274             div.style.display = "block";
275             div.appendChild(textNode);
276         }
277     }
278 };
279
280 // if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
281 // navigator.language
282 if (OpenLayers.Lang[SypStrings.language]) {
283     OpenLayers.Lang.setCode(SypStrings.language);
284 }
285
286 // avoid alerts
287 OpenLayers.Console.userError = function(error) { 
288     SYP.Utils.displayUserMessage(error, "error");
289 }
290
291 // sometimes, especially when cache is clear, firefox does not compute
292 // correctly popup size. That's because at the end of getRenderedDimensions,
293 // dimensions of image is not known. So, we work around that problem by setting
294 // image width and image height. That way, dimensions of image are set in
295 // innerHTML, and are therefore known in getRenderedDimensions
296 OpenLayers.Popup.Anchored.prototype.registerImageListeners = function() {
297     var onImgLoad = function() {
298         this.img.width =  this.img.width;
299         this.img.height =  this.img.height;
300         this.popup.updateSize();
301         OpenLayers.Event.stopObserving(
302             this.img, "load", this.img._onImageLoad
303         );
304     };
305
306     var images = this.contentDiv.getElementsByTagName("img");
307     for (var i = 0, len = images.length; i < len; i++) {
308         var img = images[i];
309         if (img.width == 0 || img.height == 0) {
310
311             var context = {
312                 'popup': this,
313                 'img': img
314             };
315
316             img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
317
318             OpenLayers.Event.observe(img, 'load', img._onImgLoad);
319         }    
320     } 
321 }