From 469a3d226b5aa572bdf91620b3e7d0f192a4054b Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Fri, 19 Oct 2012 18:31:21 +0000 Subject: [PATCH] Make the admin menu and toolbar work well on mobile devices, props georgestephanis, see #20614 git-svn-id: https://develop.svn.wordpress.org/trunk@22262 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/js/common.js | 88 ++++++++++++++++++++++--------------- wp-includes/js/admin-bar.js | 57 +++++++++++++++++------- 2 files changed, 93 insertions(+), 52 deletions(-) diff --git a/wp-admin/js/common.js b/wp-admin/js/common.js index 2a62179b12..b2017f593a 100644 --- a/wp-admin/js/common.js +++ b/wp-admin/js/common.js @@ -195,49 +195,67 @@ $(document).ready( function() { } }); - $('li.wp-has-submenu', menu).hoverIntent({ - over: function(e){ - var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 ); + if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device + // close any open submenus when touch/click is not on the menu + $(document.body).on('click.wp-mobile-hover', function(e) { + if ( !$(e.target).closest('#adminmenu').length ) + menu.find('li.wp-has-submenu.opensub').removeClass('opensub'); + }); - if ( isNaN(top) || top > -5 ) // meaning the submenu is visible - return; + menu.find('li.wp-has-submenu').on('click.wp-mobile-hover', function(e) { + var el = $(this); - menutop = $(this).offset().top; - wintop = $(window).scrollTop(); - maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar + if ( !el.hasClass('opensub') ) { + e.preventDefault(); + menu.find('li.wp-has-submenu.opensub').removeClass('opensub'); + el.addClass('opensub'); + } + }); + } else { + menu.find('li.wp-has-submenu').hoverIntent({ + over: function(e){ + var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 ); - b = menutop + m.height() + 1; // Bottom offset of the menu - h = $('#wpwrap').height(); // Height of the entire page - o = 60 + b - h; - f = $(window).height() + wintop - 15; // The fold + if ( isNaN(top) || top > -5 ) // meaning the submenu is visible + return; - if ( f < (b - o) ) - o = b - f; + menutop = $(this).offset().top; + wintop = $(window).scrollTop(); + maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar - if ( o > maxtop ) - o = maxtop; + b = menutop + m.height() + 1; // Bottom offset of the menu + h = $('#wpwrap').height(); // Height of the entire page + o = 60 + b - h; + f = $(window).height() + wintop - 15; // The fold - if ( o > 1 ) - m.css('margin-top', '-'+o+'px'); - else - m.css('margin-top', ''); + if ( f < (b - o) ) + o = b - f; - menu.find('li.menu-top').removeClass('opensub'); - $(this).addClass('opensub'); - }, - out: function(){ - $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', ''); - }, - timeout: 200, - sensitivity: 7, - interval: 90 - }); + if ( o > maxtop ) + o = maxtop; - menu.on('focus.adminmenu', '.wp-submenu a', function(e){ - $(e.target).closest('li.menu-top').addClass('opensub'); - }).on('blur.adminmenu', '.wp-submenu a', function(e){ - $(e.target).closest('li.menu-top').removeClass('opensub'); - }); + if ( o > 1 ) + m.css('margin-top', '-'+o+'px'); + else + m.css('margin-top', ''); + + menu.find('li.menu-top').removeClass('opensub'); + $(this).addClass('opensub'); + }, + out: function(){ + $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', ''); + }, + timeout: 200, + sensitivity: 7, + interval: 90 + }); + + menu.on('focus.adminmenu', '.wp-submenu a', function(e){ + $(e.target).closest('li.menu-top').addClass('opensub'); + }).on('blur.adminmenu', '.wp-submenu a', function(e){ + $(e.target).closest('li.menu-top').removeClass('opensub'); + }); + } // Move .updated and .error alert boxes. Don't move boxes designed to be inline. $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2'); diff --git a/wp-includes/js/admin-bar.js b/wp-includes/js/admin-bar.js index 7cd03d4ba1..fc85d9738b 100644 --- a/wp-includes/js/admin-bar.js +++ b/wp-includes/js/admin-bar.js @@ -4,23 +4,45 @@ if ( typeof(jQuery) != 'undefined' ) { (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))