Roo/form/BasicForm.js
[roojs1] / Roo / form / BasicForm.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.form.BasicForm
14  * @extends Roo.util.Observable
15  * Supplies the functionality to do "actions" on forms and initialize Roo.form.Field types on existing markup.
16  * @constructor
17  * @param {String/HTMLElement/Roo.Element} el The form element or its id
18  * @param {Object} config Configuration options
19  */
20 Roo.form.BasicForm = function(el, config){
21     this.allItems = [];
22     this.childForms = [];
23     Roo.apply(this, config);
24     /*
25      * The Roo.form.Field items in this form.
26      * @type MixedCollection
27      */
28      
29      
30     this.items = new Roo.util.MixedCollection(false, function(o){
31         return o.id || (o.id = Roo.id());
32     });
33     this.addEvents({
34         /**
35          * @event beforeaction
36          * Fires before any action is performed. Return false to cancel the action.
37          * @param {Form} this
38          * @param {Action} action The action to be performed
39          */
40         beforeaction: true,
41         /**
42          * @event actionfailed
43          * Fires when an action fails.
44          * @param {Form} this
45          * @param {Action} action The action that failed
46          */
47         actionfailed : true,
48         /**
49          * @event actioncomplete
50          * Fires when an action is completed.
51          * @param {Form} this
52          * @param {Action} action The action that completed
53          */
54         actioncomplete : true
55     });
56     if(el){
57         this.initEl(el);
58     }
59     Roo.form.BasicForm.superclass.constructor.call(this);
60 };
61
62 Roo.extend(Roo.form.BasicForm, Roo.util.Observable, {
63     /**
64      * @cfg {String} method
65      * The request method to use (GET or POST) for form actions if one isn't supplied in the action options.
66      */
67     /**
68      * @cfg {DataReader} reader
69      * An Roo.data.DataReader (e.g. {@link Roo.data.XmlReader}) to be used to read data when executing "load" actions.
70      * This is optional as there is built-in support for processing JSON.
71      */
72     /**
73      * @cfg {DataReader} errorReader
74      * An Roo.data.DataReader (e.g. {@link Roo.data.XmlReader}) to be used to read data when reading validation errors on "submit" actions.
75      * This is completely optional as there is built-in support for processing JSON.
76      */
77     /**
78      * @cfg {String} url
79      * The URL to use for form actions if one isn't supplied in the action options.
80      */
81     /**
82      * @cfg {Boolean} fileUpload
83      * Set to true if this form is a file upload.
84      */
85     /**
86      * @cfg {Object} baseParams
87      * Parameters to pass with all requests. e.g. baseParams: {id: '123', foo: 'bar'}.
88      */
89      /**
90      * @cfg {Roo.Element} maskEl
91      * Element to mask when loading or saving (used by Roo.form.Action*)
92      */
93     /**
94      * @cfg {Number} timeout Timeout for form actions in seconds (default is 30 seconds).
95      */
96     timeout: 30,
97
98     // private
99     activeAction : null,
100
101     /**
102      * @cfg {Boolean} trackResetOnLoad If set to true, form.reset() resets to the last loaded
103      * or setValues() data instead of when the form was first created.
104      */
105     trackResetOnLoad : false,
106     
107     
108     /**
109      * childForms - used for multi-tab forms
110      * @type {Array}
111      */
112     childForms : false,
113     
114     /**
115      * allItems - full list of fields.
116      * @type {Array}
117      */
118     allItems : false,
119     
120     /**
121      * By default wait messages are displayed with Roo.MessageBox.wait. You can target a specific
122      * element by passing it or its id or mask the form itself by passing in true.
123      * @type Mixed
124      */
125     waitMsgTarget : undefined,
126
127     // private
128     initEl : function(el){
129         this.el = Roo.get(el);
130         this.id = this.el.id || Roo.id();
131         this.el.on('submit', this.onSubmit, this);
132         this.el.addClass('x-form');
133     },
134
135     // private
136     onSubmit : function(e){
137         e.stopEvent();
138     },
139
140     /**
141      * Returns true if client-side validation on the form is successful.
142      * @return Boolean
143      */
144     isValid : function(){
145         var valid = true;
146         this.items.each(function(f){
147            if(!f.validate()){
148                valid = false;
149            }
150         });
151         return valid;
152     },
153
154     /**
155      * Returns true if any fields in this form have changed since their original load.
156      * @return Boolean
157      */
158     isDirty : function(){
159         var dirty = false;
160         this.items.each(function(f){
161            if(f.isDirty()){
162                dirty = true;
163                return false;
164            }
165         });
166         return dirty;
167     },
168
169     /**
170      * Performs a predefined action (submit or load) or custom actions you define on this form.
171      * @param {String} actionName The name of the action type
172      * @param {Object} options (optional) The options to pass to the action.  All of the config options listed
173      * below are supported by both the submit and load actions unless otherwise noted (custom actions could also
174      * accept other config options):
175      * <pre>
176 Property          Type             Description
177 ----------------  ---------------  ----------------------------------------------------------------------------------
178 url               String           The url for the action (defaults to the form's url)
179 method            String           The form method to use (defaults to the form's method, or POST if not defined)
180 params            String/Object    The params to pass (defaults to the form's baseParams, or none if not defined)
181 clientValidation  Boolean          Applies to submit only.  Pass true to call form.isValid() prior to posting to
182                                    validate the form on the client (defaults to false)
183      * </pre>
184      * @return {BasicForm} this
185      */
186     doAction : function(action, options){
187         if(typeof action == 'string'){
188             action = new Roo.form.Action.ACTION_TYPES[action](this, options);
189         }
190         if(this.fireEvent('beforeaction', this, action) !== false){
191             this.beforeAction(action);
192             action.run.defer(100, action);
193         }
194         return this;
195     },
196
197     /**
198      * Shortcut to do a submit action.
199      * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
200      * @return {BasicForm} this
201      */
202     submit : function(options){
203         this.doAction('submit', options);
204         return this;
205     },
206
207     /**
208      * Shortcut to do a load action.
209      * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
210      * @return {BasicForm} this
211      */
212     load : function(options){
213         this.doAction('load', options);
214         return this;
215     },
216
217     /**
218      * Persists the values in this form into the passed Roo.data.Record object in a beginEdit/endEdit block.
219      * @param {Record} record The record to edit
220      * @return {BasicForm} this
221      */
222     updateRecord : function(record){
223         record.beginEdit();
224         var fs = record.fields;
225         fs.each(function(f){
226             var field = this.findField(f.name);
227             if(field){
228                 record.set(f.name, field.getValue());
229             }
230         }, this);
231         record.endEdit();
232         return this;
233     },
234
235     /**
236      * Loads an Roo.data.Record into this form.
237      * @param {Record} record The record to load
238      * @return {BasicForm} this
239      */
240     loadRecord : function(record){
241         this.setValues(record.data);
242         return this;
243     },
244
245     // private
246     beforeAction : function(action){
247         var o = action.options;
248         if(o.waitMsg){
249             if(this.waitMsgTarget === true){
250                 this.el.mask(o.waitMsg, 'x-mask-loading');
251             }else if(this.waitMsgTarget){
252                 this.waitMsgTarget = Roo.get(this.waitMsgTarget);
253                 this.waitMsgTarget.mask(o.waitMsg, 'x-mask-loading');
254             }else{
255                 Roo.MessageBox.wait(o.waitMsg, o.waitTitle || this.waitTitle || 'Please Wait...');
256             }
257         }
258     },
259
260     // private
261     afterAction : function(action, success){
262         this.activeAction = null;
263         var o = action.options;
264         if(o.waitMsg){
265             if(this.waitMsgTarget === true){
266                 this.el.unmask();
267             }else if(this.waitMsgTarget){
268                 this.waitMsgTarget.unmask();
269             }else{
270                 Roo.MessageBox.updateProgress(1);
271                 Roo.MessageBox.hide();
272             }
273         }
274         if(success){
275             if(o.reset){
276                 this.reset();
277             }
278             Roo.callback(o.success, o.scope, [this, action]);
279             this.fireEvent('actioncomplete', this, action);
280             
281         }else{
282             Roo.callback(o.failure, o.scope, [this, action]);
283             // show an error message if no failed handler is set..
284             if (!this.hasListener('actionfailed')) {
285                 Roo.MessageBox.alert("Error", "Saving Failed, please check your entries");
286             }
287             
288             this.fireEvent('actionfailed', this, action);
289         }
290         
291     },
292
293     /**
294      * Find a Roo.form.Field in this form by id, dataIndex, name or hiddenName
295      * @param {String} id The value to search for
296      * @return Field
297      */
298     findField : function(id){
299         var field = this.items.get(id);
300         if(!field){
301             this.items.each(function(f){
302                 if(f.isFormField && (f.dataIndex == id || f.id == id || f.getName() == id)){
303                     field = f;
304                     return false;
305                 }
306             });
307         }
308         return field || null;
309     },
310
311     /**
312      * Add a secondary form to this one, 
313      * Used to provide tabbed forms. One form is primary, with hidden values 
314      * which mirror the elements from the other forms.
315      * 
316      * @param {Roo.form.Form} form to add.
317      * 
318      */
319     addForm : function(form)
320     {
321        
322         if (this.childForms.indexOf(form) > -1) {
323             // already added..
324             return;
325         }
326         this.childForms.push(form);
327         var n = '';
328         Roo.each(form.allItems, function (fe) {
329             
330             n = typeof(fe.getName) == 'undefined' ? fe.name : fe.getName();
331             if (this.findField(n)) { // already added..
332                 return;
333             }
334             var add = new Roo.form.Hidden({
335                 name : n
336             });
337             add.render(this.el);
338             
339             this.add( add );
340         }, this);
341         
342     },
343     /**
344      * Mark fields in this form invalid in bulk.
345      * @param {Array/Object} errors Either an array in the form [{id:'fieldId', msg:'The message'},...] or an object hash of {id: msg, id2: msg2}
346      * @return {BasicForm} this
347      */
348     markInvalid : function(errors){
349         if(errors instanceof Array){
350             for(var i = 0, len = errors.length; i < len; i++){
351                 var fieldError = errors[i];
352                 var f = this.findField(fieldError.id);
353                 if(f){
354                     f.markInvalid(fieldError.msg);
355                 }
356             }
357         }else{
358             var field, id;
359             for(id in errors){
360                 if(typeof errors[id] != 'function' && (field = this.findField(id))){
361                     field.markInvalid(errors[id]);
362                 }
363             }
364         }
365         Roo.each(this.childForms || [], function (f) {
366             f.markInvalid(errors);
367         });
368         
369         return this;
370     },
371
372     /**
373      * Set values for fields in this form in bulk.
374      * @param {Array/Object} values Either an array in the form [{id:'fieldId', value:'foo'},...] or an object hash of {id: value, id2: value2}
375      * @return {BasicForm} this
376      */
377     setValues : function(values){
378         if(values instanceof Array){ // array of objects
379             for(var i = 0, len = values.length; i < len; i++){
380                 var v = values[i];
381                 var f = this.findField(v.id);
382                 if(f){
383                     f.setValue(v.value);
384                     if(this.trackResetOnLoad){
385                         f.originalValue = f.getValue();
386                     }
387                 }
388             }
389         }else{ // object hash
390             var field, id;
391             for(id in values){
392                 if(typeof values[id] != 'function' && (field = this.findField(id))){
393                     
394                     if (field.setFromData && 
395                         field.valueField && 
396                         field.displayField &&
397                         // combos' with local stores can 
398                         // be queried via setValue()
399                         // to set their value..
400                         (field.store && !field.store.isLocal)
401                         ) {
402                         // it's a combo
403                         var sd = { };
404                         sd[field.valueField] = typeof(values[field.hiddenName]) == 'undefined' ? '' : values[field.hiddenName];
405                         sd[field.displayField] = typeof(values[field.name]) == 'undefined' ? '' : values[field.name];
406                         field.setFromData(sd);
407                         
408                     } else {
409                         field.setValue(values[id]);
410                     }
411                     
412                     
413                     if(this.trackResetOnLoad){
414                         field.originalValue = field.getValue();
415                     }
416                 }
417             }
418         }
419          
420         Roo.each(this.childForms || [], function (f) {
421             f.setValues(values);
422         });
423                 
424         return this;
425     },
426
427     /**
428      * Returns the fields in this form as an object with key/value pairs. If multiple fields exist with the same name
429      * they are returned as an array.
430      * @param {Boolean} asString
431      * @return {Object}
432      */
433     getValues : function(asString){
434         if (this.childForms) {
435             // copy values from the child forms
436             Roo.each(this.childForms, function (f) {
437                 this.setValues(f.getValues());
438             }, this);
439         }
440         
441         
442         
443         var fs = Roo.lib.Ajax.serializeForm(this.el.dom);
444         if(asString === true){
445             return fs;
446         }
447         return Roo.urlDecode(fs);
448     },
449     
450     /**
451      * Returns the fields in this form as an object with key/value pairs. 
452      * This differs from getValues as it calls getValue on each child item, rather than using dom data.
453      * @return {Object}
454      */
455     getFieldValues : function()
456     {
457         if (this.childForms) {
458             // copy values from the child forms
459             Roo.each(this.childForms, function (f) {
460                 this.setValues(f.getValues());
461             }, this);
462         }
463         
464         var ret = {};
465         this.items.each(function(f){
466             if (!f.getName()) {
467                 return;
468             }
469             var v = f.getValue();
470             if ((typeof(v) == 'object') && f.getRawValue) {
471                 v = f.getRawValue() ; // dates..
472             }
473             ret[f.getName()] = v;
474         });
475         
476         return ret;
477     },
478
479     /**
480      * Clears all invalid messages in this form.
481      * @return {BasicForm} this
482      */
483     clearInvalid : function(){
484         this.items.each(function(f){
485            f.clearInvalid();
486         });
487         
488         Roo.each(this.childForms || [], function (f) {
489             f.clearInvalid();
490         });
491         
492         
493         return this;
494     },
495
496     /**
497      * Resets this form.
498      * @return {BasicForm} this
499      */
500     reset : function(){
501         this.items.each(function(f){
502             f.reset();
503         });
504         
505         Roo.each(this.childForms || [], function (f) {
506             f.reset();
507         });
508        
509         
510         return this;
511     },
512
513     /**
514      * Add Roo.form components to this form.
515      * @param {Field} field1
516      * @param {Field} field2 (optional)
517      * @param {Field} etc (optional)
518      * @return {BasicForm} this
519      */
520     add : function(){
521         this.items.addAll(Array.prototype.slice.call(arguments, 0));
522         return this;
523     },
524
525
526     /**
527      * Removes a field from the items collection (does NOT remove its markup).
528      * @param {Field} field
529      * @return {BasicForm} this
530      */
531     remove : function(field){
532         this.items.remove(field);
533         return this;
534     },
535
536     /**
537      * Looks at the fields in this form, checks them for an id attribute,
538      * and calls applyTo on the existing dom element with that id.
539      * @return {BasicForm} this
540      */
541     render : function(){
542         this.items.each(function(f){
543             if(f.isFormField && !f.rendered && document.getElementById(f.id)){ // if the element exists
544                 f.applyTo(f.id);
545             }
546         });
547         return this;
548     },
549
550     /**
551      * Calls {@link Ext#apply} for all fields in this form with the passed object.
552      * @param {Object} values
553      * @return {BasicForm} this
554      */
555     applyToFields : function(o){
556         this.items.each(function(f){
557            Roo.apply(f, o);
558         });
559         return this;
560     },
561
562     /**
563      * Calls {@link Ext#applyIf} for all field in this form with the passed object.
564      * @param {Object} values
565      * @return {BasicForm} this
566      */
567     applyIfToFields : function(o){
568         this.items.each(function(f){
569            Roo.applyIf(f, o);
570         });
571         return this;
572     }
573 });
574
575 // back compat
576 Roo.BasicForm = Roo.form.BasicForm;