Skip to content

Commit

Permalink
fix JS coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Jan 30, 2024
1 parent 19528d8 commit d030029
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 347 deletions.
155 changes: 81 additions & 74 deletions src/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import apiFetch from '@wordpress/api-fetch';
import { __ } from '@wordpress/i18n';

const hideElement = (hide, element) => {
if (hide) {
element.classList.add('hidden');
element.setAttribute('aria-hidden', 'true');
const hideElement = ( hide, element ) => {
if ( hide ) {
element.classList.add( 'hidden' );
element.setAttribute( 'aria-hidden', 'true' );
} else {
element.classList.remove('hidden');
element.setAttribute('aria-hidden', 'false');
element.classList.remove( 'hidden' );
element.setAttribute( 'aria-hidden', 'false' );
}
};

document
.querySelector('#icw-fix-content-btn')
.addEventListener('click', async (event) => {
.querySelector( '#icw-fix-content-btn' )
.addEventListener( 'click', async ( event ) => {
event.preventDefault();

const postSelection = document.querySelector(
'#icw-post-selection'
).value;
const postIds = document.querySelector('#icw-post-ids').value;
const postType = document.querySelector('#icw-post-type').value;
const batchSize = +document.querySelector('#icw-batch-size').value;
const dryRun = document.querySelector('#icw-dry-run').checked;
const log = document.querySelector('#icw-fix-log');
const loadingSpinner = document.querySelector('#icw-loading-spinner');
const btn = document.querySelector('#icw-fix-content-btn');
const postIds = document.querySelector( '#icw-post-ids' ).value;
const postType = document.querySelector( '#icw-post-type' ).value;
const batchSize = +document.querySelector( '#icw-batch-size' ).value;
const dryRun = document.querySelector( '#icw-dry-run' ).checked;
const log = document.querySelector( '#icw-fix-log' );
const loadingSpinner = document.querySelector( '#icw-loading-spinner' );
const btn = document.querySelector( '#icw-fix-content-btn' );

const toggleSpinnerVisibility = () => {
loadingSpinner.classList.toggle('hidden');
loadingSpinner.setAttribute('aria-hidden', 'true');
loadingSpinner.classList.toggle( 'hidden' );
loadingSpinner.setAttribute( 'aria-hidden', 'true' );
};

const disableButton = (disable) => {
if (disable) {
btn.setAttribute('disabled', '');
const disableButton = ( disable ) => {
if ( disable ) {
btn.setAttribute( 'disabled', '' );
} else {
btn.removeAttribute('disabled');
btn.removeAttribute( 'disabled' );
}
};

const setLogHTML = (html) => {
const setLogHTML = ( html ) => {
log.innerHTML = html;

log.scrollTop = log.scrollHeight;
};

toggleSpinnerVisibility();
disableButton(true);
setLogHTML('');
hideElement(true, log);
disableButton( true );
setLogHTML( '' );
hideElement( true, log );

const count = await apiFetch({
const count = await apiFetch( {
path: `/icw/v1/count-for-fix`,
method: 'POST',
data: {
Expand All @@ -61,30 +61,31 @@ document
postSelection === 'all_from_post_type' ? postType : '',
batchSize,
},
});
} );

const times = Math.ceil(count / batchSize);
const times = Math.ceil( count / batchSize );

hideElement(false, log);
hideElement( false, log );

const getProgressBar = (time, __times, __batchSize, __count) => {
const getProgressBar = ( time, __times, __batchSize, __count ) => {
const progressBarMax = 50;
const percentage = time > 0 ? Math.ceil((100 / __times) * time) : 0;
const value = (percentage * progressBarMax) / 100;
const percentage =
time > 0 ? Math.ceil( ( 100 / __times ) * time ) : 0;
const value = ( percentage * progressBarMax ) / 100;
const completed = time !== __times ? __batchSize * time : __count;
return `<span>[${'#'.repeat(value)}${'-'.repeat(
return `<span>[${ '#'.repeat( value ) }${ '-'.repeat(
progressBarMax - value
)}] | ${percentage}% || ${completed}/${__count} ${__(
) }] | ${ percentage }% || ${ completed }/${ __count } ${ __(
'posts',
'insecure-content-warning'
)}</span>`;
) }</span>`;
};

setLogHTML(getProgressBar(0, times, batchSize, count));
setLogHTML( getProgressBar( 0, times, batchSize, count ) );
let innerHTML = '';
for (let i = 1; i <= times; i++) {
for ( let i = 1; i <= times; i++ ) {
// eslint-disable-next-line no-await-in-loop
const data = await apiFetch({
const data = await apiFetch( {
path: `/icw/v1/fix`,
method: 'POST',
data: {
Expand All @@ -93,46 +94,52 @@ document
postType:
postSelection === 'all_from_post_type' ? postType : '',
batchSize,
offset: (i - 1) * batchSize,
offset: ( i - 1 ) * batchSize,
dryRun,
},
});
} );

innerHTML += data;
setLogHTML(innerHTML + getProgressBar(i, times, batchSize, count));
setLogHTML(
innerHTML + getProgressBar( i, times, batchSize, count )
);
}

toggleSpinnerVisibility();
disableButton(false);
});

document.querySelector('#icw-post-selection').addEventListener('change', () => {
const postSelection = document.querySelector('#icw-post-selection').value;

const hidePostIdsRow = (hide) => {
const postIdsRow = document.querySelector('#icw-post-ids-row');
hideElement(hide, postIdsRow);
};

const hidePostTypeRow = (hide) => {
const postTypeRow = document.querySelector('#icw-post-type-row');
hideElement(hide, postTypeRow);
};

switch (postSelection) {
case 'all':
hidePostIdsRow(true);
hidePostTypeRow(true);
break;
case 'posts':
hidePostIdsRow(false);
hidePostTypeRow(true);
break;
case 'all_from_post_type':
hidePostTypeRow(false);
hidePostIdsRow(true);
break;
default:
break;
}
});
disableButton( false );
} );

document
.querySelector( '#icw-post-selection' )
.addEventListener( 'change', () => {
const postSelection = document.querySelector(
'#icw-post-selection'
).value;

const hidePostIdsRow = ( hide ) => {
const postIdsRow = document.querySelector( '#icw-post-ids-row' );
hideElement( hide, postIdsRow );
};

const hidePostTypeRow = ( hide ) => {
const postTypeRow = document.querySelector( '#icw-post-type-row' );
hideElement( hide, postTypeRow );
};

switch ( postSelection ) {
case 'all':
hidePostIdsRow( true );
hidePostTypeRow( true );
break;
case 'posts':
hidePostIdsRow( false );
hidePostTypeRow( true );
break;
case 'all_from_post_type':
hidePostTypeRow( false );
hidePostIdsRow( true );
break;
default:
break;
}
} );
80 changes: 41 additions & 39 deletions src/js/classic-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ import replaceContent from './utils/replace';
import '../css/admin.scss';

// Listen for clicks on the publish button
jQuery(document).on('click', '#publish', (event) => {
jQuery( document ).on( 'click', '#publish', ( event ) => {
blurInsecure();
if (
!jQuery(event.target).hasClass('disabled') &&
jQuery('.js-icw-force-checkbox').prop('checked') !== true
! jQuery( event.target ).hasClass( 'disabled' ) &&
jQuery( '.js-icw-force-checkbox' ).prop( 'checked' ) !== true
) {
checkContent(event);
checkContent( event );
}
});
} );

// Listen for clicks on the force publish checkbox
jQuery(document).on('change', '#icw-force-checkbox', function () {
jQuery( document ).on( 'change', '#icw-force-checkbox', function () {
// Enable or disable the publish button as needed
blurInsecure();
if (jQuery(this).is(':checked')) {
jQuery('#publish').removeClass('disabled');
if ( jQuery( this ).is( ':checked' ) ) {
jQuery( '#publish' ).removeClass( 'disabled' );
} else {
jQuery('#publish').addClass('disabled');
jQuery( '#publish' ).addClass( 'disabled' );
}
});
} );

/**
* If the preview button is clicked after
Expand All @@ -34,72 +34,74 @@ jQuery(document).on('change', '#icw-force-checkbox', function () {
* the update button and then the preview button,
* as it expects a page refresh after updates
*/
jQuery(document).on('click', '#post-preview', () => {
jQuery( document ).on( 'click', '#post-preview', () => {
blurInsecure();
if (document.querySelector('.js-icw-error')) {
if (wp.autosave) {
if ( document.querySelector( '.js-icw-error' ) ) {
if ( wp.autosave ) {
wp.autosave.server.resume();
wp.autosave.enableButtons();
} else {
jQuery(document).trigger('autosave-enable-buttons');
jQuery( document ).trigger( 'autosave-enable-buttons' );
}

jQuery('#major-publishing-actions .spinner').removeClass('is-active');
jQuery( '#major-publishing-actions .spinner' ).removeClass(
'is-active'
);
}
});
} );

// Listen for clicks on the fix asset links
jQuery(document).on('click', '.js-icw-check', function (e) {
jQuery( document ).on( 'click', '.js-icw-check', function ( e ) {
e.preventDefault();
blurInsecure();

const spinner = jQuery(this).next('.js-icw-spinner');
const url = jQuery(this).data('check');
const spinner = jQuery( this ).next( '.js-icw-spinner' );
const url = jQuery( this ).data( 'check' );

spinner.show();

wp.apiRequest({ path: `/icw/v1/check?url=${url}` }).then(
(data) => {
wp.apiRequest( { path: `/icw/v1/check?url=${ url }` } ).then(
( data ) => {
spinner.hide();

// Attempt to replace if https equivalent found.
if (data === true) {
jQuery(this).nextAll('.js-icw-fixed').show();
replaceContent(url);
if ( data === true ) {
jQuery( this ).nextAll( '.js-icw-fixed' ).show();
replaceContent( url );
} else {
// show the error
jQuery(this).nextAll('.js-icw-error').show();
throw new Error('No https equivalent found.');
jQuery( this ).nextAll( '.js-icw-error' ).show();
throw new Error( 'No https equivalent found.' );
}

setTimeout(function () {
checkContent(e);
}, 1000);
setTimeout( function () {
checkContent( e );
}, 1000 );
},
(err) => {
( err ) => {
// Don't recheck if replace unsuccessful.
return err;
}
);
});
} );

jQuery(document).on('click', '.js-icw-view', function (e) {
jQuery( document ).on( 'click', '.js-icw-view', function ( e ) {
e.preventDefault();
blurInsecure();

const url = jQuery(this).data('check');
const elements = findElements(url);
const url = jQuery( this ).data( 'check' );
const elements = findElements( url );

if (elements.length) {
const $element = jQuery(elements[0]);
if ( elements.length ) {
const $element = jQuery( elements[ 0 ] );

jQuery('html, body').animate(
jQuery( 'html, body' ).animate(
{
scrollTop: $element.offset().top,
},
0
);

$element.addClass('js-icw-is-insecure');
$element.addClass( 'js-icw-is-insecure' );
}
});
} );
Loading

0 comments on commit d030029

Please sign in to comment.