admin-menu-nav-unification-rtl.css
2 years ago
admin-menu-nav-unification-rtl.min.css
2 years ago
admin-menu-nav-unification.css
2 years ago
admin-menu-nav-unification.min.css
2 years ago
admin-menu-rtl.css
2 years ago
admin-menu-rtl.min.css
2 years ago
admin-menu.css
2 years ago
admin-menu.js
2 years ago
admin-menu.min.css
2 years ago
class-admin-menu.php
2 years ago
class-atomic-admin-menu.php
2 years ago
class-dashboard-switcher-tracking.php
2 years ago
class-domain-only-admin-menu.php
2 years ago
class-jetpack-admin-menu.php
2 years ago
class-p2-admin-menu.php
2 years ago
class-wpcom-admin-menu.php
2 years ago
class-wpcom-email-subscription-checker.php
2 years ago
dashicon-set.php
2 years ago
globe-icon.svg
2 years ago
load.php
2 years ago
menu-mappings.php
2 years ago
admin-menu.js
185 lines
| 1 | /* global ajaxurl, jetpackAdminMenu */ |
| 2 | |
| 3 | ( function ( $ ) { |
| 4 | function init() { |
| 5 | var adminbar = document.querySelector( '#wpadminbar' ); |
| 6 | var wpwrap = document.querySelector( '#wpwrap' ); |
| 7 | var adminMenu = document.querySelector( '#adminmenu' ); |
| 8 | var dismissClass = 'dismissible-card__close-icon'; |
| 9 | |
| 10 | if ( ! adminbar ) { |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | function setAriaExpanded( value ) { |
| 15 | var anchors = adminbar.querySelectorAll( '#wp-admin-bar-blog a' ); |
| 16 | for ( var i = 0; i < anchors.length; i++ ) { |
| 17 | anchors[ i ].setAttribute( 'aria-expanded', value ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | setFocusOnActiveMenuItem(); |
| 22 | setAriaExpanded( 'false' ); |
| 23 | |
| 24 | var adminbarBlog = adminbar.querySelector( '#wp-admin-bar-blog.my-sites > a' ); |
| 25 | |
| 26 | // Toggle sidebar when toggle is clicked. |
| 27 | if ( adminbarBlog && ! document.body.classList.contains( 'wpcom-admin-interface' ) ) { |
| 28 | // We need to remove an event listener and attribute from the my sites button to prevent default behavior of the wp-responsive-overlay. |
| 29 | $( '#wp-admin-bar-blog.my-sites > a' ).off( 'click.wp-responsive' ); |
| 30 | adminbarBlog.removeAttribute( 'aria-haspopup' ); |
| 31 | // Toggle the sidebar when the 'My Sites' button is clicked in a mobile view. |
| 32 | adminbarBlog.addEventListener( 'click', toggleSidebar ); |
| 33 | // Detect a click outside the sidebar and close it if its open. |
| 34 | document.addEventListener( 'click', closeSidebarWhenClickedOutside ); |
| 35 | } |
| 36 | |
| 37 | function closeSidebarWhenClickedOutside( event ) { |
| 38 | const isClickOnToggle = !! event.target.closest( '#wp-admin-bar-blog > a' ); |
| 39 | const isClickInsideMenu = document.getElementById( 'adminmenu' ).contains( event.target ); |
| 40 | const sidebarIsOpen = wpwrap.classList.contains( 'wp-responsive-open' ); |
| 41 | const shouldCloseSidebar = sidebarIsOpen && ! isClickOnToggle && ! isClickInsideMenu; |
| 42 | if ( shouldCloseSidebar ) { |
| 43 | toggleSidebar( event ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function toggleSidebar( event ) { |
| 48 | // Prevent the menu toggle from being triggered when the screen is not in mobile view. |
| 49 | // This allows the toggle to function as a link to the site's dashboard the same way it works in Calypso. |
| 50 | if ( $( window ).width() > 782 ) { |
| 51 | event.stopImmediatePropagation(); // Prevent propagation to conflicting event handlers. |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | event.preventDefault(); |
| 56 | |
| 57 | // Remove event handlers from the original toggle as its hidden and conflicts with the new toggle. |
| 58 | $( '#wp-admin-bar-menu-toggle' ).off( 'click.wp-responsive' ); |
| 59 | |
| 60 | // Close any open toolbar submenus. |
| 61 | var hovers = adminbar.querySelectorAll( '.hover' ); |
| 62 | |
| 63 | const hoverLength = hovers.length; |
| 64 | for ( var i = 0; i < hoverLength; i++ ) { |
| 65 | hovers[ i ].classList.remove( 'hover' ); |
| 66 | } |
| 67 | wpwrap.classList.toggle( 'wp-responsive-open' ); |
| 68 | if ( wpwrap.classList.contains( 'wp-responsive-open' ) ) { |
| 69 | setAriaExpanded( 'true' ); |
| 70 | var first = document.querySelector( '#adminmenu a' ); |
| 71 | if ( first ) { |
| 72 | first.focus(); |
| 73 | } |
| 74 | } else { |
| 75 | setAriaExpanded( 'false' ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if ( adminMenu ) { |
| 80 | var collapseButton = adminMenu.querySelector( '#collapse-button' ); |
| 81 | // Nav-Unification feature: |
| 82 | // Saves the sidebar state in server when "Collapse menu" is clicked. |
| 83 | // This is needed so that we update WPCOM for this preference in real-time. |
| 84 | if ( collapseButton ) { |
| 85 | collapseButton.addEventListener( 'click', function ( event ) { |
| 86 | // Let's the core event listener be triggered first. |
| 87 | setTimeout( function () { |
| 88 | saveSidebarIsExpanded( event.target.parentNode.getAttribute( 'aria-expanded' ) ); |
| 89 | }, 50 ); |
| 90 | } ); |
| 91 | } |
| 92 | |
| 93 | adminMenu.addEventListener( 'click', function ( event ) { |
| 94 | if ( |
| 95 | event.target.classList.contains( dismissClass ) || |
| 96 | event.target.closest( '.' + dismissClass ) |
| 97 | ) { |
| 98 | event.preventDefault(); |
| 99 | |
| 100 | const siteNotice = document.getElementById( 'toplevel_page_site-notices' ); |
| 101 | if ( siteNotice ) { |
| 102 | siteNotice.style.display = 'none'; |
| 103 | } |
| 104 | |
| 105 | const jitmDismissButton = event.target; |
| 106 | |
| 107 | makeAjaxRequest( |
| 108 | 'POST', |
| 109 | ajaxurl, |
| 110 | 'application/x-www-form-urlencoded; charset=UTF-8', |
| 111 | 'id=' + |
| 112 | encodeURIComponent( jitmDismissButton.dataset.feature_id ) + |
| 113 | '&feature_class=' + |
| 114 | encodeURIComponent( jitmDismissButton.dataset.feature_class ) + |
| 115 | '&action=jitm_dismiss' + |
| 116 | '&_ajax_nonce=' + |
| 117 | jetpackAdminMenu.jitmDismissNonce |
| 118 | ); |
| 119 | } |
| 120 | } ); |
| 121 | |
| 122 | makeAjaxRequest( |
| 123 | 'GET', |
| 124 | ajaxurl + '?action=upsell_nudge_jitm&_ajax_nonce=' + jetpackAdminMenu.upsellNudgeJitm, |
| 125 | undefined, |
| 126 | null, |
| 127 | function ( xhr ) { |
| 128 | try { |
| 129 | if ( xhr.readyState === XMLHttpRequest.DONE ) { |
| 130 | if ( xhr.status === 200 && xhr.responseText ) { |
| 131 | adminMenu |
| 132 | .querySelector( '#toplevel_page_site_card' ) |
| 133 | .insertAdjacentHTML( 'afterend', xhr.responseText ); |
| 134 | } |
| 135 | } |
| 136 | } catch ( error ) { |
| 137 | // On failure, we just won't display an upsell nudge |
| 138 | } |
| 139 | } |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | function makeAjaxRequest( method, url, contentType, body = null, callback = null ) { |
| 145 | var xhr = new XMLHttpRequest(); |
| 146 | xhr.open( method, url, true ); |
| 147 | xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' ); |
| 148 | if ( contentType ) { |
| 149 | xhr.setRequestHeader( 'Content-Type', contentType ); |
| 150 | } |
| 151 | xhr.withCredentials = true; |
| 152 | if ( callback ) { |
| 153 | xhr.onreadystatechange = function () { |
| 154 | callback( xhr ); |
| 155 | }; |
| 156 | } |
| 157 | xhr.send( body ); |
| 158 | } |
| 159 | |
| 160 | function saveSidebarIsExpanded( expanded ) { |
| 161 | makeAjaxRequest( |
| 162 | 'POST', |
| 163 | ajaxurl, |
| 164 | 'application/x-www-form-urlencoded; charset=UTF-8', |
| 165 | 'action=sidebar_state&expanded=' + expanded |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | function setFocusOnActiveMenuItem() { |
| 170 | var currentMenuItem = document.querySelector( '.wp-submenu .current > a' ); |
| 171 | |
| 172 | if ( ! currentMenuItem ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | currentMenuItem.focus( { preventScroll: true } ); |
| 177 | } |
| 178 | |
| 179 | if ( document.readyState === 'loading' ) { |
| 180 | document.addEventListener( 'DOMContentLoaded', init ); |
| 181 | } else { |
| 182 | init(); |
| 183 | } |
| 184 | } )( jQuery ); |
| 185 |