PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/builder/lib/ext/src/form/action/Action.js

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