PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/adminextras/extjs/static/js/extjs/src/form/action/Action.js

https://github.com/D3f0/django-admin-extras
JavaScript | 313 lines | 51 code | 36 blank | 226 comment | 8 complexity | 8a698ed421d2495db0b2711e86e05775 MD5 | raw file
  1. /**
  2. * @class Ext.form.action.Action
  3. * @extends Ext.Base
  4. * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.Basic Form}s.</p>
  5. * <p>Instances of this class are only created by a {@link Ext.form.Basic Form} when
  6. * the Form needs to perform an action such as submit or load. The Configuration options
  7. * listed for this class are set through the Form's action methods: {@link Ext.form.Basic#submit submit},
  8. * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}</p>
  9. * <p>The instance of Action which performed the action is passed to the success
  10. * and failure callbacks of the Form's action methods ({@link Ext.form.Basic#submit submit},
  11. * {@link Ext.form.Basic#load load} and {@link Ext.form.Basic#doAction doAction}),
  12. * and to the {@link Ext.form.Basic#actioncomplete actioncomplete} and
  13. * {@link Ext.form.Basic#actionfailed actionfailed} event handlers.</p>
  14. * @constructor
  15. * @param {Object} config The configuration for this instance.
  16. */
  17. Ext.define('Ext.form.action.Action', {
  18. /**
  19. * @cfg {Ext.form.Basic} form The {@link Ext.form.Basic BasicForm} instance that
  20. * is invoking this Action. Required.
  21. */
  22. /**
  23. * @cfg {String} url The URL that the Action is to invoke. Will default to the {@link Ext.form.Basic#url url}
  24. * configured on the {@link #form}.
  25. */
  26. /**
  27. * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
  28. * {@link Ext.form.Basic.reset reset} on Action success. If specified, this happens
  29. * before the {@link #success} callback is called and before the Form's
  30. * {@link Ext.form.Basic#actioncomplete actioncomplete} event fires.
  31. */
  32. /**
  33. * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
  34. * {@link Ext.form.Basic#method BasicForm's method}, or 'POST' if not specified.
  35. */
  36. /**
  37. * @cfg {Object/String} params <p>Extra parameter values to pass. These are added to the Form's
  38. * {@link Ext.form.Basic#baseParams} and passed to the specified URL along with the Form's
  39. * input fields.</p>
  40. * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
  41. */
  42. /**
  43. * @cfg {Object} headers <p>Extra headers to be sent in the AJAX request for submit and load actions. See
  44. * {@link Ext.data.Connection#headers}.</p>
  45. */
  46. /**
  47. * @cfg {Number} timeout The number of seconds to wait for a server response before
  48. * failing with the {@link #failureType} as {@link Ext.form.action.Action.CONNECT_FAILURE}. If not specified,
  49. * defaults to the configured <tt>{@link Ext.form.Basic#timeout timeout}</tt> of the
  50. * {@link #form}.
  51. */
  52. /**
  53. * @cfg {Function} success The function to call when a valid success return packet is received.
  54. * The function is passed the following parameters:<ul class="mdetail-params">
  55. * <li><b>form</b> : Ext.form.Basic<div class="sub-desc">The form that requested the action</div></li>
  56. * <li><b>action</b> : Ext.form.action.Action<div class="sub-desc">The Action class. The {@link #result}
  57. * property of this object may be examined to perform custom postprocessing.</div></li>
  58. * </ul>
  59. */
  60. /**
  61. * @cfg {Function} failure The function to call when a failure packet was received, or when an
  62. * error ocurred in the Ajax communication.
  63. * The function is passed the following parameters:<ul class="mdetail-params">
  64. * <li><b>form</b> : Ext.form.Basic<div class="sub-desc">The form that requested the action</div></li>
  65. * <li><b>action</b> : Ext.form.action.Action<div class="sub-desc">The Action class. If an Ajax
  66. * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
  67. * property of this object may be examined to perform custom postprocessing.</div></li>
  68. * </ul>
  69. */
  70. /**
  71. * @cfg {Object} scope The scope in which to call the configured <tt>success</tt> and <tt>failure</tt>
  72. * callback functions (the <tt>this</tt> reference for the callback functions).
  73. */
  74. /**
  75. * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
  76. * during the time the action is being processed.
  77. */
  78. /**
  79. * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
  80. * during the time the action is being processed.
  81. */
  82. /**
  83. * @cfg {Boolean} submitEmptyText If set to <tt>true</tt>, the emptyText value will be sent with the form
  84. * when it is submitted. Defaults to <tt>true</tt>.
  85. */
  86. /**
  87. * @property type
  88. * The type of action this Action instance performs.
  89. * Currently only "submit" and "load" are supported.
  90. * @type {String}
  91. */
  92. /**
  93. * The type of failure detected will be one of these: {@link Ext.form.action.Action.CLIENT_INVALID},
  94. * {@link Ext.form.action.Action.SERVER_INVALID}, {@link Ext.form.action.Action.CONNECT_FAILURE}, or
  95. * {@link Ext.form.action.Action.LOAD_FAILURE}. Usage:
  96. * <pre><code>
  97. var fp = new Ext.form.FormPanel({
  98. ...
  99. buttons: [{
  100. text: 'Save',
  101. formBind: true,
  102. handler: function(){
  103. if(fp.getForm().isValid()){
  104. fp.getForm().submit({
  105. url: 'form-submit.php',
  106. waitMsg: 'Submitting your data...',
  107. success: function(form, action){
  108. // server responded with success = true
  109. var result = action.{@link #result};
  110. },
  111. failure: function(form, action){
  112. if (action.{@link #failureType} === Ext.form.action.Action.{@link Ext.form.action.Action.CONNECT_FAILURE}) {
  113. Ext.Msg.alert('Error',
  114. 'Status:'+action.{@link #response}.status+': '+
  115. action.{@link #response}.statusText);
  116. }
  117. if (action.failureType === Ext.form.action.Action.{@link Ext.form.action.Action.SERVER_INVALID}){
  118. // server responded with success = false
  119. Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
  120. }
  121. }
  122. });
  123. }
  124. }
  125. },{
  126. text: 'Reset',
  127. handler: function(){
  128. fp.getForm().reset();
  129. }
  130. }]
  131. * </code></pre>
  132. * @property failureType
  133. * @type {String}
  134. */
  135. /**
  136. * The raw XMLHttpRequest object used to perform the action.
  137. * @property response
  138. * @type {Object}
  139. */
  140. /**
  141. * The decoded response object containing a boolean <tt>success</tt> property and
  142. * other, action-specific properties.
  143. * @property result
  144. * @type {Object}
  145. */
  146. constructor: function(config) {
  147. if (config) {
  148. Ext.apply(this, config);
  149. }
  150. // Normalize the params option to an Object
  151. var params = config.params;
  152. if (Ext.isString(params)) {
  153. this.params = Ext.urlDecode(params);
  154. }
  155. },
  156. /**
  157. * Invokes this action using the current configuration.
  158. */
  159. run: Ext.emptyFn,
  160. /**
  161. * @private
  162. * @method onSuccess
  163. * Callback method that gets invoked when the action completes successfully. Must be implemented by subclasses.
  164. * @param {Object} response
  165. */
  166. /**
  167. * @private
  168. * @method handleResponse
  169. * Handles the raw response and builds a result object from it. Must be implemented by subclasses.
  170. * @param {Object} response
  171. */
  172. /**
  173. * @private
  174. * Handles a failure response.
  175. * @param {Object} response
  176. */
  177. onFailure : function(response){
  178. this.response = response;
  179. this.failureType = Ext.form.action.Action.CONNECT_FAILURE;
  180. this.form.afterAction(this, false);
  181. },
  182. /**
  183. * @private
  184. * Validates that a response contains either responseText or responseXML and invokes
  185. * {@link #handleResponse} to build the result object.
  186. * @param {Object} response The raw response object.
  187. * @return {Object/Boolean} result The result object as built by handleResponse, or <tt>true</tt> if
  188. * the response had empty responseText and responseXML.
  189. */
  190. processResponse : function(response){
  191. this.response = response;
  192. if (!response.responseText && !response.responseXML) {
  193. return true;
  194. }
  195. return (this.result = this.handleResponse(response));
  196. },
  197. /**
  198. * @private
  199. * Build the URL for the AJAX request. Used by the standard AJAX submit and load actions.
  200. * @return {String} The URL.
  201. */
  202. getUrl: function() {
  203. return this.url || this.form.url;
  204. },
  205. /**
  206. * @private
  207. * Determine the HTTP method to be used for the request.
  208. * @return {String} The HTTP method
  209. */
  210. getMethod: function() {
  211. return (this.method || this.form.method || 'POST').toUpperCase();
  212. },
  213. /**
  214. * @private
  215. * Get the set of parameters specified in the BasicForm's baseParams and/or the params option.
  216. * Items in params override items of the same name in baseParams.
  217. * @return {Object} the full set of parameters
  218. */
  219. getParams: function() {
  220. return Ext.apply({}, this.params, this.form.baseParams);
  221. },
  222. /**
  223. * @private
  224. * Creates a callback object.
  225. */
  226. createCallback: function() {
  227. var me = this,
  228. undef,
  229. form = me.form;
  230. return {
  231. success: me.onSuccess,
  232. failure: me.onFailure,
  233. scope: me,
  234. timeout: (this.timeout * 1000) || (form.timeout * 1000),
  235. upload: form.fileUpload ? me.onSuccess : undef
  236. };
  237. },
  238. statics: {
  239. /**
  240. * @property CLIENT_INVALID
  241. * Failure type returned when client side validation of the Form fails
  242. * thus aborting a submit action. Client side validation is performed unless
  243. * {@link Ext.form.action.Submit#clientValidation} is explicitly set to <tt>false</tt>.
  244. * @type {String}
  245. * @static
  246. */
  247. CLIENT_INVALID: 'client',
  248. /**
  249. * @property SERVER_INVALID
  250. * <p>Failure type returned when server side processing fails and the {@link #result}'s
  251. * <tt>success</tt> property is set to <tt>false</tt>.</p>
  252. * <p>In the case of a form submission, field-specific error messages may be returned in the
  253. * {@link #result}'s <tt>errors</tt> property.</p>
  254. * @type {String}
  255. * @static
  256. */
  257. SERVER_INVALID: 'server',
  258. /**
  259. * @property CONNECT_FAILURE
  260. * Failure type returned when a communication error happens when attempting
  261. * to send a request to the remote server. The {@link #response} may be examined to
  262. * provide further information.
  263. * @type {String}
  264. * @static
  265. */
  266. CONNECT_FAILURE: 'connect',
  267. /**
  268. * @property LOAD_FAILURE
  269. * Failure type returned when the response's <tt>success</tt>
  270. * property is set to <tt>false</tt>, or no field values are returned in the response's
  271. * <tt>data</tt> property.
  272. * @type {String}
  273. * @static
  274. */
  275. LOAD_FAILURE: 'load'
  276. }
  277. });