]> dev.renevier.net Git - syp.git/blobdiff - js/syp.js
small code cleanup (indentation, missing copyrights, etc)
[syp.git] / js / syp.js
index 7e60b4e858df70821f0cd2f44b154d99ac558efd..8e32d8580d4d7e86d7b0f178cfa46f627d0c8e99 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")
@@ -96,7 +111,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 +131,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,13 +148,17 @@ var SYP = {
 
     onFeatureSelect: function(feature) {
         var map = feature.layer.map;
+        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;
@@ -142,6 +170,10 @@ var SYP = {
         } else {
             contentHTML = feature.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 () {
@@ -186,7 +218,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);
+        }    
+    } 
+}