[Commits] r1788 - in sandbox/ahocevar/playground: trunk/geoext/examples trunk/geoext/lib/GeoExt/widgets trunk/geoext/tests trunk/geoext/tests/lib/GeoExt/widgets ux/Printing/ux/widgets/form

commits at geoext.org commits at geoext.org
Wed Jan 20 19:55:47 CET 2010


Author: ahocevar
Date: 2010-01-20 19:55:47 +0100 (Wed, 20 Jan 2010)
New Revision: 1788

Added:
   sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.html
   sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.js
   sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintExtent.html
Modified:
   sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js
   sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html
   sandbox/ahocevar/playground/ux/Printing/ux/widgets/form/SimplePrint.js
Log:
PrintExtent api improvements, tests and example.

Added: sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.html	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.html	2010-01-20 18:55:47 UTC (rev 1788)
@@ -0,0 +1,33 @@
+<html>
+    <head>
+        <title>GeoExt PrintExtent Example</title>
+
+        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/adapter/ext/ext-base.js"></script>
+        <script type="text/javascript" src="http://extjs.cachefly.net/ext-2.2.1/ext-all.js"></script>
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/resources/css/ext-all.css" />
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/examples/shared/examples.css" />
+        <script src="http://openlayers.org/api/2.8/OpenLayers.js"></script>
+        <script type="text/javascript" src="../lib/GeoExt.js"></script>
+
+        <script type="text/javascript" src="print-extent.js"></script>
+        
+        <!-- The script below will load the capabilities of the print service
+             and save them into the global printCapabilities variable. Instead
+             of this, the PrintProvider can be configured with a url and take
+             care of fetching the capabilities. -->
+        <script type="text/javascript" src="http://demo.opengeo.org/geoserver/pdf/info.json?var=printCapabilities"></script>
+
+    </head>
+    <body>
+        <h1>Setting the Print Extent on the Map Interactively</h1>
+        <p>This example shows the how to set the bounds of a printable PDF
+        interactively with a dynamic print extent rectangle. It requires
+        the <a href="http://trac.mapfish.org/trac/mapfish/wiki/PrintModuleInstallation">MapFish</a>
+        or <a href="http://geoserver.org/display/GEOS/Printing+2.0+HOWTO">GeoServer</a>
+        print module.</p>
+        <p>Drag one of the handles to control scale and rotation. Drag the
+        extent rectangle to change the center.</p>
+        <p>The js is not minified so it is readable. See <a href="print-extent.js">print-extent.js</a>.</p>
+        <div id="content"></div>
+    </body>
+</html>

Added: sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.js	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/examples/print-extent.js	2010-01-20 18:55:47 UTC (rev 1788)
@@ -0,0 +1,49 @@
+ /**
+ * Copyright (c) 2008-2009 The Open Source Geospatial Foundation
+ * 
+ * Published under the BSD license.
+ * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
+ * of the license.
+ */
+
+/** api: example[print-page]
+ *  Printing contents from a MapPanel
+ *  ---------------------------------
+ *  Using PrintPage and PrintProvider to print the visible extent of a MapPanel.
+ */
+
+var mapPanel, printPage;
+
+Ext.onReady(function() {
+    // The printProvider that connects us to the print service
+    var printProvider = new GeoExt.data.PrintProvider({
+        method: "GET", // "POST" recommended for production use
+        capabilities: printCapabilities, // from the info.json script in the html
+        customParams: {
+            mapTitle: "Printing Demo",
+            comment: "This is a map printed from GeoExt."
+        }
+    });
+
+    // The map we want to print, with the PrintExtent added as item.
+    mapPanel = new GeoExt.MapPanel({
+        renderTo: "content",
+        width: 450,
+        height: 320,
+        layers: [new OpenLayers.Layer.WMS("Tasmania", "http://demo.opengeo.org/geoserver/wms",
+            {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+        center: [146.56, -41.56],
+        zoom: 6,
+        items: [{
+            xtype: "gx_printextent",
+            printProvider: printProvider
+        }],
+        bbar: [{
+            text: "Create PDF",
+            handler: function() {
+                // the PrintExtent component is the mapPanel's 1st item
+                mapPanel.items.get(0).print();
+            }
+        }]
+    });
+});

Modified: sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js	2010-01-20 14:38:33 UTC (rev 1787)
+++ sandbox/ahocevar/playground/trunk/geoext/lib/GeoExt/widgets/PrintExtent.js	2010-01-20 18:55:47 UTC (rev 1788)
@@ -14,28 +14,26 @@
  */
 
 /** api: example
- *  Sample code to create a MapPanel with a PrintExtent:
+ *  Sample code to create a MapPanel with a PrintExtent, and print it
+ *  immediately:
  * 
  *  .. code-block:: javascript
  *     
  *      var mapPanel = new GeoExt.MapPanel({
  *          border: false,
  *          renderTo: "div-id",
- *          map: {
- *              maxExtent: new OpenLayers.Bounds(-90, -45, 90, 45)
- *          },
+ *          layers: [new OpenLayers.Layer.WMS("Tasmania", "http://demo.opengeo.org/geoserver/wms",
+ *              {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+ *          center: [146.56, -41.56],
+ *          zoom: 6,
  *          items: [{
  *              xtype: "gx_printextent",
- *              pages: [new GeoExt.data.PrintPage({
- *                  printProvider: new GeoExt.data.PrintProvider({
- *                      capabilities: printCapabilities
- *                  })
+ *              printProvider: new GeoExt.data.PrintProvider({
+ *                  capabilities: printCapabilities
  *              })]
  *          }]
  *      });
  *      var printExtent = mapPanel.items.get(0);
- *      // fit print extent to current map extent
- *      printExtent.fitMap(printExtent.pages[0]);
  *      // print the map
  *      printExtent.print();
  */
@@ -85,13 +83,14 @@
     
     /** api: config[pages]
      *  Array of :class:`GeoExt.data.PrintPage` The pages that this form
-     *  controls. Optional, will be created if not provided.
+     *  controls. Optional. If not provided, it will be created with one page
+     *  that fits the current map extent.
      *  
      *  .. note:: All pages must use the same PrintProvider.
      */
     
     /** api: property[pages]
-     *  Array of :class:`GeoExt.data.PrintPage` The pages that this form
+     *  Array of :class:`GeoExt.data.PrintPage` The pages that this component
      *  controls. Read-only.
      */
     pages: null,
@@ -101,7 +100,9 @@
     initComponent: function() {
         GeoExt.PrintExtent.superclass.initComponent.call(this);
         if(!this.pages) {
-            this.pages = [];
+            this.pages = [new GeoExt.data.PrintPage({
+                printProvider: this.printProvider
+            })];
         }
         if(!this.printProvider) {
             this.printProvider = this.pages[0].printProvider;
@@ -118,22 +119,26 @@
         this.printProvider.print(this.map, this.pages, options);
     },
     
-    /** api: method[fitMap]
-     *  :param page: class:`GeoExt.data.PrintPage`
-     *  
-     *  Fits a print page to the current map extent.
-     */
-    fitMap: function(page) {
-        page.fit(this.map);
-    },
-    
     /** private: method[addToMapPanel]
      *  :param mapPanel: class:`GeoExt.MapPanel`
+     *  
+     *  Adds the component to a MapPanel, and centers the first page if no
+     *  pages were configured.
      */
     addToMapPanel: function(mapPanel) {
         this.map = mapPanel.map;
         this.initLayer();
         this.initControl();
+
+        if(!this.initialConfig.pages) {
+            this.map.getCenter() ?
+                this.pages[0].fit(this.map) :
+                mapPanel.on({
+                    "afterlayout": this.pages[0].fit,
+                    scope: this.pages[0],
+                    single: true
+                });
+        }
     },
 
     /** private: method[removeFromMapPanel]
@@ -146,7 +151,7 @@
             var page = this.pages[i];
             this.layer.removeFeatures([page.feature].concat(page.handles));
         }
-        this.initialConfig.layer || this.map.removeLayer(layer);
+        this.initialConfig.layer || this.map.removeLayer(this.layer);
         this.map = null;
     },
     
@@ -202,13 +207,15 @@
     /** private: method[beforeDestroy]
      */
     beforeDestroy: function() {
-        this.removeFromMapPanel(this.map);
-        this.control.destroy();
-        if(!this.initialConfig.layer) {
+        this.map && this.removeFromMapPanel(this.map);
+        this.control && this.control.destroy();
+        if(!this.initialConfig.layer && this.layer) {
             this.layer.destroy();
         }
         delete this.layer;
         delete this.control;
+        this.rendered = false;
+        GeoExt.PrintExtent.superclass.beforeDestroy.apply(this, arguments);
     }
     
 });

Added: sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintExtent.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintExtent.html	                        (rev 0)
+++ sandbox/ahocevar/playground/trunk/geoext/tests/lib/GeoExt/widgets/PrintExtent.html	2010-01-20 18:55:47 UTC (rev 1788)
@@ -0,0 +1,96 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+        var printCapabilities = {"scales":[{"name":"1:25,000","value":"25000"},{"name":"1:50,000","value":"50000"},{"name":"1:100,000","value":"100000"},{"name":"1:200,000","value":"200000"},{"name":"1:500,000","value":"500000"},{"name":"1:1,000,000","value":"1000000"},{"name":"1:2,000,000","value":"2000000"},{"name":"1:4,000,000","value":"4000000"}],"dpis":[{"name":"75","value":"75"},{"name":"150","value":"150"},{"name":"300","value":"300"}],"layouts":[{"name":"A4 portrait","map":{"width":440,"height":483},"rotation":true},{"name":"Legal","map":{"width":440,"height":483},"rotation":false}],"printURL":"http://demo.opengeo.org/geoserver/pdf/print.pdf","createURL":"http://demo.opengeo.org/geoserver/pdf/create.json"};
+       
+        function test_initComponent(t) {
+            t.plan(1);
+            
+            var printExtent = new GeoExt.PrintExtent({
+                printProvider: new GeoExt.data.PrintProvider({
+                    capabilities: printCapabilities
+                })
+            });
+            
+            t.ok(printExtent.pages[0], "PrintPage auto-created.");
+            
+            printExtent.destroy();
+        }
+        
+        function test_addToMapPanel_removeFromMapPanel(t) {
+            t.plan(9);
+
+            var printExtent = new GeoExt.PrintExtent({
+                printProvider: new GeoExt.data.PrintProvider({
+                    capabilities: printCapabilities
+                })
+            });
+
+            var mapPanel = new GeoExt.MapPanel({
+                renderTo: "mappanel",
+                width: 256,
+                height: 256,
+                map: {controls: []},
+                layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries",
+                    "http://demo.opengeo.org/geoserver/wms",
+                    {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
+                center: [146.56, -41.56],
+                zoom: 7,
+                items: [printExtent]
+            });
+            
+            t.ok(printExtent.map === mapPanel.map, "map property of printExtent set.");
+            t.ok(printExtent.pages[0].getCenter().equals(printExtent.map.getCenter()), "Print page centered to map center.");
+            t.ok(printExtent.layer.map == mapPanel.map, "Auto-generated layer added to map.");
+            t.eq(printExtent.layer.features.length, 5, "Extent and handle features added to the auto-generated layer.");
+            t.ok(printExtent.pages[0].feature.layer, printExtent.layer, "Extent feature was added to the layer.");
+            t.ok(printExtent.control.map == mapPanel.map, "Control created and added to map.");
+            
+            mapPanel.remove(printExtent);
+            
+            t.eq(mapPanel.map.layers.length, 1, "Layer was removed.");
+            t.eq(mapPanel.map.controls.length, 0, "Control was removed.");
+            t.eq(printExtent.pages[0].feature.layer, null, "Feature was removed from the layer.")
+            
+            printExtent.destroy();
+            mapPanel.destroy();
+        }
+
+        function test_beforeDestroy(t) {
+            t.plan(4);
+
+            var printExtent = new GeoExt.PrintExtent({
+                printProvider: new GeoExt.data.PrintProvider({
+                    capabilities: printCapabilities
+                })
+            });
+
+            var mapPanel = new GeoExt.MapPanel({
+                renderTo: "mappanel",
+                map: {controls: []},
+                center: [146.56, -41.56],
+                zoom: 7,
+                items: [printExtent]
+            });
+            
+            printExtent.destroy();
+            
+            t.eq(printExtent.layer, null, "layer destroyed.");
+            t.eq(printExtent.control, null, "control destroyed.");
+            t.eq(mapPanel.map.layers.length, 0, "no layers remaining, so layer was removed.");
+            t.eq(mapPanel.map.controls.length, 0, "no controls remaining, so control was removed.")
+            
+            mapPanel.destroy();
+        }
+    </script>
+  <body>
+    <div id="mappanel"></div>
+    <div id="printmappanel"></div>
+  </body>
+</html>

Modified: sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html
===================================================================
--- sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html	2010-01-20 14:38:33 UTC (rev 1787)
+++ sandbox/ahocevar/playground/trunk/geoext/tests/list-tests.html	2010-01-20 18:55:47 UTC (rev 1788)
@@ -23,6 +23,7 @@
   <li>lib/GeoExt/widgets/MapPanel.html</li>
   <li>lib/GeoExt/widgets/Popup.html</li>
   <li>lib/GeoExt/widgets/PrintMapPanel.html</li>
+  <li>lib/GeoExt/widgets/PrintExtent.html</li>
   <li>lib/GeoExt/widgets/form.html</li>
   <li>lib/GeoExt/widgets/form/SearchAction.html</li>
   <li>lib/GeoExt/widgets/form/BasicForm.html</li>

Modified: sandbox/ahocevar/playground/ux/Printing/ux/widgets/form/SimplePrint.js
===================================================================
--- sandbox/ahocevar/playground/ux/Printing/ux/widgets/form/SimplePrint.js	2010-01-20 14:38:33 UTC (rev 1787)
+++ sandbox/ahocevar/playground/ux/Printing/ux/widgets/form/SimplePrint.js	2010-01-20 18:55:47 UTC (rev 1788)
@@ -103,15 +103,11 @@
     initComponent: function() {
         GeoExt.ux.form.SimplePrint.superclass.initComponent.call(this);
         
-        this.printPage = new GeoExt.data.PrintPage({
-            printProvider: this.initialConfig.printProvider
-        });
-        
         this.printExtent = new GeoExt.PrintExtent({
             printProvider: this.initialConfig.printProvider,
-            layer: this.initialConfig.layer,
-            pages: [this.printPage]
+            layer: this.initialConfig.layer
         });
+        this.printPage = this.printExtent.pages[0];
         
         this.mapPanel.add(this.printExtent);
         



More information about the Commits mailing list