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 MARKER_ICON: "media/marker-normal.png",
22 MARKER_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 var centerBounds = new OpenLayers.Bounds();
54 var mapProj = this.map.getProjectionObject();
55 var sypOrigProj = new OpenLayers.Projection("EPSG:4326");
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);
62 centerBounds.extend(bottomLeft);
63 centerBounds.extend(topRight);
64 this.map.zoomToExtent(centerBounds);
68 createBaseLayer: function() {
69 return new OpenLayers.Layer.OSM("OSM");
72 createDataLayer: function(map) {
73 var defaultStyle = new OpenLayers.Style({
74 externalGraphic: this.Settings.MARKER_ICON,
75 graphicHeight: "${height}"
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);
85 var selectStyle = new OpenLayers.Style({
86 externalGraphic: this.Settings.MARKER_SELECT_ICON,
87 graphicHeight: this.Settings.MARKER_HEIGHT || 32
89 var styleMap = new OpenLayers.StyleMap (
90 {"default": defaultStyle,
91 "select": selectStyle});
93 var layer = new OpenLayers.Layer.GML("KML", "items.php",
96 new OpenLayers.Strategy.Cluster()
99 format: OpenLayers.Format.KML,
100 projection: this.map.displayProjection,
101 eventListeners: { scope: this,
102 loadend: this.checkForFeatures
109 createSelectControl: function() {
110 var control = new OpenLayers.Control.SelectFeature(
112 onSelect: this.onFeatureSelect,
113 onUnselect: this.onFeatureUnselect,
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");
128 createPopup: function(position, contentHTML) {
129 var popup = new OpenLayers.Popup.Anchored("popup",
135 popup.autoSize = true;
136 popup.backgroundColor = ""; // deal with it in css
137 popup.border = ""; // deal with it in css
138 popup.closeOnMove = true;
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 = "";
148 if (!feature.popup) {
149 this.map.events.unregister("movestart", this, this._unselect);
152 var popup = feature.popup;
153 if (popup.visible()) {
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);
166 var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
167 if (permaControl[0]) {
168 permaControl[0].div.style.display = "none";
170 var popup = feature.popup;
172 var brCorner = SYP.Utils.brCorner(map, 8);
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.
180 if (feature.cluster[0].attributes.name) {
181 contentHTML = "<h2>" +
182 feature.cluster[0].attributes.name +
184 feature.cluster[0].attributes.description;
186 contentHTML = feature.cluster[0].attributes.description;
188 if (!contentHTML || !contentHTML.length) {
189 this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
192 popup = SYP.createPopup(brCorner, contentHTML);
194 popup.hide = function () {
195 OpenLayers.Element.hide(this.div);
196 control.unselectAll();
199 feature.popup = popup;
200 var anchor = popup.div.getElementsByTagName("a")[0];
202 anchor.onclick = function() {
203 SYP.showBigImage(this.href);
209 showBigImage: function (href) {
211 document.getElementById('bigimg_container').style.display = "table";
213 document.getElementById('bigimg_container').style.display = "block";
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;
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;
232 this.style.width = this.style.maxWidth;
237 document.getElementById('bigimg').src = href;
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";
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);
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);
262 div.style.color = "red";
265 div.style.color = "#FF8C00";
268 div.style.color = "green";
271 div.style.color = "black";
274 div.style.display = "block";
275 div.appendChild(textNode);
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);
287 OpenLayers.Console.userError = function(error) {
288 SYP.Utils.displayUserMessage(error, "error");
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
306 var images = this.contentDiv.getElementsByTagName("img");
307 for (var i = 0, len = images.length; i < len; i++) {
309 if (img.width == 0 || img.height == 0) {
316 img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
318 OpenLayers.Event.observe(img, 'load', img._onImgLoad);