web/wp-includes/js/admin-bar.js
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 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(b){var a=function(c,e){var f=b(e),d=f.attr("tabindex");if(d){f.attr("tabindex","0").attr("tabindex",d)}};b("#wpadminbar").removeClass("nojq").removeClass("nojs").find("li.menupop").hoverIntent({over:function(c){b(this).addClass("hover")},out:function(c){b(this).removeClass("hover")},timeout:180,sensitivity:7,interval:100});b("#wp-admin-bar-get-shortlink").click(function(c){c.preventDefault();b(this).addClass("selected").children(".shortlink-input").blur(function(){b(this).parents("#wp-admin-bar-get-shortlink").removeClass("selected")}).focus().select()});b("#wpadminbar li.menupop > .ab-item").bind("keydown.adminbar",function(f){if(f.which!=13){return}var d=b(f.target),c=d.closest("ab-sub-wrapper");f.stopPropagation();f.preventDefault();if(!c.length){c=b("#wpadminbar .quicklinks")}c.find(".menupop").removeClass("hover");d.parent().toggleClass("hover");d.siblings(".ab-sub-wrapper").find(".ab-item").each(a)}).each(a);b("#wpadminbar .ab-item").bind("keydown.adminbar",function(d){if(d.which!=27){return}var c=b(d.target);d.stopPropagation();d.preventDefault();c.closest(".hover").removeClass("hover").children(".ab-item").focus();c.siblings(".ab-sub-wrapper").find(".ab-item").each(a)});b("#wpadminbar").click(function(c){if(c.target.id!="wpadminbar"&&c.target.id!="wp-admin-bar-top-secondary"){return}c.preventDefault();b("html, body").animate({scrollTop:0},"fast")})})}else{(function(j,l){var e=function(o,n,d){if(o.addEventListener){o.addEventListener(n,d,false)}else{if(o.attachEvent){o.attachEvent("on"+n,function(){return d.call(o,window.event)})}}},f,g=new RegExp("\\bhover\\b","g"),b=[],k=new RegExp("\\bselected\\b","g"),h=function(n){var d=b.length;while(d--){if(b[d]&&n==b[d][1]){return b[d][0]}}return false},i=function(u){var o,d,r,n,q,s,v=[],p=0;while(u&&u!=f&&u!=j){if("LI"==u.nodeName.toUpperCase()){v[v.length]=u;d=h(u);if(d){clearTimeout(d)}u.className=u.className?(u.className.replace(g,"")+" hover"):"hover";n=u}u=u.parentNode}if(n&&n.parentNode){q=n.parentNode;if(q&&"UL"==q.nodeName.toUpperCase()){o=q.childNodes.length;while(o--){s=q.childNodes[o];if(s!=n){s.className=s.className?s.className.replace(k,""):""}}}}o=b.length;while(o--){r=false;p=v.length;while(p--){if(v[p]==b[o][1]){r=true}}if(!r){b[o][1].className=b[o][1].className?b[o][1].className.replace(g,""):""}}},m=function(d){while(d&&d!=f&&d!=j){if("LI"==d.nodeName.toUpperCase()){(function(n){var o=setTimeout(function(){n.className=n.className?n.className.replace(g,""):""},500);b[b.length]=[o,n]})(d)}d=d.parentNode}},c=function(q){var o,d,p,n=q.target||q.srcElement;while(true){if(!n||n==j||n==f){return}if(n.id&&n.id=="wp-admin-bar-get-shortlink"){break}n=n.parentNode}if(q.preventDefault){q.preventDefault()}q.returnValue=false;if(-1==n.className.indexOf("selected")){n.className+=" selected"}for(o=0,d=n.childNodes.length;o<d;o++){p=n.childNodes[o];if(p.className&&-1!=p.className.indexOf("shortlink-input")){p.focus();p.select();p.onblur=function(){n.className=n.className?n.className.replace(k,""):""};break}}return false},a=function(n){var s,q,p,d,r,o;if(n.id!="wpadminbar"&&n.id!="wp-admin-bar-top-secondary"){return}s=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0;if(s<1){return}o=s>800?130:100;q=Math.min(12,Math.round(s/o));p=s>800?Math.round(s/30):Math.round(s/20);d=[];r=0;while(s){s-=p;if(s<0){s=0}d.push(s);setTimeout(function(){window.scrollTo(0,d.shift())},r*q);r++}};e(l,"load",function(){f=j.getElementById("wpadminbar");if(j.body&&f){j.body.appendChild(f);if(f.className){f.className=f.className.replace(/nojs/,"")}e(f,"mouseover",function(d){i(d.target||d.srcElement)});e(f,"mouseout",function(d){m(d.target||d.srcElement)});e(f,"click",c);e(f,"click",function(d){a(d.target||d.srcElement)})}if(l.location.hash){l.scrollBy(0,-32)}})})(document,window)};
     1 // use jQuery and hoverIntent if loaded
       
     2 if ( typeof(jQuery) != 'undefined' ) {
       
     3 	if ( typeof(jQuery.fn.hoverIntent) == 'undefined' )
       
     4 		(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);
       
     5 
       
     6 	jQuery(document).ready(function($){
       
     7 		var adminbar = $('#wpadminbar'), refresh, touchOpen, touchClose, disableHoverIntent = false;
       
     8 
       
     9 		refresh = function(i, el){ // force the browser to refresh the tabbing index
       
    10 			var node = $(el), tab = node.attr('tabindex');
       
    11 			if ( tab )
       
    12 				node.attr('tabindex', '0').attr('tabindex', tab);
       
    13 		};
       
    14 
       
    15 		touchOpen = function(unbind) {
       
    16 			adminbar.find('li.menupop').on('click.wp-mobile-hover', function(e) {
       
    17 				var el = $(this);
       
    18 
       
    19 				if ( !el.hasClass('hover') ) {
       
    20 					e.preventDefault();
       
    21 					adminbar.find('li.menupop.hover').removeClass('hover');
       
    22 					el.addClass('hover');
       
    23 				}
       
    24 
       
    25 				if ( unbind ) {
       
    26 					$('li.menupop').off('click.wp-mobile-hover');
       
    27 					disableHoverIntent = false;
       
    28 				}
       
    29 			});
       
    30 		};
       
    31 
       
    32 		touchClose = function() {
       
    33 			var mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
       
    34 			// close any open drop-downs when the click/touch is not on the toolbar
       
    35 			$(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
       
    36 				if ( !$(e.target).closest('#wpadminbar').length )
       
    37 					adminbar.find('li.menupop.hover').removeClass('hover');
       
    38 			});
       
    39 		};
       
    40 
       
    41 		adminbar.removeClass('nojq').removeClass('nojs');
       
    42 
       
    43 		if ( 'ontouchstart' in window ) {
       
    44 			adminbar.on('touchstart', function(){
       
    45 				touchOpen(true);
       
    46 				disableHoverIntent = true;
       
    47 			});
       
    48 			touchClose();
       
    49 		} else if ( /IEMobile\/[1-9]/.test(navigator.userAgent) ) {
       
    50 			touchOpen();
       
    51 			touchClose();
       
    52 		}
       
    53 
       
    54 		adminbar.find('li.menupop').hoverIntent({
       
    55 			over: function(e){
       
    56 				if ( disableHoverIntent )
       
    57 					return;
       
    58 
       
    59 				$(this).addClass('hover');
       
    60 			},
       
    61 			out: function(e){
       
    62 				if ( disableHoverIntent )
       
    63 					return;
       
    64 
       
    65 				$(this).removeClass('hover');
       
    66 			},
       
    67 			timeout: 180,
       
    68 			sensitivity: 7,
       
    69 			interval: 100
       
    70 		});
       
    71 
       
    72 		if ( window.location.hash )
       
    73 			window.scrollBy( 0, -32 );
       
    74 
       
    75 		$('#wp-admin-bar-get-shortlink').click(function(e){
       
    76 			e.preventDefault();
       
    77 			$(this).addClass('selected').children('.shortlink-input').blur(function(){
       
    78 				$(this).parents('#wp-admin-bar-get-shortlink').removeClass('selected');
       
    79 			}).focus().select();
       
    80 		});
       
    81 
       
    82 		$('#wpadminbar li.menupop > .ab-item').bind('keydown.adminbar', function(e){
       
    83 			if ( e.which != 13 )
       
    84 				return;
       
    85 
       
    86 			var target = $(e.target), wrap = target.closest('ab-sub-wrapper');
       
    87 
       
    88 			e.stopPropagation();
       
    89 			e.preventDefault();
       
    90 
       
    91 			if ( !wrap.length )
       
    92 				wrap = $('#wpadminbar .quicklinks');
       
    93 
       
    94 			wrap.find('.menupop').removeClass('hover');
       
    95 			target.parent().toggleClass('hover');
       
    96 			target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
       
    97 		}).each(refresh);
       
    98 
       
    99 		$('#wpadminbar .ab-item').bind('keydown.adminbar', function(e){
       
   100 			if ( e.which != 27 )
       
   101 				return;
       
   102 
       
   103 			var target = $(e.target);
       
   104 
       
   105 			e.stopPropagation();
       
   106 			e.preventDefault();
       
   107 
       
   108 			target.closest('.hover').removeClass('hover').children('.ab-item').focus();
       
   109 			target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
       
   110 		});
       
   111 
       
   112 		$('#wpadminbar').click( function(e) {
       
   113 			if ( e.target.id != 'wpadminbar' && e.target.id != 'wp-admin-bar-top-secondary' )
       
   114 				return;
       
   115 
       
   116 			e.preventDefault();
       
   117 			$('html, body').animate({ scrollTop: 0 }, 'fast');
       
   118 		});
       
   119 
       
   120 		// fix focus bug in WebKit
       
   121 		$('.screen-reader-shortcut').keydown( function(e) {
       
   122 			if ( 13 != e.which )
       
   123 				return;
       
   124 
       
   125 			var id = $(this).attr('href');
       
   126 
       
   127 			if ( $.browser.webkit && id && id.charAt(0) == '#' ) {
       
   128 				setTimeout(function () {
       
   129 					$(id).focus();
       
   130 				}, 100);
       
   131 			}
       
   132 		});
       
   133 	});
       
   134 } else {
       
   135 	(function(d, w) {
       
   136 		var addEvent = function( obj, type, fn ) {
       
   137 			if ( obj.addEventListener )
       
   138 				obj.addEventListener(type, fn, false);
       
   139 			else if ( obj.attachEvent )
       
   140 				obj.attachEvent('on' + type, function() { return fn.call(obj, window.event);});
       
   141 		},
       
   142 
       
   143 		aB, hc = new RegExp('\\bhover\\b', 'g'), q = [],
       
   144 		rselected = new RegExp('\\bselected\\b', 'g'),
       
   145 
       
   146 		/**
       
   147 		 * Get the timeout ID of the given element
       
   148 		 */
       
   149 		getTOID = function(el) {
       
   150 			var i = q.length;
       
   151 			while ( i-- ) {
       
   152 				if ( q[i] && el == q[i][1] )
       
   153 					return q[i][0];
       
   154 			}
       
   155 			return false;
       
   156 		},
       
   157 
       
   158 		addHoverClass = function(t) {
       
   159 			var i, id, inA, hovering, ul, li,
       
   160 				ancestors = [],
       
   161 				ancestorLength = 0;
       
   162 
       
   163 			while ( t && t != aB && t != d ) {
       
   164 				if ( 'LI' == t.nodeName.toUpperCase() ) {
       
   165 					ancestors[ ancestors.length ] = t;
       
   166 					id = getTOID(t);
       
   167 					if ( id )
       
   168 						clearTimeout( id );
       
   169 					t.className = t.className ? ( t.className.replace(hc, '') + ' hover' ) : 'hover';
       
   170 					hovering = t;
       
   171 				}
       
   172 				t = t.parentNode;
       
   173 			}
       
   174 
       
   175 			// Remove any selected classes.
       
   176 			if ( hovering && hovering.parentNode ) {
       
   177 				ul = hovering.parentNode;
       
   178 				if ( ul && 'UL' == ul.nodeName.toUpperCase() ) {
       
   179 					i = ul.childNodes.length;
       
   180 					while ( i-- ) {
       
   181 						li = ul.childNodes[i];
       
   182 						if ( li != hovering )
       
   183 							li.className = li.className ? li.className.replace( rselected, '' ) : '';
       
   184 					}
       
   185 				}
       
   186 			}
       
   187 
       
   188 			/* remove the hover class for any objects not in the immediate element's ancestry */
       
   189 			i = q.length;
       
   190 			while ( i-- ) {
       
   191 				inA = false;
       
   192 				ancestorLength = ancestors.length;
       
   193 				while( ancestorLength-- ) {
       
   194 					if ( ancestors[ ancestorLength ] == q[i][1] )
       
   195 						inA = true;
       
   196 				}
       
   197 
       
   198 				if ( ! inA )
       
   199 					q[i][1].className = q[i][1].className ? q[i][1].className.replace(hc, '') : '';
       
   200 			}
       
   201 		},
       
   202 
       
   203 		removeHoverClass = function(t) {
       
   204 			while ( t && t != aB && t != d ) {
       
   205 				if ( 'LI' == t.nodeName.toUpperCase() ) {
       
   206 					(function(t) {
       
   207 						var to = setTimeout(function() {
       
   208 							t.className = t.className ? t.className.replace(hc, '') : '';
       
   209 						}, 500);
       
   210 						q[q.length] = [to, t];
       
   211 					})(t);
       
   212 				}
       
   213 				t = t.parentNode;
       
   214 			}
       
   215 		},
       
   216 
       
   217 		clickShortlink = function(e) {
       
   218 			var i, l, node,
       
   219 				t = e.target || e.srcElement;
       
   220 
       
   221 			// Make t the shortlink menu item, or return.
       
   222 			while ( true ) {
       
   223 				// Check if we've gone past the shortlink node,
       
   224 				// or if the user is clicking on the input.
       
   225 				if ( ! t || t == d || t == aB )
       
   226 					return;
       
   227 				// Check if we've found the shortlink node.
       
   228 				if ( t.id && t.id == 'wp-admin-bar-get-shortlink' )
       
   229 					break;
       
   230 				t = t.parentNode;
       
   231 			}
       
   232 
       
   233 			// IE doesn't support preventDefault, and does support returnValue
       
   234 			if ( e.preventDefault )
       
   235 				e.preventDefault();
       
   236 			e.returnValue = false;
       
   237 
       
   238 			if ( -1 == t.className.indexOf('selected') )
       
   239 				t.className += ' selected';
       
   240 
       
   241 			for ( i = 0, l = t.childNodes.length; i < l; i++ ) {
       
   242 				node = t.childNodes[i];
       
   243 				if ( node.className && -1 != node.className.indexOf('shortlink-input') ) {
       
   244 					node.focus();
       
   245 					node.select();
       
   246 					node.onblur = function() {
       
   247 						t.className = t.className ? t.className.replace( rselected, '' ) : '';
       
   248 					};
       
   249 					break;
       
   250 				}
       
   251 			}
       
   252 			return false;
       
   253 		},
       
   254 
       
   255 		scrollToTop = function(t) {
       
   256 			var distance, speed, step, steps, timer, speed_step;
       
   257 
       
   258 			// Ensure that the #wpadminbar was the target of the click.
       
   259 			if ( t.id != 'wpadminbar' && t.id != 'wp-admin-bar-top-secondary' )
       
   260 				return;
       
   261 
       
   262 			distance    = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
       
   263 
       
   264 			if ( distance < 1 )
       
   265 				return;
       
   266 
       
   267 			speed_step = distance > 800 ? 130 : 100;
       
   268 			speed     = Math.min( 12, Math.round( distance / speed_step ) );
       
   269 			step      = distance > 800 ? Math.round( distance / 30  ) : Math.round( distance / 20  );
       
   270 			steps     = [];
       
   271 			timer     = 0;
       
   272 
       
   273 			// Animate scrolling to the top of the page by generating steps to
       
   274 			// the top of the page and shifting to each step at a set interval.
       
   275 			while ( distance ) {
       
   276 				distance -= step;
       
   277 				if ( distance < 0 )
       
   278 					distance = 0;
       
   279 				steps.push( distance );
       
   280 
       
   281 				setTimeout( function() {
       
   282 					window.scrollTo( 0, steps.shift() );
       
   283 				}, timer * speed );
       
   284 
       
   285 				timer++;
       
   286 			}
       
   287 		};
       
   288 
       
   289 		addEvent(w, 'load', function() {
       
   290 			aB = d.getElementById('wpadminbar');
       
   291 
       
   292 			if ( d.body && aB ) {
       
   293 				d.body.appendChild( aB );
       
   294 
       
   295 				if ( aB.className )
       
   296 					aB.className = aB.className.replace(/nojs/, '');
       
   297 
       
   298 				addEvent(aB, 'mouseover', function(e) {
       
   299 					addHoverClass( e.target || e.srcElement );
       
   300 				});
       
   301 
       
   302 				addEvent(aB, 'mouseout', function(e) {
       
   303 					removeHoverClass( e.target || e.srcElement );
       
   304 				});
       
   305 
       
   306 				addEvent(aB, 'click', clickShortlink );
       
   307 
       
   308 				addEvent(aB, 'click', function(e) {
       
   309 					scrollToTop( e.target || e.srcElement );
       
   310 				});
       
   311 			}
       
   312 
       
   313 			if ( w.location.hash )
       
   314 				w.scrollBy(0,-32);
       
   315 		});
       
   316 	})(document, window);
       
   317 
       
   318 }