[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 | |
---|
| 9 | /** api: (define) |
---|
| 10 | * module = GeoExt.data |
---|
| 11 | * class = AttributeReader |
---|
| 12 | * base_link = `Ext.data.DataReader <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.DataReader>`_ |
---|
| 13 | */ |
---|
| 14 | Ext.namespace("GeoExt.data"); |
---|
| 15 | |
---|
| 16 | /** api: constructor |
---|
| 17 | * .. class:: AttributeReader(meta, recordType) |
---|
| 18 | * |
---|
| 19 | * :arg meta: ``Object`` Reader configuration. |
---|
| 20 | * :arg recordType: ``Array or Ext.data.Record`` An array of field |
---|
| 21 | * configuration objects or a record object. |
---|
| 22 | * |
---|
| 23 | * Create a new attributes reader object. |
---|
| 24 | * |
---|
| 25 | * Valid meta properties: |
---|
| 26 | * |
---|
| 27 | * * format - ``OpenLayers.Format`` A parser for transforming the XHR response |
---|
| 28 | * into an array of objects representing attributes. Defaults to |
---|
| 29 | * an ``OpenLayers.Format.WFSDescribeFeatureType`` parser. |
---|
| 30 | * * ignore - ``Object`` Properties of the ignore object should be field names. |
---|
| 31 | * Values are either arrays or regular expressions. |
---|
| 32 | * * feature - ``OpenLayers.Feature.Vector`` A vector feature. If provided |
---|
| 33 | * records created by the reader will include a field named "value" |
---|
| 34 | * referencing the attribute value as set in the feature. |
---|
| 35 | */ |
---|
| 36 | GeoExt.data.AttributeReader = function(meta, recordType) { |
---|
| 37 | meta = meta || {}; |
---|
| 38 | if(!meta.format) { |
---|
| 39 | meta.format = new OpenLayers.Format.WFSDescribeFeatureType(); |
---|
| 40 | } |
---|
| 41 | GeoExt.data.AttributeReader.superclass.constructor.call( |
---|
| 42 | this, meta, recordType || meta.fields |
---|
| 43 | ); |
---|
| 44 | if(meta.feature) { |
---|
| 45 | this.recordType.prototype.fields.add(new Ext.data.Field("value")); |
---|
| 46 | } |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | Ext.extend(GeoExt.data.AttributeReader, Ext.data.DataReader, { |
---|
| 50 | |
---|
| 51 | /** private: method[read] |
---|
| 52 | * :arg request: ``Object`` The XHR object that contains the parsed doc. |
---|
| 53 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
| 54 | * as a cache of ``Ext.data.Records``. |
---|
| 55 | * |
---|
| 56 | * This method is only used by a DataProxy which has retrieved data from a |
---|
| 57 | * remote server. |
---|
| 58 | */ |
---|
| 59 | read: function(request) { |
---|
| 60 | var data = request.responseXML; |
---|
| 61 | if(!data || !data.documentElement) { |
---|
| 62 | data = request.responseText; |
---|
| 63 | } |
---|
| 64 | return this.readRecords(data); |
---|
| 65 | }, |
---|
| 66 | |
---|
| 67 | /** private: method[readRecords] |
---|
| 68 | * :arg data: ``DOMElement or String or Array`` A document element or XHR |
---|
| 69 | * response string. As an alternative to fetching attributes data from |
---|
| 70 | * a remote source, an array of attribute objects can be provided given |
---|
| 71 | * that the properties of each attribute object map to a provided field |
---|
| 72 | * name. |
---|
| 73 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
| 74 | * as a cache of ``Ext.data.Records``. |
---|
| 75 | * |
---|
| 76 | * Create a data block containing Ext.data.Records from an XML document. |
---|
| 77 | */ |
---|
| 78 | readRecords: function(data) { |
---|
| 79 | var attributes; |
---|
| 80 | if(data instanceof Array) { |
---|
| 81 | attributes = data; |
---|
| 82 | } else { |
---|
| 83 | // only works with one featureType in the doc |
---|
| 84 | attributes = this.meta.format.read(data).featureTypes[0].properties; |
---|
| 85 | } |
---|
| 86 | var feature = this.meta.feature; |
---|
| 87 | var recordType = this.recordType; |
---|
| 88 | var fields = recordType.prototype.fields; |
---|
| 89 | var numFields = fields.length; |
---|
| 90 | var attr, values, name, record, ignore, value, records = []; |
---|
| 91 | for(var i=0, len=attributes.length; i<len; ++i) { |
---|
| 92 | ignore = false; |
---|
| 93 | attr = attributes[i]; |
---|
| 94 | values = {}; |
---|
| 95 | for(var j=0; j<numFields; ++j) { |
---|
| 96 | name = fields.items[j].name; |
---|
| 97 | value = attr[name]; |
---|
| 98 | if(this.ignoreAttribute(name, value)) { |
---|
| 99 | ignore = true; |
---|
| 100 | break; |
---|
| 101 | } |
---|
| 102 | values[name] = value; |
---|
| 103 | } |
---|
| 104 | if(feature) { |
---|
| 105 | value = feature.attributes[values["name"]]; |
---|
| 106 | if(value !== undefined) { |
---|
| 107 | if(this.ignoreAttribute("value", value)) { |
---|
| 108 | ignore = true; |
---|
| 109 | } else { |
---|
| 110 | values["value"] = value; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | if(!ignore) { |
---|
| 115 | records[records.length] = new recordType(values); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return { |
---|
| 120 | success: true, |
---|
| 121 | records: records, |
---|
| 122 | totalRecords: records.length |
---|
| 123 | }; |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | /** private: method[ignoreAttribute] |
---|
| 127 | * :arg name: ``String`` The field name. |
---|
| 128 | * :arg value: ``String`` The field value. |
---|
| 129 | * |
---|
| 130 | * :return: ``Boolean`` true if the attribute should be ignored. |
---|
| 131 | */ |
---|
| 132 | ignoreAttribute: function(name, value) { |
---|
| 133 | var ignore = false; |
---|
| 134 | if(this.meta.ignore && this.meta.ignore[name]) { |
---|
| 135 | var matches = this.meta.ignore[name]; |
---|
| 136 | if(typeof matches == "string") { |
---|
| 137 | ignore = (matches === value); |
---|
| 138 | } else if(matches instanceof Array) { |
---|
| 139 | ignore = (matches.indexOf(value) > -1); |
---|
| 140 | } else if(matches instanceof RegExp) { |
---|
| 141 | ignore = (matches.test(value)); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | return ignore; |
---|
| 145 | } |
---|
| 146 | }); |
---|