]> dev.renevier.net Git - syp.git/blobdiff - js/syp.js
when clicking image in popup, show it full screen
[syp.git] / js / syp.js
index 7e60b4e858df70821f0cd2f44b154d99ac558efd..d8a54b5d5a59611d44cb4663e5dbec309161abb7 100644 (file)
--- a/js/syp.js
+++ b/js/syp.js
@@ -1,6 +1,21 @@
 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
  * license. */
 
+OpenLayers.Control.SypAttribution = OpenLayers.Class (OpenLayers.Control.Attribution, {
+    updateAttribution: function() {
+        var attributions = [SypStrings.propulsedByLink];
+        if (this.map && this.map.layers) {
+            for(var i=0, len=this.map.layers.length; i<len; i++) {
+                var layer = this.map.layers[i];
+                if (layer.attribution && layer.getVisibility()) {
+                    attributions.push( layer.attribution );
+                }
+            }  
+            this.div.innerHTML = attributions.join(this.separator);
+        }
+    }
+});
+
 var SYP = {
     Settings: {
         MARKER_ICON: "openlayers/img/marker-blue.png",
@@ -17,10 +32,10 @@ var SYP = {
     init: function() {
         this.map = new OpenLayers.Map("map", {
             controls:[
+                new OpenLayers.Control.SypAttribution(),
                 new OpenLayers.Control.Navigation(),
                 new OpenLayers.Control.PanZoom(),
-                new OpenLayers.Control.Permalink(),
-                new OpenLayers.Control.Attribution()
+                new OpenLayers.Control.Permalink()
             ],
             projection: new OpenLayers.Projection("EPSG:900913"),
             displayProjection: new OpenLayers.Projection("EPSG:4326")
@@ -56,21 +71,31 @@ var SYP = {
     },
 
     createDataLayer: function(map) {
+        var defaultStyle = new OpenLayers.Style({
+            externalGraphic: this.Settings.MARKER_ICON,
+            graphicHeight: "${height}"
+        }, {
+            context: {
+                height: function(feature) {
+                    var defaultHeight = SYP.Settings.MARKER_ICON_HEIGHT || 32;
+                    var increase = 4 * (feature.attributes.count - 1);
+                    return Math.min(defaultHeight + increase, 50);
+                }
+            }
+        });
+        var selectStyle = new OpenLayers.Style({
+            externalGraphic: this.Settings.MARKER_SELECT_ICON,
+            graphicHeight: this.Settings.MARKER_SELECT_ICON_HEIGHT || 32 
+        });
         var styleMap = new OpenLayers.StyleMap (
-                        {"default": {
-                            externalGraphic: this.Settings.MARKER_ICON,
-                            graphicHeight: this.Settings.MARKER_ICON_HEIGHT
-                                            || 32 
-                                    },
-                         "select": { 
-                            externalGraphic: this.Settings.MARKER_SELECT_ICON,
-                            graphicHeight: this.Settings.MARKER_SELECT_ICON_HEIGHT 
-                                            || 32 
-                                  }
-                     });
+                        {"default": defaultStyle,
+                         "select": selectStyle});
 
         var layer = new OpenLayers.Layer.GML("KML", "items.php", 
            {
+               strategies: [
+                new OpenLayers.Strategy.Cluster()
+                ],
             styleMap: styleMap,
             format: OpenLayers.Format.KML, 
             projection: this.map.displayProjection,
@@ -96,7 +121,7 @@ var SYP = {
     checkForFeatures: function() {
         var features = this.dataLayer.features;
         if (features.length == 0) {
-            var message = "Il n'y a aucune photo enregistrĂ©e sur le site.";
+            var message = SypStrings.noImageRegistered;
             this.Utils.displayUserMessage(message, "warn");
         }
     },
@@ -116,6 +141,15 @@ var SYP = {
     },
 
     onFeatureUnselect: function (feature) {
+        var map = feature.layer.map;
+        var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
+        if (permaControl[0]) {
+            permaControl[0].div.style.display = "";
+        }
+        if (!feature.popup) {
+            this.map.events.unregister("movestart", this, this._unselect);
+            return;
+        }
         var popup = feature.popup;
         if (popup.visible()) {
             popup.hide();
@@ -124,32 +158,90 @@ var SYP = {
 
     onFeatureSelect: function(feature) {
         var map = feature.layer.map;
+        if (feature.attributes.count > 1) {
+            this.unselect(feature);
+            var lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y);
+            map.setCenter(lonlat, map.zoom + 1);
+            return;
+        }
+        var permaControl = map.getControlsByClass("OpenLayers.Control.Permalink");
+        if (permaControl[0]) {
+            permaControl[0].div.style.display = "none";
+        }
         var popup = feature.popup;
 
         var brCorner = SYP.Utils.brCorner(map, 8);
 
         // we cannot reuse popup; we need to recreate it in order for IE
         // expressions to work. Otherwise, we get a 0x0 image on second view.
-        if (popup) { 
+        if (popup) {
             popup.destroy();
         }
         var contentHTML;
-        if (feature.attributes.name) {
+        if (feature.cluster[0].attributes.name) {
             contentHTML = "<h2>" +
-                          feature.attributes.name + 
+                          feature.cluster[0].attributes.name + 
                           "</h2>" + 
-                          feature.attributes.description;
+                          feature.cluster[0].attributes.description;
         } else {
-            contentHTML = feature.attributes.description;
+            contentHTML = feature.cluster[0].attributes.description;
+        }
+        if (!contentHTML || !contentHTML.length) {
+            this.map.events.register("movestart", this, this._unselect = function () { this.unselect(feature)});
+            return;
         }
         popup = SYP.createPopup(brCorner, contentHTML);
         var control = this;
         popup.hide = function () {
             OpenLayers.Element.hide(this.div);
-            control.unselect(feature);
+            control.unselectAll();
         };
         map.addPopup(popup);
         feature.popup = popup;
+        var anchor = popup.div.getElementsByTagName("a")[0];
+        if (anchor) {
+            anchor.onclick = function() { 
+                SYP.showBigImage(this.href);
+                return false;
+            }
+        }
+    },
+
+    showBigImage: function (href) {
+        try {
+            document.getElementById('bigimg_container').style.display = "table";
+        } catch(e) {
+            document.getElementById('bigimg_container').style.display = "block";
+        }
+        var maxHeight = document.body.clientHeight * 0.9;
+        var maxWidth = document.body.clientWidth * 0.9;
+        document.getElementById('bigimg').style.height = "";
+        document.getElementById('bigimg').style.width = "";
+        document.getElementById('bigimg').style.maxHeight = maxHeight + "px";
+        document.getElementById('bigimg').style.maxWidth = maxWidth + "px";
+        document.getElementById('bigimg').onload = function () {
+            var icon = document.getElementById('bigimg_close');
+            icon.style.top = this.offsetTop;
+            icon.style.left = this.offsetLeft + this.clientWidth - icon.clientWidth;
+
+            var heightRatio = this.clientHeight / parseInt(this.style.maxHeight);
+            var widthRatio = this.clientWidth / parseInt(this.style.maxWidth);
+            if (heightRatio > 1 || widthRatio > 1) {
+                if (heightRatio > widthRatio) {
+                    this.style.height = this.style.maxHeight;
+                } else {
+                    this.style.width = this.style.maxWidth;
+                }
+            }
+
+        };
+        document.getElementById('bigimg').src = href;
+    },
+
+    closeBigImage: function() {
+        document.getElementById('bigimg').src = "";
+        document.getElementById('bigimg').parentNode.innerHTML = document.getElementById('bigimg').parentNode.innerHTML;
+        document.getElementById('bigimg_container').style.display = "none";
     },
 
     Utils: {
@@ -186,7 +278,45 @@ var SYP = {
     }
 };
 
+// if possible, determine language with HTTP_ACCEPT_LANGUAGE instead of
+// navigator.language
+if (OpenLayers.Lang[SypStrings.language]) {
+    OpenLayers.Lang.setCode(SypStrings.language);
+}
+
 // avoid alerts
 OpenLayers.Console.userError = function(error) { 
     SYP.Utils.displayUserMessage(error, "error");
 }
+
+// sometimes, especially when cache is clear, firefox does not compute
+// correctly popup size. That's because at the end of getRenderedDimensions,
+// dimensions of image is not known. So, we work around that problem by setting
+// image width and image height. That way, dimensions of image are set in
+// innerHTML, and are therefore known in getRenderedDimensions
+OpenLayers.Popup.Anchored.prototype.registerImageListeners = function() {
+    var onImgLoad = function() {
+        this.img.width =  this.img.width;
+        this.img.height =  this.img.height;
+        this.popup.updateSize();
+        OpenLayers.Event.stopObserving(
+            this.img, "load", this.img._onImageLoad
+        );
+    };
+
+    var images = this.contentDiv.getElementsByTagName("img");
+    for (var i = 0, len = images.length; i < len; i++) {
+        var img = images[i];
+        if (img.width == 0 || img.height == 0) {
+
+            var context = {
+                'popup': this,
+                'img': img
+            };
+
+            img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
+
+            OpenLayers.Event.observe(img, 'load', img._onImgLoad);
+        }    
+    } 
+}