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 | * @include GeoExt/data/AttributeReader.js |
---|
11 | */ |
---|
12 | |
---|
13 | /** api: (define) |
---|
14 | * module = GeoExt.data |
---|
15 | * class = AttributeStore |
---|
16 | * base_link = `Ext.data.Store <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.Store>`_ |
---|
17 | */ |
---|
18 | Ext.namespace("GeoExt.data"); |
---|
19 | |
---|
20 | /** |
---|
21 | * Function: GeoExt.data.AttributeStoreMixin |
---|
22 | * |
---|
23 | * This function generates a mixin object to be used when extending an Ext.data.Store |
---|
24 | * to create an attribute store. |
---|
25 | * |
---|
26 | * (start code) |
---|
27 | * var AttrStore = Ext.extend(Ext.data.Store, GeoExt.data.AttributeStoreMixin); |
---|
28 | * var store = new AttrStore(); |
---|
29 | * (end) |
---|
30 | * |
---|
31 | * For convenience, a GeoExt.data.AttributeStore class is available as a |
---|
32 | * shortcut to the Ext.extend sequence in the above code snippet. The above |
---|
33 | * is equivalent to: |
---|
34 | * (start code) |
---|
35 | * var store = new GeoExt.data.AttributeStore(); |
---|
36 | * (end) |
---|
37 | */ |
---|
38 | GeoExt.data.AttributeStoreMixin = function() { |
---|
39 | return { |
---|
40 | /** private */ |
---|
41 | constructor: function(c) { |
---|
42 | c = c || {}; |
---|
43 | arguments.callee.superclass.constructor.call( |
---|
44 | this, |
---|
45 | Ext.apply(c, { |
---|
46 | proxy: c.proxy || (!c.data ? |
---|
47 | new Ext.data.HttpProxy({url: c.url, disableCaching: false, method: "GET"}) : |
---|
48 | undefined |
---|
49 | ), |
---|
50 | reader: new GeoExt.data.AttributeReader( |
---|
51 | c, c.fields || ["name", "type", "restriction"] |
---|
52 | ) |
---|
53 | }) |
---|
54 | ); |
---|
55 | if(this.feature) { |
---|
56 | this.bind(); |
---|
57 | } |
---|
58 | }, |
---|
59 | |
---|
60 | /** private: method[bind] |
---|
61 | */ |
---|
62 | bind: function() { |
---|
63 | this.on({ |
---|
64 | "update": this.onUpdate, |
---|
65 | "load": this.onLoad, |
---|
66 | "add": this.onAdd, |
---|
67 | scope: this |
---|
68 | }); |
---|
69 | var records = []; |
---|
70 | this.each(function(record) { |
---|
71 | records.push(record); |
---|
72 | }); |
---|
73 | this.updateFeature(records); |
---|
74 | }, |
---|
75 | |
---|
76 | /** private: method[onUpdate] |
---|
77 | * :param store: ``Ext.data.Store`` |
---|
78 | * :param record: ``Ext.data.Record`` |
---|
79 | * :param operation: ``String`` |
---|
80 | * |
---|
81 | * Handler for store update event. |
---|
82 | */ |
---|
83 | onUpdate: function(store, record, operation) { |
---|
84 | this.updateFeature([record]); |
---|
85 | }, |
---|
86 | |
---|
87 | /** private: method[onLoad] |
---|
88 | * :param store: ``Ext.data.Store`` |
---|
89 | * :param records: ``Array(Ext.data.Record)`` |
---|
90 | * :param options: ``Object`` |
---|
91 | * |
---|
92 | * Handler for store load event |
---|
93 | */ |
---|
94 | onLoad: function(store, records, options) { |
---|
95 | // if options.add is true an "add" event was already |
---|
96 | // triggered, and onAdd already did the work of |
---|
97 | // adding the features to the layer. |
---|
98 | if(!options || options.add !== true) { |
---|
99 | this.updateFeature(records); |
---|
100 | } |
---|
101 | }, |
---|
102 | |
---|
103 | /** private: method[onAdd] |
---|
104 | * :param store: ``Ext.data.Store`` |
---|
105 | * :param records: ``Array(Ext.data.Record)`` |
---|
106 | * :param index: ``Number`` |
---|
107 | * |
---|
108 | * Handler for store add event |
---|
109 | */ |
---|
110 | onAdd: function(store, records, index) { |
---|
111 | this.updateFeature(records); |
---|
112 | }, |
---|
113 | |
---|
114 | /** private: method[updateFeature] |
---|
115 | * :param records: ``Array(Ext.data.Record)`` |
---|
116 | * |
---|
117 | * Update feature from records. |
---|
118 | */ |
---|
119 | updateFeature: function(records) { |
---|
120 | var feature = this.feature, layer = feature.layer; |
---|
121 | var i, len, record, name, value, oldValue, dirty; |
---|
122 | for(i=0,len=records.length; i<len; i++) { |
---|
123 | record = records[i]; |
---|
124 | name = record.get("name"); |
---|
125 | value = record.get("value"); |
---|
126 | oldValue = feature.attributes[name]; |
---|
127 | if(oldValue !== value) { |
---|
128 | dirty = true; |
---|
129 | } |
---|
130 | } |
---|
131 | if(dirty && layer && layer.events && |
---|
132 | layer.events.triggerEvent("beforefeaturemodified", |
---|
133 | {feature: feature}) !== false) { |
---|
134 | for(i=0,len=records.length; i<len; i++) { |
---|
135 | record = records[i]; |
---|
136 | name = record.get("name"); |
---|
137 | value = record.get("value"); |
---|
138 | feature.attributes[name] = value; |
---|
139 | } |
---|
140 | layer.events.triggerEvent( |
---|
141 | "featuremodified", {feature: feature}); |
---|
142 | layer.drawFeature(feature); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | }; |
---|
147 | }; |
---|
148 | |
---|
149 | /** api: constructor |
---|
150 | * .. class:: AttributeStore(config) |
---|
151 | * |
---|
152 | * Small helper class to make creating stores for remotely-loaded attributes |
---|
153 | * data easier. AttributeStore is pre-configured with a built-in |
---|
154 | * ``Ext.data.HttpProxy`` and :class:`GeoExt.data.AttributeReader`. The |
---|
155 | * HttpProxy is configured to allow caching (disableCaching: false) and |
---|
156 | * uses GET. If you require some other proxy/reader combination then you'll |
---|
157 | * have to configure this with your own proxy or create a basic |
---|
158 | * ``Ext.data.Store`` and configure as needed. |
---|
159 | */ |
---|
160 | |
---|
161 | /** api: config[format] |
---|
162 | * ``OpenLayers.Format`` |
---|
163 | * A parser for transforming the XHR response into an array of objects |
---|
164 | * representing attributes. Defaults to an |
---|
165 | * ``OpenLayers.Format.WFSDescribeFeatureType`` parser. |
---|
166 | */ |
---|
167 | |
---|
168 | /** api: config[fields] |
---|
169 | * ``Array or Function`` |
---|
170 | * Either an array of field definition objects as passed to |
---|
171 | * ``Ext.data.Record.create``, or a record constructor created using |
---|
172 | * ``Ext.data.Record.create``. Defaults to ``["name", "type", "restriction"]``. |
---|
173 | */ |
---|
174 | |
---|
175 | /** api: config[feature] |
---|
176 | * ``OpenLayers.Feature.Vector`` |
---|
177 | * A vector feature. If provided, and if the reader is a |
---|
178 | * :class:`GeoExt.data.AttributeReader` (the default), then records |
---|
179 | * of this store will include a field named "value" referencing the |
---|
180 | * corresponding attribute value in the feature. And if the "value" |
---|
181 | * field of a record is updated the update will propagate to the |
---|
182 | * corresponding feature attribute. Optional. |
---|
183 | */ |
---|
184 | GeoExt.data.AttributeStore = Ext.extend( |
---|
185 | Ext.data.Store, |
---|
186 | GeoExt.data.AttributeStoreMixin() |
---|
187 | ); |
---|