home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-includes / js / admin-bar.js next >
Encoding:
JavaScript  |  2015-07-20  |  11.5 KB  |  388 lines

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