]> dev.renevier.net Git - syj.git/blobdiff - public/js/syj.js
routes profile
[syj.git] / public / js / syj.js
index 8f5f509020fd0f6251055213aa3d050169f963fe..2f3f676be1a12b9e95170087f464b5b42064552c 100644 (file)
@@ -276,7 +276,7 @@ var SYJView = {
     mode: 'view',
 
     init: function() {
-        var externalGraphic, baseURL, baseLayer, layerOptions, hidemessenger;
+        var externalGraphic, baseURL, mapnikLayer, mapquestLayer, layerOptions, hidemessenger, layerCode, parameters;
 
         // is svg context, opera does not resolve links with base element is svg context
         externalGraphic = styleMap.edit.styles.select.defaultStyle.externalGraphic;
@@ -287,16 +287,24 @@ var SYJView = {
             controls: [
                 new OpenLayers.Control.Navigation(),
                 new OpenLayers.Control.PanZoom(),
+                this.createLayerSwitcher(),
                 new OpenLayers.Control.Attribution()
             ],
             theme: null
         });
 
-        baseLayer = new OpenLayers.Layer.OSM("OSM", [
+        mapnikLayer = new OpenLayers.Layer.OSM("Mapnik", [
                 'http://a.tile.openstreetmap.org/${z}/${x}/${y}.png',
                 'http://b.tile.openstreetmap.org/${z}/${x}/${y}.png',
                 'http://c.tile.openstreetmap.org/${z}/${x}/${y}.png'],
-                { attribution: SyjStrings.osmAttribution });
+                { attribution: SyjStrings.osmAttribution, layerCode: 'M' });
+
+        mapquestLayer = new OpenLayers.Layer.OSM("MapQuest", [
+            'http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
+            'http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
+            'http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png',
+            'http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png'],
+                { attribution: SyjStrings.mapquestAttribution, layerCode: 'Q'});
 
         layerOptions = {format:     OpenLayers.Format.WKT,
                         projection: WGS84,
@@ -304,7 +312,33 @@ var SYJView = {
                         attribution: SyjStrings.geomAttribution };
 
         this.viewLayer = new OpenLayers.Layer.Vector("View Layer", layerOptions);
-        this.map.addLayers([baseLayer, this.viewLayer]);
+        this.map.addLayers([mapnikLayer, mapquestLayer, this.viewLayer]);
+
+        this.map.setBaseLayer(mapnikLayer);
+        layerCode = null;
+        parameters = OpenLayers.Util.getParameters(window.location.href);
+        if (parameters.layer) {
+            layerCode = parameters.layer;
+            try {
+                store.remove('baselayer');
+            } catch(e) {}
+        } else {
+            try {
+                layerCode = store.get('baselayer');
+            } catch(e) {}
+        }
+
+        if (layerCode) {
+            layerCode = layerCode.toUpperCase();
+            var self = this;
+            $([mapnikLayer, mapquestLayer]).each(function(layer) {
+                if (layer.layerCode === layerCode) {
+                    self.map.setBaseLayer(layer);
+                }
+            });
+        }
+
+        this.map.events.register("changebaselayer", this, this.saveBaseLayer);
 
         if ($("edit-btn")) {
             $("edit-btn").observe('click', function() {
@@ -442,6 +476,33 @@ var SYJView = {
         SYJPathLength.update();
     },
 
+    saveBaseLayer: function(data) {
+        try {
+            store.set('baselayer', data.layer.layerCode);
+        } catch(e) {}
+    },
+
+    createLayerSwitcher: function() {
+        var control = new OpenLayers.Control.LayerSwitcher({roundedCorner: false});
+        // XXX: we need to "live-patch" LayerSwitcher to use our icons. We use
+        // a regexp instead of a string in case OpenLayers is modified and in
+        // case browsers modify the function representation
+        control.loadContents = eval('(function() { return (' + control.loadContents.toString().replace(
+                    /\s*=\s*imgLocation\s*\+\s*['"]layer-switcher-maximize\.png['"]\s*;/,
+                    " = 'icons/layer-switcher-maximize-flipped.png';"
+                    ) + ')}())');
+        var oldMaximizeControl = control.maximizeControl;
+        var self = this;
+        control.maximizeControl = (function(oldfunc) {
+            return function() {
+                oldfunc.apply(control, arguments);
+                self.messenger.hide();
+            };
+        }(control.maximizeControl));
+
+        return control;
+    },
+
     initMaPos: function (aPos) {
         var extent = null, center = null, zoom = 0;
 
@@ -613,6 +674,20 @@ var SYJView = {
           return;
       }
 
+        $('path-profile-content').select('img').each(function(img) {
+            var src = img.src;
+            var found = src.match('foo=bar(\\d*)');
+            var num;
+            if (found) {
+                num = parseInt(found[1], 10);
+                img.src = src.replace(found[0], 'foo=bar' + (num+1));
+            } else if (src.indexOf('?') === -1) {
+                img.src = img.src + '?foo=bar0';
+            } else {
+                img.src = img.src + '&foo=bar0';
+            }
+        });
+
       this.messenger.setMessage(SyjStrings.saveSuccess, "success");
       SYJDataUi.viewmode();
       document.title = $('geom_title').value;