admin-bar.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /**
  2. * @output wp-includes/js/admin-bar.js
  3. */
  4. /* jshint loopfunc: true */
  5. // use jQuery and hoverIntent if loaded
  6. if ( typeof(jQuery) != 'undefined' ) {
  7. if ( typeof(jQuery.fn.hoverIntent) == 'undefined' ) {
  8. /* jshint ignore:start */
  9. // hoverIntent v1.8.1 - Copy of wp-includes/js/hoverIntent.min.js
  10. !function(a){a.fn.hoverIntent=function(b,c,d){var e={interval:100,sensitivity:6,timeout:0};e="object"==typeof b?a.extend(e,b):a.isFunction(c)?a.extend(e,{over:b,out:c,selector:d}):a.extend(e,{over:b,out:b,selector:c});var f,g,h,i,j=function(a){f=a.pageX,g=a.pageY},k=function(b,c){return c.hoverIntent_t=clearTimeout(c.hoverIntent_t),Math.sqrt((h-f)*(h-f)+(i-g)*(i-g))<e.sensitivity?(a(c).off("mousemove.hoverIntent",j),c.hoverIntent_s=!0,e.over.apply(c,[b])):(h=f,i=g,c.hoverIntent_t=setTimeout(function(){k(b,c)},e.interval),void 0)},l=function(a,b){return b.hoverIntent_t=clearTimeout(b.hoverIntent_t),b.hoverIntent_s=!1,e.out.apply(b,[a])},m=function(b){var c=a.extend({},b),d=this;d.hoverIntent_t&&(d.hoverIntent_t=clearTimeout(d.hoverIntent_t)),"mouseenter"===b.type?(h=c.pageX,i=c.pageY,a(d).on("mousemove.hoverIntent",j),d.hoverIntent_s||(d.hoverIntent_t=setTimeout(function(){k(c,d)},e.interval))):(a(d).off("mousemove.hoverIntent",j),d.hoverIntent_s&&(d.hoverIntent_t=setTimeout(function(){l(c,d)},e.timeout)))};return this.on({"mouseenter.hoverIntent":m,"mouseleave.hoverIntent":m},e.selector)}}(jQuery);
  11. /* jshint ignore:end */
  12. }
  13. jQuery(document).ready(function($){
  14. var adminbar = $('#wpadminbar'), refresh, touchOpen, touchClose, disableHoverIntent = false;
  15. /**
  16. * Forces the browser to refresh the tabbing index.
  17. *
  18. * @since 3.3.0
  19. *
  20. * @param {number} i The index of the HTML element to remove the tab index
  21. * from. This parameter is necessary because we use this
  22. * function in .each calls.
  23. * @param {HTMLElement} el The HTML element to remove the tab index from.
  24. *
  25. * @return {void}
  26. */
  27. refresh = function(i, el){
  28. var node = $(el), tab = node.attr('tabindex');
  29. if ( tab )
  30. node.attr('tabindex', '0').attr('tabindex', tab);
  31. };
  32. /**
  33. * Adds or removes the hover class on touch.
  34. *
  35. * @since 3.5.0
  36. *
  37. * @param {boolean} unbind If true removes the wp-mobile-hover class.
  38. *
  39. * @return {void}
  40. */
  41. touchOpen = function(unbind) {
  42. adminbar.find('li.menupop').on('click.wp-mobile-hover', function(e) {
  43. var el = $(this);
  44. if ( el.parent().is('#wp-admin-bar-root-default') && !el.hasClass('hover') ) {
  45. e.preventDefault();
  46. adminbar.find('li.menupop.hover').removeClass('hover');
  47. el.addClass('hover');
  48. } else if ( !el.hasClass('hover') ) {
  49. e.stopPropagation();
  50. e.preventDefault();
  51. el.addClass('hover');
  52. } else if ( ! $( e.target ).closest( 'div' ).hasClass( 'ab-sub-wrapper' ) ) {
  53. // We're dealing with an already-touch-opened menu genericon (we know el.hasClass('hover')),
  54. // so close it on a second tap and prevent propag and defaults. See #29906
  55. e.stopPropagation();
  56. e.preventDefault();
  57. el.removeClass('hover');
  58. }
  59. if ( unbind ) {
  60. $('li.menupop').off('click.wp-mobile-hover');
  61. disableHoverIntent = false;
  62. }
  63. });
  64. };
  65. /**
  66. * Removes the hover class if clicked or touched outside the admin bar.
  67. *
  68. * @since 3.5.0
  69. *
  70. * @return {void}
  71. */
  72. touchClose = function() {
  73. var mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
  74. // close any open drop-downs when the click/touch is not on the toolbar
  75. $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
  76. if ( !$(e.target).closest('#wpadminbar').length )
  77. adminbar.find('li.menupop.hover').removeClass('hover');
  78. });
  79. };
  80. adminbar.removeClass('nojq').removeClass('nojs');
  81. // If clicked on the adminbar add the hoverclass, if clicked outside it remove
  82. // it.
  83. if ( 'ontouchstart' in window ) {
  84. adminbar.on('touchstart', function(){
  85. touchOpen(true);
  86. disableHoverIntent = true;
  87. });
  88. touchClose();
  89. } else if ( /IEMobile\/[1-9]/.test(navigator.userAgent) ) {
  90. touchOpen();
  91. touchClose();
  92. }
  93. // Adds or removes the hover class based on the hover intent.
  94. adminbar.find('li.menupop').hoverIntent({
  95. over: function() {
  96. if ( disableHoverIntent )
  97. return;
  98. $(this).addClass('hover');
  99. },
  100. out: function() {
  101. if ( disableHoverIntent )
  102. return;
  103. $(this).removeClass('hover');
  104. },
  105. timeout: 180,
  106. sensitivity: 7,
  107. interval: 100
  108. });
  109. // Prevents the toolbar from covering up content when a hash is present in the
  110. // URL.
  111. if ( window.location.hash )
  112. window.scrollBy( 0, -32 );
  113. /**
  114. * Handles the selected state of the Shortlink link.
  115. *
  116. * When the input is visible the link should be selected, when the input is
  117. * unfocused the link should be unselected.
  118. *
  119. * @param {Object} e The click event.
  120. *
  121. * @return {void}
  122. **/
  123. $('#wp-admin-bar-get-shortlink').click(function(e){
  124. e.preventDefault();
  125. $(this).addClass('selected').children('.shortlink-input').blur(function(){
  126. $(this).parents('#wp-admin-bar-get-shortlink').removeClass('selected');
  127. }).focus().select();
  128. });
  129. /**
  130. * Removes the hoverclass if the enter key is pressed.
  131. *
  132. * Makes sure the tab index is refreshed by refreshing each ab-item
  133. * and its children.
  134. *
  135. * @param {Object} e The keydown event.
  136. *
  137. * @return {void}
  138. */
  139. $('#wpadminbar li.menupop > .ab-item').bind('keydown.adminbar', function(e){
  140. // Key code 13 is the enter key.
  141. if ( e.which != 13 )
  142. return;
  143. var target = $(e.target),
  144. wrap = target.closest('.ab-sub-wrapper'),
  145. parentHasHover = target.parent().hasClass('hover');
  146. e.stopPropagation();
  147. e.preventDefault();
  148. if ( !wrap.length )
  149. wrap = $('#wpadminbar .quicklinks');
  150. wrap.find('.menupop').removeClass('hover');
  151. if ( ! parentHasHover ) {
  152. target.parent().toggleClass('hover');
  153. }
  154. target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
  155. }).each(refresh);
  156. /**
  157. * Removes the hover class when the escape key is pressed.
  158. *
  159. * Makes sure the tab index is refreshed by refreshing each ab-item
  160. * and its children.
  161. *
  162. * @param {Object} e The keydown event.
  163. *
  164. * @return {void}
  165. */
  166. $('#wpadminbar .ab-item').bind('keydown.adminbar', function(e){
  167. // Key code 27 is the escape key.
  168. if ( e.which != 27 )
  169. return;
  170. var target = $(e.target);
  171. e.stopPropagation();
  172. e.preventDefault();
  173. target.closest('.hover').removeClass('hover').children('.ab-item').focus();
  174. target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
  175. });
  176. /**
  177. * Scrolls to top of page by clicking the adminbar.
  178. *
  179. * @param {Object} e The click event.
  180. *
  181. * @return {void}
  182. */
  183. adminbar.click( function(e) {
  184. if ( e.target.id != 'wpadminbar' && e.target.id != 'wp-admin-bar-top-secondary' ) {
  185. return;
  186. }
  187. adminbar.find( 'li.menupop.hover' ).removeClass( 'hover' );
  188. $( 'html, body' ).animate( { scrollTop: 0 }, 'fast' );
  189. e.preventDefault();
  190. });
  191. /**
  192. * Sets the focus on an element with a href attribute.
  193. *
  194. * The timeout is used to fix a focus bug in WebKit.
  195. *
  196. * @param {Object} e The keydown event.
  197. *
  198. * @return {void}
  199. */
  200. $('.screen-reader-shortcut').keydown( function(e) {
  201. var id, ua;
  202. if ( 13 != e.which )
  203. return;
  204. id = $( this ).attr( 'href' );
  205. ua = navigator.userAgent.toLowerCase();
  206. if ( ua.indexOf('applewebkit') != -1 && id && id.charAt(0) == '#' ) {
  207. setTimeout(function () {
  208. $(id).focus();
  209. }, 100);
  210. }
  211. });
  212. $( '#adminbar-search' ).on({
  213. /**
  214. * Adds the adminbar-focused class on focus.
  215. *
  216. * @return {void}
  217. */
  218. focus: function() {
  219. $( '#adminbarsearch' ).addClass( 'adminbar-focused' );
  220. /**
  221. * Removes the adminbar-focused class on blur.
  222. *
  223. * @return {void}
  224. */
  225. }, blur: function() {
  226. $( '#adminbarsearch' ).removeClass( 'adminbar-focused' );
  227. }
  228. } );
  229. if ( 'sessionStorage' in window ) {
  230. /**
  231. * Empties sessionStorage on logging out.
  232. *
  233. * @return {void}
  234. */
  235. $('#wp-admin-bar-logout a').click( function() {
  236. try {
  237. for ( var key in sessionStorage ) {
  238. if ( key.indexOf('wp-autosave-') != -1 )
  239. sessionStorage.removeItem(key);
  240. }
  241. } catch(e) {}
  242. });
  243. }
  244. if ( navigator.userAgent && document.body.className.indexOf( 'no-font-face' ) === -1 &&
  245. /Android (1.0|1.1|1.5|1.6|2.0|2.1)|Nokia|Opera Mini|w(eb)?OSBrowser|webOS|UCWEB|Windows Phone OS 7|XBLWP7|ZuneWP7|MSIE 7/.test( navigator.userAgent ) ) {
  246. document.body.className += ' no-font-face';
  247. }
  248. });
  249. } else {
  250. /**
  251. * Wrapper function for the adminbar that's used if jQuery isn't available.
  252. *
  253. * @param {Object} d The document object.
  254. * @param {Object} w The window object.
  255. *
  256. * @return {void}
  257. */
  258. (function(d, w) {
  259. /**
  260. * Adds an event listener to an object.
  261. *
  262. * @since 3.1.0
  263. *
  264. * @param {Object} obj The object to add the event listener to.
  265. * @param {string} type The type of event.
  266. * @param {function} fn The function to bind to the event listener.
  267. *
  268. * @return {void}
  269. */
  270. var addEvent = function( obj, type, fn ) {
  271. if ( obj && typeof obj.addEventListener === 'function' ) {
  272. obj.addEventListener( type, fn, false );
  273. } else if ( obj && typeof obj.attachEvent === 'function' ) {
  274. obj.attachEvent( 'on' + type, function() {
  275. return fn.call( obj, window.event );
  276. } );
  277. }
  278. },
  279. aB, hc = new RegExp('\\bhover\\b', 'g'), q = [],
  280. rselected = new RegExp('\\bselected\\b', 'g'),
  281. /**
  282. * Gets the timeout ID of the given element.
  283. *
  284. * @since 3.1.0
  285. *
  286. * @param {HTMLElement} el The HTML element.
  287. *
  288. * @return {number|boolean} The ID value of the timer that is set or false.
  289. */
  290. getTOID = function(el) {
  291. var i = q.length;
  292. while ( i-- ) {
  293. if ( q[i] && el == q[i][1] )
  294. return q[i][0];
  295. }
  296. return false;
  297. },
  298. /**
  299. * Adds the hoverclass to menu items.
  300. *
  301. * @since 3.1.0
  302. *
  303. * @param {HTMLElement} t The HTML element.
  304. *
  305. * @return {void}
  306. */
  307. addHoverClass = function(t) {
  308. var i, id, inA, hovering, ul, li,
  309. ancestors = [],
  310. ancestorLength = 0;
  311. // aB is adminbar. d is document.
  312. while ( t && t != aB && t != d ) {
  313. if ( 'LI' == t.nodeName.toUpperCase() ) {
  314. ancestors[ ancestors.length ] = t;
  315. id = getTOID(t);
  316. if ( id )
  317. clearTimeout( id );
  318. t.className = t.className ? ( t.className.replace(hc, '') + ' hover' ) : 'hover';
  319. hovering = t;
  320. }
  321. t = t.parentNode;
  322. }
  323. // Removes any selected classes.
  324. if ( hovering && hovering.parentNode ) {
  325. ul = hovering.parentNode;
  326. if ( ul && 'UL' == ul.nodeName.toUpperCase() ) {
  327. i = ul.childNodes.length;
  328. while ( i-- ) {
  329. li = ul.childNodes[i];
  330. if ( li != hovering )
  331. li.className = li.className ? li.className.replace( rselected, '' ) : '';
  332. }
  333. }
  334. }
  335. // Removes the hover class for any objects not in the immediate element's ancestry.
  336. i = q.length;
  337. while ( i-- ) {
  338. inA = false;
  339. ancestorLength = ancestors.length;
  340. while( ancestorLength-- ) {
  341. if ( ancestors[ ancestorLength ] == q[i][1] )
  342. inA = true;
  343. }
  344. if ( ! inA )
  345. q[i][1].className = q[i][1].className ? q[i][1].className.replace(hc, '') : '';
  346. }
  347. },
  348. /**
  349. * Removes the hoverclass from menu items.
  350. *
  351. * @since 3.1.0
  352. *
  353. * @param {HTMLElement} t The HTML element.
  354. *
  355. * @return {void}
  356. */
  357. removeHoverClass = function(t) {
  358. while ( t && t != aB && t != d ) {
  359. if ( 'LI' == t.nodeName.toUpperCase() ) {
  360. (function(t) {
  361. var to = setTimeout(function() {
  362. t.className = t.className ? t.className.replace(hc, '') : '';
  363. }, 500);
  364. q[q.length] = [to, t];
  365. })(t);
  366. }
  367. t = t.parentNode;
  368. }
  369. },
  370. /**
  371. * Handles the click on the Shortlink link in the adminbar.
  372. *
  373. * @since 3.1.0
  374. *
  375. * @param {Object} e The click event.
  376. *
  377. * @return {boolean} Returns false to prevent default click behavior.
  378. */
  379. clickShortlink = function(e) {
  380. var i, l, node,
  381. t = e.target || e.srcElement;
  382. // Make t the shortlink menu item, or return.
  383. while ( true ) {
  384. // Check if we've gone past the shortlink node,
  385. // or if the user is clicking on the input.
  386. if ( ! t || t == d || t == aB )
  387. return;
  388. // Check if we've found the shortlink node.
  389. if ( t.id && t.id == 'wp-admin-bar-get-shortlink' )
  390. break;
  391. t = t.parentNode;
  392. }
  393. // IE doesn't support preventDefault, and does support returnValue
  394. if ( e.preventDefault )
  395. e.preventDefault();
  396. e.returnValue = false;
  397. if ( -1 == t.className.indexOf('selected') )
  398. t.className += ' selected';
  399. for ( i = 0, l = t.childNodes.length; i < l; i++ ) {
  400. node = t.childNodes[i];
  401. if ( node.className && -1 != node.className.indexOf('shortlink-input') ) {
  402. node.focus();
  403. node.select();
  404. node.onblur = function() {
  405. t.className = t.className ? t.className.replace( rselected, '' ) : '';
  406. };
  407. break;
  408. }
  409. }
  410. return false;
  411. },
  412. /**
  413. * Scrolls to the top of the page.
  414. *
  415. * @since 3.4.0
  416. *
  417. * @param {HTMLElement} t The HTML element.
  418. *
  419. * @return {void}
  420. */
  421. scrollToTop = function(t) {
  422. var distance, speed, step, steps, timer, speed_step;
  423. // Ensure that the #wpadminbar was the target of the click.
  424. if ( t.id != 'wpadminbar' && t.id != 'wp-admin-bar-top-secondary' )
  425. return;
  426. distance = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  427. if ( distance < 1 )
  428. return;
  429. speed_step = distance > 800 ? 130 : 100;
  430. speed = Math.min( 12, Math.round( distance / speed_step ) );
  431. step = distance > 800 ? Math.round( distance / 30 ) : Math.round( distance / 20 );
  432. steps = [];
  433. timer = 0;
  434. // Animate scrolling to the top of the page by generating steps to
  435. // the top of the page and shifting to each step at a set interval.
  436. while ( distance ) {
  437. distance -= step;
  438. if ( distance < 0 )
  439. distance = 0;
  440. steps.push( distance );
  441. setTimeout( function() {
  442. window.scrollTo( 0, steps.shift() );
  443. }, timer * speed );
  444. timer++;
  445. }
  446. };
  447. addEvent(w, 'load', function() {
  448. aB = d.getElementById('wpadminbar');
  449. if ( d.body && aB ) {
  450. d.body.appendChild( aB );
  451. if ( aB.className )
  452. aB.className = aB.className.replace(/nojs/, '');
  453. addEvent(aB, 'mouseover', function(e) {
  454. addHoverClass( e.target || e.srcElement );
  455. });
  456. addEvent(aB, 'mouseout', function(e) {
  457. removeHoverClass( e.target || e.srcElement );
  458. });
  459. addEvent(aB, 'click', clickShortlink );
  460. addEvent(aB, 'click', function(e) {
  461. scrollToTop( e.target || e.srcElement );
  462. });
  463. addEvent( document.getElementById('wp-admin-bar-logout'), 'click', function() {
  464. if ( 'sessionStorage' in window ) {
  465. try {
  466. for ( var key in sessionStorage ) {
  467. if ( key.indexOf('wp-autosave-') != -1 )
  468. sessionStorage.removeItem(key);
  469. }
  470. } catch(e) {}
  471. }
  472. });
  473. }
  474. if ( w.location.hash )
  475. w.scrollBy(0,-32);
  476. if ( navigator.userAgent && document.body.className.indexOf( 'no-font-face' ) === -1 &&
  477. /Android (1.0|1.1|1.5|1.6|2.0|2.1)|Nokia|Opera Mini|w(eb)?OSBrowser|webOS|UCWEB|Windows Phone OS 7|XBLWP7|ZuneWP7|MSIE 7/.test( navigator.userAgent ) ) {
  478. document.body.className += ' no-font-face';
  479. }
  480. });
  481. })(document, window);
  482. }