/wp-includes/js/admin-bar.js

https://bitbucket.org/skyarch-iijima/wordpress · JavaScript · 387 lines · 297 code · 67 blank · 23 comment · 117 complexity · d97a1899a7f641e693928f5a47c33de4 MD5 · raw file

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