[Commits] r2354 - in core/trunk/geoext: lib/GeoExt/data lib/GeoExt/plugins tests/lib/GeoExt/data

commits at geoext.org commits at geoext.org
Wed Sep 15 14:50:32 CEST 2010


Author: ahocevar
Date: 2010-09-15 14:50:32 +0200 (Wed, 15 Sep 2010)
New Revision: 2354

Modified:
   core/trunk/geoext/lib/GeoExt/data/PrintPage.js
   core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js
   core/trunk/geoext/tests/lib/GeoExt/data/PrintPage.html
Log:
better control over PrintPage::fit by adding a mode config option. r=elemoine (closes #318)


Modified: core/trunk/geoext/lib/GeoExt/data/PrintPage.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/data/PrintPage.js	2010-09-15 09:38:13 UTC (rev 2353)
+++ core/trunk/geoext/lib/GeoExt/data/PrintPage.js	2010-09-15 12:50:32 UTC (rev 2354)
@@ -166,15 +166,23 @@
     /** api: method[fit]
      *  :param fitTo: :class:`GeoExt.MapPanel` or ``OpenLayers.Map`` or ``OpenLayers.Feature.Vector``
      *      The map or feature to fit the page to.
-     *  :param loose: ``Boolean`` If true, the closest matching print extent
-     *      will be chosen. If set to false, the chosen print extent will
-     *      be the closest one that entirely fits into the visible map extent.
-     *      Default is false.
-     * 
+     *  :param options: ``Object`` Additional options to determine how to fit
+     *
      *  Fits the page layout to a map or feature extent. If the map extent has
      *  not been centered yet, this will do nothing.
+     * 
+     *  Available options:
+     *
+     *  * mode - ``String`` How to calculate the print extent? If "closest",
+     *    the closest matching print extent will be chosen. If "printer", the
+     *    chosen print extent will be the closest one that can show the entire
+     *    ``fitTo`` extent on the printer. If "screen", it will be the closest
+     *    one that is entirely visible inside the ``fitTo`` extent. Default is
+     *    "printer".
+     * 
      */
-    fit: function(fitTo, loose) {
+    fit: function(fitTo, options) {
+        options = options || {};
         var map = fitTo, extent;
         if(fitTo instanceof GeoExt.MapPanel) {
             map = fitTo.map;
@@ -193,33 +201,30 @@
         this.setCenter(center);
         var units = map.getUnits();
         var scale = this.printProvider.scales.getAt(0);
-        var closest = {
-            diff: Number.POSITIVE_INFINITY,
-            scale: scale
-        };
+        var closest = Number.POSITIVE_INFINITY;
         var mapWidth = extent.getWidth();
         var mapHeight = extent.getHeight();
         this.printProvider.scales.each(function(rec) {
             var bounds = this.calculatePageBounds(rec, units);
-            var contains = bounds.containsBounds(extent);
-            if (contains) {
-                scale = rec;
-            }
-            if (loose) {
-                var diff = Math.min(
-                    Math.abs(bounds.getWidth() - mapWidth),
-                    Math.abs(bounds.getHeight() - mapHeight)
-                );
-                if (diff < closest.diff) {
-                    closest.diff = diff;
-                    closest.scale = rec;
+            if (options.mode == "closest") {
+                var diff = 
+                    Math.abs(bounds.getWidth() - mapWidth) +
+                    Math.abs(bounds.getHeight() - mapHeight);
+                if (diff < closest) {
+                    closest = diff;
+                    scale = rec;
                 }
             } else {
-                // no need to continue if not contained and not loose
+                var contains = options.mode == "screen" ?
+                    !extent.containsBounds(bounds) :
+                    bounds.containsBounds(extent);
+                if (contains || (options.mode == "screen" && !contains)) {
+                    scale = rec;
+                }
                 return contains;
             }
         }, this);
-        this.setScale(loose ? closest.scale : scale, units);
+        this.setScale(scale, units);
         delete this._updating;
         this.updateFeature(this.feature.geometry, {
             center: center,

Modified: core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js	2010-09-15 09:38:13 UTC (rev 2353)
+++ core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js	2010-09-15 12:50:32 UTC (rev 2354)
@@ -88,9 +88,9 @@
     control: null,
     
     /** api: config[pages]
-     *  Array of :class:`GeoExt.data.PrintPage` The pages that this form
+     *  Array of :class:`GeoExt.data.PrintPage` The pages that this plugin
      *  controls. Optional. If not provided, it will be created with one page
-     *  that fits the current map extent.
+     *  that is completely contained within the visible map extent.
      *  
      *  .. note:: All pages must use the same PrintProvider.
      */
@@ -335,7 +335,7 @@
                             e.center.toShortString()
                         ));
                     } else {
-                        page.fit(e.object.box);
+                        page.fit(e.object.box, {mode: "closest"});
                         var minScale = this.printProvider.scales.getAt(0);
                         var maxScale = this.printProvider.scales.getAt(
                             this.printProvider.scales.getCount() - 1);
@@ -363,7 +363,7 @@
      */
     fitPage: function() {
         if(this.page) {
-            this.page.fit(this.map);
+            this.page.fit(this.map, {mode: "screen"});
         }
     },
 

Modified: core/trunk/geoext/tests/lib/GeoExt/data/PrintPage.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/data/PrintPage.html	2010-09-15 09:38:13 UTC (rev 2353)
+++ core/trunk/geoext/tests/lib/GeoExt/data/PrintPage.html	2010-09-15 12:50:32 UTC (rev 2354)
@@ -79,14 +79,14 @@
         }
         
         function test_fit(t) {
-            t.plan(3);
+            t.plan(4);
             
             var center = new OpenLayers.LonLat(146.56, -41.56);
             var mapPanel = new GeoExt.MapPanel({
                 renderTo: "map",
-                width: 400,
-                height: 300,
-                layers: [new OpenLayers.Layer.WMS("wms", "http://demo.opengeo.org/geoserver/wms", {layers: "topp:tasmania_water_bodies"})],
+                width: 272,
+                height: 272,
+                layers: [new OpenLayers.Layer("empty", {isBaseLayer: true})],
                 center: center,
                 zoom: 7
             });
@@ -97,12 +97,15 @@
                 })
             });
 
-            printPage.fit(mapPanel);
+            printPage.fit(mapPanel, {mode: "printer"});
             t.eq(printPage.center.toString(), center.toString(), "Print page centered correctly.");
-            t.eq(printPage.scale.get("value"), 2000000, "Print scale set correctly.");
+            t.eq(printPage.scale.get("value"), 4000000, "Print scale set correctly.");
 
-            printPage.fit(mapPanel, true);
-            t.eq(printPage.scale.get("value"), 4000000, "Print scale for 'loose' option set correctly.");
+            printPage.fit(mapPanel, {mode: "closest"});
+            t.eq(printPage.scale.get("value"), 2000000, "Print scale for 'closest' mode set correctly.");
+
+            printPage.fit(mapPanel, {mode: "screen"});
+            t.eq(printPage.scale.get("value"), 1000000, "Print scale for 'screen' mode set correctly.");
             
             printPage.destroy();
             mapPanel.destroy();



More information about the Commits mailing list