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 | |
---|
9 | /** |
---|
10 | * @requires GeoExt/widgets/MapPanel.js |
---|
11 | * @include GeoExt/data/PrintProvider.js |
---|
12 | * @include GeoExt/data/PrintPage.js |
---|
13 | */ |
---|
14 | Ext.namespace("GeoExt"); |
---|
15 | |
---|
16 | /** api: (define) |
---|
17 | * module = GeoExt |
---|
18 | * class = PrintMapPanel |
---|
19 | */ |
---|
20 | |
---|
21 | /** api: (extends) |
---|
22 | * GeoExt/widgets/MapPanel.js |
---|
23 | */ |
---|
24 | |
---|
25 | /** api: example |
---|
26 | * A map with a "Print..." button. If clicked, a dialog containing a |
---|
27 | * PrintMapPanel will open, with a "Create PDF" button. |
---|
28 | * |
---|
29 | * .. code-block:: javascript |
---|
30 | * |
---|
31 | * var mapPanel = new GeoExt.MapPanel({ |
---|
32 | * renderTo: "map", |
---|
33 | * layers: [new OpenLayers.Layer.WMS("Tasmania State Boundaries", |
---|
34 | * "http://demo.opengeo.org/geoserver/wms", |
---|
35 | * {layers: "topp:tasmania_state_boundaries"}, {singleTile: true})], |
---|
36 | * center: [146.56, -41.56], |
---|
37 | * zoom: 6, |
---|
38 | * bbar: [{ |
---|
39 | * text: "Print...", |
---|
40 | * handler: function() { |
---|
41 | * var printDialog = new Ext.Window({ |
---|
42 | * autoHeight: true, |
---|
43 | * width: 350, |
---|
44 | * items: [new GeoExt.PrintMapPanel({ |
---|
45 | * sourceMap: mapPanel, |
---|
46 | * printProvider: { |
---|
47 | * capabilities: printCapabilities |
---|
48 | * } |
---|
49 | * })], |
---|
50 | * bbar: [{ |
---|
51 | * text: "Create PDF", |
---|
52 | * handler: function() { |
---|
53 | * printDialog.items.get(0).print(); |
---|
54 | * } |
---|
55 | * }] |
---|
56 | * }); |
---|
57 | * printDialog.show(); |
---|
58 | * } |
---|
59 | * }] |
---|
60 | * }); |
---|
61 | */ |
---|
62 | |
---|
63 | /** api: constructor |
---|
64 | * .. class:: PrintMapPanel |
---|
65 | * |
---|
66 | * A map panel that controls scale and center of a print page. Based on the |
---|
67 | * current view (i.e. layers and extent) of a source map, this panel will be |
---|
68 | * sized according to the aspect ratio of the print page. As the user zooms |
---|
69 | * and pans in the :class:`GeoExt.PrintMapPanel`, the print page will update |
---|
70 | * its scale and center accordingly. If the scale on the print page changes |
---|
71 | * (e.g. by setting it using a combo box with a |
---|
72 | * :class:`GeoExt.plugins.PrintPageField`), the extent of the |
---|
73 | * :class:`GeoExt.PrintMapPanel` will be updated to match the page bounds. |
---|
74 | * |
---|
75 | * .. note:: The ``zoom``, ``center`` and ``extent`` config options will have |
---|
76 | * no affect, as they will be determined by the ``sourceMap``. |
---|
77 | */ |
---|
78 | GeoExt.PrintMapPanel = Ext.extend(GeoExt.MapPanel, { |
---|
79 | |
---|
80 | /** api: config[map] |
---|
81 | * ``Object`` Optional configuration for the ``OpenLayers.Map`` object |
---|
82 | * that this PrintMapPanel creates. Useful e.g. to configure a map with a |
---|
83 | * custom set of controls, or to add a ``preaddlayer`` listener for |
---|
84 | * filtering out layer types that cannot be printed. |
---|
85 | */ |
---|
86 | |
---|
87 | /** api: config[sourceMap] |
---|
88 | * :class:`GeoExt.MapPanel` or ``OpenLayers.Map`` The map that is to be |
---|
89 | * printed. |
---|
90 | */ |
---|
91 | |
---|
92 | /** private: property[sourceMap] |
---|
93 | * ``OpenLayers.Map`` |
---|
94 | */ |
---|
95 | sourceMap: null, |
---|
96 | |
---|
97 | /** api: config[printProvider] |
---|
98 | * :class:`GeoExt.data.PrintProvider` or ``Object`` PrintProvider to use |
---|
99 | * for printing. If an ``Object`` is provided, a new PrintProvider will |
---|
100 | * be created and configured with the object. |
---|
101 | * |
---|
102 | * .. note:: The PrintMapPanel requires the printProvider's capabilities |
---|
103 | * to be available upon initialization. This means that a PrintMapPanel |
---|
104 | * configured with an ``Object`` as ``printProvider`` will only work |
---|
105 | * when ``capabilities`` is provided in the printProvider's |
---|
106 | * configuration object. If ``printProvider`` is provided as an instance |
---|
107 | * of :class:`GeoExt.data.PrintProvider`, the capabilities must be |
---|
108 | * loaded before PrintMapPanel initialization. |
---|
109 | */ |
---|
110 | |
---|
111 | /** api: property[printProvider] |
---|
112 | * :class:`GeoExt.data.PrintProvider` PrintProvider for this |
---|
113 | * PrintMapPanel. |
---|
114 | */ |
---|
115 | printProvider: null, |
---|
116 | |
---|
117 | /** api: property[printPage] |
---|
118 | * :class:`GeoExt.data.PrintPage` PrintPage for this PrintMapPanel. |
---|
119 | * Read-only. |
---|
120 | */ |
---|
121 | printPage: null, |
---|
122 | |
---|
123 | /** api: config[limitScales] |
---|
124 | * ``Boolean`` If set to true, the printPage cannot be set to scales that |
---|
125 | * would generate a preview in this :class:`GeoExt.PrintMapPanel` with a |
---|
126 | * completely different extent than the one that would appear on the |
---|
127 | * printed map. Default is false. |
---|
128 | */ |
---|
129 | |
---|
130 | /** api: property[previewScales] |
---|
131 | * ``Ext.data.Store`` A data store with a subset of the printProvider's |
---|
132 | * scales. By default, this contains all the scales of the printProvider. |
---|
133 | * If ``limitScales`` is set to true, it will only contain print scales |
---|
134 | * that can properly be previewed with this :class:`GeoExt.PrintMapPanel`. |
---|
135 | */ |
---|
136 | previewScales: null, |
---|
137 | |
---|
138 | /** api: config[center] |
---|
139 | * ``OpenLayers.LonLat`` or ``Array(Number)`` A location for the map |
---|
140 | * center. Do not set, as this will be overridden with the ``sourceMap`` |
---|
141 | * center. |
---|
142 | */ |
---|
143 | center: null, |
---|
144 | |
---|
145 | /** api: config[zoom] |
---|
146 | * ``Number`` An initial zoom level for the map. Do not set, because the |
---|
147 | * initial extent will be determined by the ``sourceMap``. |
---|
148 | */ |
---|
149 | zoom: null, |
---|
150 | |
---|
151 | /** api: config[extent] |
---|
152 | * ``OpenLayers.Bounds or Array(Number)`` An initial extent for the map. |
---|
153 | * Do not set, because the initial extent will be determined by the |
---|
154 | * ``sourceMap``. |
---|
155 | */ |
---|
156 | extent: null, |
---|
157 | |
---|
158 | /** private: property[currentZoom] |
---|
159 | * ``Number`` |
---|
160 | */ |
---|
161 | currentZoom: null, |
---|
162 | |
---|
163 | /** |
---|
164 | * private: method[initComponent] |
---|
165 | * private override |
---|
166 | */ |
---|
167 | initComponent: function() { |
---|
168 | if(this.sourceMap instanceof GeoExt.MapPanel) { |
---|
169 | this.sourceMap = this.sourceMap.map; |
---|
170 | } |
---|
171 | |
---|
172 | if (!this.map) { |
---|
173 | this.map = {}; |
---|
174 | } |
---|
175 | Ext.applyIf(this.map, { |
---|
176 | projection: this.sourceMap.getProjection(), |
---|
177 | maxExtent: this.sourceMap.getMaxExtent(), |
---|
178 | maxResolution: this.sourceMap.getMaxResolution(), |
---|
179 | units: this.sourceMap.getUnits() |
---|
180 | }); |
---|
181 | |
---|
182 | if(!(this.printProvider instanceof GeoExt.data.PrintProvider)) { |
---|
183 | this.printProvider = new GeoExt.data.PrintProvider( |
---|
184 | this.printProvider); |
---|
185 | } |
---|
186 | this.printPage = new GeoExt.data.PrintPage({ |
---|
187 | printProvider: this.printProvider |
---|
188 | }); |
---|
189 | |
---|
190 | this.previewScales = new Ext.data.Store(); |
---|
191 | this.previewScales.add(this.printProvider.scales.getRange()); |
---|
192 | |
---|
193 | this.layers = []; |
---|
194 | var layer; |
---|
195 | Ext.each(this.sourceMap.layers, function(layer) { |
---|
196 | var clone = layer.clone(); |
---|
197 | layer.getVisibility() === true && this.layers.push(clone); |
---|
198 | }, this); |
---|
199 | |
---|
200 | this.extent = this.sourceMap.getExtent(); |
---|
201 | |
---|
202 | GeoExt.PrintMapPanel.superclass.initComponent.call(this); |
---|
203 | |
---|
204 | this.printProvider.on("layoutchange", this.syncSize, this); |
---|
205 | this.printPage.on("change", this.fitZoom, this); |
---|
206 | this.map.events.register("moveend", this, this.updatePage); |
---|
207 | }, |
---|
208 | |
---|
209 | /** private: method[afterRender] |
---|
210 | * Private method called after the panel has been rendered. |
---|
211 | */ |
---|
212 | afterRender: function() { |
---|
213 | GeoExt.PrintMapPanel.superclass.afterRender.apply(this, arguments); |
---|
214 | this.syncSize(); |
---|
215 | if (this.initialConfig.limitScales === true) { |
---|
216 | if (!this.ownerCt) { |
---|
217 | this.calculatePreviewScales(); |
---|
218 | } else { |
---|
219 | this.ownerCt.on({ |
---|
220 | "afterlayout": { |
---|
221 | fn: this.calculatePreviewScales, |
---|
222 | scope: this, |
---|
223 | single: true |
---|
224 | } |
---|
225 | }); |
---|
226 | } |
---|
227 | this.on("resize", this.calculatePreviewScales, this); |
---|
228 | } |
---|
229 | }, |
---|
230 | |
---|
231 | /** private: method[adjustSize] |
---|
232 | * :param width: ``Number`` If not provided or 0, initialConfig.width will |
---|
233 | * be used. |
---|
234 | * :param height: ``Number`` If not provided or 0, initialConfig.height |
---|
235 | * will be used. |
---|
236 | * Private override - sizing this component always takes the aspect ratio |
---|
237 | * of the print page into account. |
---|
238 | */ |
---|
239 | adjustSize: function(width, height) { |
---|
240 | var printSize = this.printProvider.layout.get("size"); |
---|
241 | var ratio = printSize.width / printSize.height; |
---|
242 | // respect width & height when sizing according to the print page's |
---|
243 | // aspect ratio - do not exceed either, but don't take values for |
---|
244 | // granted if container is configured with autoWidth or autoHeight. |
---|
245 | var ownerCt = this.ownerCt; |
---|
246 | var targetWidth = (ownerCt && ownerCt.autoWidth) ? 0 : |
---|
247 | (width || this.initialConfig.width); |
---|
248 | var targetHeight = (ownerCt && ownerCt.autoHeight) ? 0 : |
---|
249 | (height || this.initialConfig.height); |
---|
250 | if (targetWidth) { |
---|
251 | height = targetWidth / ratio; |
---|
252 | if (targetHeight && height > targetHeight) { |
---|
253 | height = targetHeight; |
---|
254 | width = height * ratio; |
---|
255 | } else { |
---|
256 | width = targetWidth; |
---|
257 | } |
---|
258 | } else if (targetHeight) { |
---|
259 | width = targetHeight * ratio; |
---|
260 | height = targetHeight; |
---|
261 | } |
---|
262 | |
---|
263 | return {width: width, height: height}; |
---|
264 | }, |
---|
265 | |
---|
266 | /** private: method[fitZoom] |
---|
267 | * Fits this PrintMapPanel's zoom to the print scale. |
---|
268 | */ |
---|
269 | fitZoom: function() { |
---|
270 | if (!this._updating && this.printPage.scale) { |
---|
271 | this._updating = true; |
---|
272 | var printBounds = this.printPage.getPrintExtent(this.map); |
---|
273 | this.currentZoom = this.map.getZoomForExtent(printBounds); |
---|
274 | this.map.zoomToExtent(printBounds); |
---|
275 | delete this._updating; |
---|
276 | } |
---|
277 | }, |
---|
278 | |
---|
279 | /** private: method[updatePage] |
---|
280 | * updates the print page to match this PrintMapPanel's center and scale. |
---|
281 | */ |
---|
282 | updatePage: function() { |
---|
283 | if (!this._updating) { |
---|
284 | var zoom = this.map.getZoom(); |
---|
285 | this._updating = true; |
---|
286 | if (zoom === this.currentZoom) { |
---|
287 | this.printPage.setCenter(this.map.getCenter()); |
---|
288 | } else { |
---|
289 | this.printPage.fit(this.map); |
---|
290 | } |
---|
291 | delete this._updating; |
---|
292 | this.currentZoom = zoom; |
---|
293 | } |
---|
294 | }, |
---|
295 | |
---|
296 | /** private: method[calculatePreviewScales] |
---|
297 | */ |
---|
298 | calculatePreviewScales: function() { |
---|
299 | this.previewScales.removeAll(); |
---|
300 | |
---|
301 | this.printPage.suspendEvents(); |
---|
302 | var scale = this.printPage.scale; |
---|
303 | |
---|
304 | // group print scales by the zoom level they would be previewed at |
---|
305 | var viewSize = this.map.getSize(); |
---|
306 | var scalesByZoom = {}; |
---|
307 | var zooms = []; |
---|
308 | this.printProvider.scales.each(function(rec) { |
---|
309 | this.printPage.setScale(rec); |
---|
310 | var extent = this.printPage.getPrintExtent(this.map); |
---|
311 | var zoom = this.map.getZoomForExtent(extent); |
---|
312 | |
---|
313 | var idealResolution = Math.max( |
---|
314 | extent.getWidth() / viewSize.w, |
---|
315 | extent.getHeight() / viewSize.h |
---|
316 | ); |
---|
317 | var resolution = this.map.getResolutionForZoom(zoom); |
---|
318 | // the closer to the ideal resolution, the better the fit |
---|
319 | var diff = Math.abs(idealResolution - resolution); |
---|
320 | if (!(zoom in scalesByZoom) || scalesByZoom[zoom].diff > diff) { |
---|
321 | scalesByZoom[zoom] = { |
---|
322 | rec: rec, |
---|
323 | diff: diff |
---|
324 | }; |
---|
325 | zooms.indexOf(zoom) == -1 && zooms.push(zoom); |
---|
326 | } |
---|
327 | }, this); |
---|
328 | |
---|
329 | // add only the preview scales that closely fit print extents |
---|
330 | for (var i=0, ii=zooms.length; i<ii; ++i) { |
---|
331 | this.previewScales.add(scalesByZoom[zooms[i]].rec); |
---|
332 | } |
---|
333 | |
---|
334 | scale && this.printPage.setScale(scale); |
---|
335 | this.printPage.resumeEvents(); |
---|
336 | |
---|
337 | if (scale && this.previewScales.getCount() > 0) { |
---|
338 | var maxScale = this.previewScales.getAt(0); |
---|
339 | var minScale = this.previewScales.getAt(this.previewScales.getCount()-1); |
---|
340 | if (scale.get("value") < minScale.get("value")) { |
---|
341 | this.printPage.setScale(minScale); |
---|
342 | } else if (scale.get("value") > maxScale.get("value")) { |
---|
343 | this.printPage.setScale(maxScale); |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | this.fitZoom(); |
---|
348 | }, |
---|
349 | |
---|
350 | /** api: method[print] |
---|
351 | * :param options: ``Object`` options for |
---|
352 | * the :class:`GeoExt.data.PrintProvider` :: ``print`` method. |
---|
353 | * |
---|
354 | * Convenience method for printing the map, without the need to |
---|
355 | * interact with the printProvider and printPage. |
---|
356 | */ |
---|
357 | print: function(options) { |
---|
358 | this.printProvider.print(this.map, [this.printPage], options); |
---|
359 | }, |
---|
360 | |
---|
361 | /** private: method[beforeDestroy] |
---|
362 | */ |
---|
363 | beforeDestroy: function() { |
---|
364 | this.map.events.unregister("moveend", this, this.updatePage); |
---|
365 | this.printPage.un("change", this.fitZoom, this); |
---|
366 | this.printProvider.un("layoutchange", this.syncSize, this); |
---|
367 | GeoExt.PrintMapPanel.superclass.beforeDestroy.apply(this, arguments); |
---|
368 | } |
---|
369 | }); |
---|
370 | |
---|
371 | /** api: xtype = gx_printmappanel */ |
---|
372 | Ext.reg('gx_printmappanel', GeoExt.PrintMapPanel); |
---|
373 | |
---|