1 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
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 );
14 this.div.innerHTML = attributions.join(this.separator);
21 ICON: "media/marker-normal.png",
22 SELECT_ICON: "media/marker-selected.png",
32 this.map = new OpenLayers.Map("map", {
34 new OpenLayers.Control.SypAttribution(),
35 new OpenLayers.Control.Navigation(),
36 new OpenLayers.Control.PanZoom(),
37 new OpenLayers.Control.Permalink()
39 projection: new OpenLayers.Projection("EPSG:900913"),
40 displayProjection: new OpenLayers.Projection("EPSG:4326")
43 this.baseLayer = this.createBaseLayer();
44 this.dataLayer = this.createDataLayer();
45 this.map.addLayers([this.baseLayer, this.dataLayer]);
47 this.selectControl = this.createSelectControl();
48 this.map.addControl(this.selectControl);
49 this.selectControl.activate();
51 if (!this.map.getCenter()) {
52 this.map.setCenter(new OpenLayers.LonLat(0, 0), 0);
56 createBaseLayer: function() {
57 return new OpenLayers.Layer.OSM("OSM");
60 createDataLayer: function(map) {
61 var defaultStyle = new OpenLayers.Style({
62 externalGraphic: this.Markers.ICON,
63 graphicHeight: "${height}"
66 height: function(feature) {
67 var defaultHeight = SYP.Markers.HEIGHT || 32;
68 var increase = 4 * (feature.attributes.count - 1);
69 return Math.min(defaultHeight + increase, 50);
73 var selectStyle = new OpenLayers.Style({
74 externalGraphic: this.Markers.SELECT_ICON,
75 graphicHeight: this.Markers.HEIGHT || 32
77 var styleMap = new OpenLayers.StyleMap (
78 {"default": defaultStyle,
79 "select": selectStyle});
81 var layer = new OpenLayers.Layer.GML("KML", "items.php",
84 new OpenLayers.Strategy.Cluster()
87 format: OpenLayers.Format.KML,
88 projection: this.map.displayProjection,
89 eventListeners: { scope: this,
90 loadend: this.dataLayerEndLoad
97 createSelectControl: function() {
98 var control = new OpenLayers.Control.SelectFeature(
100 onSelect: this.onFeatureSelect,
101 onUnselect: this.onFeatureUnselect,
108 dataLayerEndLoad: function() {
109 if (!this.checkForFeatures()) {
114 if (map.getControlsByClass("OpenLayers.Control.ArgParser")[0].lat
115 == undefined) { // map center was not set in ArgParser control.
116 var orig = this.Utils.mbr (this.dataLayer);
117 var centerBounds = new OpenLayers.Bounds();
119 var mapProj = map.getProjectionObject();
120 var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
122 var bottomLeft = new OpenLayers.LonLat(orig[0],orig[1]);
123 bottomLeft = bottomLeft.transform(sypOrigProj, mapProj);
124 var topRight = new OpenLayers.LonLat(orig[2],orig[3])
125 topRight = topRight.transform(sypOrigProj, mapProj);
127 centerBounds.extend(bottomLeft);
128 centerBounds.extend(topRight);
129 map.zoomToExtent(centerBounds);
133 checkForFeatures: function() {
134 var features = this.dataLayer.features;
135 if (features.length == 0) {
136 var message = SypStrings.noImageRegistered;
137 this.Utils.displayUserMessage(message, "warn");
139 return !!features.length;
142 createPopup: function(position, contentHTML) {
143 var popup = new OpenLayers.Popup.Anchored("popup",
149 popup.autoSize = true;
150 popup.backgroundColor = ""; // deal with it in css
151 popup.border = ""; // deal with it in css
152 popup.closeOnMove = true;
156 onFeatureUnselect: function (feature) {
157 var map = feature.layer.map;
158 var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
159 if (permaControl[0]) {
160 permaControl[0].div.style.display = "";
162 if (!feature.popup) {
163 this.map.events.unregister("movestart", this, this._unselect);
166 var popup = feature.popup;
167 if (popup.visible()) {
172 onFeatureSelect: function(feature) {
173 var map = feature.layer.map;
174 if (feature.attributes.count > 1) {
175 this.unselect(feature);
176 var lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y);
177 map.setCenter(lonlat, map.zoom + 1);
180 var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
181 if (permaControl[0]) {
182 permaControl[0].div.style.display = "none";
184 var popup = feature.popup;
187 switch (sypSettings.popupPos) {
189 popupPos = feature.geometry.getBounds().getCenterLonLat();
192 popupPos = SYP.Utils.tlCorner(map, 8);
195 popupPos = SYP.Utils.trCorner(map, 8);
198 popupPos = SYP.Utils.brCorner(map, 8);
201 popupPos = SYP.Utils.blCorner(map, 8);
204 popupPos = SYP.Utils.brCorner(map, 8);
208 // we cannot reuse popup; we need to recreate it in order for IE
209 // expressions to work. Otherwise, we get a 0x0 image on second view.
214 if (feature.cluster[0].attributes.name) {
215 contentHTML = "<h2>" +
216 feature.cluster[0].attributes.name +
218 feature.cluster[0].attributes.description;
220 contentHTML = feature.cluster[0].attributes.description;
222 if (!contentHTML || !contentHTML.length) {
223 this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
226 popup = SYP.createPopup(popupPos, contentHTML);
228 popup.hide = function () {
229 OpenLayers.Element.hide(this.div);
230 control.unselectAll();
233 feature.popup = popup;
234 var anchor = popup.div.getElementsByTagName("a")[0];
236 anchor.onclick = function() {
237 SYP.showBigImage(this.href);
243 showBigImage: function (href) {
244 if (OpenLayers.Util.getBrowserName() == "msie") {
245 document.getElementById('bigimg_container').style.display = "block";
247 document.getElementById('bigimg_container').style.display = "table";
250 var maxHeight = document.body.clientHeight * 0.9;
251 var maxWidth = document.body.clientWidth * 0.9;
252 document.getElementById('bigimg').style.height = "";
253 document.getElementById('bigimg').style.width = "";
254 document.getElementById('bigimg').style.maxHeight = maxHeight + "px";
255 document.getElementById('bigimg').style.maxWidth = maxWidth + "px";
256 document.getElementById('bigimg').onload = function () {
257 var heightRatio = this.clientHeight / parseInt(this.style.maxHeight);
258 var widthRatio = this.clientWidth / parseInt(this.style.maxWidth);
259 if (heightRatio > 1 || widthRatio > 1) {
260 if (heightRatio > widthRatio) {
261 this.style.height = this.style.maxHeight;
263 this.style.width = this.style.maxWidth;
267 var icon = document.getElementById('bigimg_close');
268 icon.style.top = this.offsetTop;
269 icon.style.left = this.offsetLeft + this.clientWidth - icon.clientWidth;
272 document.getElementById('bigimg').src = href;
275 closeBigImage: function() {
276 document.getElementById('bigimg').src = "";
277 document.getElementById('bigimg').parentNode.innerHTML = document.getElementById('bigimg').parentNode.innerHTML;
278 document.getElementById('bigimg_container').style.display = "none";
282 tlCorner: function(map, margin) {
283 var bounds = map.calculateBounds();
284 var corner = new OpenLayers.LonLat(bounds.left, bounds.top);
285 var cornerAsPx = map.getPixelFromLonLat(corner);
286 cornerAsPx = cornerAsPx.add( +margin, +margin);
287 return map.getLonLatFromPixel(cornerAsPx);
290 trCorner: function(map, margin) {
291 var bounds = map.calculateBounds();
292 var corner = new OpenLayers.LonLat(bounds.right, bounds.top);
293 var cornerAsPx = map.getPixelFromLonLat(corner);
294 cornerAsPx = cornerAsPx.add( -margin, +margin);
295 return map.getLonLatFromPixel(cornerAsPx);
298 brCorner: function(map, margin) {
299 var bounds = map.calculateBounds();
300 var corner = new OpenLayers.LonLat(bounds.right, bounds.bottom);
301 var cornerAsPx = map.getPixelFromLonLat(corner);
302 cornerAsPx = cornerAsPx.add( -margin, -margin);
303 return map.getLonLatFromPixel(cornerAsPx);
306 blCorner: function(map, margin) {
307 var bounds = map.calculateBounds();
308 var corner = new OpenLayers.LonLat(bounds.left, bounds.bottom);
309 var cornerAsPx = map.getPixelFromLonLat(corner);
310 cornerAsPx = cornerAsPx.add( +margin, -margin);
311 return map.getLonLatFromPixel(cornerAsPx);
314 /* minimum bounds rectangle containing all feature locations.
315 * FIXME: if two features are close, but separated by 180th meridian,
316 * their mbr will span the whole earth. Actually, 179° lon and -170°
317 * lon are considerated very near.
319 mbr: function (layer) {
323 var mapProj = map.getProjectionObject();
324 var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
326 for (var i =0; i < layer.features.length; i++) {
327 if (layer.features[i].cluster) {
328 features = features.concat(layer.features[i].cluster);
330 features = features.concat(layer.features);
339 if (features.length == 0) {
340 // keep default values
341 } else if (features.length == 1) {
342 // in case there's only one feature, we show an area of at least
344 var pos = features[0].geometry.getBounds().getCenterLonLat().clone();
345 var lonlat = pos.transform(mapProj, sypOrigProj);
347 minlon = Math.max (lonlat.lon - 2, -180);
348 maxlon = Math.min (lonlat.lon + 2, 180);
349 minlat = Math.max (lonlat.lat - 2, -90);
350 maxlat = Math.min (lonlat.lat + 2, 90);
352 for (var i = 0; i < features.length; i++) {
353 var pos = features[i].geometry.getBounds().getCenterLonLat().clone();
354 var lonlat = pos.transform(mapProj, sypOrigProj);
355 minlon = Math.min (lonlat.lon, minlon);
356 minlat = Math.min (lonlat.lat, minlat);
357 maxlon = Math.max (lonlat.lon, maxlon);
358 maxlat = Math.max (lonlat.lat, maxlat);
362 return [minlon, minlat, maxlon, maxlat];
366 displayUserMessage: function(message, status) {
367 var div = document.getElementById('message');
368 while (div.firstChild)
369 div.removeChild(div.firstChild);
370 var textNode = document.createTextNode(message);
373 div.style.color = "red";
376 div.style.color = "#FF8C00";
379 div.style.color = "green";
382 div.style.color = "black";
385 div.style.display = "block";
386 div.appendChild(textNode);
391 // if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
392 // navigator.language
393 if (OpenLayers.Lang[SypStrings.language]) {
394 OpenLayers.Lang.setCode(SypStrings.language);
398 OpenLayers.Console.userError = function(error) {
399 SYP.Utils.displayUserMessage(error, "error");
402 // sometimes, especially when cache is clear, firefox does not compute
403 // correctly popup size. That's because at the end of getRenderedDimensions,
404 // dimensions of image is not known. Then, popup size is too small for its
405 // content. We work around the problem by checking that computed size is at
406 // least as big as content. To achieve that, we need to override
407 // OpenLayers.Popup.Anchored.prototype.updateSize to modify it slightly.
408 OpenLayers.Popup.Anchored.prototype.updateSize = function() {
409 // determine actual render dimensions of the contents by putting its
410 // contents into a fake contentDiv (for the CSS) and then measuring it
411 var preparedHTML = "<div class='" + this.contentDisplayClass+ "'>" +
412 this.contentDiv.innerHTML +
415 var containerElement = (this.map) ? this.map.layerContainerDiv
417 var realSize = OpenLayers.Util.getRenderedDimensions(
418 preparedHTML, null, {
419 displayClass: this.displayClass,
420 containerElement: containerElement
425 * XXX: next four lines are added by SYP!
427 if (this.contentDiv) {
428 realSize.w = Math.max (realSize.w, this.contentDiv.scrollWidth);
429 realSize.h = Math.max (realSize.h, this.contentDiv.scrollHeight);
432 // is the "real" size of the div is safe to display in our map?
433 var safeSize = this.getSafeContentSize(realSize);
436 if (safeSize.equals(realSize)) {
437 //real size of content is small enough to fit on the map,
438 // so we use real size.
443 //make a new OL.Size object with the clipped dimensions
444 // set or null if not clipped.
445 var fixedSize = new OpenLayers.Size();
446 fixedSize.w = (safeSize.w < realSize.w) ? safeSize.w : null;
447 fixedSize.h = (safeSize.h < realSize.h) ? safeSize.h : null;
449 if (fixedSize.w && fixedSize.h) {
450 //content is too big in both directions, so we will use
451 // max popup size (safeSize), knowing well that it will
452 // overflow both ways.
455 //content is clipped in only one direction, so we need to
456 // run getRenderedDimensions() again with a fixed dimension
457 var clippedSize = OpenLayers.Util.getRenderedDimensions(
458 preparedHTML, fixedSize, {
459 displayClass: this.contentDisplayClass,
460 containerElement: containerElement
464 //if the clipped size is still the same as the safeSize,
465 // that means that our content must be fixed in the
466 // offending direction. If overflow is 'auto', this means
467 // we are going to have a scrollbar for sure, so we must
470 var currentOverflow = OpenLayers.Element.getStyle(
471 this.contentDiv, "overflow"
473 if ( (currentOverflow != "hidden") &&
474 (clippedSize.equals(safeSize)) ) {
475 var scrollBar = OpenLayers.Util.getScrollbarWidth();
477 clippedSize.h += scrollBar;
479 clippedSize.w += scrollBar;
483 newSize = this.getSafeContentSize(clippedSize);
486 this.setSize(newSize);