1 | /** |
---|
2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
3 | * |
---|
4 | * Published under the BSD license. |
---|
5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
6 | * of the license. |
---|
7 | */ |
---|
8 | Ext.namespace("GeoExt.plugins"); |
---|
9 | |
---|
10 | /** api: (define) |
---|
11 | * module = GeoExt.plugins |
---|
12 | * class = PrintExtent |
---|
13 | */ |
---|
14 | |
---|
15 | /** api: example |
---|
16 | * Sample code to create a MapPanel with a PrintExtent, and print it |
---|
17 | * immediately: |
---|
18 | * |
---|
19 | * .. code-block:: javascript |
---|
20 | * |
---|
21 | * var printExtent = new GeoExt.plugins.PrintExtent({ |
---|
22 | * printProvider: new GeoExt.data.PrintProvider({ |
---|
23 | * capabilities: printCapabilities |
---|
24 | * }) |
---|
25 | * }); |
---|
26 | * |
---|
27 | * var mapPanel = new GeoExt.MapPanel({ |
---|
28 | * border: false, |
---|
29 | * renderTo: "div-id", |
---|
30 | * layers: [new OpenLayers.Layer.WMS("Tasmania", "http://demo.opengeo.org/geoserver/wms", |
---|
31 | * {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})], |
---|
32 | * center: [146.56, -41.56], |
---|
33 | * zoom: 6, |
---|
34 | * plugins: printExtent |
---|
35 | * }); |
---|
36 | * |
---|
37 | * printExtent.addPage(); |
---|
38 | * |
---|
39 | * // print the map |
---|
40 | * printExtent.print(); |
---|
41 | */ |
---|
42 | |
---|
43 | /** api: constructor |
---|
44 | * .. class:: PrintExtent |
---|
45 | * |
---|
46 | * Provides a way to show and modify the extents of print pages on the map. It |
---|
47 | * uses a layer to render the page extent and handle features of print pages, |
---|
48 | * and provides a control to modify them. Must be set as a plugin to a |
---|
49 | * :class:`GeoExt.MapPanel`. |
---|
50 | */ |
---|
51 | GeoExt.plugins.PrintExtent = Ext.extend(Ext.util.Observable, { |
---|
52 | |
---|
53 | /** private: initialConfig |
---|
54 | * ``Object`` Holds the initial config object passed to the |
---|
55 | * constructor. |
---|
56 | */ |
---|
57 | initialConfig: null, |
---|
58 | |
---|
59 | /** api: config[printProvider] |
---|
60 | * :class:`GeoExt.data.PrintProvider` The print provider this form |
---|
61 | * is connected to. Optional if pages are provided. |
---|
62 | */ |
---|
63 | /** api: property[printProvider] |
---|
64 | * :class:`GeoExt.data.PrintProvider` The print provider this form |
---|
65 | * is connected to. Read-only. |
---|
66 | */ |
---|
67 | printProvider: null, |
---|
68 | |
---|
69 | /** private: property[map] |
---|
70 | * ``OpenLayers.Map`` The map the layer and control are added to. |
---|
71 | */ |
---|
72 | map: null, |
---|
73 | |
---|
74 | /** api: config[layer] |
---|
75 | * ``OpenLayers.Layer.Vector`` The layer used to render extent and handle |
---|
76 | * features to. Optional, will be created if not provided. |
---|
77 | */ |
---|
78 | /** private: property[layer] |
---|
79 | * ``OpenLayers.Layer.Vector`` The layer used to render extent and handle |
---|
80 | * features to. |
---|
81 | */ |
---|
82 | layer: null, |
---|
83 | |
---|
84 | /** private: property[control] |
---|
85 | * ``OpenLayers.Control.TransformFeature`` The control used to change |
---|
86 | * extent, center, rotation and scale. |
---|
87 | */ |
---|
88 | control: null, |
---|
89 | |
---|
90 | /** api: config[pages] |
---|
91 | * Array of :class:`GeoExt.data.PrintPage` The pages that this plugin |
---|
92 | * controls. Optional. If not provided, it will be created with one page |
---|
93 | * that is completely contained within the visible map extent. |
---|
94 | * |
---|
95 | * .. note:: All pages must use the same PrintProvider. |
---|
96 | */ |
---|
97 | /** api: property[pages] |
---|
98 | * Array of :class:`GeoExt.data.PrintPage` The pages that this component |
---|
99 | * controls. Read-only. |
---|
100 | */ |
---|
101 | pages: null, |
---|
102 | |
---|
103 | /** api: property[page] |
---|
104 | * :class:`GeoExt.data.PrintPage` The page currently set for |
---|
105 | * transformation. |
---|
106 | */ |
---|
107 | page: null, |
---|
108 | |
---|
109 | /** private: method[constructor] |
---|
110 | * Private constructor override. |
---|
111 | */ |
---|
112 | constructor: function(config) { |
---|
113 | config = config || {}; |
---|
114 | |
---|
115 | Ext.apply(this, config); |
---|
116 | this.initialConfig = config; |
---|
117 | |
---|
118 | if(!this.printProvider) { |
---|
119 | this.printProvider = this.pages[0].printProvider; |
---|
120 | } |
---|
121 | |
---|
122 | if(!this.pages) { |
---|
123 | this.pages = []; |
---|
124 | } |
---|
125 | |
---|
126 | this.addEvents( |
---|
127 | /** api: event[selectpage] |
---|
128 | * Triggered when a page has been selected using the control |
---|
129 | * |
---|
130 | * Listener arguments: |
---|
131 | * * printPage - :class:`GeoExt.data.PrintPage` this printPage |
---|
132 | */ |
---|
133 | "selectpage" |
---|
134 | ); |
---|
135 | |
---|
136 | GeoExt.plugins.PrintExtent.superclass.constructor.apply(this, arguments); |
---|
137 | }, |
---|
138 | |
---|
139 | /** api: method[print] |
---|
140 | * :param options: ``Object`` Options to send to the PrintProvider's |
---|
141 | * print method. See :class:`GeoExt.data.PrintProvider` :: ``print``. |
---|
142 | * |
---|
143 | * Prints all pages as shown on the map. |
---|
144 | */ |
---|
145 | print: function(options) { |
---|
146 | this.printProvider.print(this.map, this.pages, options); |
---|
147 | }, |
---|
148 | |
---|
149 | /** private: method[init] |
---|
150 | * :param mapPanel: class:`GeoExt.MapPanel` |
---|
151 | * |
---|
152 | * Initializes the plugin. |
---|
153 | */ |
---|
154 | init: function(mapPanel) { |
---|
155 | this.map = mapPanel.map; |
---|
156 | mapPanel.on("destroy", this.onMapPanelDestroy, this); |
---|
157 | |
---|
158 | if (!this.layer) { |
---|
159 | this.layer = new OpenLayers.Layer.Vector(null, { |
---|
160 | displayInLayerSwitcher: false |
---|
161 | }); |
---|
162 | } |
---|
163 | this.createControl(); |
---|
164 | |
---|
165 | for(var i=0, len=this.pages.length; i<len; ++i) { |
---|
166 | this.addPage(this.pages[i]); |
---|
167 | } |
---|
168 | this.show(); |
---|
169 | }, |
---|
170 | |
---|
171 | /** api: method[addPage] |
---|
172 | * :param page: :class:`GeoExt.data.PrintPage` The page to add |
---|
173 | * to this plugin. If not provided, a page that fits the current |
---|
174 | * extent is created. |
---|
175 | * :return: page :class:``GeoExt.data.PrintPage`` |
---|
176 | * |
---|
177 | * Adds a page to the list of pages that this plugin controls. |
---|
178 | */ |
---|
179 | addPage: function(page) { |
---|
180 | page = page || new GeoExt.data.PrintPage({ |
---|
181 | printProvider: this.printProvider |
---|
182 | }); |
---|
183 | if(this.pages.indexOf(page) === -1) { |
---|
184 | this.pages.push(page); |
---|
185 | } |
---|
186 | this.layer.addFeatures([page.feature]); |
---|
187 | page.on("change", this.onPageChange, this); |
---|
188 | |
---|
189 | this.page = page; |
---|
190 | var map = this.map; |
---|
191 | if(map.getCenter()) { |
---|
192 | this.fitPage(); |
---|
193 | } else { |
---|
194 | map.events.register("moveend", this, function() { |
---|
195 | map.events.unregister("moveend", this, arguments.callee); |
---|
196 | this.fitPage(); |
---|
197 | }); |
---|
198 | } |
---|
199 | return page; |
---|
200 | }, |
---|
201 | |
---|
202 | /** api: method[removePage] |
---|
203 | * :param page: :class:`GeoExt.data.PrintPage` The page to remove |
---|
204 | * from this plugin. |
---|
205 | * |
---|
206 | * Removes a page from the list of pages that this plugin controls. |
---|
207 | */ |
---|
208 | removePage: function(page) { |
---|
209 | this.pages.remove(page); |
---|
210 | if (page.feature.layer) { |
---|
211 | this.layer.removeFeatures([page.feature]); |
---|
212 | } |
---|
213 | page.un("change", this.onPageChange, this); |
---|
214 | }, |
---|
215 | |
---|
216 | /** api: method[selectPage] |
---|
217 | * :param page: :class:`GeoExt.data.PrintPage` The page to select |
---|
218 | * |
---|
219 | * Selects the given page (ie. calls the setFeature on the modify feature |
---|
220 | * control) |
---|
221 | */ |
---|
222 | selectPage: function(page) { |
---|
223 | this.control.active && this.control.setFeature(page.feature); |
---|
224 | // FIXME raise the feature up so that it is on top |
---|
225 | }, |
---|
226 | |
---|
227 | /** api: method[show] |
---|
228 | * |
---|
229 | * Sets up the plugin, initializing the ``OpenLayers.Layer.Vector`` |
---|
230 | * layer and ``OpenLayers.Control.TransformFeature``, and centering |
---|
231 | * the first page if no pages were specified in the configuration. |
---|
232 | */ |
---|
233 | show: function() { |
---|
234 | this.map.addLayer(this.layer); |
---|
235 | this.map.addControl(this.control); |
---|
236 | this.control.activate(); |
---|
237 | |
---|
238 | // if we have a page and if the map has a center then update the |
---|
239 | // transform box for that page, in case the transform control |
---|
240 | // was deactivated when fitPage (and therefore onPageChange) |
---|
241 | // was called. |
---|
242 | if (this.page && this.map.getCenter()) { |
---|
243 | this.updateBox(); |
---|
244 | } |
---|
245 | }, |
---|
246 | |
---|
247 | /** api: method[hide] |
---|
248 | * |
---|
249 | * Tear downs the plugin, removing the |
---|
250 | * ``OpenLayers.Control.TransformFeature`` control and |
---|
251 | * the ``OpenLayers.Layer.Vector`` layer. |
---|
252 | */ |
---|
253 | hide: function() { |
---|
254 | // note: we need to be extra cautious when destroying OpenLayers |
---|
255 | // objects here (the tests will fail if we're not cautious anyway). |
---|
256 | // We use obj.events to test whether an OpenLayers object is |
---|
257 | // destroyed or not. |
---|
258 | |
---|
259 | var map = this.map; |
---|
260 | |
---|
261 | var control = this.control; |
---|
262 | if(control && control.events) { |
---|
263 | control.deactivate(); |
---|
264 | if(map && map.events && control.map) { |
---|
265 | map.removeControl(control); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | var layer = this.layer; |
---|
270 | |
---|
271 | if(!this.initialConfig.layer && |
---|
272 | map && map.events && |
---|
273 | layer && layer.map) { |
---|
274 | map.removeLayer(layer); |
---|
275 | } |
---|
276 | }, |
---|
277 | |
---|
278 | /** private: method[onMapPanelDestroy] |
---|
279 | */ |
---|
280 | onMapPanelDestroy: function() { |
---|
281 | |
---|
282 | var map = this.map; |
---|
283 | |
---|
284 | for(var len = this.pages.length - 1, i = len; i>=0; i--) { |
---|
285 | this.removePage(this.pages[i]); |
---|
286 | } |
---|
287 | |
---|
288 | this.hide(); |
---|
289 | |
---|
290 | var control = this.control; |
---|
291 | if(map && map.events && |
---|
292 | control && control.events) { |
---|
293 | control.destroy(); |
---|
294 | } |
---|
295 | |
---|
296 | var layer = this.layer; |
---|
297 | if(!this.initialConfig.layer && |
---|
298 | map && map.events && |
---|
299 | layer && layer.events) { |
---|
300 | layer.destroy(); |
---|
301 | } |
---|
302 | |
---|
303 | delete this.layer; |
---|
304 | delete this.control; |
---|
305 | delete this.page; |
---|
306 | this.map = null; |
---|
307 | }, |
---|
308 | |
---|
309 | /** private: method[createControl] |
---|
310 | */ |
---|
311 | createControl: function() { |
---|
312 | this.control = new OpenLayers.Control.TransformFeature(this.layer, { |
---|
313 | preserveAspectRatio: true, |
---|
314 | eventListeners: { |
---|
315 | "beforesetfeature": function(e) { |
---|
316 | for(var i=0, len=this.pages.length; i<len; ++i) { |
---|
317 | if(this.pages[i].feature === e.feature) { |
---|
318 | this.page = this.pages[i]; |
---|
319 | e.object.rotation = -this.pages[i].rotation; |
---|
320 | break; |
---|
321 | } |
---|
322 | } |
---|
323 | }, |
---|
324 | "setfeature": function(e) { |
---|
325 | for(var i=0, len=this.pages.length; i<len; ++i) { |
---|
326 | if(this.pages[i].feature === e.feature) { |
---|
327 | this.fireEvent("selectpage", this.pages[i]); |
---|
328 | break; |
---|
329 | } |
---|
330 | } |
---|
331 | }, |
---|
332 | "beforetransform": function(e) { |
---|
333 | this._updating = true; |
---|
334 | var page = this.page; |
---|
335 | if(e.rotation) { |
---|
336 | if(this.printProvider.layout.get("rotation")) { |
---|
337 | page.setRotation(-e.object.rotation); |
---|
338 | } else { |
---|
339 | e.object.setFeature(page.feature); |
---|
340 | } |
---|
341 | } else if(e.center) { |
---|
342 | page.setCenter(OpenLayers.LonLat.fromString( |
---|
343 | e.center.toShortString() |
---|
344 | )); |
---|
345 | } else { |
---|
346 | page.fit(e.object.box, {mode: "closest"}); |
---|
347 | var minScale = this.printProvider.scales.getAt(0); |
---|
348 | var maxScale = this.printProvider.scales.getAt( |
---|
349 | this.printProvider.scales.getCount() - 1); |
---|
350 | var boxBounds = e.object.box.geometry.getBounds(); |
---|
351 | var pageBounds = page.feature.geometry.getBounds(); |
---|
352 | var tooLarge = page.scale === minScale && |
---|
353 | boxBounds.containsBounds(pageBounds); |
---|
354 | var tooSmall = page.scale === maxScale && |
---|
355 | pageBounds.containsBounds(boxBounds); |
---|
356 | if(tooLarge === true || tooSmall === true) { |
---|
357 | this.updateBox(); |
---|
358 | } |
---|
359 | } |
---|
360 | delete this._updating; |
---|
361 | return false; |
---|
362 | }, |
---|
363 | "transformcomplete": this.updateBox, |
---|
364 | scope: this |
---|
365 | } |
---|
366 | }); |
---|
367 | }, |
---|
368 | |
---|
369 | /** private: method[fitPage] |
---|
370 | * Fits the current print page to the map. |
---|
371 | */ |
---|
372 | fitPage: function() { |
---|
373 | if(this.page) { |
---|
374 | this.page.fit(this.map, {mode: "screen"}); |
---|
375 | } |
---|
376 | }, |
---|
377 | |
---|
378 | /** private: method[updateBox] |
---|
379 | * Updates the transformation box after setting a new scale or |
---|
380 | * layout, or to fit the box to the extent feature after a tranform. |
---|
381 | */ |
---|
382 | updateBox: function() { |
---|
383 | var page = this.page; |
---|
384 | this.control.active && |
---|
385 | this.control.setFeature(page.feature, {rotation: -page.rotation}); |
---|
386 | }, |
---|
387 | |
---|
388 | /** private: method[onPageChange] |
---|
389 | * Handler for a page's change event. |
---|
390 | */ |
---|
391 | onPageChange: function(page, mods) { |
---|
392 | if(!this._updating) { |
---|
393 | this.control.active && |
---|
394 | this.control.setFeature(page.feature, {rotation: -page.rotation}); |
---|
395 | } |
---|
396 | } |
---|
397 | }); |
---|
398 | |
---|
399 | /** api: ptype = gx_printextent */ |
---|
400 | Ext.preg("gx_printextent", GeoExt.plugins.PrintExtent); |
---|