Changeset 14473

Show
Ignore:
Timestamp:
07/16/08 19:43:22 (4 months ago)
Author:
doughays
Message:

Fixes #5417 !strict. Added lazy connecting event handlers to _Widget.js

Location:
dijit/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • dijit/trunk/tests/form/test_DateTextBox.html

    r14346 r14473  
    4343                        dojo.require("dojo.date.locale"); 
    4444                        dojo.require("dojo.parser");    // scan page for widgets and instantiate them 
     45 
     46                        function eventHandler(e){ 
     47                                // use this.domNode.getAttribute('widgetId') to show "this" is the widget 
     48                                // mouseleave/enter map to mouseout/over in all browsers except IE 
     49                                console.log(this.domNode.getAttribute('widgetId') + ' ' + arguments[0].type); 
     50                        } 
    4551                </script> 
    4652        </head> 
     
    5864                        <div class="testExample"> 
    5965                                <input id="q1" name="noDOMvalue" value="2008-12-31" type="text" dojoType="dijit.form.DateTextBox" 
    60                                          onChange="dojo.byId('oc1').value=''+arguments[0]" 
     66                                        onMouseEnter="eventHandler" 
     67                                        onMouseLeave="eventHandler" 
     68                                        onKeyDown="eventHandler" 
     69                                        onChange="dojo.byId('oc1').value=''+arguments[0]" 
    6170                                > 
    6271                                onChange:<input id="oc1" size="34" disabled value="not fired yet!" autocomplete="off"> 
     
    7382                        </div> 
    7483                        <div class="testExample"> 
    75                                 <input id="q2" type="text" name="date1" value="2005-12-30" 
     84                                <div id="q2" type="text" name="date1" value="2005-12-30" 
    7685                                        dojoType="dijit.form.DateTextBox" 
    7786                                        constraints="{min:'2004-01-01',max:'2006-12-31',formatLength:'long'}" 
     
    7988                                        trim="true" 
    8089                                        onChange="dojo.byId('oc2').value=arguments[0]" 
    81                                         invalidMessage="Invalid date." /> 
     90                                        invalidMessage="Invalid date." > 
     91                                        <script type="dojo/connect" event="onMouseEnter">eventHandler.apply(this, arguments)</script> 
     92                                        <script type="dojo/connect" event="onMouseLeave">eventHandler.apply(this, arguments)</script> 
     93                                        <script type="dojo/connect" event="onKeyDown">eventHandler.apply(this, arguments)</script> 
     94                                </div> 
    8295                                onChange:<input id="oc2" size="34" disabled value="not fired yet!" autocomplete="off"> 
    8396                                <input type="button" value="Destroy" onClick="dijit.byId('q2').destroy(); return false;"> 
     
    90103                        </div> 
    91104                        <div class="testExample"> 
    92                                 <input id="q3" type="text" name="date2" value="2005-12-30" 
     105                                <div id="q3" type="text" name="date2" value="2005-12-30" 
    93106                                        dojoType="dijit.form.DateTextBox" 
    94107                                        constraints="{min:'2004-01-01',max:'2006-12-31'}" 
     
    96109                                        required="true" 
    97110                                        promptMessage="mm/dd/yyyy" 
    98                                         invalidMessage="Invalid date. Use mm/dd/yyyy format." /> 
     111                                        invalidMessage="Invalid date. Use mm/dd/yyyy format."> 
     112                                        <script type="dojo/method" event="onMouseEnter">eventHandler.call(this, arguments[0])</script> 
     113                                        <script type="dojo/method" event="onMouseLeave">eventHandler.call(this, arguments[0])</script> 
     114                                        <script type="dojo/method" event="onKeyDown">eventHandler.call(this, arguments[0])</script> 
     115                                </div> 
    99116                        </div> 
    100117                        <div class="dojoTitlePaneLabel"> 
     
    118135                        // See if we can make a widget in script and attach it to the DOM ourselves. 
    119136                        dojo.addOnLoad(function(){ 
     137                                dojo.connect(dijit.byId('q5'), "onMouseEnter", eventHandler); 
     138                                dojo.connect(dijit.byId('q5'), "onMouseLeave", eventHandler); 
     139                                dojo.connect(dijit.byId('q5'), "onKeyDown", eventHandler); 
     140 
    120141                                var props = { 
    121142                                        name: "date4", 
     
    123144                                        constraints: {min:new Date(2004,0,1),max:new Date(2006,11,31)}, 
    124145                                        lang: "de-de", 
     146                                        onMouseEnter: eventHandler, 
     147                                        onMouseLeave: eventHandler, 
     148                                        onKeyDown: eventHandler, 
    125149                                        promptMessage: "dd.mm.yy", 
    126150                                        rangeMessage: "Enter a date in the year range 2004-2006.", 
  • dijit/trunk/_Widget.js

    r14145 r14473  
    44dojo.require( "dijit._base" ); 
    55//>>excludeEnd("dijitBaseExclude"); 
     6 
     7dojo.connect(dojo, "connect",  
     8        function(/*Widget*/ widget, /*String*/ event){ 
     9                if(widget && dojo.isFunction(widget._onConnect)){ 
     10                        widget._onConnect.apply(widget, arguments); 
     11                } 
     12        }); 
     13 
     14dijit._connectOnUseEventHandler = function(/*Event*/ event){}; 
    615 
    716dojo.declare("dijit._Widget", null, { 
     
    8291        //              on the widget's dom, at the "domNode" attach point, by default. 
    8392        //              Other node references can be specified as properties of 'this' 
    84         attributeMap: {id:"", dir:"", lang:"", "class":"", style:"", title:""},  // TODO: add on* handlers? 
     93        attributeMap: {id:"", dir:"", lang:"", "class":"", style:"", title:"", 
     94                onClick: "", 
     95                onDblClick: "", 
     96                onKeyDown: "", 
     97                onKeyPress: "", 
     98                onKeyUp: "", 
     99                onMouseMove: "", 
     100                onMouseDown: "", 
     101                onMouseOut: "", 
     102                onMouseOver: "", 
     103                onMouseLeave: "", 
     104                onMouseEnter: "", 
     105                onMouseUp: ""}, 
     106 
     107        onClick: dijit._connectOnUseEventHandler, 
     108        /*===== 
     109        onClick: function(event){ 
     110                // summary:  
     111                //      Connect to this function to receive notifications of mouse click events. 
     112                //      event: mouse Event 
     113        }, 
     114        =====*/ 
     115        onDblClick: dijit._connectOnUseEventHandler, 
     116        /*===== 
     117        onDblClick: function(event){ 
     118                // summary:  
     119                //      Connect to this function to receive notifications of mouse double click events. 
     120                //      event: mouse Event 
     121        }, 
     122        =====*/ 
     123        onKeyDown: dijit._connectOnUseEventHandler, 
     124        /*===== 
     125        onKeyDown: function(event){ 
     126                // summary:  
     127                //      Connect to this function to receive notifications of keys being pressed down. 
     128                //      event: key Event 
     129        }, 
     130        =====*/ 
     131        onKeyPress: dijit._connectOnUseEventHandler, 
     132        /*===== 
     133        onKeyPress: function(event){ 
     134                // summary:  
     135                //      Connect to this function to receive notifications of printable keys being typed. 
     136                //      event: key Event 
     137        }, 
     138        =====*/ 
     139        onKeyUp: dijit._connectOnUseEventHandler, 
     140        /*===== 
     141        onKeyUp: function(event){ 
     142                // summary:  
     143                //      Connect to this function to receive notifications of keys being released. 
     144                //      event: key Event 
     145        }, 
     146        =====*/ 
     147        onMouseDown: dijit._connectOnUseEventHandler, 
     148        /*===== 
     149        onMouseDown: function(event){ 
     150                // summary:  
     151                //      Connect to this function to receive notifications of when the mouse button is pressed down. 
     152                //      event: mouse Event 
     153        }, 
     154        =====*/ 
     155        onMouseMove: dijit._connectOnUseEventHandler, 
     156        /*===== 
     157        onMouseMove: function(event){ 
     158                // summary:  
     159                //      Connect to this function to receive notifications of when the mouse moves over nodes contained within this widget. 
     160                //      event: mouse Event 
     161        }, 
     162        =====*/ 
     163        onMouseOut: dijit._connectOnUseEventHandler, 
     164        /*===== 
     165        onMouseOut: function(event){ 
     166                // summary:  
     167                //      Connect to this function to receive notifications of when the mouse moves off of nodes contained within this widget. 
     168                //      event: mouse Event 
     169        }, 
     170        =====*/ 
     171        onMouseOver: dijit._connectOnUseEventHandler, 
     172        /*===== 
     173        onMouseOver: function(event){ 
     174                // summary:  
     175                //      Connect to this function to receive notifications of when the mouse moves onto nodes contained within this widget. 
     176                //      event: mouse Event 
     177        }, 
     178        =====*/ 
     179        onMouseLeave: dijit._connectOnUseEventHandler, 
     180        /*===== 
     181        onMouseLeave: function(event){ 
     182                // summary:  
     183                //      Connect to this function to receive notifications of when the mouse moves off of this widget. 
     184                //      event: mouse Event 
     185        }, 
     186        =====*/ 
     187        onMouseEnter: dijit._connectOnUseEventHandler, 
     188        /*===== 
     189        onMouseEnter: function(event){ 
     190                // summary:  
     191                //      Connect to this function to receive notifications of when the mouse moves onto this widget. 
     192                //      event: mouse Event 
     193        }, 
     194        =====*/ 
     195        onMouseUp: dijit._connectOnUseEventHandler, 
     196        /*===== 
     197        onMouseUp: function(event){ 
     198                // summary:  
     199                //      Connect to this function to receive notifications of when the mouse button is released. 
     200                //      event: mouse Event 
     201        }, 
     202        =====*/ 
    85203 
    86204        // Constants used in templates 
     
    92210        postscript: function(/*Object?*/params, /*DomNode|String*/srcNodeRef){ 
    93211                this.create(params, srcNodeRef); 
     212        }, 
     213 
     214        constructor: function(){ 
     215                this._onUseEvents = []; // list of events that needs to be connected on first use 
     216                for(var attr in this.attributeMap){ 
     217                        if(this[attr] === dijit._connectOnUseEventHandler){ 
     218                                this._onUseEvents[attr] = true; 
     219                        } 
     220                } 
    94221        }, 
    95222 
     
    158285                        for(var attr in this.attributeMap){ 
    159286                                var value = this[attr]; 
    160                                 if(typeof value != "object" && ((value !== "" && value !== false) || (params && params[attr]))){ 
     287                                if(value !== dijit._connectOnUseEventHandler && (typeof value != "undefined") && (typeof value != "object") && ((value !== "" && value !== false) || (params && params[attr]))){ 
    161288                                        this.setAttribute(attr, value); 
    162289                                } 
     
    319446        _onBlur: function(){ 
    320447                this.onBlur(); 
     448        }, 
     449 
     450        _onConnect: function(/*Widget*/ widget, /*String*/ event){ 
     451                if(widget && (typeof widget == "object") && widget._onUseEvents && widget._onUseEvents[event]){ 
     452                        widget.setAttribute(event, widget[event]); 
     453                } 
    321454        }, 
    322455 
     
    345478                                break; 
    346479                        default: 
    347                                 if(/^on[A-Z]/.test(attr)){ // eg. onSubmit needs to be onsubmit 
     480                                if(dojo.isFunction(value)){ // functions execute in the context of the widget 
     481                                        value = dojo.hitch(this, value); 
     482                                } 
     483                                if(/^on[A-Z][a-zA-Z]*$/.test(attr)){ // eg. onSubmit needs to be onsubmit 
     484                                        if(this._onUseEvents[attr]){ 
     485                                                delete this._onUseEvents[attr]; 
     486                                        } 
    348487                                        attr = attr.toLowerCase(); 
    349                                 } 
    350                                 if(typeof value == "function"){ // functions execute in the context of the widget 
    351                                         value = dojo.hitch(this, value); 
    352488                                } 
    353489                                dojo.attr(mapNode, attr, value);