1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
/**
* @output wp-admin/js/tags-box.js
*/
/* jshint curly: false, eqeqeq: false */
/* global ajaxurl, tagBox, array_unique_noempty */
( function( $ ) {
var tagDelimiter = ( window.tagsSuggestL10n && window.tagsSuggestL10n.tagDelimiter ) || ',';
/**
* Filters unique items and returns a new array.
*
* Filters all items from an array into a new array containing only the unique
* items. This also excludes whitespace or empty values.
*
* @since 2.8.0
*
* @global
*
* @param {Array} array The array to filter through.
*
* @return {Array} A new array containing only the unique items.
*/
window.array_unique_noempty = function( array ) {
var out = [];
// Trim the values and ensure they are unique.
$.each( array, function( key, val ) {
val = $.trim( val );
if ( val && $.inArray( val, out ) === -1 ) {
out.push( val );
}
} );
return out;
};
/**
* The TagBox object.
*
* Contains functions to create and manage tags that can be associated with a
* post.
*
* @since 2.9.0
*
* @global
*/
window.tagBox = {
/**
* Cleans up tags by removing redundant characters.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @param {string} tags Comma separated tags that need to be cleaned up.
*
* @return {string} The cleaned up tags.
*/
clean : function( tags ) {
if ( ',' !== tagDelimiter ) {
tags = tags.replace( new RegExp( tagDelimiter, 'g' ), ',' );
}
tags = tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
if ( ',' !== tagDelimiter ) {
tags = tags.replace( /,/g, tagDelimiter );
}
return tags;
},
/**
* Parses tags and makes them editable.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @param {Object} el The tag element to retrieve the ID from.
*
* @return {boolean} Always returns false.
*/
parseTags : function(el) {
var id = el.id,
num = id.split('-check-num-')[1],
taxbox = $(el).closest('.tagsdiv'),
thetags = taxbox.find('.the-tags'),
current_tags = thetags.val().split( tagDelimiter ),
new_tags = [];
delete current_tags[num];
// Sanitize the current tags and push them as if they're new tags.
$.each( current_tags, function( key, val ) {
val = $.trim( val );
if ( val ) {
new_tags.push( val );
}
});
thetags.val( this.clean( new_tags.join( tagDelimiter ) ) );
this.quickClicks( taxbox );
return false;
},
/**
* Creates clickable links, buttons and fields for adding or editing tags.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @param {Object} el The container HTML element.
*
* @return {void}
*/
quickClicks : function( el ) {
var thetags = $('.the-tags', el),
tagchecklist = $('.tagchecklist', el),
id = $(el).attr('id'),
current_tags, disabled;
if ( ! thetags.length )
return;
disabled = thetags.prop('disabled');
current_tags = thetags.val().split( tagDelimiter );
tagchecklist.empty();
/**
* Creates a delete button if tag editing is enabled, before adding it to the tag list.
*
* @since 2.5.0
*
* @memberOf tagBox
*
* @param {string} key The index of the current tag.
* @param {string} val The value of the current tag.
*
* @return {void}
*/
$.each( current_tags, function( key, val ) {
var listItem, xbutton;
val = $.trim( val );
if ( ! val )
return;
// Create a new list item, and ensure the text is properly escaped.
listItem = $( '<li />' ).text( val );
// If tags editing isn't disabled, create the X button.
if ( ! disabled ) {
/*
* Build the X buttons, hide the X icon with aria-hidden and
* use visually hidden text for screen readers.
*/
xbutton = $( '<button type="button" id="' + id + '-check-num-' + key + '" class="ntdelbutton">' +
'<span class="remove-tag-icon" aria-hidden="true"></span>' +
'<span class="screen-reader-text">' + window.tagsSuggestL10n.removeTerm + ' ' + listItem.html() + '</span>' +
'</button>' );
/**
* Handles the click and keypress event of the tag remove button.
*
* Makes sure the focus ends up in the tag input field when using
* the keyboard to delete the tag.
*
* @since 4.2.0
*
* @param {Event} e The click or keypress event to handle.
*
* @return {void}
*/
xbutton.on( 'click keypress', function( e ) {
// On click or when using the Enter/Spacebar keys.
if ( 'click' === e.type || 13 === e.keyCode || 32 === e.keyCode ) {
/*
* When using the keyboard, move focus back to the
* add new tag field. Note: when releasing the pressed
* key this will fire the `keyup` event on the input.
*/
if ( 13 === e.keyCode || 32 === e.keyCode ) {
$( this ).closest( '.tagsdiv' ).find( 'input.newtag' ).focus();
}
tagBox.userAction = 'remove';
tagBox.parseTags( this );
}
});
listItem.prepend( ' ' ).prepend( xbutton );
}
// Append the list item to the tag list.
tagchecklist.append( listItem );
});
// The buttons list is built now, give feedback to screen reader users.
tagBox.screenReadersMessage();
},
/**
* Adds a new tag.
*
* Also ensures that the quick links are properly generated.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @param {Object} el The container HTML element.
* @param {Object|boolean} a When this is an HTML element the text of that
* element will be used for the new tag.
* @param {number|boolean} f If this value is not passed then the tag input
* field is focused.
*
* @return {boolean} Always returns false.
*/
flushTags : function( el, a, f ) {
var tagsval, newtags, text,
tags = $( '.the-tags', el ),
newtag = $( 'input.newtag', el );
a = a || false;
text = a ? $(a).text() : newtag.val();
/*
* Return if there's no new tag or if the input field is empty.
* Note: when using the keyboard to add tags, focus is moved back to
* the input field and the `keyup` event attached on this field will
* fire when releasing the pressed key. Checking also for the field
* emptiness avoids to set the tags and call quickClicks() again.
*/
if ( 'undefined' == typeof( text ) || '' === text ) {
return false;
}
tagsval = tags.val();
newtags = tagsval ? tagsval + tagDelimiter + text : text;
newtags = this.clean( newtags );
newtags = array_unique_noempty( newtags.split( tagDelimiter ) ).join( tagDelimiter );
tags.val( newtags );
this.quickClicks( el );
if ( ! a )
newtag.val('');
if ( 'undefined' == typeof( f ) )
newtag.focus();
return false;
},
/**
* Retrieves the available tags and creates a tagcloud.
*
* Retrieves the available tags from the database and creates an interactive
* tagcloud. Clicking a tag will add it.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @param {string} id The ID to extract the taxonomy from.
*
* @return {void}
*/
get : function( id ) {
var tax = id.substr( id.indexOf('-') + 1 );
/**
* Puts a received tag cloud into a DOM element.
*
* The tag cloud HTML is generated on the server.
*
* @since 2.9.0
*
* @param {number|string} r The response message from the AJAX call.
* @param {string} stat The status of the AJAX request.
*
* @return {void}
*/
$.post( ajaxurl, { 'action': 'get-tagcloud', 'tax': tax }, function( r, stat ) {
if ( 0 === r || 'success' != stat ) {
return;
}
r = $( '<div id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</div>' );
/**
* Adds a new tag when a tag in the tagcloud is clicked.
*
* @since 2.9.0
*
* @return {boolean} Returns false to prevent the default action.
*/
$( 'a', r ).click( function() {
tagBox.userAction = 'add';
tagBox.flushTags( $( '#' + tax ), this );
return false;
});
$( '#' + id ).after( r );
});
},
/**
* Track the user's last action.
*
* @since 4.7.0
*/
userAction: '',
/**
* Dispatches an audible message to screen readers.
*
* This will inform the user when a tag has been added or removed.
*
* @since 4.7.0
*
* @return {void}
*/
screenReadersMessage: function() {
var message;
switch ( this.userAction ) {
case 'remove':
message = window.tagsSuggestL10n.termRemoved;
break;
case 'add':
message = window.tagsSuggestL10n.termAdded;
break;
default:
return;
}
window.wp.a11y.speak( message, 'assertive' );
},
/**
* Initializes the tags box by setting up the links, buttons. Sets up event
* handling.
*
* This includes handling of pressing the enter key in the input field and the
* retrieval of tag suggestions.
*
* @since 2.9.0
*
* @memberOf tagBox
*
* @return {void}
*/
init : function() {
var ajaxtag = $('div.ajaxtag');
$('.tagsdiv').each( function() {
tagBox.quickClicks( this );
});
$( '.tagadd', ajaxtag ).click( function() {
tagBox.userAction = 'add';
tagBox.flushTags( $( this ).closest( '.tagsdiv' ) );
});
/**
* Handles pressing enter on the new tag input field.
*
* Prevents submitting the post edit form. Uses `keypress` to take
* into account Input Method Editor (IME) converters.
*
* @since 2.9.0
*
* @param {Event} event The keypress event that occurred.
*
* @return {void}
*/
$( 'input.newtag', ajaxtag ).keypress( function( event ) {
if ( 13 == event.which ) {
tagBox.userAction = 'add';
tagBox.flushTags( $( this ).closest( '.tagsdiv' ) );
event.preventDefault();
event.stopPropagation();
}
}).each( function( i, element ) {
$( element ).wpTagsSuggest();
});
/**
* Before a post is saved the value currently in the new tag input field will be
* added as a tag.
*
* @since 2.9.0
*
* @return {void}
*/
$('#post').submit(function(){
$('div.tagsdiv').each( function() {
tagBox.flushTags(this, false, 1);
});
});
/**
* Handles clicking on the tag cloud link.
*
* Makes sure the ARIA attributes are set correctly.
*
* @since 2.9.0
*
* @return {void}
*/
$('.tagcloud-link').click(function(){
// On the first click, fetch the tag cloud and insert it in the DOM.
tagBox.get( $( this ).attr( 'id' ) );
// Update button state, remove previous click event and attach a new one to toggle the cloud.
$( this )
.attr( 'aria-expanded', 'true' )
.unbind()
.click( function() {
$( this )
.attr( 'aria-expanded', 'false' === $( this ).attr( 'aria-expanded' ) ? 'true' : 'false' )
.siblings( '.the-tagcloud' ).toggle();
});
});
}
};
}( jQuery ));