Skip to content

Commit ff11664

Browse files
committed
Refactor mc_build_toggles to vanilla JS
See #654
1 parent d8b8eeb commit ff11664

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/js/mcjs.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -330,43 +330,48 @@
330330

331331
function mc_build_toggles( targetId ) {
332332
if ( targetId ) {
333-
const subscribe = $( '#' + targetId + ' .mc-subscribe' );
334-
const exports = $( '#' + targetId + ' .mc-download' );
335-
if ( subscribe.length > 0 ) {
333+
const subscribe = document.querySelector( '#' + targetId + ' .mc-subscribe' );
334+
const exports = document.querySelector( '#' + targetId + ' .mc-download' );
335+
if ( null !== subscribe ) {
336336
let controls_id = 'mc_control_' + Math.floor(Math.random() * 1000 ).toString();
337337
const toggle = document.createElement( 'button' );
338338
toggle.setAttribute( 'type', 'button' );
339339
toggle.setAttribute( 'aria-controls', controls_id );
340340
toggle.setAttribute( 'aria-expanded', false );
341341
toggle.innerHTML = my_calendar.subscribe + ' <span class="dashicons dashicons-arrow-right" aria-hidden="true"></span>';
342-
subscribe.find( 'ul' ).attr( 'id', controls_id );
343-
subscribe.find( 'ul' ).css( { 'display' : 'none' } );
344-
subscribe.prepend( toggle );
342+
subscribe.querySelector( 'ul' ).setAttribute( 'id', controls_id );
343+
subscribe.querySelector( 'ul' ).style.display = 'none';
344+
subscribe.insertAdjacentElement( 'afterbegin', toggle );
345345
}
346-
if ( exports.length > 0 ) {
346+
if ( null !== exports ) {
347347
let controls_id = 'mc_control_' + Math.floor(Math.random() * 1000 ).toString();
348348
const toggle = document.createElement( 'button' );
349349
toggle.setAttribute( 'type', 'button' );
350350
toggle.setAttribute( 'aria-controls', controls_id );
351351
toggle.setAttribute( 'aria-expanded', false );
352352
toggle.innerHTML = my_calendar.export + ' <span class="dashicons dashicons-arrow-right" aria-hidden="true"></span>';
353-
exports.find( 'ul' ).attr( 'id', controls_id );
354-
exports.find( 'ul' ).css( { 'display' : 'none' } );
355-
exports.prepend( toggle );
353+
exports.querySelector( 'ul' ).setAttribute( 'id', controls_id );
354+
exports.querySelector( 'ul' ).style.display = 'none';
355+
exports.insertAdjacentElement( 'afterbegin', toggle );
356356
}
357-
const toggles = $( '#' + targetId + ' .mc-export button' );
358-
toggles.each( function() {
359-
$( this ).on( 'click', function() {
360-
let controlled = $( this ).attr( 'aria-controls' );
361-
let target = $( '#' + controlled );
362-
if ( target.is( ':visible' ) ) {
363-
target.css( { 'display' : 'none' } );
364-
$( this ).attr( 'aria-expanded', 'false' );
365-
$( this ).find( '.dashicons' ).removeClass( 'dashicons-arrow-down' ).addClass( 'dashicons-arrow-right' );
357+
const toggles = document.querySelectorAll( '#' + targetId + ' .mc-export button' );
358+
let icon;
359+
toggles.forEach( (el) => {
360+
el.addEventListener( 'click', function() {
361+
let controlled = el.getAttribute( 'aria-controls' );
362+
let target = document.getElementById( controlled );
363+
if ( target.checkVisibility() ) {
364+
target.style.display = 'none';
365+
el.setAttribute( 'aria-expanded', 'false' );
366+
icon = el.querySelector( '.dashicons' );
367+
icon.classList.remove( 'dashicons-arrow-down' );
368+
icon.classList.add( 'dashicons-arrow-right' );
366369
} else {
367-
target.css( { 'display' : 'block' } );
368-
$( this ).attr( 'aria-expanded', 'true' );
369-
$( this ).find( '.dashicons' ).removeClass( 'dashicons-arrow-right' ).addClass( 'dashicons-arrow-down' );
370+
target.style.display = 'block';
371+
el.setAttribute( 'aria-expanded', 'true' );
372+
icon = el.querySelector( '.dashicons' );
373+
icon.classList.remove( 'dashicons-arrow-right' )
374+
icon.classList.add( 'dashicons-arrow-down' );
370375
}
371376
});
372377
});

0 commit comments

Comments
 (0)