Save.js 7.3 KB

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