~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/ext/source/widgets/form/Action.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 2.2
 
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/**
 
10
 * @class Ext.form.Action
 
11
 * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p>
 
12
 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
 
13
 * the Form needs to perform an action such as submit or load. The Configuration options
 
14
 * listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit},
 
15
 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p>
 
16
 * <p>The instance of Action which performed the action is passed to the success
 
17
 * and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit},
 
18
 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}),
 
19
 * and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and
 
20
 * {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p>
 
21
 */
 
22
Ext.form.Action = function(form, options){
 
23
    this.form = form;
 
24
    this.options = options || {};
 
25
};
 
26
 
 
27
/**
 
28
 * Failure type returned when client side validation of the Form fails
 
29
 * thus aborting a submit action.
 
30
 * @type {String}
 
31
 * @static
 
32
 */
 
33
Ext.form.Action.CLIENT_INVALID = 'client';
 
34
/**
 
35
 * Failure type returned when server side validation of the Form fails
 
36
 * indicating that field-specific error messages have been returned in the
 
37
 * response's <tt style="font-weight:bold">errors</tt> property.
 
38
 * @type {String}
 
39
 * @static
 
40
 */
 
41
Ext.form.Action.SERVER_INVALID = 'server';
 
42
/**
 
43
 * Failure type returned when a communication error happens when attempting
 
44
 * to send a request to the remote server.
 
45
 * @type {String}
 
46
 * @static
 
47
 */
 
48
Ext.form.Action.CONNECT_FAILURE = 'connect';
 
49
/**
 
50
 * Failure type returned when no field values are returned in the response's
 
51
 * <tt style="font-weight:bold">data</tt> property.
 
52
 * @type {String}
 
53
 * @static
 
54
 */
 
55
Ext.form.Action.LOAD_FAILURE = 'load';
 
56
 
 
57
Ext.form.Action.prototype = {
 
58
/**
 
59
 * @cfg {String} url The URL that the Action is to invoke.
 
60
 */
 
61
/**
 
62
 * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
 
63
 * {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens
 
64
 * <b>before</b> the {@link #success} callback is called and before the Form's
 
65
 * {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires.
 
66
 */
 
67
/**
 
68
 * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
 
69
 * {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method.
 
70
 */
 
71
/**
 
72
 * @cfg {Mixed} params Extra parameter values to pass. These are added to the Form's
 
73
 * {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's
 
74
 * input fields.
 
75
 */
 
76
/**
 
77
 * @cfg {Number} timeout The number of milliseconds to wait for a server response before
 
78
 * failing with the {@link #failureType} as {@link #CONNECT_FAILURE}.
 
79
 */
 
80
/**
 
81
 * @cfg {Function} success The function to call when a valid success return packet is recieved.
 
82
 * The function is passed the following parameters:<ul class="mdetail-params">
 
83
 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
 
84
 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result}
 
85
 * property of this object may be examined to perform custom postprocessing.</div></li>
 
86
 * </ul>
 
87
 */
 
88
/**
 
89
 * @cfg {Function} failure The function to call when a failure packet was recieved, or when an
 
90
 * error ocurred in the Ajax communication.
 
91
 * The function is passed the following parameters:<ul class="mdetail-params">
 
92
 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
 
93
 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax
 
94
 * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
 
95
 * property of this object may be examined to perform custom postprocessing.</div></li>
 
96
 * </ul>
 
97
*/
 
98
/**
 
99
 * @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference
 
100
 * for the callback functions).
 
101
 */
 
102
/**
 
103
 * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
 
104
 * during the time the action is being processed.
 
105
 */
 
106
/**
 
107
 * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
 
108
 * during the time the action is being processed.
 
109
 */
 
110
 
 
111
/**
 
112
 * The type of action this Action instance performs.
 
113
 * Currently only "submit" and "load" are supported.
 
114
 * @type {String}
 
115
 */
 
116
    type : 'default',
 
117
/**
 
118
 * The type of failure detected. See {@link #Ext.form.Action.CLIENT_INVALID CLIENT_INVALID}, {@link #Ext.form.Action.SERVER_INVALID SERVER_INVALID},
 
119
 * {@link #Ext.form.Action.CONNECT_FAILURE CONNECT_FAILURE}, {@link #Ext.form.Action.LOAD_FAILURE LOAD_FAILURE}
 
120
 * @property failureType
 
121
 * @type {String}
 
122
 *//**
 
123
 * The XMLHttpRequest object used to perform the action.
 
124
 * @property response
 
125
 * @type {Object}
 
126
 *//**
 
127
 * The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and
 
128
 * other, action-specific properties.
 
129
 * @property result
 
130
 * @type {Object}
 
131
 */
 
132
 
 
133
    // interface method
 
134
    run : function(options){
 
135
 
 
136
    },
 
137
 
 
138
    // interface method
 
139
    success : function(response){
 
140
 
 
141
    },
 
142
 
 
143
    // interface method
 
144
    handleResponse : function(response){
 
145
 
 
146
    },
 
147
 
 
148
    // default connection failure
 
149
    failure : function(response){
 
150
        this.response = response;
 
151
        this.failureType = Ext.form.Action.CONNECT_FAILURE;
 
152
        this.form.afterAction(this, false);
 
153
    },
 
154
 
 
155
    // private
 
156
    processResponse : function(response){
 
157
        this.response = response;
 
158
        if(!response.responseText){
 
159
            return true;
 
160
        }
 
161
        this.result = this.handleResponse(response);
 
162
        return this.result;
 
163
    },
 
164
 
 
165
    // utility functions used internally
 
166
    getUrl : function(appendParams){
 
167
        var url = this.options.url || this.form.url || this.form.el.dom.action;
 
168
        if(appendParams){
 
169
            var p = this.getParams();
 
170
            if(p){
 
171
                url += (url.indexOf('?') != -1 ? '&' : '?') + p;
 
172
            }
 
173
        }
 
174
        return url;
 
175
    },
 
176
 
 
177
    // private
 
178
    getMethod : function(){
 
179
        return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
 
180
    },
 
181
 
 
182
    // private
 
183
    getParams : function(){
 
184
        var bp = this.form.baseParams;
 
185
        var p = this.options.params;
 
186
        if(p){
 
187
            if(typeof p == "object"){
 
188
                p = Ext.urlEncode(Ext.applyIf(p, bp));
 
189
            }else if(typeof p == 'string' && bp){
 
190
                p += '&' + Ext.urlEncode(bp);
 
191
            }
 
192
        }else if(bp){
 
193
            p = Ext.urlEncode(bp);
 
194
        }
 
195
        return p;
 
196
    },
 
197
 
 
198
    // private
 
199
    createCallback : function(opts){
 
200
                var opts = opts || {};
 
201
        return {
 
202
            success: this.success,
 
203
            failure: this.failure,
 
204
            scope: this,
 
205
            timeout: (opts.timeout*1000) || (this.form.timeout*1000),
 
206
            upload: this.form.fileUpload ? this.success : undefined
 
207
        };
 
208
    }
 
209
};
 
210
 
 
211
/**
 
212
 * @class Ext.form.Action.Submit
 
213
 * @extends Ext.form.Action
 
214
 * <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s
 
215
 * and processes the returned response.</p>
 
216
 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
 
217
 * {@link Ext.form.BasicForm#submit submit}ting.</p>
 
218
 * <p>A response packet must contain a boolean <tt style="font-weight:bold">success</tt> property, and, optionally
 
219
 * an <tt style="font-weight:bold">errors</tt> property. The <tt style="font-weight:bold">errors</tt> property contains error
 
220
 * messages for invalid fields.</p>
 
221
 * <p>By default, response packets are assumed to be JSON, so a typical response
 
222
 * packet may look like this:</p><pre><code>
 
223
{
 
224
    success: false,
 
225
    errors: {
 
226
        clientCode: "Client not found",
 
227
        portOfLoading: "This field must not be null"
 
228
    }
 
229
}</code></pre>
 
230
 * <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback
 
231
 * or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p>
 
232
 * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code>
 
233
    errorReader: new Ext.data.XmlReader({
 
234
            record : 'field',
 
235
            success: '@success'
 
236
        }, [
 
237
            'id', 'msg'
 
238
        ]
 
239
    )
 
240
</code></pre>
 
241
 * <p>then the results may be sent back in XML format:</p><pre><code>
 
242
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 
243
&lt;message success="false"&gt;
 
244
&lt;errors&gt;
 
245
    &lt;field&gt;
 
246
        &lt;id&gt;clientCode&lt;/id&gt;
 
247
        &lt;msg&gt;&lt;![CDATA[Code not found. &lt;br /&gt;&lt;i&gt;This is a test validation message from the server &lt;/i&gt;]]&gt;&lt;/msg&gt;
 
248
    &lt;/field&gt;
 
249
    &lt;field&gt;
 
250
        &lt;id&gt;portOfLoading&lt;/id&gt;
 
251
        &lt;msg&gt;&lt;![CDATA[Port not found. &lt;br /&gt;&lt;i&gt;This is a test validation message from the server &lt;/i&gt;]]&gt;&lt;/msg&gt;
 
252
    &lt;/field&gt;
 
253
&lt;/errors&gt;
 
254
&lt;/message&gt;
 
255
</code></pre>
 
256
 * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback
 
257
 * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p>
 
258
 */
 
259
Ext.form.Action.Submit = function(form, options){
 
260
    Ext.form.Action.Submit.superclass.constructor.call(this, form, options);
 
261
};
 
262
 
 
263
Ext.extend(Ext.form.Action.Submit, Ext.form.Action, {
 
264
    /**
 
265
    * @cfg {Ext.data.DataReader} errorReader <b>Optional. JSON is interpreted with no need for an errorReader.</b>
 
266
    * <p>A Reader which reads a single record from the returned data. The DataReader's <b>success</b> property specifies
 
267
    * how submission success is determined. The Record's data provides the error messages to apply to any invalid form Fields.</p>.
 
268
    */
 
269
    /**
 
270
    * @cfg {boolean} clientValidation Determines whether a Form's fields are validated
 
271
    * in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission.
 
272
    * Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation
 
273
    * is performed.
 
274
    */
 
275
    type : 'submit',
 
276
 
 
277
    // private
 
278
    run : function(){
 
279
        var o = this.options;
 
280
        var method = this.getMethod();
 
281
        var isGet = method == 'GET';
 
282
        if(o.clientValidation === false || this.form.isValid()){
 
283
            Ext.Ajax.request(Ext.apply(this.createCallback(o), {
 
284
                form:this.form.el.dom,
 
285
                url:this.getUrl(isGet),
 
286
                method: method,
 
287
                headers: o.headers,
 
288
                params:!isGet ? this.getParams() : null,
 
289
                isUpload: this.form.fileUpload
 
290
            }));
 
291
        }else if (o.clientValidation !== false){ // client validation failed
 
292
            this.failureType = Ext.form.Action.CLIENT_INVALID;
 
293
            this.form.afterAction(this, false);
 
294
        }
 
295
    },
 
296
 
 
297
    // private
 
298
    success : function(response){
 
299
        var result = this.processResponse(response);
 
300
        if(result === true || result.success){
 
301
            this.form.afterAction(this, true);
 
302
            return;
 
303
        }
 
304
        if(result.errors){
 
305
            this.form.markInvalid(result.errors);
 
306
            this.failureType = Ext.form.Action.SERVER_INVALID;
 
307
        }
 
308
        this.form.afterAction(this, false);
 
309
    },
 
310
 
 
311
    // private
 
312
    handleResponse : function(response){
 
313
        if(this.form.errorReader){
 
314
            var rs = this.form.errorReader.read(response);
 
315
            var errors = [];
 
316
            if(rs.records){
 
317
                for(var i = 0, len = rs.records.length; i < len; i++) {
 
318
                    var r = rs.records[i];
 
319
                    errors[i] = r.data;
 
320
                }
 
321
            }
 
322
            if(errors.length < 1){
 
323
                errors = null;
 
324
            }
 
325
            return {
 
326
                success : rs.success,
 
327
                errors : errors
 
328
            };
 
329
        }
 
330
        return Ext.decode(response.responseText);
 
331
    }
 
332
});
 
333
 
 
334
 
 
335
/**
 
336
 * @class Ext.form.Action.Load
 
337
 * @extends Ext.form.Action
 
338
 * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p>
 
339
 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
 
340
 * {@link Ext.form.BasicForm#load load}ing.</p>
 
341
 * <p>A response packet <b>must</b> contain a boolean <tt style="font-weight:bold">success</tt> property, and
 
342
 * a <tt style="font-weight:bold">data</tt> property. The <tt style="font-weight:bold">data</tt> property
 
343
 * contains the values of Fields to load. The individual value object for each Field
 
344
 * is passed to the Field's {@link Ext.form.Field#setValue setValue} method.</p>
 
345
 * <p>By default, response packets are assumed to be JSON, so a typical response
 
346
 * packet may look like this:</p><pre><code>
 
347
{
 
348
    success: true,
 
349
    data: {
 
350
        clientName: "Fred. Olsen Lines",
 
351
        portOfLoading: "FXT",
 
352
        portOfDischarge: "OSL"
 
353
    }
 
354
}</code></pre>
 
355
 * <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s callback
 
356
 * or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p>
 
357
 */
 
358
Ext.form.Action.Load = function(form, options){
 
359
    Ext.form.Action.Load.superclass.constructor.call(this, form, options);
 
360
    this.reader = this.form.reader;
 
361
};
 
362
 
 
363
Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
 
364
    // private
 
365
    type : 'load',
 
366
 
 
367
    // private
 
368
    run : function(){
 
369
        Ext.Ajax.request(Ext.apply(
 
370
                this.createCallback(this.options), {
 
371
                    method:this.getMethod(),
 
372
                    url:this.getUrl(false),
 
373
                    headers: this.options.headers,
 
374
                    params:this.getParams()
 
375
        }));
 
376
    },
 
377
 
 
378
    // private
 
379
    success : function(response){
 
380
        var result = this.processResponse(response);
 
381
        if(result === true || !result.success || !result.data){
 
382
            this.failureType = Ext.form.Action.LOAD_FAILURE;
 
383
            this.form.afterAction(this, false);
 
384
            return;
 
385
        }
 
386
        this.form.clearInvalid();
 
387
        this.form.setValues(result.data);
 
388
        this.form.afterAction(this, true);
 
389
    },
 
390
 
 
391
    // private
 
392
    handleResponse : function(response){
 
393
        if(this.form.reader){
 
394
            var rs = this.form.reader.read(response);
 
395
            var data = rs.records && rs.records[0] ? rs.records[0].data : null;
 
396
            return {
 
397
                success : rs.success,
 
398
                data : data
 
399
            };
 
400
        }
 
401
        return Ext.decode(response.responseText);
 
402
    }
 
403
});
 
404
 
 
405
Ext.form.Action.ACTION_TYPES = {
 
406
    'load' : Ext.form.Action.Load,
 
407
    'submit' : Ext.form.Action.Submit
 
408
};