-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert script to vanilla JS, use button element, adjust spacing
- Loading branch information
Showing
3 changed files
with
141 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,135 @@ | ||
;jQuery( function( $ ) { | ||
|
||
/** | ||
* Get a link to an "Add another repeatable group" link. | ||
* | ||
* @return {String} | ||
*/ | ||
function wp_seo_add_more_button() { | ||
return $( '<a href="#" class="button-secondary wp-seo-add" />' ).text( wp_seo_admin.repeatable_add_more_label ); | ||
} | ||
|
||
/** | ||
* Toggle the display of the "Remove group" links for a group of nodes. | ||
* | ||
* @param {Object} $parent The .node parent | ||
*/ | ||
function wp_seo_toggle_removes( $parent ) { | ||
$( '.wp-seo-delete', $parent ).toggle( $parent.children().length > 1 ); | ||
} | ||
|
||
/** | ||
* Update the description and title character counts displayed to the user. | ||
*/ | ||
function wp_seo_update_character_counts() { | ||
_.each( wp_seo_admin.character_count_fields, function( field ) { | ||
var input = $( '#wp_seo_meta_' + field ); | ||
if ( input.length > 0 ) { | ||
$( '.' + field + '-character-count' ).html( input.val().length ); | ||
} | ||
}); | ||
} | ||
|
||
wp_seo_update_character_counts(); | ||
$( '.wp-seo-post-meta-fields, .wp-seo-term-meta-fields' ).find( 'input, textarea' ).keyup( wp_seo_update_character_counts ); | ||
// Update the character counts after a term is added via AJAX. | ||
$( document ).ajaxComplete( function() { | ||
if ( $( '#addtag' ).length > 0 ) { | ||
wp_seo_update_character_counts(); | ||
} | ||
} ); | ||
|
||
/** | ||
* Add a "Remove" link to groups. | ||
* | ||
* Appended here to easily use the same localized field label. | ||
*/ | ||
$( '.wp-seo-repeatable-group' ).append( $( '<a href="#" class="wp-seo-delete" />' ).text( wp_seo_admin.repeatable_remove_label ) ); | ||
|
||
$( '.wp-seo-repeatable' ) | ||
// Append the "Add More" button to each repeatable field. | ||
.append( wp_seo_add_more_button() ) | ||
// Toggle the "Remove" link from each group as needed. | ||
.each( function( i, el ) { | ||
wp_seo_toggle_removes( $( el ).find( '> .nodes' ) ); | ||
} ); | ||
|
||
/** | ||
* Add a repeatable group on click. | ||
*/ | ||
$( '#wp_seo_settings' ).on( 'click', '.wp-seo-add', function( e ) { | ||
e.preventDefault(); | ||
var $tpl = $( this ).siblings( '.wp-seo-template' ); | ||
var html = _.template( $tpl.html() ); | ||
$tpl.data( 'start', $tpl.data( 'start' ) + 1 ); | ||
$( this ).siblings( '.nodes' ).append( html( { i: $tpl.data( 'start' ) } ) ); | ||
wp_seo_toggle_removes( $( this ).siblings( '.nodes' ) ); | ||
} ); | ||
|
||
/** | ||
* Remove a repeatable group on click. | ||
*/ | ||
$( '#wp_seo_settings' ).on( 'click', '.wp-seo-delete', function( e ) { | ||
e.preventDefault(); | ||
$( this ).parent().hide( 'fast', function(){ | ||
$parent = $( this ).parent(); | ||
$( this ).remove(); | ||
wp_seo_toggle_removes( $parent ); | ||
} ); | ||
} ); | ||
|
||
} ); | ||
/** | ||
* Functionality related to the WP SEO admin controls. | ||
*/ | ||
|
||
const init = () => { | ||
setUpRepeatableGroups(); | ||
setUpCharacterCountFields(); | ||
} | ||
|
||
/** | ||
* Create an "Add another repeatable group" button. | ||
* | ||
* @return {HTMLButtonElement} | ||
*/ | ||
const wpSeoAddMoreButton = () => { | ||
const addMoreButton = document.createElement('button'); | ||
addMoreButton.setAttribute('type', 'button'); | ||
addMoreButton.classList.add('button-secondary', 'wp-seo-add'); | ||
addMoreButton.textContent = wp_seo_admin.repeatable_add_more_label; | ||
return addMoreButton; | ||
} | ||
|
||
/** | ||
* Toggle the display of the "Remove group" links for a group of nodes. | ||
* | ||
* @param {Object} $parent The .node parent | ||
*/ | ||
const wpSeoToggleRemoves = (parent) => { | ||
const deleteButton = parent.querySelector('.wp-seo-delete'); | ||
|
||
if (parent.children.length > 1) { | ||
deleteButton.style.display = 'block'; | ||
} else { | ||
deleteButton.style.display = 'none'; | ||
} | ||
} | ||
|
||
/** | ||
* Update the description and title character counts displayed to the user. | ||
*/ | ||
const wpSeoUpdateCharacterCounts = () => { | ||
wp_seo_admin.character_count_fields.forEach((field) => { | ||
var input = document.querySelector(`#wp_seo_meta_${field}`); | ||
|
||
if (input.value.length >= 0) { | ||
const countElement = document.querySelector(`.${field}-character-count`); | ||
countElement.innerHTML = input.value.length; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Handles clicks on add and remove buttons for each repeatable group. | ||
* | ||
* @param {event} Event button event. | ||
*/ | ||
const handleButtonClick = (event) => { | ||
event.preventDefault(); | ||
|
||
const buttonsOnly = event.target.closest("button"); | ||
if (buttonsOnly === null) return; | ||
|
||
if (event.target.classList.contains('wp-seo-add')) { | ||
// Add a repeatable group on click. | ||
const template = event.target.previousElementSibling; | ||
const html = _.template(event.target.previousElementSibling.innerHTML); | ||
const templateStart = template.getAttribute('data-start'); | ||
|
||
template.previousElementSibling.insertAdjacentHTML('beforeend', html({ i: templateStart })); | ||
|
||
template.setAttribute('data-start', parseInt(templateStart) + 1); | ||
wpSeoToggleRemoves(template.previousElementSibling); | ||
} else { | ||
// Remove a repeatable group on click. | ||
const parentElement = event.target.parentNode; | ||
|
||
parentElement.remove(); | ||
wpSeoToggleRemoves(parentElement); | ||
} | ||
}; | ||
|
||
/** | ||
* Add character count to fields where it is enabled. | ||
*/ | ||
const setUpCharacterCountFields = () => { | ||
const characterCountFields = document.querySelectorAll('.wp-seo-post-meta-fields input, .wp-seo-post-meta-fields textarea, .wp-seo-term-meta-fields input, .wp-seo-term-meta-fields textarea'); | ||
|
||
if (characterCountFields.length) { | ||
wpSeoUpdateCharacterCounts(); | ||
} | ||
|
||
characterCountFields?.forEach((field) => { | ||
field.addEventListener('keyup', wpSeoUpdateCharacterCounts); | ||
}); | ||
|
||
// Update the character counts after a term is added via AJAX. | ||
jQuery(document).ajaxComplete(function () { | ||
if (jQuery('#addtag').length > 0) { | ||
wpSeoUpdateCharacterCounts(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Add controls to repeatable groups. | ||
*/ | ||
const setUpRepeatableGroups = () => { | ||
const repeatableGroups = document.querySelectorAll('.wp-seo-repeatable-group'); | ||
const repeatableFields = document.querySelectorAll('.wp-seo-repeatable'); | ||
|
||
/** | ||
* Add a "Remove" button to each field group. | ||
*/ | ||
repeatableGroups?.forEach((group) => { | ||
const deleteButton = document.createElement('button'); | ||
deleteButton.setAttribute('type', 'button'); | ||
deleteButton.classList.add('wp-seo-delete'); | ||
deleteButton.textContent = wp_seo_admin.repeatable_remove_label; | ||
|
||
group.appendChild(deleteButton); | ||
}); | ||
|
||
/** | ||
* Add an "Add" button to each repeatable group. | ||
*/ | ||
repeatableFields?.forEach((repeatable) => { | ||
repeatable.appendChild(wpSeoAddMoreButton()); | ||
const repeatableContainer = repeatable.querySelector('.nodes'); | ||
wpSeoToggleRemoves(repeatableContainer); | ||
|
||
repeatable.addEventListener('click', handleButtonClick); | ||
}); | ||
} | ||
|
||
init(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters