[76] | 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.data"); |
---|
| 9 | |
---|
| 10 | /** api: (define) |
---|
| 11 | * module = GeoExt.data |
---|
| 12 | * class = PrintProvider |
---|
| 13 | * base_link = `Ext.util.Observable <http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.Observable>`_ |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /** api: example |
---|
| 17 | * Minimal code to print as much of the current map extent as possible as |
---|
| 18 | * soon as the print service capabilities are loaded, using the first layout |
---|
| 19 | * reported by the print service: |
---|
| 20 | * |
---|
| 21 | * .. code-block:: javascript |
---|
| 22 | * |
---|
| 23 | * var mapPanel = new GeoExt.MapPanel({ |
---|
| 24 | * renderTo: "mappanel", |
---|
| 25 | * layers: [new OpenLayers.Layer.WMS("wms", "/geoserver/wms", |
---|
| 26 | * {layers: "topp:tasmania_state_boundaries"})], |
---|
| 27 | * center: [146.56, -41.56], |
---|
| 28 | * zoom: 7 |
---|
| 29 | * }); |
---|
| 30 | * var printProvider = new GeoExt.data.PrintProvider({ |
---|
| 31 | * url: "/geoserver/pdf", |
---|
| 32 | * listeners: { |
---|
| 33 | * "loadcapabilities": function() { |
---|
| 34 | * var printPage = new GeoExt.data.PrintPage({ |
---|
| 35 | * printProvider: printProvider |
---|
| 36 | * }); |
---|
| 37 | * printPage.fit(mapPanel, true); |
---|
| 38 | * printProvider.print(mapPanel, printPage); |
---|
| 39 | * } |
---|
| 40 | * } |
---|
| 41 | * }); |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | /** api: constructor |
---|
| 45 | * .. class:: PrintProvider |
---|
| 46 | * |
---|
| 47 | * Provides an interface to a Mapfish or GeoServer print module. For printing, |
---|
| 48 | * one or more instances of :class:`GeoExt.data.PrintPage` are also required |
---|
| 49 | * to tell the PrintProvider about the scale and extent (and optionally |
---|
| 50 | * rotation) of the page(s) we want to print. |
---|
| 51 | */ |
---|
| 52 | GeoExt.data.PrintProvider = Ext.extend(Ext.util.Observable, { |
---|
| 53 | |
---|
| 54 | /** api: config[url] |
---|
| 55 | * ``String`` Base url of the print service. Only required if |
---|
| 56 | * ``capabilities`` is not provided. This |
---|
| 57 | * is usually something like http://path/to/mapfish/print for Mapfish, |
---|
| 58 | * and http://path/to/geoserver/pdf for GeoServer with the printing |
---|
| 59 | * extension installed. This property requires that the print service is |
---|
| 60 | * at the same origin as the application (or accessible via proxy). |
---|
| 61 | */ |
---|
| 62 | |
---|
| 63 | /** private: property[url] |
---|
| 64 | * ``String`` Base url of the print service. Will always have a trailing |
---|
| 65 | * "/". |
---|
| 66 | */ |
---|
| 67 | url: null, |
---|
| 68 | |
---|
| 69 | /** api: config[autoLoad] |
---|
| 70 | * ``Boolean`` If set to true, the capabilities will be loaded upon |
---|
| 71 | * instance creation, and ``loadCapabilities`` does not need to be called |
---|
| 72 | * manually. Setting this when ``capabilities`` and no ``url`` is provided |
---|
| 73 | * has no effect. Default is false. |
---|
| 74 | */ |
---|
| 75 | |
---|
| 76 | /** api: config[capabilities] |
---|
| 77 | * ``Object`` Capabilities of the print service. Only required if ``url`` |
---|
| 78 | * is not provided. This is the object returned by the ``info.json`` |
---|
| 79 | * endpoint of the print service, and is usually obtained by including a |
---|
| 80 | * script tag pointing to |
---|
| 81 | * http://path/to/printservice/info.json?var=myvar in the head of the |
---|
| 82 | * html document, making the capabilities accessible as ``window.myvar``. |
---|
| 83 | * This property should be used when no local print service or proxy is |
---|
| 84 | * available, or when you do not listen for the ``loadcapabilities`` |
---|
| 85 | * events before creating components that require the PrintProvider's |
---|
| 86 | * capabilities to be available. |
---|
| 87 | */ |
---|
| 88 | |
---|
| 89 | /** private: property[capabilities] |
---|
| 90 | * ``Object`` Capabilities as returned from the print service. |
---|
| 91 | */ |
---|
| 92 | capabilities: null, |
---|
| 93 | |
---|
| 94 | /** api: config[method] |
---|
| 95 | * ``String`` Either ``POST`` or ``GET`` (case-sensitive). Method to use |
---|
| 96 | * when sending print requests to the servlet. If the print service is at |
---|
| 97 | * the same origin as the application (or accessible via proxy), then |
---|
| 98 | * ``POST`` is recommended. Use ``GET`` when accessing a remote print |
---|
| 99 | * service with no proxy available, but expect issues with character |
---|
| 100 | * encoding and URLs exceeding the maximum length. Default is ``POST``. |
---|
| 101 | */ |
---|
| 102 | |
---|
| 103 | /** private: property[method] |
---|
| 104 | * ``String`` Either ``POST`` or ``GET`` (case-sensitive). Method to use |
---|
| 105 | * when sending print requests to the servlet. |
---|
| 106 | */ |
---|
| 107 | method: "POST", |
---|
| 108 | |
---|
| 109 | /** api: config[customParams] |
---|
| 110 | * ``Object`` Key-value pairs of custom data to be sent to the print |
---|
| 111 | * service. Optional. This is e.g. useful for complex layout definitions |
---|
| 112 | * on the server side that require additional parameters. |
---|
| 113 | */ |
---|
| 114 | |
---|
| 115 | /** api: property[customParams] |
---|
| 116 | * ``Object`` Key-value pairs of custom data to be sent to the print |
---|
| 117 | * service. Optional. This is e.g. useful for complex layout definitions |
---|
| 118 | * on the server side that require additional parameters. |
---|
| 119 | */ |
---|
| 120 | customParams: null, |
---|
| 121 | |
---|
| 122 | /** api: config[baseParams] |
---|
| 123 | * ``Object`` Key-value pairs of base params to be add to every |
---|
| 124 | * request to the service. Optional. |
---|
| 125 | */ |
---|
| 126 | |
---|
| 127 | /** api: property[scales] |
---|
| 128 | * ``Ext.data.JsonStore`` read-only. A store representing the scales |
---|
| 129 | * available. |
---|
| 130 | * |
---|
| 131 | * Fields of records in this store: |
---|
| 132 | * |
---|
| 133 | * * name - ``String`` the name of the scale |
---|
| 134 | * * value - ``Float`` the scale denominator |
---|
| 135 | */ |
---|
| 136 | scales: null, |
---|
| 137 | |
---|
| 138 | /** api: property[dpis] |
---|
| 139 | * ``Ext.data.JsonStore`` read-only. A store representing the dpis |
---|
| 140 | * available. |
---|
| 141 | * |
---|
| 142 | * Fields of records in this store: |
---|
| 143 | * |
---|
| 144 | * * name - ``String`` the name of the dpi |
---|
| 145 | * * value - ``Float`` the dots per inch |
---|
| 146 | */ |
---|
| 147 | dpis: null, |
---|
| 148 | |
---|
| 149 | /** api: property[layouts] |
---|
| 150 | * ``Ext.data.JsonStore`` read-only. A store representing the layouts |
---|
| 151 | * available. |
---|
| 152 | * |
---|
| 153 | * Fields of records in this store: |
---|
| 154 | * |
---|
| 155 | * * name - ``String`` the name of the layout |
---|
| 156 | * * size - ``Object`` width and height of the map in points |
---|
| 157 | * * rotation - ``Boolean`` indicates if rotation is supported |
---|
| 158 | */ |
---|
| 159 | layouts: null, |
---|
| 160 | |
---|
| 161 | /** api: property[dpi] |
---|
| 162 | * ``Ext.data.Record`` the record for the currently used resolution. |
---|
| 163 | * Read-only, use ``setDpi`` to set the value. |
---|
| 164 | */ |
---|
| 165 | dpi: null, |
---|
| 166 | |
---|
| 167 | /** api: property[layout] |
---|
| 168 | * ``Ext.data.Record`` the record of the currently used layout. Read-only, |
---|
| 169 | * use ``setLayout`` to set the value. |
---|
| 170 | */ |
---|
| 171 | layout: null, |
---|
| 172 | |
---|
| 173 | /** private: method[constructor] |
---|
| 174 | * Private constructor override. |
---|
| 175 | */ |
---|
| 176 | constructor: function(config) { |
---|
| 177 | this.initialConfig = config; |
---|
| 178 | Ext.apply(this, config); |
---|
| 179 | |
---|
| 180 | if(!this.customParams) { |
---|
| 181 | this.customParams = {}; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | this.addEvents( |
---|
| 185 | /** api: event[loadcapabilities] |
---|
| 186 | * Triggered when the capabilities have finished loading. This |
---|
| 187 | * event will only fire when ``capabilities`` is not configured. |
---|
| 188 | * |
---|
| 189 | * Listener arguments: |
---|
| 190 | * |
---|
| 191 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 192 | * PrintProvider |
---|
| 193 | * * capabilities - ``Object`` the capabilities |
---|
| 194 | */ |
---|
| 195 | "loadcapabilities", |
---|
| 196 | |
---|
| 197 | /** api: event[layoutchange] |
---|
| 198 | * Triggered when the layout is changed. |
---|
| 199 | * |
---|
| 200 | * Listener arguments: |
---|
| 201 | * |
---|
| 202 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 203 | * PrintProvider |
---|
| 204 | * * layout - ``Ext.data.Record`` the new layout |
---|
| 205 | */ |
---|
| 206 | "layoutchange", |
---|
| 207 | |
---|
| 208 | /** api: event[dpichange] |
---|
| 209 | * Triggered when the dpi value is changed. |
---|
| 210 | * |
---|
| 211 | * Listener arguments: |
---|
| 212 | * |
---|
| 213 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 214 | * PrintProvider |
---|
| 215 | * * dpi - ``Ext.data.Record`` the new dpi record |
---|
| 216 | */ |
---|
| 217 | "dpichange", |
---|
| 218 | |
---|
| 219 | /** api: event[beforeprint] |
---|
| 220 | * Triggered when the print method is called. |
---|
| 221 | * |
---|
| 222 | * Listener arguments: |
---|
| 223 | * |
---|
| 224 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 225 | * PrintProvider |
---|
| 226 | * * map - ``OpenLayers.Map`` the map being printed |
---|
| 227 | * * pages - Array of :class:`GeoExt.data.PrintPage` the print |
---|
| 228 | * pages being printed |
---|
| 229 | * * options - ``Object`` the options to the print command |
---|
| 230 | */ |
---|
| 231 | "beforeprint", |
---|
| 232 | |
---|
| 233 | /** api: event[print] |
---|
| 234 | * Triggered when the print document is opened. |
---|
| 235 | * |
---|
| 236 | * Listener arguments: |
---|
| 237 | * |
---|
| 238 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 239 | * PrintProvider |
---|
| 240 | * * url - ``String`` the url of the print document |
---|
| 241 | */ |
---|
| 242 | "print", |
---|
| 243 | |
---|
| 244 | /** api: event[printexception] |
---|
| 245 | * Triggered when using the ``POST`` method, when the print |
---|
| 246 | * backend returns an exception. |
---|
| 247 | * |
---|
| 248 | * Listener arguments: |
---|
| 249 | * |
---|
| 250 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 251 | * PrintProvider |
---|
| 252 | * * response - ``Object`` the response object of the XHR |
---|
| 253 | */ |
---|
| 254 | "printexception", |
---|
| 255 | |
---|
| 256 | /** api: event[beforeencodelayer] |
---|
| 257 | * Triggered before a layer is encoded. This can be used to |
---|
| 258 | * exclude layers from the printing, by having the listener |
---|
| 259 | * return false. |
---|
| 260 | * |
---|
| 261 | * Listener arguments: |
---|
| 262 | * |
---|
| 263 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 264 | * PrintProvider |
---|
| 265 | * * layer - ``OpenLayers.Layer`` the layer which is about to be |
---|
| 266 | * encoded. |
---|
| 267 | */ |
---|
| 268 | "beforeencodelayer", |
---|
| 269 | |
---|
| 270 | /** api: event[encodelayer] |
---|
| 271 | * Triggered when a layer is encoded. This can be used to modify |
---|
| 272 | * the encoded low-level layer object that will be sent to the |
---|
| 273 | * print service. |
---|
| 274 | * |
---|
| 275 | * Listener arguments: |
---|
| 276 | * |
---|
| 277 | * * printProvider - :class:`GeoExt.data.PrintProvider` this |
---|
| 278 | * PrintProvider |
---|
| 279 | * * layer - ``OpenLayers.Layer`` the layer which is about to be |
---|
| 280 | * encoded. |
---|
| 281 | * * encodedLayer - ``Object`` the encoded layer that will be |
---|
| 282 | * sent to the print service. |
---|
| 283 | */ |
---|
| 284 | "encodelayer" |
---|
| 285 | |
---|
| 286 | ); |
---|
| 287 | |
---|
| 288 | GeoExt.data.PrintProvider.superclass.constructor.apply(this, arguments); |
---|
| 289 | |
---|
| 290 | this.scales = new Ext.data.JsonStore({ |
---|
| 291 | root: "scales", |
---|
| 292 | sortInfo: {field: "value", direction: "DESC"}, |
---|
| 293 | fields: ["name", {name: "value", type: "float"}] |
---|
| 294 | }); |
---|
| 295 | |
---|
| 296 | this.dpis = new Ext.data.JsonStore({ |
---|
| 297 | root: "dpis", |
---|
| 298 | fields: ["name", {name: "value", type: "float"}] |
---|
| 299 | }); |
---|
| 300 | |
---|
| 301 | this.layouts = new Ext.data.JsonStore({ |
---|
| 302 | root: "layouts", |
---|
| 303 | fields: [ |
---|
| 304 | "name", |
---|
| 305 | {name: "size", mapping: "map"}, |
---|
| 306 | {name: "rotation", type: "boolean"} |
---|
| 307 | ] |
---|
| 308 | }); |
---|
| 309 | |
---|
| 310 | if(config.capabilities) { |
---|
| 311 | this.loadStores(); |
---|
| 312 | } else { |
---|
| 313 | if(this.url.split("/").pop()) { |
---|
| 314 | this.url += "/"; |
---|
| 315 | } |
---|
| 316 | this.initialConfig.autoLoad && this.loadCapabilities(); |
---|
| 317 | } |
---|
| 318 | }, |
---|
| 319 | |
---|
| 320 | /** api: method[setLayout] |
---|
| 321 | * :param layout: ``Ext.data.Record`` the record of the layout. |
---|
| 322 | * |
---|
| 323 | * Sets the layout for this printProvider. |
---|
| 324 | */ |
---|
| 325 | setLayout: function(layout) { |
---|
| 326 | this.layout = layout; |
---|
| 327 | this.fireEvent("layoutchange", this, layout); |
---|
| 328 | }, |
---|
| 329 | |
---|
| 330 | /** api: method[setDpi] |
---|
| 331 | * :param dpi: ``Ext.data.Record`` the dpi record. |
---|
| 332 | * |
---|
| 333 | * Sets the dpi for this printProvider. |
---|
| 334 | */ |
---|
| 335 | setDpi: function(dpi) { |
---|
| 336 | this.dpi = dpi; |
---|
| 337 | this.fireEvent("dpichange", this, dpi); |
---|
| 338 | }, |
---|
| 339 | |
---|
| 340 | /** api: method[print] |
---|
| 341 | * :param map: ``GeoExt.MapPanel`` or ``OpenLayers.Map`` The map to print. |
---|
| 342 | * :param pages: ``Array`` of :class:`GeoExt.data.PrintPage` or |
---|
| 343 | * :class:`GeoExt.data.PrintPage` page(s) to print. |
---|
| 344 | * :param options: ``Object`` of additional options, see below. |
---|
| 345 | * |
---|
| 346 | * Sends the print command to the print service and opens a new window |
---|
| 347 | * with the resulting PDF. |
---|
| 348 | * |
---|
| 349 | * Valid properties for the ``options`` argument: |
---|
| 350 | * |
---|
| 351 | * * ``legend`` - :class:`GeoExt.LegendPanel` If provided, the legend |
---|
| 352 | * will be added to the print document. For the printed result to |
---|
| 353 | * look like the LegendPanel, the following ``!legends`` block |
---|
| 354 | * should be included in the ``items`` of your page layout in the |
---|
| 355 | * print module's configuration file: |
---|
| 356 | * |
---|
| 357 | * .. code-block:: none |
---|
| 358 | * |
---|
| 359 | * - !legends |
---|
| 360 | * maxIconWidth: 0 |
---|
| 361 | * maxIconHeight: 0 |
---|
| 362 | * classIndentation: 0 |
---|
| 363 | * layerSpace: 5 |
---|
| 364 | * layerFontSize: 10 |
---|
| 365 | * |
---|
| 366 | * * ``overview`` - :class:`OpenLayers.Control.OverviewMap` If provided, |
---|
| 367 | * the layers for the overview map in the printout will be taken from |
---|
| 368 | * the OverviewMap control. If not provided, the print service will |
---|
| 369 | * use the main map's layers for the overview map. Applies only for |
---|
| 370 | * layouts configured to print an overview map. |
---|
| 371 | */ |
---|
| 372 | print: function(map, pages, options) { |
---|
| 373 | if(map instanceof GeoExt.MapPanel) { |
---|
| 374 | map = map.map; |
---|
| 375 | } |
---|
| 376 | pages = pages instanceof Array ? pages : [pages]; |
---|
| 377 | options = options || {}; |
---|
| 378 | if(this.fireEvent("beforeprint", this, map, pages, options) === false) { |
---|
| 379 | return; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | var jsonData = Ext.apply({ |
---|
| 383 | units: map.getUnits(), |
---|
| 384 | srs: map.baseLayer.projection.getCode(), |
---|
| 385 | layout: this.layout.get("name"), |
---|
| 386 | dpi: this.dpi.get("value") |
---|
| 387 | }, this.customParams); |
---|
| 388 | |
---|
| 389 | var pagesLayer = pages[0].feature.layer; |
---|
| 390 | var encodedLayers = []; |
---|
| 391 | Ext.each(map.layers, function(layer){ |
---|
| 392 | if(layer !== pagesLayer && layer.getVisibility() === true) { |
---|
| 393 | var enc = this.encodeLayer(layer); |
---|
| 394 | enc && encodedLayers.push(enc); |
---|
| 395 | } |
---|
| 396 | }, this); |
---|
| 397 | jsonData.layers = encodedLayers; |
---|
| 398 | |
---|
| 399 | var encodedPages = []; |
---|
| 400 | Ext.each(pages, function(page) { |
---|
| 401 | encodedPages.push(Ext.apply({ |
---|
| 402 | center: [page.center.lon, page.center.lat], |
---|
| 403 | scale: page.scale.get("value"), |
---|
| 404 | rotation: page.rotation |
---|
| 405 | }, page.customParams)); |
---|
| 406 | }, this); |
---|
| 407 | jsonData.pages = encodedPages; |
---|
| 408 | |
---|
| 409 | if (options.overview) { |
---|
| 410 | var encodedOverviewLayers = []; |
---|
| 411 | Ext.each(options.overview.layers, function(layer) { |
---|
| 412 | var enc = this.encodeLayer(layer); |
---|
| 413 | enc && encodedOverviewLayers.push(enc); |
---|
| 414 | }, this); |
---|
| 415 | jsonData.overviewLayers = encodedOverviewLayers; |
---|
| 416 | } |
---|
| 417 | |
---|
| 418 | if(options.legend) { |
---|
| 419 | var legend = options.legend; |
---|
| 420 | var rendered = legend.rendered; |
---|
| 421 | if (!rendered) { |
---|
| 422 | legend = legend.cloneConfig({ |
---|
| 423 | renderTo: document.body, |
---|
| 424 | hidden: true |
---|
| 425 | }); |
---|
| 426 | } |
---|
| 427 | var encodedLegends = []; |
---|
| 428 | legend.items.each(function(cmp) { |
---|
| 429 | if(!cmp.hidden) { |
---|
| 430 | var encFn = this.encoders.legends[cmp.getXType()]; |
---|
| 431 | encodedLegends = encodedLegends.concat( |
---|
| 432 | encFn.call(this, cmp)); |
---|
| 433 | } |
---|
| 434 | }, this); |
---|
| 435 | if (!rendered) { |
---|
| 436 | legend.destroy(); |
---|
| 437 | } |
---|
| 438 | jsonData.legends = encodedLegends; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | if(this.method === "GET") { |
---|
| 442 | var url = Ext.urlAppend(this.capabilities.printURL, |
---|
| 443 | "spec=" + encodeURIComponent(Ext.encode(jsonData))); |
---|
| 444 | window.open(url); |
---|
| 445 | this.fireEvent("print", this, url); |
---|
| 446 | } else { |
---|
| 447 | Ext.Ajax.request({ |
---|
| 448 | url: this.capabilities.createURL, |
---|
| 449 | jsonData: jsonData, |
---|
| 450 | success: function(response) { |
---|
| 451 | // In IE, using a Content-disposition: attachment header |
---|
| 452 | // may make it hard or impossible to download the pdf due |
---|
| 453 | // to security settings. So we'll display the pdf inline. |
---|
| 454 | var url = Ext.decode(response.responseText).getURL + |
---|
| 455 | (Ext.isIE ? "?inline=true" : ""); |
---|
| 456 | if(Ext.isOpera || Ext.isIE) { |
---|
| 457 | // Make sure that Opera and IE don't replace the |
---|
| 458 | // content tab with the pdf |
---|
| 459 | window.open(url); |
---|
| 460 | } else { |
---|
| 461 | // This avoids popup blockers for all other browsers |
---|
| 462 | window.location.href = url; |
---|
| 463 | } |
---|
| 464 | this.fireEvent("print", this, url); |
---|
| 465 | }, |
---|
| 466 | failure: function(response) { |
---|
| 467 | this.fireEvent("printexception", this, response); |
---|
| 468 | }, |
---|
| 469 | params: this.initialConfig.baseParams, |
---|
| 470 | scope: this |
---|
| 471 | }); |
---|
| 472 | } |
---|
| 473 | }, |
---|
| 474 | |
---|
| 475 | /** api: method[loadCapabilities] |
---|
| 476 | * |
---|
| 477 | * Loads the capabilities from the print service. If this instance is |
---|
| 478 | * configured with either ``capabilities`` or a ``url`` and ``autoLoad`` |
---|
| 479 | * set to true, then this method does not need to be called from the |
---|
| 480 | * application. |
---|
| 481 | */ |
---|
| 482 | loadCapabilities: function() { |
---|
| 483 | if (!this.url) { |
---|
| 484 | return; |
---|
| 485 | } |
---|
| 486 | var url = this.url + "info.json"; |
---|
| 487 | Ext.Ajax.request({ |
---|
| 488 | url: url, |
---|
| 489 | method: "GET", |
---|
| 490 | disableCaching: false, |
---|
| 491 | success: function(response) { |
---|
| 492 | this.capabilities = Ext.decode(response.responseText); |
---|
| 493 | this.loadStores(); |
---|
| 494 | }, |
---|
| 495 | params: this.initialConfig.baseParams, |
---|
| 496 | scope: this |
---|
| 497 | }); |
---|
| 498 | }, |
---|
| 499 | |
---|
| 500 | /** private: method[loadStores] |
---|
| 501 | */ |
---|
| 502 | loadStores: function() { |
---|
| 503 | this.scales.loadData(this.capabilities); |
---|
| 504 | this.dpis.loadData(this.capabilities); |
---|
| 505 | this.layouts.loadData(this.capabilities); |
---|
| 506 | |
---|
| 507 | this.setLayout(this.layouts.getAt(0)); |
---|
| 508 | this.setDpi(this.dpis.getAt(0)); |
---|
| 509 | this.fireEvent("loadcapabilities", this, this.capabilities); |
---|
| 510 | }, |
---|
| 511 | |
---|
| 512 | /** private: method[encodeLayer] |
---|
| 513 | * :param layer: ``OpenLayers.Layer`` |
---|
| 514 | * :return: ``Object`` |
---|
| 515 | * |
---|
| 516 | * Encodes a layer for the print service. |
---|
| 517 | */ |
---|
| 518 | encodeLayer: function(layer) { |
---|
| 519 | var encLayer; |
---|
| 520 | for(var c in this.encoders.layers) { |
---|
| 521 | if(OpenLayers.Layer[c] && layer instanceof OpenLayers.Layer[c]) { |
---|
| 522 | if(this.fireEvent("beforeencodelayer", this, layer) === false) { |
---|
| 523 | return; |
---|
| 524 | } |
---|
| 525 | encLayer = this.encoders.layers[c].call(this, layer); |
---|
| 526 | this.fireEvent("encodelayer", this, layer, encLayer); |
---|
| 527 | break; |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | // only return the encLayer object when we have a type. Prevents a |
---|
| 531 | // fallback on base encoders like HTTPRequest. |
---|
| 532 | return (encLayer && encLayer.type) ? encLayer : null; |
---|
| 533 | }, |
---|
| 534 | |
---|
| 535 | /** private: method[getAbsoluteUrl] |
---|
| 536 | * :param url: ``String`` |
---|
| 537 | * :return: ``String`` |
---|
| 538 | * |
---|
| 539 | * Converts the provided url to an absolute url. |
---|
| 540 | */ |
---|
| 541 | getAbsoluteUrl: function(url) { |
---|
| 542 | var a; |
---|
| 543 | if(Ext.isIE) { |
---|
| 544 | a = document.createElement("<a href='" + url + "'/>"); |
---|
| 545 | a.style.display = "none"; |
---|
| 546 | document.body.appendChild(a); |
---|
| 547 | a.href = a.href; |
---|
| 548 | document.body.removeChild(a); |
---|
| 549 | } else { |
---|
| 550 | a = document.createElement("a"); |
---|
| 551 | a.href = url; |
---|
| 552 | } |
---|
| 553 | return a.href; |
---|
| 554 | }, |
---|
| 555 | |
---|
| 556 | /** private: property[encoders] |
---|
| 557 | * ``Object`` Encoders for all print content |
---|
| 558 | */ |
---|
| 559 | encoders: { |
---|
| 560 | "layers": { |
---|
| 561 | "WMS": function(layer) { |
---|
| 562 | var enc = this.encoders.layers.HTTPRequest.call(this, layer); |
---|
| 563 | Ext.apply(enc, { |
---|
| 564 | type: 'WMS', |
---|
| 565 | layers: [layer.params.LAYERS].join(",").split(","), |
---|
| 566 | format: layer.params.FORMAT, |
---|
| 567 | styles: [layer.params.STYLES].join(",").split(",") |
---|
| 568 | }); |
---|
| 569 | var param; |
---|
| 570 | for(var p in layer.params) { |
---|
| 571 | param = p.toLowerCase(); |
---|
| 572 | if(!layer.DEFAULT_PARAMS[param] && |
---|
| 573 | "layers,styles,width,height,srs".indexOf(param) == -1) { |
---|
| 574 | if(!enc.customParams) { |
---|
| 575 | enc.customParams = {}; |
---|
| 576 | } |
---|
| 577 | enc.customParams[p] = layer.params[p]; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | return enc; |
---|
| 581 | }, |
---|
| 582 | "OSM": function(layer) { |
---|
| 583 | var enc = this.encoders.layers.TileCache.call(this, layer); |
---|
| 584 | return Ext.apply(enc, { |
---|
| 585 | type: 'OSM', |
---|
| 586 | baseURL: enc.baseURL.substr(0, enc.baseURL.indexOf("$")), |
---|
| 587 | extension: "png" |
---|
| 588 | }); |
---|
| 589 | }, |
---|
| 590 | "TMS": function(layer) { |
---|
| 591 | var enc = this.encoders.layers.TileCache.call(this, layer); |
---|
| 592 | return Ext.apply(enc, { |
---|
| 593 | type: 'TMS', |
---|
| 594 | format: layer.type |
---|
| 595 | }); |
---|
| 596 | }, |
---|
| 597 | "TileCache": function(layer) { |
---|
| 598 | var enc = this.encoders.layers.HTTPRequest.call(this, layer); |
---|
| 599 | return Ext.apply(enc, { |
---|
| 600 | type: 'TileCache', |
---|
| 601 | layer: layer.layername, |
---|
| 602 | maxExtent: layer.maxExtent.toArray(), |
---|
| 603 | tileSize: [layer.tileSize.w, layer.tileSize.h], |
---|
| 604 | extension: layer.extension, |
---|
| 605 | resolutions: layer.serverResolutions || layer.resolutions |
---|
| 606 | }); |
---|
| 607 | }, |
---|
| 608 | "KaMapCache": function(layer) { |
---|
| 609 | var enc = this.encoders.layers.KaMap.call(this, layer); |
---|
| 610 | return Ext.apply(enc, { |
---|
| 611 | type: 'KaMapCache', |
---|
| 612 | // group param is mandatory when using KaMapCache |
---|
| 613 | group: layer.params['g'], |
---|
| 614 | metaTileWidth: layer.params['metaTileSize']['w'], |
---|
| 615 | metaTileHeight: layer.params['metaTileSize']['h'] |
---|
| 616 | }); |
---|
| 617 | }, |
---|
| 618 | "KaMap": function(layer) { |
---|
| 619 | var enc = this.encoders.layers.HTTPRequest.call(this, layer); |
---|
| 620 | return Ext.apply(enc, { |
---|
| 621 | type: 'KaMap', |
---|
| 622 | map: layer.params['map'], |
---|
| 623 | extension: layer.params['i'], |
---|
| 624 | // group param is optional when using KaMap |
---|
| 625 | group: layer.params['g'] || "", |
---|
| 626 | maxExtent: layer.maxExtent.toArray(), |
---|
| 627 | tileSize: [layer.tileSize.w, layer.tileSize.h], |
---|
| 628 | resolutions: layer.serverResolutions || layer.resolutions |
---|
| 629 | }); |
---|
| 630 | }, |
---|
| 631 | "HTTPRequest": function(layer) { |
---|
| 632 | return { |
---|
| 633 | baseURL: this.getAbsoluteUrl(layer.url instanceof Array ? |
---|
| 634 | layer.url[0] : layer.url), |
---|
| 635 | opacity: (layer.opacity != null) ? layer.opacity : 1.0, |
---|
| 636 | singleTile: layer.singleTile |
---|
| 637 | }; |
---|
| 638 | }, |
---|
| 639 | "Image": function(layer) { |
---|
| 640 | return { |
---|
| 641 | type: 'Image', |
---|
| 642 | baseURL: this.getAbsoluteUrl(layer.getURL(layer.extent)), |
---|
| 643 | opacity: (layer.opacity != null) ? layer.opacity : 1.0, |
---|
| 644 | extent: layer.extent.toArray(), |
---|
| 645 | pixelSize: [layer.size.w, layer.size.h], |
---|
| 646 | name: layer.name |
---|
| 647 | }; |
---|
| 648 | }, |
---|
| 649 | "Vector": function(layer) { |
---|
| 650 | if(!layer.features.length) { |
---|
| 651 | return; |
---|
| 652 | } |
---|
| 653 | |
---|
| 654 | var encFeatures = []; |
---|
| 655 | var encStyles = {}; |
---|
| 656 | var features = layer.features; |
---|
| 657 | var featureFormat = new OpenLayers.Format.GeoJSON(); |
---|
| 658 | var styleFormat = new OpenLayers.Format.JSON(); |
---|
| 659 | var nextId = 1; |
---|
| 660 | var styleDict = {}; |
---|
| 661 | var feature, style, dictKey, dictItem, styleName; |
---|
| 662 | for(var i=0, len=features.length; i<len; ++i) { |
---|
| 663 | feature = features[i]; |
---|
| 664 | style = feature.style || layer.style || |
---|
| 665 | layer.styleMap.createSymbolizer(feature, |
---|
| 666 | feature.renderIntent); |
---|
| 667 | dictKey = styleFormat.write(style); |
---|
| 668 | dictItem = styleDict[dictKey]; |
---|
| 669 | if(dictItem) { |
---|
| 670 | //this style is already known |
---|
| 671 | styleName = dictItem; |
---|
| 672 | } else { |
---|
| 673 | //new style |
---|
| 674 | styleDict[dictKey] = styleName = nextId++; |
---|
| 675 | if(style.externalGraphic) { |
---|
| 676 | encStyles[styleName] = Ext.applyIf({ |
---|
| 677 | externalGraphic: this.getAbsoluteUrl( |
---|
| 678 | style.externalGraphic)}, style); |
---|
| 679 | } else { |
---|
| 680 | encStyles[styleName] = style; |
---|
| 681 | } |
---|
| 682 | } |
---|
| 683 | var featureGeoJson = featureFormat.extract.feature.call( |
---|
| 684 | featureFormat, feature); |
---|
| 685 | |
---|
| 686 | featureGeoJson.properties = OpenLayers.Util.extend({ |
---|
| 687 | _gx_style: styleName |
---|
| 688 | }, featureGeoJson.properties); |
---|
| 689 | |
---|
| 690 | encFeatures.push(featureGeoJson); |
---|
| 691 | } |
---|
| 692 | |
---|
| 693 | return { |
---|
| 694 | type: 'Vector', |
---|
| 695 | styles: encStyles, |
---|
| 696 | styleProperty: '_gx_style', |
---|
| 697 | geoJson: { |
---|
| 698 | type: "FeatureCollection", |
---|
| 699 | features: encFeatures |
---|
| 700 | }, |
---|
| 701 | name: layer.name, |
---|
| 702 | opacity: (layer.opacity != null) ? layer.opacity : 1.0 |
---|
| 703 | }; |
---|
| 704 | } |
---|
| 705 | }, |
---|
| 706 | "legends": { |
---|
| 707 | "gx_wmslegend": function(legend) { |
---|
| 708 | var enc = this.encoders.legends.base.call(this, legend); |
---|
| 709 | var icons = []; |
---|
| 710 | for(var i=1, len=legend.items.getCount(); i<len; ++i) { |
---|
| 711 | icons.push(this.getAbsoluteUrl(legend.items.get(i).url)); |
---|
| 712 | } |
---|
| 713 | enc[0].classes[0] = { |
---|
| 714 | name: "", |
---|
| 715 | icons: icons |
---|
| 716 | }; |
---|
| 717 | return enc; |
---|
| 718 | }, |
---|
| 719 | "gx_urllegend": function(legend) { |
---|
| 720 | var enc = this.encoders.legends.base.call(this, legend); |
---|
| 721 | enc[0].classes.push({ |
---|
| 722 | name: "", |
---|
| 723 | icon: this.getAbsoluteUrl(legend.items.get(1).url) |
---|
| 724 | }); |
---|
| 725 | return enc; |
---|
| 726 | }, |
---|
| 727 | "base": function(legend){ |
---|
| 728 | return [{ |
---|
| 729 | name: legend.items.get(0).text, |
---|
| 730 | classes: [] |
---|
| 731 | }]; |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | }); |
---|