[Commits] r1531 - in sandbox/ahocevar/playground/ux/Printing: examples ux/data

commits at geoext.org commits at geoext.org
Tue Dec 1 23:49:09 CET 2009


Author: ahocevar
Date: 2009-12-01 23:49:09 +0100 (Tue, 01 Dec 2009)
New Revision: 1531

Modified:
   sandbox/ahocevar/playground/ux/Printing/examples/Printing.html
   sandbox/ahocevar/playground/ux/Printing/examples/Printing.js
   sandbox/ahocevar/playground/ux/Printing/ux/data/PrintProvider.js
Log:
initial working, untested version of PrintProvider; example now uses mapfish demo server as print service.

Modified: sandbox/ahocevar/playground/ux/Printing/examples/Printing.html
===================================================================
--- sandbox/ahocevar/playground/ux/Printing/examples/Printing.html	2009-12-01 22:47:17 UTC (rev 1530)
+++ sandbox/ahocevar/playground/ux/Printing/examples/Printing.html	2009-12-01 22:49:09 UTC (rev 1531)
@@ -11,6 +11,7 @@
         <script type="text/javascript" src="../ux/data/PrintProvider.js"></script>
         <script type="text/javascript" src="Printing.js"></script>
 
+        <script type="text/javascript" src="http://demo.mapfish.org/mapfishsample/1.2/print/info.json?var=printCapabilities"></script>
     </head>
     <body>
     </body>

Modified: sandbox/ahocevar/playground/ux/Printing/examples/Printing.js
===================================================================
--- sandbox/ahocevar/playground/ux/Printing/examples/Printing.js	2009-12-01 22:47:17 UTC (rev 1530)
+++ sandbox/ahocevar/playground/ux/Printing/examples/Printing.js	2009-12-01 22:49:09 UTC (rev 1531)
@@ -3,12 +3,22 @@
 Ext.onReady(function() {
 
     printProvider = new GeoExt.ux.data.PrintProvider({
-        url: "/geoserver/pdf",
-        listeners: {
-            loadcapabilities: function() {
-                console.log(printProvider.capabilities);
-            }
-        } 
+        // capabilities from script tag in Printing.html
+        capabilities: printCapabilities,
+        method: "GET"
     });
+    
+    var mapPanel = new GeoExt.MapPanel({
+        renderTo: document.body,
+        width: 500,
+        height: 300,
+        layers: [new OpenLayers.Layer.WMS("Global Imagery",
+            "http://labs.metacarta.com/wms/vmap0",
+            {layers: "basic"})] ,
+        center: [16,48],
+        zoom: 5
+    });
+    
+    printProvider.print();
 
 });

Modified: sandbox/ahocevar/playground/ux/Printing/ux/data/PrintProvider.js
===================================================================
--- sandbox/ahocevar/playground/ux/Printing/ux/data/PrintProvider.js	2009-12-01 22:47:17 UTC (rev 1530)
+++ sandbox/ahocevar/playground/ux/Printing/ux/data/PrintProvider.js	2009-12-01 22:49:09 UTC (rev 1531)
@@ -6,6 +6,10 @@
     
     capabilities: null,
     
+    customData: null,
+    
+    method: "POST",
+    
     constructor: function(config) {
         this.initialConfig = config;
         Ext.apply(this, config);
@@ -30,8 +34,15 @@
             root: "layouts",
             fields: ["name", "map"]
         });
-
-        this.loadCapabilities();
+        
+        if(config.capabilities) {
+            this.loadStores();
+        } else {
+            if(this.url.split("/").pop()) {
+                this.url += "/";            
+            }
+            this.loadCapabilities();
+        }
     },
     
     loadCapabilities: function() {
@@ -41,54 +52,131 @@
             disableCaching: false,
             success: function(response) {
                 this.capabilities = Ext.decode(response.responseText);
-                this.scales.loadData(this.capabilities);
-                this.dpis.loadData(this.capabilities);
-                this.layouts.loadData(this.capabilities);
+                this.loadStores();
                 this.fireEvent("loadcapabilities", this.capabilities);
             },
             scope: this
         });
     },
     
-    printPage: function(config) {
-        var map =  config.map instanceof GeoExt.MapPanel ? mapPanel.map : map;
+    loadStores: function() {
+        this.scales.loadData(this.capabilities);
+        this.scales.sort("value", "DESC");
+        this.dpis.loadData(this.capabilities);
+        this.dpis.sort("value", "ASC");
+        this.layouts.loadData(this.capabilities);
+    },
+    
+    print: function(title, comment, map, layout, dpi, center, scale, rotation) {
+        map = map || GeoExt.MapPanel.guess();
+        if(map instanceof GeoExt.MapPanel) {
+            map = map.map;
+        }
+        if(layout instanceof Ext.data.Record) {
+            layout = layout.get("name");
+        }
+        layout = layout || this.layouts.getAt(0).get("name");
+        if(dpi instanceof Ext.data.Record) {
+            dpi = dpi.get("value");
+        }
+        dpi = dpi || this.dpis.getAt(0).get("value");
+        center = center || map.getCenter();
+        if(center instanceof OpenLayers.LonLat) {
+            center = [center.lon, center.lat]
+        }
+        if(scale instanceof Ext.data.Record) {
+            scale = scale.get("value");
+        }
+        scale = scale || this.getBestScale(map, layout);
+
         var layers = [];
         Ext.each(map.layers, function(layer){
             var enc = this.encodeLayer(layer);
-            enc && layers.push[enc];
+            enc && layers.push(enc);
         }, this);
-        var printCommand = {
+        var payload = Ext.apply({
             pages: [{
-                center: config.center,
-                mapTitle: config.title,
-                comment: config.comment,
-                scale: config.scale,
-                rotation: config.rotation
+                center: center,
+                mapTitle: title || "",
+                comment: comment || "",
+                scale: scale,
+                rotation: rotation
             }],
-            dpi: config.dpi,
+            dpi: dpi,
             units: map.baseLayer.units,
             srs: map.baseLayer.projection.getCode(),
-            layers: [],
-            layout: config.layout
-        };
+            layers: layers,
+            layout: layout
+        }, this.customData);
+        if(this.method === "GET") {
+            window.open(this.capabilities.printURL + "?spec=" +
+                escape(Ext.encode(payload)));
+        } else {
+            Ext.Ajax.request({
+                url: this.capabilities.createURL,
+                jsonData: payload,
+                success: function(response) {
+                    window.open(Ext.decode(response.responseText).getURL);
+                }
+            })
+        }
     },
     
     encodeLayer: function(layer) {
         var encLayer;
-        for(var c in GeoExt.data.PrintProvider.encode) {
+        for(var c in GeoExt.ux.data.PrintProvider.encode) {
             if(layer instanceof OpenLayers.Layer[c]) {
-                encLayer = GeoExt.data.PrintProvider.encode[c](layer);
+                encLayer = GeoExt.ux.data.PrintProvider.encode[c](layer);
                 break;
             }
         }
         return (encLayer && encLayer.type) ? encLayer : null;
+    },
+    
+    getLayoutBounds: function(map, scale, layout) {
+        scale = scale || this.getBestScale(map, layout)
+        layout = layout || this.layouts.getAt(0);
+
+        var size = layout instanceof Ext.data.Record ?
+            layout.get("map") :
+            layout;
+        if(scale instanceof Ext.data.Record) {
+            scale = scale.get("value");
+        }
+        var unitsRatio = OpenLayers.INCHES_PER_UNIT[map.baseLayer.units];
+        var w = size.width / 72 / unitsRatio * scale / 2;
+        var h = size.height / 72 / unitsRatio * scale / 2;
+        var center = map.getCenter();
+        return new OpenLayers.Bounds(center.lon - w, center.lat - h,
+            center.lon + w, center.lat + h);
+    },
+    
+    getBestScale: function(map, layout) {
+        if(map instanceof GeoExt.MapPanel) {
+            map = map.map;
+        }
+        if(typeof layout === "string") {
+            layout = this.layouts.getAt(this.layouts.find("name", layout));
+        }
+        layout = layout || this.layouts.getAt(0);
+        var extent = map.getExtent();
+
+        var bounds;
+        this.scales.each(function(rec) {
+            scale = rec.get("value");
+            bounds = this.getLayoutBounds(map, scale, layout);
+            if(extent.containsBounds(bounds)) {
+                return false;
+            }
+        }, this)
+        return scale;
     }
     
 });
 
-GeoExt.data.PrintProvider.encode = {
+GeoExt.ux.data.PrintProvider.encode = {
     "WMS": function(layer){
-        var enc = Ext.apply(GeoExt.data.PrintProvider.encode["HTTPRequest"](layer), {
+        var enc = Ext.apply(GeoExt.ux.data.PrintProvider.encode["HTTPRequest"](layer), {
             type: 'WMS',
             layers: [layer.params.LAYERS].join(",").split(","),
             format: layer.params.FORMAT,
@@ -98,21 +186,21 @@
         for (var p in layer.params) {
             param = p.toLowerCase();
             if (!layer.DEFAULT_PARAMS[param] &&
-                            "layers,width,height,srs".indexOf(param) == -1) {
+                            "layers,styles,width,height,srs".indexOf(param) == -1) {
                 enc.customParams[p] = layer.params[p];
             }
         }
         return enc;
     },
     "OSM": function(layer) {
-        return Ext.apply(GeoExt.data.PrintProvider.encode["TileCache"](layer), {
+        return Ext.apply(GeoExt.ux.data.PrintProvider.encode["TileCache"](layer), {
             type: 'Osm',
             baseURL: enc.baseURL.substr(0, enc.baseURL.indexOf("$")),
             extension: "png"
         });
     },
     "TileCache": function(layer) {
-        return Ext.apply(GeoExt.data.PrintProvider.encode["HTTPRequest"](layer), {
+        return Ext.apply(GeoExt.ux.data.PrintProvider.encode["HTTPRequest"](layer), {
             type: 'TileCache',
             layer: layer.layername,
             maxExtent: layer.maxExtent.toArray(),



More information about the Commits mailing list