1 /* global ajaxurl, wpAjax, tagsl10n, showNotice, validateForm */
3 jQuery(document).ready(function($) {
5 $( '#the-list' ).on( 'click', '.delete-tag', function() {
6 var t = $(this), tr = t.parents('tr'), r = true, data;
7 if ( 'undefined' != showNotice )
10 data = t.attr('href').replace(/[^?]*\?/, '').replace(/action=delete/, 'action=delete-tag');
11 $.post(ajaxurl, data, function(r){
13 $('#ajax-response').empty();
14 tr.fadeOut('normal', function(){ tr.remove(); });
15 // Remove the term from the parent box and tag cloud
16 $('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
17 $('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
18 } else if ( '-1' == r ) {
19 $('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.noPerm + '</p></div>');
20 tr.children().css('backgroundColor', '');
22 $('#ajax-response').empty().append('<div class="error"><p>' + tagsl10n.broken + '</p></div>');
23 tr.children().css('backgroundColor', '');
26 tr.children().css('backgroundColor', '#f33');
31 $('#submit').click(function(){
32 var form = $(this).parents('form');
34 if ( ! validateForm( form ) )
37 $.post(ajaxurl, $('#addtag').serialize(), function(r){
38 var res, parent, term, indent, i;
40 $('#ajax-response').empty();
41 res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
42 if ( ! res || res.errors )
45 parent = form.find( 'select#parent' ).val();
47 if ( parent > 0 && $('#tag-' + parent ).length > 0 ) // If the parent exists on this page, insert it below. Else insert it at the top of the list.
48 $( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents ); // As the parent exists, Insert the version with - - - prefixed
50 $( '.tags' ).prepend( res.responses[0].supplemental.parents ); // As the parent is not visible, Insert the version with Parent - Child - ThisTerm
52 $('.tags .no-items').remove();
54 if ( form.find('select#parent') ) {
55 // Parents field exists, Add new term to the list.
56 term = res.responses[1].supplemental;
58 // Create an indent for the Parent field
60 for ( i = 0; i < res.responses[1].position; i++ )
61 indent += ' ';
63 form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
66 $('input[type="text"]:visible, textarea:visible', form).val('');