[Commits] r2065 - in core/trunk/geoext: examples lib/GeoExt/plugins tests/lib/GeoExt/plugins

commits at geoext.org commits at geoext.org
Thu Apr 1 11:28:04 CEST 2010


Author: pgiraud
Date: 2010-04-01 11:28:04 +0200 (Thu, 01 Apr 2010)
New Revision: 2065

Modified:
   core/trunk/geoext/examples/print-extent.js
   core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js
   core/trunk/geoext/tests/lib/GeoExt/plugins/PrintExtent.html
Log:
prepare git local commit before svn commit

Modified: core/trunk/geoext/examples/print-extent.js
===================================================================
--- core/trunk/geoext/examples/print-extent.js	2010-04-01 09:27:59 UTC (rev 2064)
+++ core/trunk/geoext/examples/print-extent.js	2010-04-01 09:28:04 UTC (rev 2065)
@@ -26,6 +26,10 @@
         }
     });
 
+    var printExtent = new GeoExt.plugins.PrintExtent({
+        printProvider: printProvider
+    });
+
     // The map we want to print, with the PrintExtent added as item.
     mapPanel = new GeoExt.MapPanel({
         renderTo: "content",
@@ -35,11 +39,7 @@
             {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})],
         center: [146.56, -41.56],
         zoom: 6,
-        plugins: [
-            new GeoExt.plugins.PrintExtent({
-                printProvider: printProvider
-            })
-        ],
+        plugins: [printExtent],
         bbar: [{
             text: "Create PDF",
             handler: function() {
@@ -48,4 +48,5 @@
             }
         }]
     });
+    printExtent.addPage();
 });

Modified: core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js	2010-04-01 09:27:59 UTC (rev 2064)
+++ core/trunk/geoext/lib/GeoExt/plugins/PrintExtent.js	2010-04-01 09:28:04 UTC (rev 2065)
@@ -34,6 +34,8 @@
  *          plugins: printExtent
  *      });
  *
+ *      printExtent.addPage();
+ *
  *      // print the map
  *      printExtent.print();
  */
@@ -46,26 +48,8 @@
  *  and provides a control to modify them. Must be set as a plugin to a
  *  :class:`GeoExt.MapPanel`.
  */
+GeoExt.plugins.PrintExtent = Ext.extend(Ext.util.Observable, {
 
-GeoExt.plugins.PrintExtent = function(config) {
-    config = config || {};
-
-    Ext.apply(this, config);
-    this.initialConfig = config;
-
-    if(!this.pages) {
-        this.pages = [new GeoExt.data.PrintPage({
-            printProvider: this.printProvider
-        })];
-    }
-
-    if(!this.printProvider) {
-        this.printProvider = this.pages[0].printProvider;
-    }
-};
-
-GeoExt.plugins.PrintExtent.prototype = {
-
     /** private: initialConfig
      *  ``Object`` Holds the initial config object passed to the
      *  constructor.
@@ -122,6 +106,36 @@
      */
     page: null,
 
+    /** private: method[constructor]
+     *  Private constructor override.
+     */
+    constructor: function(config) {
+        config = config || {};
+
+        Ext.apply(this, config);
+        this.initialConfig = config;
+
+        if(!this.printProvider) {
+            this.printProvider = this.pages[0].printProvider;
+        }
+
+        if(!this.pages) {
+            this.pages = [];
+        }
+        
+        this.addEvents(
+            /** api: events[selectpage]
+             *  Triggered when a page has been selected using the control
+             *  
+             *  Listener arguments:
+             *  * printPage - :class:`GeoExt.data.PrintPage` this printPage
+             */
+            "selectpage"
+        );
+
+        GeoExt.plugins.PrintExtent.superclass.constructor.apply(this, arguments);
+    },
+
     /** api: method[print]
      *  :param options: ``Object`` Options to send to the PrintProvider's
      *      print method. See :class:`GeoExt.data.PrintProvider` :: ``print``.
@@ -140,35 +154,84 @@
     init: function(mapPanel) {
         this.map = mapPanel.map;
         mapPanel.on("destroy", this.onMapPanelDestroy, this);
+
+        if (!this.layer) {
+            this.layer = new OpenLayers.Layer.Vector(null, {
+                displayInLayerSwitcher: false
+            });
+        }
+        this.createControl();
+
+        for(var i=0, len=this.pages.length; i<len; ++i) {
+            this.addPage(this.pages[i]);
+        }
         this.setUp();
     },
 
+    /** api: method[addPage]
+     *  Adds a page to the list of pages that this plugin controls.
+     *  :param page: :class:`GeoExt.data.PrintPage` The page to add
+     *       to this plugin. If not provided, a page that fits the current
+     *       extent is created.
+     *  :return: page :class:``GeoExt.data.PrintPage``
+     */
+    addPage: function(page) {
+        page = page || new GeoExt.data.PrintPage({
+            printProvider: this.printProvider
+        });
+        if(this.pages.indexOf(page) === -1) {
+            this.pages.push(page);
+        }
+        this.layer.addFeatures([page.feature]);
+        page.on("change", this.onPageChange, this);
+
+        this.page = page;
+        var map = this.map;
+        if(map.getCenter()) {
+            this.fitPage();
+        } else {
+            map.events.register("moveend", this, function() {
+                map.events.unregister("moveend", this, arguments.callee);
+                this.fitPage();
+            });
+        }
+        return page;
+    },
+
+    /** api: method[removePage]
+     *  Removes a page from the list of pages that this plugin controls.
+     *  :param page: :class:`GeoExt.data.PrintPage` The page to remove
+     *       from this plugin.
+     */
+    removePage: function(page) {
+        this.pages.remove(page);
+        if (page.feature.layer) {
+            this.layer.removeFeatures([page.feature]);
+        }
+        page.un("change", this.onPageChange, this);
+    },
+    
+    /** api: method[selectPage]
+     *  Selects the given page (ie. calls the setFeature on the
+     *      modify feature control)
+     *  :param page: :class:`GeoExt.data.PrintPage` The page to select
+     */
+    selectPage: function(page) {
+        this.control.active && this.control.setFeature(page.feature);
+        // FIXME raise the feature up so that it is on top
+    },
+
     /** api: method[setUp]
      *  Sets up the plugin, initializing the ``OpenLayers.Layer.Vector``
      *  layer and ``OpenLayers.Control.TransformFeature``, and centering
      *  the first page if no pages were specified in the configuration.
      */
     setUp: function() {
-        this.initLayer();
-
-        this.initControl();
+        this.map.addLayer(this.layer);
         this.map.addControl(this.control);
         this.control.activate();
 
         this.printProvider.on("layoutchange", this.updateBox, this);
-
-        if(!this.initialConfig.pages) {
-            this.page = this.pages[0];
-            var map = this.map;
-            if(map.getCenter()) {
-                this.fitPage();
-            } else {
-                map.events.register("moveend", this, function() {
-                    map.events.unregister("moveend", this, arguments.callee);
-                    this.fitPage();
-                });
-            }
-        }
     },
 
     /** private: method[tearDown]
@@ -195,13 +258,6 @@
         }
 
         var layer = this.layer;
-        if(layer && layer.events) {
-            for(var i=0, len=this.pages.length; i<len; ++i) {
-                var page = this.pages[i];
-                page.un("change", this.onPageChange, this);
-                layer.removeFeatures([page.feature]);
-            }
-        }
 
         if(!this.initialConfig.layer &&
            map && map.events &&
@@ -213,10 +269,15 @@
     /** private: method[onMapPanelDestroy]
      */
     onMapPanelDestroy: function() {
-        this.tearDown();
 
         var map = this.map;
 
+        for(var len = this.pages.length - 1, i = len; i>=0; i--) {
+            this.removePage(this.pages[i]);
+        }
+
+        this.tearDown();
+
         var control = this.control;
         if(map && map.events &&
            control && control.events) {
@@ -235,79 +296,65 @@
         delete this.page;
         this.map = null;
     },
-
-    /** private: method[initLayer]
-     */
-    initLayer: function() {
-        if(!this.layer) {
-            this.layer = new OpenLayers.Layer.Vector(null, {
-                displayInLayerSwitcher: false
-            });
-        }
-        for(var i=0, len=this.pages.length; i<len; ++i) {
-            var page = this.pages[i];
-            this.layer.addFeatures([page.feature]);
-            page.on("change", this.onPageChange, this);
-        }
-        if(!this.layer.map) {
-            this.map.addLayer(this.layer);
-        }
-    },
     
-    /** private: method[initControl]
+    /** private: method[createControl]
      */
-    initControl: function() {
-        var pages = this.pages;
-
-        if(!this.control) {
-            this.control = new OpenLayers.Control.TransformFeature(this.layer, {
-                preserveAspectRatio: true,
-                eventListeners: {
-                    "beforesetfeature": function(e) {
-                        for(var i=0, len=this.pages.length; i<len; ++i) {
-                            if(this.pages[i].feature === e.feature) {
-                                this.page = this.pages[i];
-                                e.object.rotation = -this.pages[i].rotation;
-                                break;
-                            }
+    createControl: function() {
+        this.control = new OpenLayers.Control.TransformFeature(this.layer, {
+            preserveAspectRatio: true,
+            eventListeners: {
+                "beforesetfeature": function(e) {
+                    for(var i=0, len=this.pages.length; i<len; ++i) {
+                        if(this.pages[i].feature === e.feature) {
+                            this.page = this.pages[i];
+                            e.object.rotation = -this.pages[i].rotation;
+                            break;
                         }
-                    },
-                    "beforetransform": function(e) {
-                        this._updating = true;
-                        var page = this.page;
-                        if(e.rotation) {
-                            if(this.printProvider.layout.get("rotation")) {
-                                page.setRotation(-e.object.rotation);
-                            } else {
-                                e.object.setFeature(page.feature);
-                            }
-                        } else if(e.center) {
-                            page.setCenter(OpenLayers.LonLat.fromString(
-                                e.center.toShortString()
-                            ));
+                    }
+                },
+                "setfeature": function(e) {
+                    for(var i=0, len=this.pages.length; i<len; ++i) {
+                        if(this.pages[i].feature === e.feature) {
+                            this.fireEvent("selectpage", this.pages[i]);
+                            break;
+                        }
+                    }
+                },
+                "beforetransform": function(e) {
+                    this._updating = true;
+                    var page = this.page;
+                    if(e.rotation) {
+                        if(this.printProvider.layout.get("rotation")) {
+                            page.setRotation(-e.object.rotation);
                         } else {
-                            page.fit(e.object.box);
-                            var minScale = this.printProvider.scales.getAt(0);
-                            var maxScale = this.printProvider.scales.getAt(
-                                this.printProvider.scales.getCount() - 1);
-                            var boxBounds = e.object.box.geometry.getBounds();
-                            var pageBounds = page.feature.geometry.getBounds();
-                            var tooLarge = page.scale === minScale &&
-                                boxBounds.containsBounds(pageBounds);
-                            var tooSmall = page.scale === maxScale &&
-                                pageBounds.containsBounds(boxBounds);
-                            if(tooLarge === true || tooSmall === true) {
-                                this.updateBox();
-                            }
+                            e.object.setFeature(page.feature);
                         }
-                        delete this._updating;
-                        return false;
-                    },
-                    "transformcomplete": this.updateBox,
-                    scope: this
-                }
-            });
-        }
+                    } else if(e.center) {
+                        page.setCenter(OpenLayers.LonLat.fromString(
+                            e.center.toShortString()
+                        ));
+                    } else {
+                        page.fit(e.object.box);
+                        var minScale = this.printProvider.scales.getAt(0);
+                        var maxScale = this.printProvider.scales.getAt(
+                            this.printProvider.scales.getCount() - 1);
+                        var boxBounds = e.object.box.geometry.getBounds();
+                        var pageBounds = page.feature.geometry.getBounds();
+                        var tooLarge = page.scale === minScale &&
+                            boxBounds.containsBounds(pageBounds);
+                        var tooSmall = page.scale === maxScale &&
+                            pageBounds.containsBounds(boxBounds);
+                        if(tooLarge === true || tooSmall === true) {
+                            this.updateBox();
+                        }
+                    }
+                    delete this._updating;
+                    return false;
+                },
+                "transformcomplete": this.updateBox,
+                scope: this
+            }
+        });
     },
 
     /** private: method[fitPage]
@@ -325,7 +372,8 @@
      */
     updateBox: function() {
         var page = this.page;
-        this.control.setFeature(page.feature, {rotation: -page.rotation});
+        this.control.active &&
+            this.control.setFeature(page.feature, {rotation: -page.rotation});
     },
 
     /** private: method[onPageChange]
@@ -333,10 +381,11 @@
      */
     onPageChange: function(page, mods) {
         if(!this._updating) {
-            this.control.setFeature(page.feature, {rotation: -page.rotation});
+            this.control.active &&
+                this.control.setFeature(page.feature, {rotation: -page.rotation});
         }
     }
-};
+});
 
 /** api: ptype = gx_printextent */
 Ext.preg && Ext.preg("gx_printextent", GeoExt.plugins.PrintExtent);

Modified: core/trunk/geoext/tests/lib/GeoExt/plugins/PrintExtent.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/plugins/PrintExtent.html	2010-04-01 09:27:59 UTC (rev 2064)
+++ core/trunk/geoext/tests/lib/GeoExt/plugins/PrintExtent.html	2010-04-01 09:28:04 UTC (rev 2065)
@@ -10,7 +10,7 @@
         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_ctor(t) {
-            t.plan(2);
+            t.plan(1);
 
             var printProvider = new GeoExt.data.PrintProvider({
                 capabilities: printCapabilities
@@ -22,8 +22,6 @@
             
             t.ok(printExtent.printProvider === printProvider,
                  "ctor sets printProvider in the instance");
-            t.ok(printExtent.pages[0] instanceof GeoExt.data.PrintPage,
-                 "ctor creates a PrintPage");
         }
         
         function test_init(t) {
@@ -103,6 +101,7 @@
                 zoom: 7,
                 plugins: [printExtent]
             });
+            printExtent.addPage();
 
             t.ok(printExtent.pages[0].center.equals(mapPanel.map.getCenter()),
                  "print page centered to map center");
@@ -117,7 +116,7 @@
         }
 
         function test_tearDown(t) {
-            t.plan(3);
+            t.plan(2);
  
             var printExtent = new GeoExt.plugins.PrintExtent({
                 printProvider: new GeoExt.data.PrintProvider({
@@ -144,11 +143,89 @@
                  "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.")
 
             mapPanel.destroy();
         }
+
+        function test_addPage(t) {
+
+            t.plan(2);
+
+            var printExtent = new GeoExt.plugins.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("layer", {isBaseLayer: true})
+                ],
+                center: [146.56, -41.56],
+                zoom: 7,
+                plugins: [printExtent]
+            });
+
+            var page = printExtent.addPage();
+
+            t.ok(printExtent.pages[printExtent.pages.length - 1] == page,
+                "addPage correctly creates and add a new page when none given ");
+
+            var p = {
+                feature: new OpenLayers.Feature(),
+                on: function() {},
+                un: function() {},
+                fit: function() {}
+            };
+            var page2 = printExtent.addPage(p);
+            t.ok(printExtent.pages[printExtent.pages.length - 1] == page2,
+                "addPage corectly add a page");
+
+            mapPanel.destroy();
+        }
+
+        function test_removePage(t) {
+
+            t.plan(1);
+
+            var page = {
+                feature: new OpenLayers.Feature(),
+                 on: function() {},
+                 un: function() {},
+                 fit: function() {}
+            };
+
+            var printExtent = new GeoExt.plugins.PrintExtent({
+                printProvider: new GeoExt.data.PrintProvider({
+                    capabilities: printCapabilities
+                }),
+                pages: [page] 
+            });
+
+            var mapPanel = new GeoExt.MapPanel({
+                renderTo: "mappanel",
+                width: 256,
+                height: 256,
+                map: {controls: []},
+                layers: [
+                    new OpenLayers.Layer("layer", {isBaseLayer: true})
+                ],
+                center: [146.56, -41.56],
+                zoom: 7,
+                plugins: [printExtent]
+            });
+
+            printExtent.removePage(page);
+
+            t.eq(printExtent.pages.length, 0,
+                "page is correctly removed");
+            mapPanel.destroy();
+        }
+
     </script>
   <body>
     <div id="mappanel"></div>



More information about the Commits mailing list