Admin bar: use jQuery if loaded to improve UX, fixes #18299

git-svn-id: http://svn.automattic.com/wordpress/trunk@18488 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2011-07-29 22:57:30 +00:00
parent 7e94f474cd
commit 670980920d
5 changed files with 157 additions and 135 deletions

View File

@ -89,7 +89,7 @@ class WP_Admin_Bar {
function render() {
?>
<div id="wpadminbar">
<div id="wpadminbar" class="nojq">
<div class="quicklinks">
<ul>
<?php foreach ( (array) $this->menu as $id => $menu_item ) : ?>

File diff suppressed because one or more lines are too long

View File

@ -133,7 +133,7 @@
text-shadow: #666 0px -1px 0px;
}
#wpadminbar .quicklinks li:hover > ul,
#wpadminbar.nojq .quicklinks li:hover > ul,
#wpadminbar .quicklinks li.hover > ul {
display: block;
}

View File

@ -1,140 +1,162 @@
(function(d, w) {
var addEvent = function( obj, type, fn ) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else if (obj.attachEvent)
obj.attachEvent('on' + type, function() { return fn.call(obj, window.event);});
},
// use jQuery and hoverIntent if loaded
if ( typeof(jQuery) != 'undefined' ) {
if ( typeof(jQuery.fn.hoverIntent) == 'undefined' )
(function(a){a.fn.hoverIntent=function(l,j){var m={sensitivity:7,interval:100,timeout:0};m=a.extend(m,j?{over:l,out:j}:l);var o,n,h,d;var e=function(f){o=f.pageX;n=f.pageY};var c=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);if((Math.abs(h-o)+Math.abs(d-n))<m.sensitivity){a(f).unbind("mousemove",e);f.hoverIntent_s=1;return m.over.apply(f,[g])}else{h=o;d=n;f.hoverIntent_t=setTimeout(function(){c(g,f)},m.interval)}};var i=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);f.hoverIntent_s=0;return m.out.apply(f,[g])};var b=function(q){var f=this;var g=(q.type=="mouseover"?q.fromElement:q.toElement)||q.relatedTarget;while(g&&g!=this){try{g=g.parentNode}catch(q){g=this}}if(g==this){if(a.browser.mozilla){if(q.type=="mouseout"){f.mtout=setTimeout(function(){k(q,f)},30)}else{if(f.mtout){f.mtout=clearTimeout(f.mtout)}}}return}else{if(f.mtout){f.mtout=clearTimeout(f.mtout)}k(q,f)}};var k=function(p,f){var g=jQuery.extend({},p);if(f.hoverIntent_t){f.hoverIntent_t=clearTimeout(f.hoverIntent_t)}if(p.type=="mouseover"){h=g.pageX;d=g.pageY;a(f).bind("mousemove",e);if(f.hoverIntent_s!=1){f.hoverIntent_t=setTimeout(function(){c(g,f)},m.interval)}}else{a(f).unbind("mousemove",e);if(f.hoverIntent_s==1){f.hoverIntent_t=setTimeout(function(){i(g,f)},m.timeout)}}};return this.mouseover(b).mouseout(b)}})(jQuery);
aB, hc = new RegExp('\\bhover\\b', 'g'), q = [],
rselected = new RegExp('\\bselected\\b', 'g'),
/**
* Get the timeout ID of the given element
*/
getTOID = function(el) {
var i = q.length;
while( i-- )
if ( q[i] && el == q[i][1] )
return q[i][0];
return false;
},
addHoverClass = function(t) {
var i, id, inA, hovering, ul, li,
ancestors = [],
ancestorLength = 0;
while ( t && t != aB && t != d ) {
if( 'LI' == t.nodeName.toUpperCase() ) {
ancestors[ ancestors.length ] = t;
id = getTOID(t);
if ( id )
clearTimeout( id );
t.className = t.className ? ( t.className.replace(hc, '') + ' hover' ) : 'hover';
hovering = t;
jQuery(document).ready(function($){
$('#wpadminbar').removeClass('nojq').find('li.menupop').hoverIntent({
over: function(e){
$(this).addClass('hover');
},
out: function(e){
$(this).removeClass('hover');
},
timeout: 200,
sensitivity: 7,
interval: 120
});
});
} else {
(function(d, w) {
var addEvent = function( obj, type, fn ) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else if (obj.attachEvent)
obj.attachEvent('on' + type, function() { return fn.call(obj, window.event);});
},
aB, hc = new RegExp('\\bhover\\b', 'g'), q = [],
rselected = new RegExp('\\bselected\\b', 'g'),
/**
* Get the timeout ID of the given element
*/
getTOID = function(el) {
var i = q.length;
while( i-- )
if ( q[i] && el == q[i][1] )
return q[i][0];
return false;
},
addHoverClass = function(t) {
var i, id, inA, hovering, ul, li,
ancestors = [],
ancestorLength = 0;
while ( t && t != aB && t != d ) {
if( 'LI' == t.nodeName.toUpperCase() ) {
ancestors[ ancestors.length ] = t;
id = getTOID(t);
if ( id )
clearTimeout( id );
t.className = t.className ? ( t.className.replace(hc, '') + ' hover' ) : 'hover';
hovering = t;
}
t = t.parentNode;
}
t = t.parentNode;
}
// Remove any selected classes.
if ( hovering && hovering.parentNode ) {
ul = hovering.parentNode;
if ( ul && 'UL' == ul.nodeName.toUpperCase() ) {
i = ul.childNodes.length;
while ( i-- ) {
li = ul.childNodes[i];
if ( li != hovering )
li.className = li.className ? li.className.replace( rselected, '' ) : '';
// Remove any selected classes.
if ( hovering && hovering.parentNode ) {
ul = hovering.parentNode;
if ( ul && 'UL' == ul.nodeName.toUpperCase() ) {
i = ul.childNodes.length;
while ( i-- ) {
li = ul.childNodes[i];
if ( li != hovering )
li.className = li.className ? li.className.replace( rselected, '' ) : '';
}
}
}
}
/* remove the hover class for any objects not in the immediate element's ancestry */
i = q.length;
while ( i-- ) {
inA = false;
ancestorLength = ancestors.length;
while( ancestorLength-- ) {
if ( ancestors[ ancestorLength ] == q[i][1] )
inA = true;
/* remove the hover class for any objects not in the immediate element's ancestry */
i = q.length;
while ( i-- ) {
inA = false;
ancestorLength = ancestors.length;
while( ancestorLength-- ) {
if ( ancestors[ ancestorLength ] == q[i][1] )
inA = true;
}
if ( ! inA )
q[i][1].className = q[i][1].className ? q[i][1].className.replace(hc, '') : '';
}
if ( ! inA )
q[i][1].className = q[i][1].className ? q[i][1].className.replace(hc, '') : '';
}
},
removeHoverClass = function(t) {
while ( t && t != aB && t != d ) {
if( 'LI' == t.nodeName.toUpperCase() ) {
(function(t) {
var to = setTimeout(function() {
t.className = t.className ? t.className.replace(hc, '') : '';
}, 500);
q[q.length] = [to, t];
})(t);
},
removeHoverClass = function(t) {
while ( t && t != aB && t != d ) {
if( 'LI' == t.nodeName.toUpperCase() ) {
(function(t) {
var to = setTimeout(function() {
t.className = t.className ? t.className.replace(hc, '') : '';
}, 500);
q[q.length] = [to, t];
})(t);
}
t = t.parentNode;
}
t = t.parentNode;
}
},
clickShortlink = function(e) {
var i, l, node,
t = e.target || e.srcElement;
// Make t the shortlink menu item, or return.
while ( true ) {
// Check if we've gone past the shortlink node,
// or if the user is clicking on the input.
if ( ! t || t == d || t == aB )
return;
// Check if we've found the shortlink node.
if ( t.id && t.id == 'wp-admin-bar-get-shortlink' )
break;
t = t.parentNode;
}
// IE doesn't support preventDefault, and does support returnValue
if ( e.preventDefault )
e.preventDefault();
e.returnValue = false;
if ( -1 == t.className.indexOf('selected') )
t.className += ' selected';
for ( i = 0, l = t.childNodes.length; i < l; i++ ) {
node = t.childNodes[i];
if ( node.className && -1 != node.className.indexOf('shortlink-input') ) {
node.focus();
node.select();
node.onblur = function() {
t.className = t.className ? t.className.replace( rselected, '' ) : '';
};
break;
},
clickShortlink = function(e) {
var i, l, node,
t = e.target || e.srcElement;
// Make t the shortlink menu item, or return.
while ( true ) {
// Check if we've gone past the shortlink node,
// or if the user is clicking on the input.
if ( ! t || t == d || t == aB )
return;
// Check if we've found the shortlink node.
if ( t.id && t.id == 'wp-admin-bar-get-shortlink' )
break;
t = t.parentNode;
}
}
return false;
};
// IE doesn't support preventDefault, and does support returnValue
if ( e.preventDefault )
e.preventDefault();
e.returnValue = false;
if ( -1 == t.className.indexOf('selected') )
t.className += ' selected';
for ( i = 0, l = t.childNodes.length; i < l; i++ ) {
node = t.childNodes[i];
if ( node.className && -1 != node.className.indexOf('shortlink-input') ) {
node.focus();
node.select();
node.onblur = function() {
t.className = t.className ? t.className.replace( rselected, '' ) : '';
};
break;
}
}
return false;
};
addEvent(w, 'load', function() {
aB = d.getElementById('wpadminbar');
if ( d.body && aB ) {
d.body.appendChild( aB );
addEvent(aB, 'mouseover', function(e) {
addHoverClass( e.target || e.srcElement );
});
addEvent(aB, 'mouseout', function(e) {
removeHoverClass( e.target || e.srcElement );
});
addEvent(aB, 'click', clickShortlink );
}
if ( w.location.hash )
w.scrollBy(0,-32);
});
})(document, window);
addEvent(w, 'load', function() {
aB = d.getElementById('wpadminbar');
}
if ( d.body && aB ) {
d.body.appendChild( aB );
addEvent(aB, 'mouseover', function(e) {
addHoverClass( e.target || e.srcElement );
});
addEvent(aB, 'mouseout', function(e) {
removeHoverClass( e.target || e.srcElement );
});
addEvent(aB, 'click', clickShortlink );
}
if ( w.location.hash )
w.scrollBy(0,-32);
});
})(document, window);

View File

@ -1 +1 @@
(function(i,k){var c=function(n,m,d){if(n.addEventListener){n.addEventListener(m,d,false)}else{if(n.attachEvent){n.attachEvent("on"+m,function(){return d.call(n,window.event)})}}},e,f=new RegExp("\\bhover\\b","g"),a=[],j=new RegExp("\\bselected\\b","g"),g=function(m){var d=a.length;while(d--){if(a[d]&&m==a[d][1]){return a[d][0]}}return false},h=function(s){var n,d,q,m,p,r,u=[],o=0;while(s&&s!=e&&s!=i){if("LI"==s.nodeName.toUpperCase()){u[u.length]=s;d=g(s);if(d){clearTimeout(d)}s.className=s.className?(s.className.replace(f,"")+" hover"):"hover";m=s}s=s.parentNode}if(m&&m.parentNode){p=m.parentNode;if(p&&"UL"==p.nodeName.toUpperCase()){n=p.childNodes.length;while(n--){r=p.childNodes[n];if(r!=m){r.className=r.className?r.className.replace(j,""):""}}}}n=a.length;while(n--){q=false;o=u.length;while(o--){if(u[o]==a[n][1]){q=true}}if(!q){a[n][1].className=a[n][1].className?a[n][1].className.replace(f,""):""}}},l=function(d){while(d&&d!=e&&d!=i){if("LI"==d.nodeName.toUpperCase()){(function(m){var n=setTimeout(function(){m.className=m.className?m.className.replace(f,""):""},500);a[a.length]=[n,m]})(d)}d=d.parentNode}},b=function(p){var n,d,o,m=p.target||p.srcElement;while(true){if(!m||m==i||m==e){return}if(m.id&&m.id=="wp-admin-bar-get-shortlink"){break}m=m.parentNode}if(p.preventDefault){p.preventDefault()}p.returnValue=false;if(-1==m.className.indexOf("selected")){m.className+=" selected"}for(n=0,d=m.childNodes.length;n<d;n++){o=m.childNodes[n];if(o.className&&-1!=o.className.indexOf("shortlink-input")){o.focus();o.select();o.onblur=function(){m.className=m.className?m.className.replace(j,""):""};break}}return false};c(k,"load",function(){e=i.getElementById("wpadminbar");if(i.body&&e){i.body.appendChild(e);c(e,"mouseover",function(d){h(d.target||d.srcElement)});c(e,"mouseout",function(d){l(d.target||d.srcElement)});c(e,"click",b)}if(k.location.hash){k.scrollBy(0,-32)}})})(document,window);
if(typeof(jQuery)!="undefined"){if(typeof(jQuery.fn.hoverIntent)=="undefined"){(function(b){b.fn.hoverIntent=function(p,r){var g={sensitivity:7,interval:100,timeout:0};g=b.extend(g,r?{over:p,out:r}:p);var a,f,t,v;var u=function(c){a=c.pageX;f=c.pageY};var w=function(c,d){d.hoverIntent_t=clearTimeout(d.hoverIntent_t);if((Math.abs(t-a)+Math.abs(v-f))<g.sensitivity){b(d).unbind("mousemove",u);d.hoverIntent_s=1;return g.over.apply(d,[c])}else{t=a;v=f;d.hoverIntent_t=setTimeout(function(){w(c,d)},g.interval)}};var s=function(c,d){d.hoverIntent_t=clearTimeout(d.hoverIntent_t);d.hoverIntent_s=0;return g.out.apply(d,[c])};var x=function(e){var d=this;var c=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(c&&c!=this){try{c=c.parentNode}catch(e){c=this}}if(c==this){if(b.browser.mozilla){if(e.type=="mouseout"){d.mtout=setTimeout(function(){q(e,d)},30)}else{if(d.mtout){d.mtout=clearTimeout(d.mtout)}}}return}else{if(d.mtout){d.mtout=clearTimeout(d.mtout)}q(e,d)}};var q=function(e,d){var c=jQuery.extend({},e);if(d.hoverIntent_t){d.hoverIntent_t=clearTimeout(d.hoverIntent_t)}if(e.type=="mouseover"){t=c.pageX;v=c.pageY;b(d).bind("mousemove",u);if(d.hoverIntent_s!=1){d.hoverIntent_t=setTimeout(function(){w(c,d)},g.interval)}}else{b(d).unbind("mousemove",u);if(d.hoverIntent_s==1){d.hoverIntent_t=setTimeout(function(){s(c,d)},g.timeout)}}};return this.mouseover(x).mouseout(x)}})(jQuery)}jQuery(document).ready(function(a){a("#wpadminbar").removeClass("nojq").find("li.menupop").hoverIntent({over:function(b){a(this).addClass("hover")},out:function(b){a(this).removeClass("hover")},timeout:200,sensitivity:7,interval:120})})}else{(function(i,k){var c=function(n,m,d){if(n.addEventListener){n.addEventListener(m,d,false)}else{if(n.attachEvent){n.attachEvent("on"+m,function(){return d.call(n,window.event)})}}},e,f=new RegExp("\\bhover\\b","g"),a=[],j=new RegExp("\\bselected\\b","g"),g=function(m){var d=a.length;while(d--){if(a[d]&&m==a[d][1]){return a[d][0]}}return false},h=function(s){var n,d,q,m,p,r,u=[],o=0;while(s&&s!=e&&s!=i){if("LI"==s.nodeName.toUpperCase()){u[u.length]=s;d=g(s);if(d){clearTimeout(d)}s.className=s.className?(s.className.replace(f,"")+" hover"):"hover";m=s}s=s.parentNode}if(m&&m.parentNode){p=m.parentNode;if(p&&"UL"==p.nodeName.toUpperCase()){n=p.childNodes.length;while(n--){r=p.childNodes[n];if(r!=m){r.className=r.className?r.className.replace(j,""):""}}}}n=a.length;while(n--){q=false;o=u.length;while(o--){if(u[o]==a[n][1]){q=true}}if(!q){a[n][1].className=a[n][1].className?a[n][1].className.replace(f,""):""}}},l=function(d){while(d&&d!=e&&d!=i){if("LI"==d.nodeName.toUpperCase()){(function(m){var n=setTimeout(function(){m.className=m.className?m.className.replace(f,""):""},500);a[a.length]=[n,m]})(d)}d=d.parentNode}},b=function(p){var n,d,o,m=p.target||p.srcElement;while(true){if(!m||m==i||m==e){return}if(m.id&&m.id=="wp-admin-bar-get-shortlink"){break}m=m.parentNode}if(p.preventDefault){p.preventDefault()}p.returnValue=false;if(-1==m.className.indexOf("selected")){m.className+=" selected"}for(n=0,d=m.childNodes.length;n<d;n++){o=m.childNodes[n];if(o.className&&-1!=o.className.indexOf("shortlink-input")){o.focus();o.select();o.onblur=function(){m.className=m.className?m.className.replace(j,""):""};break}}return false};c(k,"load",function(){e=i.getElementById("wpadminbar");if(i.body&&e){i.body.appendChild(e);c(e,"mouseover",function(d){h(d.target||d.srcElement)});c(e,"mouseout",function(d){l(d.target||d.srcElement)});c(e,"click",b)}if(k.location.hash){k.scrollBy(0,-32)}})})(document,window)};