X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=jquery%2Fjquery.js;h=352b849ad2a3b283681e7a6437caba53fe22768c;hb=0a9c5698398f7a8347d2d5956c9d11a3eb2933d6;hp=c24e7949ea5887b469a2b42520d6ac91e4f606b7;hpb=f197c7469f43c02bcbc65f2f41e2b419f0379700;p=jquery.git diff --git a/jquery/jquery.js b/jquery/jquery.js index c24e794..352b849 100644 --- a/jquery/jquery.js +++ b/jquery/jquery.js @@ -12,50 +12,60 @@ // Global undefined variable window.undefined = window.undefined; -// Map over the $ in case of overwrite -if ( $ ) var _$ = $; - /** * Create a new jQuery Object * @constructor */ -var $ = jQuery = function(a,c) { +function jQuery(a,c) { /* - * Handle support for overriding other $() functions. Way too many libraries - * provide this function to simply ignore it and overwrite it. - */ + * Handle support for overriding other $() functions. Way too many libraries + * provide this function to simply ignore it and overwrite it. + */ // Check to see if this is a possible collision case - if ( _$ && !c && ( a.constructor == String && + if ( jQuery._$ && !c && a.constructor == String && // Make sure that the expression is a colliding one !/[^a-zA-Z0-9_-]/.test(a) && // and that there are no elements that match it // (this is the one truly ambiguous case) - !document.getElementsByTagName(a).length ) || - - // Watch for an array being passed in (Prototype 1.5) - a.constructor == Array ) + !document.getElementsByTagName(a).length ) // Use the default method, in case it works some voodoo - return _$( a ); + return jQuery._$( a ); + + // Make sure t hat a selection was provided + a = a || jQuery.context || document; - // Watch for when a jQuery object is passed in as an arg - if ( a && a.jquery ) + // Watch for when a jQuery object is passed as the selector + if ( a.jquery ) return a; + + // Watch for when a jQuery object is passed at the context + if ( c && c.jquery ) + return $(c.get()).find(a); // If the context is global, return a new object if ( window == this ) return new jQuery(a,c); - - // Find the matching elements and save them for later - this.cur = jQuery.Select( - a || jQuery.context || document, - c && c.jquery && c.get(0) || c - ); + + // Watch for when an array is passed in + if ( a.constructor == Array ) + // Assume that it's an array of DOM Elements + this.cur = a; + else + // Find the matching elements and save them for later + this.cur = jQuery.Select( a, c ); } +// Map over the $ in case of overwrite +if ( $ ) + jQuery._$ = $; + +// Map the jQuery namespace to the '$' one +var $ = jQuery; + jQuery.fn = jQuery.prototype = { /** * The current SVN version of jQuery. @@ -291,7 +301,15 @@ jQuery.fn = jQuery.prototype = { }, filter: function(t) { - return this.pushStack( jQuery.filter(t,this.cur).r ); + if ( /,/.test(t) ) { + var p = t.split(/\s*,\s*/); + return this.pushStack( $.map(this.cur,function(a){ + for ( var i = 0; i < p.length; i++ ) + if ( jQuery.filter(p[i],[a]).r.length ) + return a; + }) ); + } else + return this.pushStack( jQuery.filter(t,this.cur).r ); }, not: function(t) { return this.pushStack( t.constructor == String ? @@ -345,6 +363,22 @@ jQuery.fn = jQuery.prototype = { } }; +/** + * Similar to the Prototype $A() function, only it allows you to + * forcefully pass array-like structures into $(). + */ +jQuery.A = function(a){ + // Create a temporary, clean, array + var r = []; + + // and copy the old array contents over to it + for ( var i = 0; i < a.length; i++ ) + r.push( a[i] ); + + // Return the sane jQuery object + return $(r); +}; + jQuery.className = { add: function(o,c){ if (jQuery.hasWord(o,c)) return; @@ -372,42 +406,39 @@ jQuery.className = { jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" ); })(); +$.swap = function(e,o,f) { + for ( var i in o ) { + e.style["old"+i] = e.style[i]; + e.style[i] = o[i]; + } + f.apply( e, [] ); + for ( var i in o ) + e.style[i] = e.style["old"+i]; +}; + jQuery.css = function(e,p) { // Adapted from Prototype 1.4.0 if ( p == "height" || p == "width" ) { + var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"]; - // Handle extra width/height provided by the W3C box model - var ph = (!jQuery.boxModel ? 0 : - jQuery.css(e,"paddingTop") + jQuery.css(e,"paddingBottom") + - jQuery.css(e,"borderTopWidth") + jQuery.css(e,"borderBottomWidth")) || 0; - - var pw = (!jQuery.boxModel ? 0 : - jQuery.css(e,"paddingLeft") + jQuery.css(e,"paddingRight") + - jQuery.css(e,"borderLeftWidth") + jQuery.css(e,"borderRightWidth")) || 0; - - var oHeight, oWidth; - - if (jQuery.css(e,"display") != 'none') { - oHeight = e.offsetHeight || parseInt(e.style.height) || 0; - oWidth = e.offsetWidth || parseInt(e.style.width) || 0; - } else { - var els = e.style; - var ov = els.visibility; - var op = els.position; - var od = els.display; - els.visibility = "hidden"; - els.position = "absolute"; - els.display = ""; - oHeight = e.clientHeight || parseInt(e.style.height); - oWidth = e.clientWidth || parseInt(e.style.width); - els.display = od; - els.position = op; - els.visibility = ov; + for ( var i in d ) { + old["padding" + d[i]] = 0; + old["border" + d[i] + "Width"] = 0; } - return p == "height" ? - (oHeight - ph < 0 ? 0 : oHeight - ph) : - (oWidth - pw < 0 ? 0 : oWidth - pw); + $.swap( e, old, function() { + if (jQuery.css(e,"display") != 'none') { + oHeight = e.offsetHeight; + oWidth = e.offsetWidth; + } else + $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' }, + function(){ + oHeight = e.clientHeight; + oWidth = e.clientWidth; + }); + }); + + return p == "height" ? oHeight : oWidth; } var r; @@ -422,7 +453,7 @@ jQuery.css = function(e,p) { r = s ? s.getPropertyValue(p) : null; } - return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r; + return r; }; jQuery.clean = function(a) { @@ -520,8 +551,8 @@ jQuery.token = [ jQuery.Select = function( t, context ) { context = context || jQuery.context || document; - if ( t.constructor != String ) - return t.constructor == Array ? t : [t]; + + if ( t.constructor != String ) return [t]; if ( !t.indexOf("//") ) { context = context.documentElement; @@ -780,10 +811,15 @@ jQuery.event = { handlers[0] = element["on" + type]; } handlers[handler.guid] = handler; - element["on" + type] = jQuery.event.handle; + element["on" + type] = this.handle; + + if (!this.global[type]) + this.global[type] = []; + this.global[type].push( element ); }, guid: 1, + global: {}, // Detach an event or set of events from an element remove: function(element, type, handler) { @@ -796,13 +832,28 @@ jQuery.event = { delete element.events[type][i]; else for ( var j in element.events ) - jQuery.event.remove( element, j ); + this.remove( element, j ); }, - trigger: function(element,type,data) { - data = data || [ jQuery.event.fix({ type: type }) ]; - if ( element && element["on" + type] ) + trigger: function(type,data,element) { + // Touch up the incoming data + data = data || []; + + // Handle a global trigger + if ( !element ) { + var g = this.global[type]; + if ( g ) + for ( var i = 0; i < g.length; i++ ) + this.trigger( type, data, g[i] ); + + // Handle triggering a single element + } else if ( element["on" + type] ) { + // Pass along a fake event + data.unshift( this.fix({ type: type, target: element }) ); + + // Trigger the event element["on" + type].apply( element, data ); + } }, handle: function(event) {