jQuery: The Write Less, Do More JavaScript Library

Changeset 5357

Show
Ignore:
Timestamp:
04/29/08 23:34:50 (3 months ago)
Author:
aflesler
Message:

mainly made the code shorter:
- removed some needless if's
- replace multiple "var x" for one, comma separated declaration.
- added a local fn called now() for the (new Date)s
- fixed the indentation of a block, and a typo in a comment.
- used fn instead of prototype where possible
- jquery fx: exposed the speeds hash as jQuery.fx.speeds.

Also fixed (again) line endings

Location:
trunk/jquery/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/ajax.js

    r5282 r5357  
    9292}); 
    9393 
    94 var jsc = (new Date).getTime(); 
     94var jsc = now(); 
    9595 
    9696jQuery.extend({ 
     
    212212 
    213213        if ( s.cache === false && s.type.toLowerCase() == "get" ) { 
    214             var ts = (new Date()).getTime(); 
     214            var ts = now(); 
    215215            // try replacing _= if it is there 
    216216            var ret = s.url.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2"); 
     
    449449 
    450450    httpData: function( r, type ) { 
    451         var ct = r.getResponseHeader("content-type"); 
    452         var xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0; 
    453         var data = xml ? r.responseXML : r.responseText; 
     451        var ct = r.getResponseHeader("content-type"), 
     452            xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0, 
     453            data = xml ? r.responseXML : r.responseText; 
    454454 
    455455        if ( xml && data.documentElement.tagName == "parsererror" ) 
  • trunk/jquery/src/core.js

    r5349 r5357  
    1111 
    1212// Map over jQuery in case of overwrite 
    13 if ( window.jQuery ) 
    14     var _jQuery = window.jQuery; 
    15  
    16 var jQuery = window.jQuery = function( selector, context ) { 
     13var _jQuery = window.jQuery, 
     14// Map over the $ in case of overwrite   
     15    _$ = window.$; 
     16 
     17var jQuery = window.jQuery = window.$ = function( selector, context ) { 
    1718    // The jQuery object is actually just the init constructor 'enhanced' 
    18     return new jQuery.prototype.init( selector, context ); 
     19    return new jQuery.fn.init( selector, context ); 
    1920}; 
    20  
    21 // Map over the $ in case of overwrite 
    22 if ( window.$ ) 
    23     var _$ = window.$; 
    24      
    25 // Map the jQuery namespace to the '$' one 
    26 window.$ = jQuery; 
    2721 
    2822// A simple way to check for HTML strings or ID strings 
    2923// (both of which we optimize for) 
    30 var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/; 
     24var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/, 
    3125 
    3226// Is it a simple selector 
    33 var isSimple = /^.[^:#\[\.]*$/; 
     27    isSimple = /^.[^:#\[\.]*$/; 
    3428 
    3529jQuery.fn = jQuery.prototype = { 
     
    360354 
    361355    is: function( selector ) { 
    362         return selector ? 
    363             jQuery.multiFilter( selector, this ).length > 0 : 
    364             false; 
     356        return !!selector && jQuery.multiFilter( selector, this ).length > 0; 
    365357    }, 
    366358 
     
    537529 
    538530// Give the init function the jQuery prototype for later instantiation 
    539 jQuery.prototype.init.prototype = jQuery.prototype; 
     531jQuery.fn.init.prototype = jQuery.fn; 
    540532 
    541533function evalScript( i, elem ) { 
     
    552544    if ( elem.parentNode ) 
    553545        elem.parentNode.removeChild( elem ); 
     546} 
     547 
     548function now(){ 
     549    return +new Date; 
    554550} 
    555551 
     
    599595}; 
    600596 
    601 var expando = "jQuery" + (new Date()).getTime(), uuid = 0, windowData = {}; 
     597var expando = "jQuery" + now(), uuid = 0, windowData = {}, 
    602598 
    603599// exclude the following css properties to add px 
    604 var exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i; 
     600    exclude = /z-?index|font-?weight|opacity|zoom|line-?height/i, 
    605601// cache getComputedStyle 
    606 var getComputedStyle = document.defaultView && document.defaultView.getComputedStyle; 
     602    getComputedStyle = document.defaultView && document.defaultView.getComputedStyle; 
    607603 
    608604jQuery.extend({ 
     
    875871            // then some display: none elements are involved 
    876872            else { 
    877                 var swap = [], stack = []; 
     873                var swap = [], stack = [], a = elem, i = 0; 
    878874 
    879875                // Locate all of the parent display: none elements 
    880                 for ( var a = elem; a && color(a); a = a.parentNode ) 
     876                for ( ; a && color(a); a = a.parentNode ) 
    881877                    stack.unshift(a); 
    882878 
    883879                // Go through and make them visible, but in reverse 
    884880                // (It would be better if we knew the exact display type that they had) 
    885                 for ( var i = 0; i < stack.length; i++ ) 
     881                for ( ; i < stack.length; i++ ) 
    886882                    if ( color( stack[ i ] ) ) { 
    887883                        swap[ i ] = stack[ i ].style.display; 
     
    896892 
    897893                // Finally, revert the display styles back 
    898                 for ( var i = 0; i < swap.length; i++ ) 
     894                for ( i = 0; i < swap.length; i++ ) 
    899895                    if ( swap[ i ] != null ) 
    900896                        stack[ i ].style.display = swap[ i ]; 
     
    947943 
    948944            if ( elem.constructor == Number ) 
    949                 elem = elem.toString(); 
     945                elem += ''; 
    950946             
    951947            // Convert html string into DOM nodes 
  • trunk/jquery/src/event.js

    r5352 r5357  
    1414        // For whatever reason, IE has trouble passing the window object 
    1515        // around, causing it to be cloned in the process 
    16         if ( jQuery.browser.msie && elem.setInterval != undefined ) 
     16        if ( jQuery.browser.msie && elem.setInterval ) 
    1717            elem = window; 
    1818 
     
    5252        handle.elem = elem; 
    5353             
    54             // Handle multiple events seperated by a space 
    55             // jQuery(...).bind("mouseover mouseout", fn); 
    56             jQuery.each(types.split(/\s+/), function(index, type) { 
    57                 // Namespaced event handlers 
    58                 var parts = type.split("."); 
    59                 type = parts[0]; 
    60                 handler.type = parts[1]; 
    61  
    62                 // Get the current list of functions bound to this event 
    63                 var handlers = events[type]; 
    64  
    65                 // Init the event handler queue 
    66                 if (!handlers) { 
    67                     handlers = events[type] = {}; 
    68          
    69                     // Check for a special event handler 
    70                     // Only use addEventListener/attachEvent if the special 
    71                     // events handler returns false 
    72                     if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) { 
    73                         // Bind the global event handler to the element 
    74                         if (elem.addEventListener) 
    75                             elem.addEventListener(type, handle, false); 
    76                         else if (elem.attachEvent) 
    77                             elem.attachEvent("on" + type, handle); 
    78                     } 
     54        // Handle multiple events separated by a space 
     55        // jQuery(...).bind("mouseover mouseout", fn); 
     56        jQuery.each(types.split(/\s+/), function(index, type) { 
     57            // Namespaced event handlers 
     58            var parts = type.split("."); 
     59            type = parts[0]; 
     60            handler.type = parts[1]; 
     61 
     62            // Get the current list of functions bound to this event 
     63            var handlers = events[type]; 
     64 
     65            // Init the event handler queue 
     66            if (!handlers) { 
     67                handlers = events[type] = {}; 
     68     
     69                // Check for a special event handler 
     70                // Only use addEventListener/attachEvent if the special 
     71                // events handler returns false 
     72                if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem) === false ) { 
     73                    // Bind the global event handler to the element 
     74                    if (elem.addEventListener) 
     75                        elem.addEventListener(type, handle, false); 
     76                    else if (elem.attachEvent) 
     77                        elem.attachEvent("on" + type, handle); 
    7978                } 
    80  
    81                 // Add the function to the element's handler list 
    82                 handlers[handler.guid] = handler; 
    83  
    84                 // Keep track of which events have been used, for global triggering 
    85                 jQuery.event.global[type] = true; 
    86             }); 
     79            } 
     80 
     81            // Add the function to the element's handler list 
     82            handlers[handler.guid] = handler; 
     83 
     84            // Keep track of which events have been used, for global triggering 
     85            jQuery.event.global[type] = true; 
     86        }); 
    8787         
    8888        // Nullify elem to prevent memory leaks in IE 
     
    191191                    preventDefault: function(){},  
    192192                    stopPropagation: function(){},  
    193                     timeStamp: +new Date 
     193                    timeStamp: now() 
    194194                }); 
    195195                data[0][expando] = true; // no need to fix fake event 
     
    310310         
    311311        // Fix timeStamp 
    312         event.timeStamp = event.timeStamp || +new Date; 
     312        event.timeStamp = event.timeStamp || now(); 
    313313         
    314314        // Fix target property, if necessary 
     
    381381                if ( withinElement(event, this) ) return true; 
    382382                // Execute the right handlers by setting the event type to mouseenter 
    383                 arguments[0].type = "mouseenter"; 
     383                event.type = "mouseenter"; 
    384384                return jQuery.event.handle.apply(this, arguments); 
    385385            } 
     
    403403                if ( withinElement(event, this) ) return true; 
    404404                // Execute the right handlers by setting the event type to mouseleave 
    405                 arguments[0].type = "mouseleave"; 
     405                event.type = "mouseleave"; 
    406406                return jQuery.event.handle.apply(this, arguments); 
    407407            } 
     
    440440 
    441441    triggerHandler: function( type, data, fn ) { 
    442         if ( this[0] ) 
    443             return jQuery.event.trigger( type, data, this[0], false, fn ); 
    444         return undefined; 
     442        return this[0] && jQuery.event.trigger( type, data, this[0], false, fn ); 
    445443    }, 
    446444 
  • trunk/jquery/src/fx.js

    r5350 r5357  
    7777                return false; 
    7878 
    79             var opt = jQuery.extend({}, optall); 
    80             var hidden = jQuery(this).is(":hidden"), self = this; 
     79            var opt = jQuery.extend({}, optall), p, 
     80                hidden = jQuery(this).is(":hidden"), self = this; 
    8181             
    82             for ( var p in prop ) { 
     82            for ( p in prop ) { 
    8383                if ( prop[p] == "hide" && hidden || prop[p] == "show" && !hidden ) 
    8484                    return jQuery.isFunction(opt.complete) && opt.complete.apply(this); 
     
    181181 
    182182var queue = function( elem, type, array ) { 
    183     if ( !elem ) 
    184         return undefined; 
    185  
    186     type = type || "fx"; 
    187  
    188     var q = jQuery.data( elem, type + "queue" ); 
    189  
    190     if ( !q || array ) 
    191         q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) ); 
    192  
     183    if ( elem ){ 
     184     
     185        type = type || "fx"; 
     186     
     187        var q = jQuery.data( elem, type + "queue" ); 
     188     
     189        if ( !q || array ) 
     190            q = jQuery.data( elem, type + "queue", jQuery.makeArray(array) ); 
     191 
     192    } 
    193193    return q; 
    194194}; 
     
    219219        opt.duration = (opt.duration && opt.duration.constructor == Number ?  
    220220            opt.duration :  
    221             { slow: 600, fast: 200 }[opt.duration]) || 400; 
     221            jQuery.fx.speeds[opt.duration]) || 400; 
    222222     
    223223        // Queueing 
     
    281281    // Start an animation from one number to another 
    282282    custom: function(from, to, unit){ 
    283         this.startTime = (new Date()).getTime(); 
     283        this.startTime = now(); 
    284284        this.start = from; 
    285285        this.end = to; 
     
    344344    // Each step of an animation 
    345345    step: function(gotoEnd){ 
    346         var t = (new Date()).getTime(); 
     346        var t = now(); 
    347347 
    348348        if ( gotoEnd || t > this.options.duration + this.startTime ) { 
     
    402402}; 
    403403 
    404 jQuery.fx.step = { 
    405     scrollLeft: function(fx){ 
    406         fx.elem.scrollLeft = fx.now; 
    407     }, 
    408  
    409     scrollTop: function(fx){ 
    410         fx.elem.scrollTop = fx.now; 
    411     }, 
    412  
    413     opacity: function(fx){ 
    414         jQuery.attr(fx.elem.style, "opacity", fx.now); 
    415     }, 
    416  
    417     _default: function(fx){ 
    418         fx.elem.style[ fx.prop ] = fx.now + fx.unit; 
     404jQuery.extend( jQuery.fx, { 
     405    speeds:{ 
     406        slow: 600,   
     407        fast: 200   
     408    }, 
     409    step: { 
     410        scrollLeft: function(fx){ 
     411            fx.elem.scrollLeft = fx.now; 
     412        }, 
     413     
     414        scrollTop: function(fx){ 
     415            fx.elem.scrollTop = fx.now; 
     416        }, 
     417     
     418        opacity: function(fx){ 
     419            jQuery.attr(fx.elem.style, "opacity", fx.now); 
     420        }, 
     421     
     422        _default: function(fx){ 
     423            fx.elem.style[ fx.prop ] = fx.now + fx.unit; 
     424        } 
    419425    } 
    420 }; 
     426}); 
  • trunk/jquery/src/selector.js

    r5354 r5357  
    115115            t = jQuery.trim(t); 
    116116 
    117             var foundToken = false; 
     117            var foundToken = false, 
    118118 
    119119            // An attempt at speeding up child selectors that 
    120120            // point to a specific element tag 
    121             var re = quickChild; 
    122             var m = re.exec(t); 
     121                re = quickChild, 
     122                 
     123                m = re.exec(t); 
    123124 
    124125            if ( m ) { 
     
    417418 
    418419    dir: function( elem, dir ){ 
    419         var matched = []; 
    420         var cur = elem[dir]; 
     420        var matched = [], 
     421            cur = elem[dir]; 
    421422        while ( cur && cur != document ) { 
    422423            if ( cur.nodeType == 1 ) 
     
    450451}); 
    451452 
     453