admin-menu.js
4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* global ajaxurl, jetpackAdminMenu */
( function () {
function init() {
var adminbar = document.querySelector( '#wpadminbar' );
var wpwrap = document.querySelector( '#wpwrap' );
var adminMenu = document.querySelector( '#adminmenu' );
var dismissClass = 'dismissible-card__close-icon';
if ( ! adminbar ) {
return;
}
function setAriaExpanded( value ) {
var anchors = adminbar.querySelectorAll( '#wp-admin-bar-blog a' );
for ( var i = 0; i < anchors.length; i++ ) {
anchors[ i ].setAttribute( 'aria-expanded', value );
}
}
setFocusOnActiveMenuItem();
setAriaExpanded( 'false' );
var adminbarBlog = adminbar.querySelector( '#wp-admin-bar-blog' );
// Toggle sidebar when toggle is clicked.
if ( adminbarBlog ) {
adminbarBlog.addEventListener( 'click', function ( event ) {
event.preventDefault();
// Close any open toolbar submenus.
var hovers = adminbar.querySelectorAll( '.hover' );
for ( var i = 0; i < hovers.length; i++ ) {
hovers[ i ].classList.remove( 'hover' );
}
wpwrap.classList.toggle( 'wp-responsive-open' );
if ( wpwrap.classList.contains( 'wp-responsive-open' ) ) {
setAriaExpanded( 'true' );
var first = document.querySelector( '#adminmenu a' );
if ( first ) {
first.focus();
}
} else {
setAriaExpanded( 'false' );
}
} );
}
if ( adminMenu ) {
var collapseButton = adminMenu.querySelector( '#collapse-button' );
// Nav-Unification feature:
// Saves the sidebar state in server when "Collapse menu" is clicked.
// This is needed so that we update WPCOM for this preference in real-time.
if ( collapseButton ) {
collapseButton.addEventListener( 'click', function ( event ) {
// Let's the core event listener be triggered first.
setTimeout( function () {
saveSidebarIsExpanded( event.target.parentNode.ariaExpanded );
}, 50 );
} );
}
adminMenu.addEventListener( 'click', function ( event ) {
if (
event.target.classList.contains( dismissClass ) ||
event.target.closest( '.' + dismissClass )
) {
event.preventDefault();
const siteNotice = document.getElementById( 'toplevel_page_site-notices' );
if ( siteNotice ) {
siteNotice.style.display = 'none';
}
const jitmDismissButton = event.target;
makeAjaxRequest(
'POST',
ajaxurl,
'application/x-www-form-urlencoded; charset=UTF-8',
'id=' +
encodeURIComponent( jitmDismissButton.dataset.feature_id ) +
'&feature_class=' +
encodeURIComponent( jitmDismissButton.dataset.feature_class ) +
'&action=jitm_dismiss' +
'&_ajax_nonce=' +
jetpackAdminMenu.jitmDismissNonce
);
}
} );
makeAjaxRequest(
'GET',
ajaxurl + '?action=upsell_nudge_jitm&_ajax_nonce=' + jetpackAdminMenu.upsellNudgeJitm,
undefined,
null,
function ( xhr ) {
try {
if ( xhr.readyState === XMLHttpRequest.DONE ) {
if ( xhr.status === 200 && xhr.responseText ) {
adminMenu
.querySelector( '#toplevel_page_site_card' )
.insertAdjacentHTML( 'afterend', xhr.responseText );
}
}
} catch ( error ) {
// On failure, we just won't display an upsell nudge
}
}
);
}
}
function makeAjaxRequest( method, url, contentType, body = null, callback = null ) {
var xhr = new XMLHttpRequest();
xhr.open( method, url, true );
xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );
if ( contentType ) {
xhr.setRequestHeader( 'Content-Type', contentType );
}
xhr.withCredentials = true;
if ( callback ) {
xhr.onreadystatechange = function () {
callback( xhr );
};
}
xhr.send( body );
}
function saveSidebarIsExpanded( expanded ) {
makeAjaxRequest(
'POST',
ajaxurl,
'application/x-www-form-urlencoded; charset=UTF-8',
'action=sidebar_state&expanded=' + expanded
);
}
function setFocusOnActiveMenuItem() {
var currentMenuItem = document.querySelector( '.wp-submenu .current > a' );
if ( ! currentMenuItem ) {
return;
}
currentMenuItem.focus( { preventScroll: true } );
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', init );
} else {
init();
}
} )();