Changeset 256


Ignore:
Timestamp:
03/24/09 00:39:10 (9 years ago)
Author:
tschaub
Message:

making the wms capabilities reader look a bit more like other readers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox/opengeo/geoexplorer/lib/GeoExt/data/WMSCapabilitiesReader.js

    r255 r256  
     1/**
     2 * Copyright (c) 2008 The Open Planning Project
     3 */
     4
    15Ext.namespace("GeoExt.data");
    26
    3 GeoExt.data.WMSCapabilitiesReader = function(opts){
    4     GeoExt.data.WMSCapabilitiesReader.superclass.constructor.apply(this, arguments);
    5     this.recordType = this.recordType || Ext.data.Record.create([
    6         {name: 'name', type: 'string'},
    7         {name: 'abstract', type: 'string'},
    8         {name: 'title', type: 'string'},
    9         {name: 'prefix', type: 'string', defaultValue: ""},
    10         {name: 'llbbox', type: 'auto'},
    11         {name: 'styles', type: 'auto'},
    12         {name: 'formats', type: 'auto'},
    13         {name: 'queryable', type: 'boolean'}
    14     ]);
     7/**
     8 * Class: GeoExt.data.WMSCapabilitiesReader
     9 * Data reader class to provide an array of {Ext.data.Record} objects given
     10 *     a WMS GetCapabilities response for use by an {Ext.data.Store}
     11 *     object.
     12 *
     13 * Extends:
     14 *  - Ext.data.DataReader
     15 */
     16
     17/**
     18 * Constructor: GeoExt.data.WMSCapabilitiesReader
     19 * Create a new attributes reader object.
     20 *
     21 * Parameters:
     22 * meta - {Object} Reader configuration.
     23 * recordType - {Array | Ext.data.Record} An array of field configuration
     24 *     objects or a record object.  Default is <GeoExt.data.LayerRecord>.
     25 *     TODO: only works with LayerRecord right now (see #27).
     26 *
     27 * Configuration options (meta properties):
     28 * format - {OpenLayers.Format} A parser for transforming the XHR response
     29 *     into an array of objects representing attributes.  Defaults to
     30 *     an {OpenLayers.Format.WMSCapabilities} parser.
     31 */
     32GeoExt.data.WMSCapabilitiesReader = function(meta, recordType) {
     33    meta = meta || {};
     34    if(!meta.format) {
     35        meta.format = new OpenLayers.Format.WMSCapabilities();
     36    }
     37    GeoExt.data.WMSCapabilitiesReader.superclass.constructor.call(
     38        this, meta, recordType || meta.fields || GeoExt.data.LayerRecord
     39    );
    1540};
    1641
    1742Ext.extend(GeoExt.data.WMSCapabilitiesReader, Ext.data.DataReader, {
    18     read: function (response) {
     43
     44    /**
     45     * Method: read
     46     * This method is only used by a DataProxy which has retrieved data from a
     47     *     remote server.
     48     *
     49     * Parameters:
     50     * request - {Object} The XHR object which contains the parsed XML
     51     *     document.
     52     *
     53     * Returns:
     54     * {Object} A data block which is used by an {Ext.data.Store} as a cache
     55     *     of Ext.data.Records.
     56     */
     57    read: function(request) {
     58        var data = request.responseXML;
     59        if(!data || !data.documentElement) {
     60            data = request.responseText;
     61        }
     62        return this.readRecords(data);
     63    },
     64
     65    /**
     66     * Method: readRecords
     67     * Create a data block containing Ext.data.Records from an XML document.
     68     *
     69     * Parameters:
     70     * data - {DOMElement | Strint | Object} A document element or XHR response
     71     *     string.  As an alternative to fetching capabilities data from a remote
     72     *     source, an object representing the capabilities can be provided given
     73     *     that the structure mirrors that returned from the capabilities parser.
     74     *
     75     * Returns:
     76     * {Object} A data block which is used by an {Ext.data.Store} as a cache of
     77     *     Ext.data.Records.
     78     */
     79    readRecords: function(data) {
    1980       
    20         var fmt = new OpenLayers.Format.WMSCapabilities();
    21         var caps = fmt.read(response.responseXML);
    22         var records = [];
    23        
    24         for (var i = 0; i < caps.capability.layers.length; i++){
    25             var l = caps.capability.layers[i];
    26             if (l.name){
    27                 records.push(new this.recordType(l));
     81        if(typeof data === "string" || data.nodeType) {
     82            data = this.meta.format.read(data).featureTypes[0].properties;
     83        }
     84        var url = data.capability.request.getmap.href;
     85        var records = [], layer;       
     86        for(var i=0, len=data.capability.layers.length; i<len i++){
     87            layer = data.capability.layers[i];
     88            if(layer.name) {
     89                // this is specific to the LayerRecord
     90                // TODO: make more generic when #27 is addressed
     91                records.push(new this.recordType(
     92                    new OpenLayers.Layer.WMS(
     93                        layer.title || layer.name,
     94                        url,
     95                        {layers: layer.name}
     96                        {isBaseLayer: false} // TODO: decide where to fix this
     97                    ),
     98                    layer // tack on remaining properties
     99                ));
    28100            }
    29101        }
     
    34106            records: records
    35107        };
     108
    36109    }
    37110});
Note: See TracChangeset for help on using the changeset viewer.