Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create GitHub JS lint action #136

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
webpack.config.js
tests/
dist/
node_modules/
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
env: { jquery: true },
extends: ['@10up/eslint-config/wordpress'],
};
39 changes: 39 additions & 0 deletions .github/workflows/jslint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: JS Lint

on:
push:
branches:
- develop
- trunk
pull_request:
branches:
- develop

jobs:
jslint:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Use desired version of NodeJS
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Generate linting report
run: npm run lint-js -- --output-file eslint-report.json --format json
continue-on-error: true
- name: Annotate code linting results
uses: ataylorme/eslint-annotate-action@1.2.0
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
report-json: 'eslint-report.json'
- name: Update summary
run: |
npm_config_yes=true npx github:10up/eslint-json-to-md --path ./eslint-report.json --output ./eslint-report.md
cat eslint-report.md >> $GITHUB_STEP_SUMMARY
if: ${{ failure() }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ vendor
.wp-env.override.json
dist/
tests/cypress/screenshots
tests/cypress/videos
tests/cypress/videos
eslint-report.json
200 changes: 115 additions & 85 deletions assets/js/src/simple-page-ordering.js
Original file line number Diff line number Diff line change
@@ -1,170 +1,200 @@
import '../../css/scss/simple-page-ordering.scss';

// eslint-disable-next-line import/no-unresolved
import 'jquery-ui-sortable';
import 'jquery';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the plugin uses @wordpress/dependency-extraction-webpack-plugin, import jquery should work fine and allow us to avoid the need for window.jQuery throughout.

Were you experiencing issues with the import?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kmgalanakis could you please respond to Peter's comment above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sidsector9 want to handle any final updates here so this can get merged in as part of the 2.5.0 release?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response @peterwilsoncc and @jeffpaul. I had very limited bandwidth for the past couple of weeks.

To address @peterwilsoncc concern, I modified the eslint configuration to be able to use jQuery without having to do it through window.

This should be good to go @jeffpaul.


const sortable_post_table = jQuery('.wp-list-table tbody');
function update_simple_ordering_callback(response) {
if ( 'children' === response ) {
if (response === 'children') {
window.location.reload();
return;
}

var changes = jQuery.parseJSON( response );
const changes = jQuery.parseJSON(response);

var new_pos = changes.new_pos;
for ( var key in new_pos ) {
if ( 'next' === key ) {
const { new_pos } = changes;
// eslint-disable-next-line no-restricted-syntax
for (const key in new_pos) {
if (key === 'next') {
// eslint-disable-next-line no-continue
continue;
}

var inline_key = document.getElementById('inline_' + key);
if ( null !== inline_key && new_pos.hasOwnProperty(key) ) {
var dom_menu_order = inline_key.querySelector('.menu_order');
const inline_key = document.getElementById(`inline_${key}`);
// eslint-disable-next-line no-prototype-builtins
if (inline_key !== null && new_pos.hasOwnProperty(key)) {
const dom_menu_order = inline_key.querySelector('.menu_order');

if ( undefined !== new_pos[key]['menu_order'] ) {
if ( null !== dom_menu_order ) {
dom_menu_order.textContent = new_pos[key]['menu_order'];
if (undefined !== new_pos[key].menu_order) {
if (dom_menu_order !== null) {
dom_menu_order.textContent = new_pos[key].menu_order;
}

var dom_post_parent = inline_key.querySelector('.post_parent');
if ( null !== dom_post_parent ) {
dom_post_parent.textContent = new_pos[key]['post_parent'];
const dom_post_parent = inline_key.querySelector('.post_parent');
if (dom_post_parent !== null) {
dom_post_parent.textContent = new_pos[key].post_parent;
}

var post_title = null;
var dom_post_title = inline_key.querySelector('.post_title');
if ( null !== dom_post_title ) {
let post_title = null;
const dom_post_title = inline_key.querySelector('.post_title');
if (dom_post_title !== null) {
post_title = dom_post_title.innerHTML;
}

var dashes = 0;
while ( dashes < new_pos[key]['depth'] ) {
post_title = '&mdash; ' + post_title;
let dashes = 0;
while (dashes < new_pos[key].depth) {
post_title = `&mdash; ${post_title}`;
dashes++;
}
var dom_row_title = inline_key.parentNode.querySelector('.row-title');
if ( null !== dom_row_title && null !== post_title ) {
const dom_row_title = inline_key.parentNode.querySelector('.row-title');
if (dom_row_title !== null && post_title !== null) {
// Decode mdash character before rendering the title.
var textArea = document.createElement('textarea');
const textArea = document.createElement('textarea');
textArea.innerHTML = post_title;
dom_row_title.textContent = textArea.value;
}
} else if ( null !== dom_menu_order ) {
} else if (dom_menu_order !== null) {
dom_menu_order.textContent = new_pos[key];
}
}
}

if ( changes.next ) {
jQuery.post( ajaxurl, {
action: 'simple_page_ordering',
id: changes.next['id'],
previd: changes.next['previd'],
nextid: changes.next['nextid'],
start: changes.next['start'],
_wpnonce: simple_page_ordering_localized_data._wpnonce,
screen_id: simple_page_ordering_localized_data.screen_id,
excluded: JSON.stringify( changes.next['excluded'] )
}, update_simple_ordering_callback );
if (changes.next) {
jQuery.post(
window.ajaxurl,
{
action: 'simple_page_ordering',
id: changes.next.id,
previd: changes.next.previd,
nextid: changes.next.nextid,
start: changes.next.start,
_wpnonce: window.simple_page_ordering_localized_data._wpnonce,
screen_id: window.simple_page_ordering_localized_data.screen_id,
excluded: JSON.stringify(changes.next.excluded),
},
update_simple_ordering_callback,
);
} else {
jQuery('.spo-updating-row').removeClass('spo-updating-row').find('.check-column').removeClass('spinner is-active');
jQuery('.spo-updating-row')
.removeClass('spo-updating-row')
.find('.check-column')
.removeClass('spinner is-active');
sortable_post_table.removeClass('spo-updating').sortable('enable');
}
}

var sortable_post_table = jQuery(".wp-list-table tbody");
sortable_post_table.sortable({
items: '> tr',
cursor: 'move',
axis: 'y',
containment: 'table.widefat',
cancel: 'input, textarea, button, select, option, .inline-edit-row',
cancel: 'input, textarea, button, select, option, .inline-edit-row',
distance: 2,
opacity: .8,
opacity: 0.8,
tolerance: 'pointer',
create: function() {
jQuery( document ).keydown(function(e) {
var key = e.key || e.keyCode;
if ( 'Escape' === key || 'Esc' === key || 27 === key ) {
sortable_post_table.sortable( 'option', 'preventUpdate', true );
sortable_post_table.sortable( 'cancel' );
create() {
jQuery(document).keydown(function (e) {
const key = e.key || e.keyCode;
if (key === 'Escape' || key === 'Esc' || key === 27) {
sortable_post_table.sortable('option', 'preventUpdate', true);
sortable_post_table.sortable('cancel');
}
});
},
start: function(e, ui){
if ( typeof(inlineEditPost) !== 'undefined' ) {
start(e, ui) {
if (typeof inlineEditPost !== 'undefined') {
// eslint-disable-next-line no-undef
inlineEditPost.revert();
}
ui.placeholder.height(ui.item.height());
ui.placeholder.empty();
},
helper: function(e, ui) {
var children = ui.children();
for ( var i=0; i<children.length; i++ ) {
var selector = jQuery(children[i]);
selector.width( selector.width() );
};
helper(e, ui) {
const children = ui.children();
for (let i = 0; i < children.length; i++) {
const selector = jQuery(children[i]);
selector.width(selector.width());
}
return ui;
},
stop: function(e, ui) {
if ( sortable_post_table.sortable( 'option', 'preventUpdate') ) {
sortable_post_table.sortable( 'option', 'preventUpdate', false );
stop(e, ui) {
if (sortable_post_table.sortable('option', 'preventUpdate')) {
sortable_post_table.sortable('option', 'preventUpdate', false);
}

// remove fixed widths
ui.item.children().css('width','');
ui.item.children().css('width', '');
},
update: function(e, ui) {
if ( sortable_post_table.sortable( 'option', 'preventUpdate') ) {
sortable_post_table.sortable( 'option', 'preventUpdate', false );
update(e, ui) {
if (sortable_post_table.sortable('option', 'preventUpdate')) {
sortable_post_table.sortable('option', 'preventUpdate', false);
return;
}

sortable_post_table.sortable('disable').addClass('spo-updating');
ui.item.addClass('spo-updating-row');
ui.item.find('.check-column').addClass('spinner is-active');

var postid = ui.item[0].id.substr(5); // post id
const postid = ui.item[0].id.substr(5); // post id

var prevpostid = false;
var prevpost = ui.item.prev();
if ( prevpost.length > 0 ) {
let prevpostid = false;
const prevpost = ui.item.prev();
if (prevpost.length > 0) {
prevpostid = prevpost.attr('id').substr(5);
}

var nextpostid = false;
var nextpost = ui.item.next();
if ( nextpost.length > 0 ) {
let nextpostid = false;
const nextpost = ui.item.next();
if (nextpost.length > 0) {
nextpostid = nextpost.attr('id').substr(5);
}

// go do the sorting stuff via ajax
jQuery.post( ajaxurl, { action: 'simple_page_ordering', id: postid, previd: prevpostid, nextid: nextpostid, _wpnonce: simple_page_ordering_localized_data._wpnonce, screen_id: simple_page_ordering_localized_data.screen_id, }, update_simple_ordering_callback );
jQuery.post(
window.ajaxurl,
{
action: 'simple_page_ordering',
id: postid,
previd: prevpostid,
nextid: nextpostid,
_wpnonce: window.simple_page_ordering_localized_data._wpnonce,
screen_id: window.simple_page_ordering_localized_data.screen_id,
},
update_simple_ordering_callback,
);

// fix cell colors
var table_rows = document.querySelectorAll('tr.iedit'),
table_row_count = table_rows.length;
while( table_row_count-- ) {
if ( 0 === table_row_count%2 ) {
const table_rows = document.querySelectorAll('tr.iedit');
let table_row_count = table_rows.length;
while (table_row_count--) {
if (table_row_count % 2 === 0) {
jQuery(table_rows[table_row_count]).addClass('alternate');
} else {
jQuery(table_rows[table_row_count]).removeClass('alternate');
}
}
}
},
});

jQuery( function() {
jQuery(function () {
// set up click handler for order reset link
jQuery( '#simple-page-ordering-reset' ).on( 'click', function(e) {
jQuery('#simple-page-ordering-reset').on('click', function (e) {
e.preventDefault();
var post_type = jQuery( this ).data( 'posttype' );
if ( window.confirm( 'Are you sure you want to reset the ' + post_type + ' order?' ) ) {
jQuery.post( ajaxurl, {
action: 'reset_simple_page_ordering',
post_type: post_type,
_wpnonce: simple_page_ordering_localized_data._wpnonce,
screen_id: simple_page_ordering_localized_data.screen_id
}, function() { window.location.reload(); } );
const post_type = jQuery(this).data('posttype');
// eslint-disable-next-line no-alert
if (window.confirm(`Are you sure you want to reset the ${post_type} order?`)) {
jQuery.post(
window.ajaxurl,
{
action: 'reset_simple_page_ordering',
post_type,
_wpnonce: window.simple_page_ordering_localized_data._wpnonce,
screen_id: window.simple_page_ordering_localized_data.screen_id,
},
function () {
window.location.reload();
},
);
}
} );
});
});
Loading