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;
173 if (sypPopupNearFeature) {
174 popupPos = feature.geometry.getBounds().getCenterLonLat();
176 popupPos = SYP.Utils.brCorner(map, 8);
179 // we cannot reuse popup; we need to recreate it in order for IE
180 // expressions to work. Otherwise, we get a 0x0 image on second view.
185 if (feature.cluster[0].attributes.name) {
186 contentHTML = "<h2>" +
187 feature.cluster[0].attributes.name +
189 feature.cluster[0].attributes.description;
191 contentHTML = feature.cluster[0].attributes.description;
193 if (!contentHTML || !contentHTML.length) {
194 this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
197 popup = SYP.createPopup(popupPos, contentHTML);
199 popup.hide = function () {
200 OpenLayers.Element.hide(this.div);
201 control.unselectAll();
204 feature.popup = popup;
205 var anchor = popup.div.getElementsByTagName("a")[0];
207 anchor.onclick = function() {
208 SYP.showBigImage(this.href);
214 showBigImage: function (href) {
216 document.getElementById('bigimg_container').style.display = "table";
218 document.getElementById('bigimg_container').style.display = "block";
220 var maxHeight = document.body.clientHeight * 0.9;
221 var maxWidth = document.body.clientWidth * 0.9;
222 document.getElementById('bigimg').style.height = "";
223 document.getElementById('bigimg').style.width = "";
224 document.getElementById('bigimg').style.maxHeight = maxHeight + "px";
225 document.getElementById('bigimg').style.maxWidth = maxWidth + "px";
226 document.getElementById('bigimg').onload = function () {
227 var icon = document.getElementById('bigimg_close');
228 icon.style.top = this.offsetTop;
229 icon.style.left = this.offsetLeft + this.clientWidth - icon.clientWidth;
231 var heightRatio = this.clientHeight / parseInt(this.style.maxHeight);
232 var widthRatio = this.clientWidth / parseInt(this.style.maxWidth);
233 if (heightRatio > 1 || widthRatio > 1) {
234 if (heightRatio > widthRatio) {
235 this.style.height = this.style.maxHeight;
237 this.style.width = this.style.maxWidth;
242 document.getElementById('bigimg').src = href;
245 closeBigImage: function() {
246 document.getElementById('bigimg').src = "";
247 document.getElementById('bigimg').parentNode.innerHTML = document.getElementById('bigimg').parentNode.innerHTML;
248 document.getElementById('bigimg_container').style.display = "none";
252 brCorner: function(map, margin) {
253 var bounds = map.calculateBounds();
254 var corner = new OpenLayers.LonLat(bounds.right, bounds.bottom);
255 var cornerAsPx = map.getPixelFromLonLat(corner);
256 cornerAsPx = cornerAsPx.add( -margin, -margin);
257 corner = map.getLonLatFromPixel(cornerAsPx);
260 displayUserMessage: function(message, status) {
261 var div = document.getElementById('message');
262 while (div.firstChild)
263 div.removeChild(div.firstChild);
264 var textNode = document.createTextNode(message);
267 div.style.color = "red";
270 div.style.color = "#FF8C00";
273 div.style.color = "green";
276 div.style.color = "black";
279 div.style.display = "block";
280 div.appendChild(textNode);
285 // if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
286 // navigator.language
287 if (OpenLayers.Lang[SypStrings.language]) {
288 OpenLayers.Lang.setCode(SypStrings.language);
292 OpenLayers.Console.userError = function(error) {
293 SYP.Utils.displayUserMessage(error, "error");
296 // sometimes, especially when cache is clear, firefox does not compute
297 // correctly popup size. That's because at the end of getRenderedDimensions,
298 // dimensions of image is not known. So, we work around that problem by setting
299 // image width and image height. That way, dimensions of image are set in
300 // innerHTML, and are therefore known in getRenderedDimensions
301 OpenLayers.Popup.Anchored.prototype.registerImageListeners = function() {
302 var onImgLoad = function() {
303 this.img.width = this.img.width;
304 this.img.height = this.img.height;
305 this.popup.updateSize();
306 OpenLayers.Event.stopObserving(
307 this.img, "load", this.img._onImageLoad
311 var images = this.contentDiv.getElementsByTagName("img");
312 for (var i = 0, len = images.length; i < len; i++) {
314 if (img.width == 0 || img.height == 0) {
321 img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
323 OpenLayers.Event.observe(img, 'load', img._onImgLoad);