Changeset 12482

Show
Ignore:
Timestamp:
02/17/08 05:07:53 (11 months ago)
Author:
doughays
Message:

Fixes #2666. Add reset() method to form widgets.
Changed MultiSelect? get/setValues to get/setValue.
Added field reset call to 1 textbox and 1 checkbox example.
Added reset automated test to Form.html.

Location:
dijit/trunk
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • dijit/trunk/form/CheckBox.js

    r12387 r12482  
    4444                }, 
    4545 
    46                 _getValueDeprecated: false, // remove when _FormWidget:getValue is removed 
     46                _getValueDeprecated: false, // remove when _FormWidget:_getValueDeprecated is removed 
    4747                getValue: function(){ 
    4848                        // summary: get the value of the widget. 
    4949                        return (this.checked ? this.value : false); 
     50                }, 
     51 
     52                reset: function(){ 
     53                        this.inherited(arguments); 
     54                        this.setAttribute('value', this._resetValueAttr); 
     55                }, 
     56 
     57                postCreate: function(){ 
     58                        this.inherited(arguments); 
     59                        this._resetValueAttr = this.value; 
    5060                } 
    5161        } 
  • dijit/trunk/form/Form.js

    r12387 r12482  
    2626        //       
    2727 
     28                reset: function(){ 
     29                        dojo.forEach(this.getDescendants(), function(widget){ 
     30                                if(widget.reset){ 
     31                                        widget.reset(); 
     32                                } 
     33                        }); 
     34                }, 
     35 
    2836                setValues: function(/*object*/obj){ 
    2937                        // summary: fill in form values from a JSON structure 
     
    4957                                                w.setValue(dojo.indexOf(values, w.value) != -1); 
    5058                                        }); 
    51                                 }else if(widgets[0].setValues){ 
    52                                         // it's a multi-select 
    53                                         widgets[0].setValues(values); 
     59                                }else if(widgets[0]._multiValue){ 
     60                                        // it takes an array (e.g. multi-select) 
     61                                        widgets[0].setValue(values); 
    5462                                }else{ 
    5563                                        // otherwise, values is a list of values to be assigned sequentially to each widget 
     
    141149                                if(!name){ return; } 
    142150 
    143                                 if(widget.getValues){ 
    144                                         // A multi-value widget (ex: MultiSelect) 
    145                                         dojo.setObject(name, widget.getValues(), obj); 
     151                                // Single value widget (checkbox, radio, or plain <input> type widget 
     152                                var value = (widget.getValue && !widget._getValueDeprecated) ? widget.getValue() : widget.value; 
     153 
     154                                // Store widget's value(s) as a scalar, except for checkboxes which are automatically arrays 
     155                                if(typeof widget.checked == 'boolean'){ 
     156                                        if(/Radio/.test(widget.declaredClass)){ 
     157                                                // radio button 
     158                                                if(value !== false){ 
     159                                                        dojo.setObject(name, value, obj); 
     160                                                } 
     161                                        }else{ 
     162                                                // checkbox/toggle button 
     163                                                var ary=dojo.getObject(name, false, obj); 
     164                                                if(!ary){ 
     165                                                        ary=[]; 
     166                                                        dojo.setObject(name, ary, obj); 
     167                                                } 
     168                                                if(value !== false){ 
     169                                                        ary.push(value); 
     170                                                } 
     171                                        } 
    146172                                }else{ 
    147                                         // Single value widget (checkbox, radio, or plain <input> type widget 
    148                                         var value = (widget.getValue && !widget._getValueDeprecated) ? widget.getValue() : widget.value; 
    149  
    150                                         // Store widget's value(s) as a scalar, except for checkboxes which are automatically arrays 
    151                                         if(typeof widget.checked == 'boolean'){ 
    152                                                 if(/Radio/.test(widget.declaredClass)){ 
    153                                                         // radio button 
    154                                                         if(value !== false){ 
    155                                                                 dojo.setObject(name, value, obj); 
    156                                                         } 
    157                                                 }else{ 
    158                                                         // checkbox/toggle button 
    159                                                         var ary=dojo.getObject(name, false, obj); 
    160                                                         if(!ary){ 
    161                                                                 ary=[]; 
    162                                                                 dojo.setObject(name, ary, obj); 
    163                                                         } 
    164                                                         if(value !== false){ 
    165                                                                 ary.push(value); 
    166                                                         } 
    167                                                 } 
    168                                         }else{ 
    169                                                 // plain input 
    170                                                 dojo.setObject(name, value, obj); 
    171                                         } 
     173                                        // plain input 
     174                                        dojo.setObject(name, value, obj); 
    172175                                } 
    173176                        }); 
     
    286289                        } 
    287290                        this.inherited(arguments); 
     291                        dojo.attr(this.domNode, 'onreset', dojo.hitch(this, this._onReset)); 
     292                }, 
     293 
     294                // onReset: Function 
     295                //      Callback when user resets the form 
     296                //      (user can override - return false to cancel the default action) 
     297                onReset: function(){  
     298                        return true; 
     299                }, 
     300 
     301                _onReset: function(){ 
     302                        if(this.onReset()){ 
     303                                this.reset(); 
     304                        } 
     305                        return false; 
    288306                }, 
    289307 
  • dijit/trunk/form/MultiSelect.js

    r12182 r12482  
    3838        }, 
    3939         
    40         getValues: function(){ 
     40        _getValueDeprecated: false, // remove when _FormWidget:_getValueDeprecated is removed in 2.0 
     41        getValue: function(){ 
    4142                // summary: Returns an array of the selected options' values 
    4243                return this.getSelected().map(function(n){ 
     
    4546        }, 
    4647         
    47         setValues: function(/* Array */values){ 
     48        _multiValue: true, // for Form 
     49        setValue: function(/* Array */values){ 
    4850                // summary: Set the value(s) of this Select based on passed values 
    4951                dojo.query("option",this.domNode).forEach(function(n){ 
     
    5961                        n.selected = !n.selected; 
    6062                }); 
    61                 if(onChange){ this.onChange(this.getValues()); } 
     63                this._handleOnChange(this.getValue(), onChange==true); 
    6264        }, 
    6365 
    6466        _onChange: function(/*Event*/ e){ 
    65                 this.onChange(this.getValues()); 
     67                this._handleOnChange(this.getValue(), true); 
    6668        }, 
    6769         
     
    7375        }, 
    7476         
    75         onChange: function(/*String[] */ l){ 
    76                 // summary: a stub -- over-ride, or connect 
     77        postCreate: function(){ 
     78                this._onChange(); 
    7779        } 
    7880}); 
  • dijit/trunk/form/NumberTextBox.js

    r11788 r12482  
    3333 
    3434                format: function(/*Number*/ value, /*Object*/ constraints){ 
     35                        if(typeof value == "string") { return value; } 
    3536                        if(isNaN(value)){ return ""; } 
    3637                        if(this.editOptions && this._editing){ 
  • dijit/trunk/form/ValidationTextBox.js

    r12453 r12482  
    136136                }, 
    137137 
     138                reset: function(){ 
     139                        this.inherited(arguments); 
     140                        this._hasBeenBlurred = false; 
     141                }, 
     142 
    138143                //////////// INITIALIZATION METHODS /////////////////////////////////////// 
    139144 
  • dijit/trunk/form/_FormWidget.js

    r12387 r12482  
    224224                // summary: set the value of the widget. 
    225225                this._lastValue = newValue; 
    226                 if(this._lastValueReported == undefined && priorityChange === null){ 
    227                         this._lastValueReported = newValue; 
     226                if(this._lastValueReported == undefined && (priorityChange === null || !this._onChangeActive)){ 
     227                        this._resetValue = this._lastValueReported = newValue; 
    228228                } 
    229229                if((this.intermediateChanges || priorityChange || priorityChange === undefined) &&  
     
    231231                        this._lastValueReported = newValue; 
    232232                        if(this._onChangeActive){ this.onChange(newValue); } 
     233                } 
     234        }, 
     235 
     236        reset: function(){ 
     237                if(this._resetValue !== undefined){ 
     238                        if(this.setValue && !this._getValueDeprecated){ 
     239                                this.setValue(this._resetValue, true); 
     240                        }else{ 
     241                                this.setAttribute(this._onChangeMonitor, this._resetValue); 
     242                        } 
    233243                } 
    234244        }, 
     
    244254                        clearTimeout(this._layoutHackHandle); 
    245255                this.inherited(arguments); 
    246         }, 
    247  
    248         postCreate: function(){ 
    249                 this._lastValueReported = this[this._onChangeMonitor]; 
    250256        }, 
    251257 
  • dijit/trunk/tests/form/Form.html

    r12387 r12482  
    6262                                                }; 
    6363 
    64                 // we reset the form to these values 
     64                // we change the form to these values 
    6565                var changed =   { 
    6666                                                        foo: {bar: {baz: {quux: d("2005-01-01")} } }, 
     
    7474                                                        st1: "new simple line 1\nnew simple line 2", 
    7575                                                        richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>", 
     76                                                        filename: "" 
     77                                                }; 
     78                // we reset the form to these values 
     79                var reset =     { 
     80                                                        foo: {bar: {baz: {quux: d("2007-12-30")} } }, 
     81                                                        available: {from: d("2005-01-02"), to: d("2006-01-02")}, 
     82                                                        plop: {combo: "one"}, 
     83                                                        cb2: ["2", "3"], 
     84                                                        r2: "2", 
     85                                                        ms1: ["VA", "WA"], 
     86                                                        h1: "hidden", 
     87                                                        t1: "line 1\nline 2", 
     88                                                        st1: "simple line 1\nsimple line 2", 
     89                                                        richtext: "<h1>changed</h1><p>This is the changed content set by setValues</p>", // not a form element, so not reset 
    7690                                                        filename: "" 
    7791                                                }; 
     
    93107                                                        }); 
    94108                                                 
     109                                        }, 
     110                                        function resetTest(){ 
     111                                                dijit.byId("myForm").reset(); 
     112                                                doh.is( dojo.toJson(reset), dojo.toJson(dijit.byId("myForm").getValues()) ); 
    95113                                        } 
    96114                                ] 
     
    111129        http://www.utexas.edu/teamweb/cgi-bin/generic.cgi  
    112130        http://www.tipjar.com/cgi-bin/test --> 
    113 <form dojoType="dijit.form.Form" id="myForm" encType="multipart/form-data" action="" method="" onsubmit="alert('Attempting to submit form w/values:\n'+dojo.toJson(this.getValues(),true));if(this.isValid()){return confirm('Form is valid, press OK to submit');}else{alert('Form contains invalid data.  Please correct first');return false;}"> 
     131<form dojoType="dijit.form.Form" id="myForm" encType="multipart/form-data" action="" method=""  
     132        onreset="return confirm('Press OK to reset widget values')" 
     133        onsubmit="alert('Attempting to submit form w/values:\n'+dojo.toJson(this.getValues(),true));if(this.isValid()){return confirm('Form is valid, press OK to submit');}else{alert('Form contains invalid data.  Please correct first');return false;}" 
     134> 
    114135<p>Just HTML text</p> 
    115136<table border=2> 
     
    273294<button dojoType=dijit.form.Button onClick="setValues();">Set Values to form!</button> 
    274295<button dojoType=dijit.form.Button type="submit">Submit</button> 
     296<button dojoType=dijit.form.Button type="reset">Reset</button> 
    275297</form> 
    276298 
  • dijit/trunk/tests/form/test_CheckBox.html

    r11982 r12482  
    8383                        <input type="button" onclick='dijit.byId("cb7").setAttribute("disabled",false);' value="enable" /> 
    8484                        <input type="button" onclick='dijit.byId("cb7").setValue("fish");' value='set value to "fish"' /> 
     85                        <input type="button" onclick='dijit.byId("cb7").reset();' value='Reset value+checked' /> 
    8586                        <span>"onChange" handler updates: [<span id="onvaluechangedoutput"></span>]</span> 
    8687                <br> 
  • dijit/trunk/tests/form/test_MultiSelect.html

    r12182 r12482  
    7373                        // there is only one debug button 
    7474                        dojo.query(".debug").connect("onclick",function(e){ 
    75                                 console.log('select getValues:',dijit.byId("select").getValues());       
    76                                 console.log('select2 getValues:',dijit.byId("select2").getValues()); 
    77                                 console.log('select3 getValues:',dijit.byId("select3").getValues()); 
     75                                console.log('select getValue:',dijit.byId("select").getValue()); 
     76                                console.log('select2 getValue:',dijit.byId("select2").getValue()); 
     77                                console.log('select3 getValue:',dijit.byId("select3").getValue()); 
    7878                        }); 
    7979                                 
     
    116116                </form> 
    117117 
    118                 <button class="debug">call getValues()</button> 
     118                <button class="debug">call getValue()</button> 
    119119 
    120120                <h3>markup:</h3> 
     
    134134                <br><br> 
    135135                <button class='invert' id="i3">invert markup list</button> 
    136                 <button class='set' id="s1" onclick="dijit.byId('select3').setValues(['VA', 'WA']);">set markup list to [VA, WA]</button> 
     136                <button class='set' id="s1" onclick="dijit.byId('select3').setValue(['VA', 'WA']);">set markup list to [VA, WA]</button> 
    137137</body> 
    138138</html> 
  • dijit/trunk/tests/form/test_validate.html

    r12044 r12482  
    220220                                <button onclick="dijit.byId('q08eur').setAttribute('disabled',true);return false">Disable</button> 
    221221                                <button onclick="dijit.byId('q08eur').setAttribute('disabled',false);return false">Enable</button> 
     222                                <button onclick="dijit.byId('q08eur').reset();return false">Reset</button> 
    222223                        </div> 
    223224