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