]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Strategy/Save.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Strategy / Save.js
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5 /**
6  * @requires OpenLayers/Strategy.js
7  */
8
9 /**
10  * Class: OpenLayers.Strategy.Save
11  * A strategy that commits newly created or modified features.  By default
12  *     the strategy waits for a call to <save> before persisting changes.  By
13  *     configuring the strategy with the <auto> option, changes can be saved
14  *     automatically.
15  *
16  * Inherits from:
17  *  - <OpenLayers.Strategy>
18  */
19 OpenLayers.Strategy.Save = OpenLayers.Class(OpenLayers.Strategy, {
20     
21     /**
22      * APIProperty: auto
23      * {Boolean | Number} Auto-save.  Default is false.  If true, features will be
24      *     saved immediately after being added to the layer and with each
25      *     modification or deletion.  If auto is a number, features will be
26      *     saved on an interval provided by the value (in seconds).
27      */
28     auto: false,
29     
30     /**
31      * Property: timer
32      * {Number} The id of the timer.
33      */
34     timer: null,
35
36     /**
37      * Constructor: OpenLayers.Strategy.Save
38      * Create a new Save strategy.
39      *
40      * Parameters:
41      * options - {Object} Optional object whose properties will be set on the
42      *     instance.
43      */
44     initialize: function(options) {
45         OpenLayers.Strategy.prototype.initialize.apply(this, [options]);
46     },
47    
48     /**
49      * APIMethod: activate
50      * Activate the strategy.  Register any listeners, do appropriate setup.
51      * 
52      * Returns:
53      * {Boolean} The strategy was successfully activated.
54      */
55     activate: function() {
56         var activated = OpenLayers.Strategy.prototype.activate.call(this);
57         if(activated) {
58             if(this.auto) {
59                 if(typeof this.auto === "number") {
60                     this.timer = window.setInterval(
61                         OpenLayers.Function.bind(this.save, this),
62                         this.auto * 1000
63                     )
64                 } else {
65                     this.layer.events.on({
66                         "featureadded": this.triggerSave,
67                         "afterfeaturemodified": this.triggerSave,
68                         scope: this
69                     });
70                 }
71             }
72         }
73         return activated;
74     },
75     
76     /**
77      * APIMethod: deactivate
78      * Deactivate the strategy.  Unregister any listeners, do appropriate
79      *     tear-down.
80      * 
81      * Returns:
82      * {Boolean} The strategy was successfully deactivated.
83      */
84     deactivate: function() {
85         var deactivated = OpenLayers.Strategy.prototype.deactivate.call(this);
86         if(deactivated) {
87             if(this.auto) {
88                 if(typeof this.auto === "number") {
89                     window.clearInterval(this.timer);
90                 } else {
91                     this.layer.events.un({
92                         "featureadded": this.triggerSave,
93                         "afterfeaturemodified": this.triggerSave,
94                         scope: this
95                     })
96                 }
97             }
98         }
99         return deactivated;
100     },
101     
102     /**
103      * Method: triggerSave
104      * Registered as a listener.  Calls save if a feature has insert, update,
105      *     or delete state.
106      *
107      * Parameters:
108      * event - {Object} The event this function is listening for.
109      */
110     triggerSave: function(event) {
111         var feature = event.feature;
112         if(feature.state === OpenLayers.State.INSERT ||
113            feature.state === OpenLayers.State.UPDATE ||
114            feature.state === OpenLayers.State.DELETE) {
115             this.save([event.feature]);
116         }
117     },
118     
119     /**
120      * APIMethod: save
121      * Tell the layer protocol to commit unsaved features.  If the layer
122      *     projection differs from the map projection, features will be
123      *     transformed into the layer projection before being committed.
124      *
125      * Parameters:
126      * features - {Array} Features to be saved.  If null, then default is all
127      *     features in the layer.  Features are assumed to be in the map
128      *     projection.
129      */
130     save: function(features) {
131         if(!features) {
132             features = this.layer.features;
133         }
134         var remote = this.layer.projection;
135         var local = this.layer.map.getProjectionObject();
136         if(!local.equals(remote)) {
137             var len = features.length;
138             var clones = new Array(len);
139             var orig, clone;
140             for(var i=0; i<len; ++i) {
141                 orig = features[i];
142                 clone = orig.clone();
143                 clone.fid = orig.fid;
144                 clone.state = orig.state;
145                 clone._original = orig;
146                 clone.geometry.transform(local, remote);
147                 clones[i] = clone;
148             }
149             features = clones;
150         }
151         this.layer.protocol.commit(features, {
152             callback: this.onCommit,
153             scope: this
154         });
155     },
156     
157     /**
158      * Method: onCommit
159      * Called after protocol commit.
160      *
161      * Parameters:
162      * response - {<OpenLayers.Protocol.Response>} A response object.
163      */
164     onCommit: function(response) {
165         if(response.success()) {
166             var features = response.reqFeatures;
167             // deal with inserts, updates, and deletes
168             var state, feature;
169             var destroys = [];
170             var insertIds = response.insertIds || [];
171             var j = 0;
172             for(var i=0, len=features.length; i<len; ++i) {
173                 feature = features[i];
174                 // if projection was different, we may be dealing with clones
175                 feature = feature._original || feature;
176                 state = feature.state;
177                 if(state) {
178                     if(state == OpenLayers.State.DELETE) {
179                         destroys.push(feature);
180                     } else if(state == OpenLayers.State.INSERT) {
181                         feature.fid = insertIds[j];
182                         ++j;
183                     }
184                     feature.state = null;
185                 }
186             }
187             if(destroys.length > 0) {
188                 this.layer.destroyFeatures(destroys);
189             }
190         }
191     },    
192    
193     CLASS_NAME: "OpenLayers.Strategy.Save" 
194 });