Protocol.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
  2. * full list of contributors). Published under the 2-clause BSD license.
  3. * See license.txt in the OpenLayers distribution or repository for the
  4. * full text of the license. */
  5. /**
  6. * @requires OpenLayers/BaseTypes/Class.js
  7. */
  8. /**
  9. * Class: OpenLayers.Protocol
  10. * Abstract vector layer protocol class. Not to be instantiated directly. Use
  11. * one of the protocol subclasses instead.
  12. */
  13. OpenLayers.Protocol = OpenLayers.Class({
  14. /**
  15. * Property: format
  16. * {<OpenLayers.Format>} The format used by this protocol.
  17. */
  18. format: null,
  19. /**
  20. * Property: options
  21. * {Object} Any options sent to the constructor.
  22. */
  23. options: null,
  24. /**
  25. * Property: autoDestroy
  26. * {Boolean} The creator of the protocol can set autoDestroy to false
  27. * to fully control when the protocol is destroyed. Defaults to
  28. * true.
  29. */
  30. autoDestroy: true,
  31. /**
  32. * Property: defaultFilter
  33. * {<OpenLayers.Filter>} Optional default filter to read requests
  34. */
  35. defaultFilter: null,
  36. /**
  37. * Constructor: OpenLayers.Protocol
  38. * Abstract class for vector protocols. Create instances of a subclass.
  39. *
  40. * Parameters:
  41. * options - {Object} Optional object whose properties will be set on the
  42. * instance.
  43. */
  44. initialize: function(options) {
  45. options = options || {};
  46. OpenLayers.Util.extend(this, options);
  47. this.options = options;
  48. },
  49. /**
  50. * Method: mergeWithDefaultFilter
  51. * Merge filter passed to the read method with the default one
  52. *
  53. * Parameters:
  54. * filter - {<OpenLayers.Filter>}
  55. */
  56. mergeWithDefaultFilter: function(filter) {
  57. var merged;
  58. if (filter && this.defaultFilter) {
  59. merged = new OpenLayers.Filter.Logical({
  60. type: OpenLayers.Filter.Logical.AND,
  61. filters: [this.defaultFilter, filter]
  62. });
  63. } else {
  64. merged = filter || this.defaultFilter || undefined;
  65. }
  66. return merged;
  67. },
  68. /**
  69. * APIMethod: destroy
  70. * Clean up the protocol.
  71. */
  72. destroy: function() {
  73. this.options = null;
  74. this.format = null;
  75. },
  76. /**
  77. * APIMethod: read
  78. * Construct a request for reading new features.
  79. *
  80. * Parameters:
  81. * options - {Object} Optional object for configuring the request.
  82. *
  83. * Returns:
  84. * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
  85. * object, the same object will be passed to the callback function passed
  86. * if one exists in the options object.
  87. */
  88. read: function(options) {
  89. options = options || {};
  90. options.filter = this.mergeWithDefaultFilter(options.filter);
  91. },
  92. /**
  93. * APIMethod: create
  94. * Construct a request for writing newly created features.
  95. *
  96. * Parameters:
  97. * features - {Array({<OpenLayers.Feature.Vector>})} or
  98. * {<OpenLayers.Feature.Vector>}
  99. * options - {Object} Optional object for configuring the request.
  100. *
  101. * Returns:
  102. * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
  103. * object, the same object will be passed to the callback function passed
  104. * if one exists in the options object.
  105. */
  106. create: function() {
  107. },
  108. /**
  109. * APIMethod: update
  110. * Construct a request updating modified features.
  111. *
  112. * Parameters:
  113. * features - {Array({<OpenLayers.Feature.Vector>})} or
  114. * {<OpenLayers.Feature.Vector>}
  115. * options - {Object} Optional object for configuring the request.
  116. *
  117. * Returns:
  118. * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
  119. * object, the same object will be passed to the callback function passed
  120. * if one exists in the options object.
  121. */
  122. update: function() {
  123. },
  124. /**
  125. * APIMethod: delete
  126. * Construct a request deleting a removed feature.
  127. *
  128. * Parameters:
  129. * feature - {<OpenLayers.Feature.Vector>}
  130. * options - {Object} Optional object for configuring the request.
  131. *
  132. * Returns:
  133. * {<OpenLayers.Protocol.Response>} An <OpenLayers.Protocol.Response>
  134. * object, the same object will be passed to the callback function passed
  135. * if one exists in the options object.
  136. */
  137. "delete": function() {
  138. },
  139. /**
  140. * APIMethod: commit
  141. * Go over the features and for each take action
  142. * based on the feature state. Possible actions are create,
  143. * update and delete.
  144. *
  145. * Parameters:
  146. * features - {Array({<OpenLayers.Feature.Vector>})}
  147. * options - {Object} Object whose possible keys are "create", "update",
  148. * "delete", "callback" and "scope", the values referenced by the
  149. * first three are objects as passed to the "create", "update", and
  150. * "delete" methods, the value referenced by the "callback" key is
  151. * a function which is called when the commit operation is complete
  152. * using the scope referenced by the "scope" key.
  153. *
  154. * Returns:
  155. * {Array({<OpenLayers.Protocol.Response>})} An array of
  156. * <OpenLayers.Protocol.Response> objects.
  157. */
  158. commit: function() {
  159. },
  160. /**
  161. * Method: abort
  162. * Abort an ongoing request.
  163. *
  164. * Parameters:
  165. * response - {<OpenLayers.Protocol.Response>}
  166. */
  167. abort: function(response) {
  168. },
  169. /**
  170. * Method: createCallback
  171. * Returns a function that applies the given public method with resp and
  172. * options arguments.
  173. *
  174. * Parameters:
  175. * method - {Function} The method to be applied by the callback.
  176. * response - {<OpenLayers.Protocol.Response>} The protocol response object.
  177. * options - {Object} Options sent to the protocol method
  178. */
  179. createCallback: function(method, response, options) {
  180. return OpenLayers.Function.bind(function() {
  181. method.apply(this, [response, options]);
  182. }, this);
  183. },
  184. CLASS_NAME: "OpenLayers.Protocol"
  185. });
  186. /**
  187. * Class: OpenLayers.Protocol.Response
  188. * Protocols return Response objects to their users.
  189. */
  190. OpenLayers.Protocol.Response = OpenLayers.Class({
  191. /**
  192. * Property: code
  193. * {Number} - OpenLayers.Protocol.Response.SUCCESS or
  194. * OpenLayers.Protocol.Response.FAILURE
  195. */
  196. code: null,
  197. /**
  198. * Property: requestType
  199. * {String} The type of request this response corresponds to. Either
  200. * "create", "read", "update" or "delete".
  201. */
  202. requestType: null,
  203. /**
  204. * Property: last
  205. * {Boolean} - true if this is the last response expected in a commit,
  206. * false otherwise, defaults to true.
  207. */
  208. last: true,
  209. /**
  210. * Property: features
  211. * {Array({<OpenLayers.Feature.Vector>})} or {<OpenLayers.Feature.Vector>}
  212. * The features returned in the response by the server. Depending on the
  213. * protocol's read payload, either features or data will be populated.
  214. */
  215. features: null,
  216. /**
  217. * Property: data
  218. * {Object}
  219. * The data returned in the response by the server. Depending on the
  220. * protocol's read payload, either features or data will be populated.
  221. */
  222. data: null,
  223. /**
  224. * Property: reqFeatures
  225. * {Array({<OpenLayers.Feature.Vector>})} or {<OpenLayers.Feature.Vector>}
  226. * The features provided by the user and placed in the request by the
  227. * protocol.
  228. */
  229. reqFeatures: null,
  230. /**
  231. * Property: priv
  232. */
  233. priv: null,
  234. /**
  235. * Property: error
  236. * {Object} The error object in case a service exception was encountered.
  237. */
  238. error: null,
  239. /**
  240. * Constructor: OpenLayers.Protocol.Response
  241. *
  242. * Parameters:
  243. * options - {Object} Optional object whose properties will be set on the
  244. * instance.
  245. */
  246. initialize: function(options) {
  247. OpenLayers.Util.extend(this, options);
  248. },
  249. /**
  250. * Method: success
  251. *
  252. * Returns:
  253. * {Boolean} - true on success, false otherwise
  254. */
  255. success: function() {
  256. return this.code > 0;
  257. },
  258. CLASS_NAME: "OpenLayers.Protocol.Response"
  259. });
  260. OpenLayers.Protocol.Response.SUCCESS = 1;
  261. OpenLayers.Protocol.Response.FAILURE = 0;