| | 733 | var wpTouchMenu = { |
| | 734 | started: false, |
| | 735 | tracking: false, |
| | 736 | startPosition: 0, |
| | 737 | open: false |
| | 738 | }; |
| | 739 | |
| | 740 | $( window ).on( 'touchstart.wp-responsive', function( e ) { |
| | 741 | var touches = e.originalEvent.touches, |
| | 742 | touch, |
| | 743 | winWidth = Math.max( window.innerWidth, document.documentElement.clientWidth ); |
| | 744 | |
| | 745 | // if it's multi touch bail |
| | 746 | if ( 1 != touches.length ) { |
| | 747 | return; |
| | 748 | } |
| | 749 | |
| | 750 | touch = touches[0]; |
| | 751 | |
| | 752 | // if the menu is open and we're starting from the |
| | 753 | // outside it start closing the menu |
| | 754 | if ( wpTouchMenu.open && ! $( e.target ).closest( '#adminmenu' ).length ) { |
| | 755 | wpTouchMenu.started = true; |
| | 756 | wpTouchMenu.startPosition = touch.screenX; |
| | 757 | return; |
| | 758 | } |
| | 759 | |
| | 760 | // if it's within the left 20% of the screen |
| | 761 | // note that it has started |
| | 762 | if ( touch.screenX <= ( winWidth * 0.2 ) ) { |
| | 763 | wpTouchMenu.started = true; |
| | 764 | wpTouchMenu.startPosition = touch.screenX; |
| | 765 | } |
| | 766 | } ); |
| | 767 | |
| | 768 | $( window ).on( 'touchmove.wp-responsive', function( e ) { |
| | 769 | var touches, touch; |
| | 770 | // if it didn't qualify for potential menu |
| | 771 | // open bail |
| | 772 | if ( ! wpTouchMenu.started ) { |
| | 773 | return; |
| | 774 | } |
| | 775 | |
| | 776 | touches = e.originalEvent.changedTouches; |
| | 777 | |
| | 778 | // no multitouch |
| | 779 | if ( 1 != touches.length ) { |
| | 780 | return; |
| | 781 | } |
| | 782 | |
| | 783 | // stop back action |
| | 784 | e.preventDefault(); |
| | 785 | |
| | 786 | touch = touches[0]; |
| | 787 | |
| | 788 | // cancel open if we're not going in the right direction |
| | 789 | if ( ( ! wpTouchMenu.open && touch.screenX <= wpTouchMenu.startPosition ) || |
| | 790 | ( wpTouchMenu.open && wpTouchMenu.startPosition <= touch.screenX ) ) { |
| | 791 | wpTouchMenu.started = false; |
| | 792 | wpTouchMenu.startPosition = 0; |
| | 793 | return; |
| | 794 | } |
| | 795 | |
| | 796 | wpTouchMenu.tracking = true; |
| | 797 | } ); |
| | 798 | |
| | 799 | $( window ).on( 'touchend.wp-responsive', function( e ) { |
| | 800 | var touches = e.originalEvent.changedTouches, |
| | 801 | touch, |
| | 802 | winWidth = Math.max( window.innerWidth, document.documentElement.clientWidth ), |
| | 803 | moved = 0; |
| | 804 | |
| | 805 | // only process if we're tracking |
| | 806 | if ( ! wpTouchMenu.tracking ) { |
| | 807 | return; |
| | 808 | } |
| | 809 | |
| | 810 | // no multitouch |
| | 811 | if ( 1 != touches.length ) { |
| | 812 | wpTouchMenu.started = false; |
| | 813 | wpTouchMenu.startPosition = 0; |
| | 814 | wpTouchMenu.tracking = false; |
| | 815 | return; |
| | 816 | } |
| | 817 | |
| | 818 | touch = touches[0]; |
| | 819 | // calculate how far we've moved |
| | 820 | if ( wpTouchMenu.open ) { |
| | 821 | moved = wpTouchMenu.startPosition - touch.screenX; |
| | 822 | } else { |
| | 823 | moved = touch.screenX - wpTouchMenu.startPosition; |
| | 824 | } |
| | 825 | |
| | 826 | // if it's 20% or more trigger the menu |
| | 827 | if ( ( winWidth * 0.2 ) <= moved ) { |
| | 828 | $( '#wp-admin-bar-menu-toggle' ).trigger( 'click' ); |
| | 829 | wpTouchMenu.open = ! wpTouchMenu.open; |
| | 830 | } |
| | 831 | |
| | 832 | // reset our values |
| | 833 | wpTouchMenu.started = false; |
| | 834 | wpTouchMenu.startPosition = 0; |
| | 835 | wpTouchMenu.tracking = false; |
| | 836 | } ); |
| | 837 | |