-
-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate jQuery code to native JavaScript #11
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
Changes from 1 commit
b4a0a1c
670d3d4
42feba3
8fd9d1e
3a9b273
3099574
c9647c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,55 +4,40 @@ | |
| * @package unique-title-checker | ||
| */ | ||
|
|
||
| jQuery( document ).ready( | ||
| function () { | ||
|
|
||
| jQuery( '#title' ).blur( | ||
| function () { | ||
|
|
||
| var title = jQuery( this ).val(); | ||
|
|
||
| // Show no warning on empty titles. | ||
| if ( '' === title ) { | ||
| return; | ||
| } | ||
|
|
||
| var request_data = { | ||
| action : 'unique_title_check', | ||
| ajax_nonce : unique_title_checker.nonce, | ||
| post__not_in: [ jQuery( '#post_ID' ).val() ], | ||
| post_type : jQuery( '#post_type' ).val(), | ||
| post_title : title | ||
| }; | ||
|
|
||
| jQuery.ajax( | ||
| { | ||
| url : ajaxurl, | ||
| data : request_data, | ||
| dataType: 'json' | ||
| } | ||
| ).done( | ||
| function ( data ) { | ||
| jQuery( '#unique-title-message' ).remove(); | ||
| document.addEventListener( "DOMContentLoaded", function () { | ||
| document.getElementById( "title" ).addEventListener( "blur", function () { | ||
| var title = this.value; | ||
|
|
||
| // Show no warning on empty titles. | ||
| if (title === "") { | ||
| return; | ||
| } | ||
|
|
||
| var requestData = new URLSearchParams( { | ||
| action: "unique_title_check", | ||
| ajax_nonce: unique_title_checker.nonce, | ||
| post_id: document.getElementById( "post_ID" ).value, | ||
| post_type: document.getElementById( "post_type" ).value, | ||
| post_title: title | ||
| } ); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap these in multiple lines as well. |
||
|
|
||
| fetch( ajaxurl + "?" + requestData.toString() ) | ||
| .then( response => response.json() ) | ||
| .then( data => { | ||
| var messageElement = document.getElementById( "unique-title-message" ); | ||
| if (messageElement) { | ||
| messageElement.remove(); | ||
| } | ||
|
|
||
| if ( 'error' === data.status || ! unique_title_checker.only_unique_error ) { | ||
| jQuery( '#post' ).before( | ||
| jQuery( | ||
| '<div>', | ||
| { | ||
| id: 'unique-title-message', | ||
| class: data.status | ||
| } | ||
| ).append( | ||
| jQuery( '<p>' ).text( data.message ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| if (data.status === "error" || !unique_title_checker.only_unique_error) { | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add "globals" to get rid of the warnings. |
||
| document.getElementById( "post" ).insertAdjacentHTML( | ||
| "beforebegin", | ||
| `<div id="unique-title-message" class="${ data.status }"> | ||
| <p>${ data.message }</p> | ||
| </div>` | ||
|
Comment on lines
+40
to
+42
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe wrap in multiple lines. |
||
| ); | ||
|
|
||
| } | ||
| ); | ||
|
|
||
| } | ||
| ); | ||
| } ) | ||
| .catch( error => console.error( "Error fetching unique title check:", error ) ); | ||
| } ); | ||
| } ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap these into multiple lines (PHPCS)