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

Rewrite admin/includes/js/admin.js without using jQuery. #75

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

itpixelz
Copy link
Collaborator

@itpixelz itpixelz commented Apr 8, 2020

I have rewritten the entire code of admin/includes/js/admin.js in VanillaJS without using any library like jQuery. #10

There is some portion of the code which I found of no use so I did not write it, let me know if that is still required, as I have doubled checked in the Settings page and that is working fine without using this portion of the code as below.

$( function() {
		$( 'body.options-general-php select#WPLANG' ).each( function() {
			$( this ).find( 'option[selected="selected"]' ).removeAttr( 'selected' );
			var val = bogo.defaultLocale || 'en_US';
			val = ( 'en_US' == val ? '' : val );
			$( this ).find( 'option[value="' + val + '"]' ).first().attr( 'selected', 'selected' );
		} );
	} );

@takayukister
Copy link
Collaborator

This is admin.js that is used only on admin pages, and only a few latest versions of major browsers are allowed to access WordPress admin screen, so you can and should use modern JS coding more aggressively. You don't have to care about backward compatibility for legacy browsers that doesn't support ES2015.

Some points where you can improve the code are:

  1. Use const and let instead of var. This is also mentioned in the WordPress coding standard.

  2. Use querySelector() and querySelectorAll() where available. All getElementById() can be replaced to them.

  3. Use IIFE.

function init_bogo_admin() {
  ...
}

init_bogo_admin();

This can be written with IIFE as:

( function() {
  ...
} )();
  1. Don't use XMLHttpRequest directly. You can use wp.apiFetch to trigger REST API requests.

  2. When you code something that manipulates DOM tree, always put it inside a DOMContentLoaded event function.

  3. For consistency, avoid using on(action) properties for events. Use addEventListener() instead.

@itpixelz
Copy link
Collaborator Author

I will submit the code in a couple of days with the implementation of TypeScript to match it with the latest coding trends and to match the suggestions above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants