angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

tags.js (4726B)


      1 /**
      2  * Contains logic for deleting and adding tags.
      3  *
      4  * For deleting tags it makes a request to the server to delete the tag.
      5  * For adding tags it makes a request to the server to add the tag.
      6  *
      7  * @output wp-admin/js/tags.js
      8  */
      9 
     10  /* global ajaxurl, wpAjax, showNotice, validateForm */
     11 
     12 jQuery( function($) {
     13 
     14 	var addingTerm = false;
     15 
     16 	/**
     17 	 * Adds an event handler to the delete term link on the term overview page.
     18 	 *
     19 	 * Cancels default event handling and event bubbling.
     20 	 *
     21 	 * @since 2.8.0
     22 	 *
     23 	 * @return {boolean} Always returns false to cancel the default event handling.
     24 	 */
     25 	$( '#the-list' ).on( 'click', '.delete-tag', function() {
     26 		var t = $(this), tr = t.parents('tr'), r = true, data;
     27 
     28 		if ( 'undefined' != showNotice )
     29 			r = showNotice.warn();
     30 
     31 		if ( r ) {
     32 			data = t.attr('href').replace(/[^?]*\?/, '').replace(/action=delete/, 'action=delete-tag');
     33 
     34 			/**
     35 			 * Makes a request to the server to delete the term that corresponds to the
     36 			 * delete term button.
     37 			 *
     38 			 * @param {string} r The response from the server.
     39 			 *
     40 			 * @return {void}
     41 			 */
     42 			$.post(ajaxurl, data, function(r){
     43 				if ( '1' == r ) {
     44 					$('#ajax-response').empty();
     45 					tr.fadeOut('normal', function(){ tr.remove(); });
     46 
     47 					/**
     48 					 * Removes the term from the parent box and the tag cloud.
     49 					 *
     50 					 * `data.match(/tag_ID=(\d+)/)[1]` matches the term ID from the data variable.
     51 					 * This term ID is then used to select the relevant HTML elements:
     52 					 * The parent box and the tag cloud.
     53 					 */
     54 					$('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
     55 					$('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
     56 
     57 				} else if ( '-1' == r ) {
     58 					$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Sorry, you are not allowed to do that.' ) + '</p></div>');
     59 					tr.children().css('backgroundColor', '');
     60 
     61 				} else {
     62 					$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Something went wrong.' ) + '</p></div>');
     63 					tr.children().css('backgroundColor', '');
     64 				}
     65 			});
     66 
     67 			tr.children().css('backgroundColor', '#f33');
     68 		}
     69 
     70 		return false;
     71 	});
     72 
     73 	/**
     74 	 * Adds a deletion confirmation when removing a tag.
     75 	 *
     76 	 * @since 4.8.0
     77 	 *
     78 	 * @return {void}
     79 	 */
     80 	$( '#edittag' ).on( 'click', '.delete', function( e ) {
     81 		if ( 'undefined' === typeof showNotice ) {
     82 			return true;
     83 		}
     84 
     85 		// Confirms the deletion, a negative response means the deletion must not be executed.
     86 		var response = showNotice.warn();
     87 		if ( ! response ) {
     88 			e.preventDefault();
     89 		}
     90 	});
     91 
     92 	/**
     93 	 * Adds an event handler to the form submit on the term overview page.
     94 	 *
     95 	 * Cancels default event handling and event bubbling.
     96 	 *
     97 	 * @since 2.8.0
     98 	 *
     99 	 * @return {boolean} Always returns false to cancel the default event handling.
    100 	 */
    101 	$('#submit').on( 'click', function(){
    102 		var form = $(this).parents('form');
    103 
    104 		if ( ! validateForm( form ) )
    105 			return false;
    106 
    107 		if ( addingTerm ) {
    108 			// If we're adding a term, noop the button to avoid duplicate requests.
    109 			return false;
    110 		}
    111 
    112 		addingTerm = true;
    113 		form.find( '.submit .spinner' ).addClass( 'is-active' );
    114 
    115 		/**
    116 		 * Does a request to the server to add a new term to the database
    117 		 *
    118 		 * @param {string} r The response from the server.
    119 		 *
    120 		 * @return {void}
    121 		 */
    122 		$.post(ajaxurl, $('#addtag').serialize(), function(r){
    123 			var res, parent, term, indent, i;
    124 
    125 			addingTerm = false;
    126 			form.find( '.submit .spinner' ).removeClass( 'is-active' );
    127 
    128 			$('#ajax-response').empty();
    129 			res = wpAjax.parseAjaxResponse( r, 'ajax-response' );
    130 			if ( ! res || res.errors )
    131 				return;
    132 
    133 			parent = form.find( 'select#parent' ).val();
    134 
    135 			// If the parent exists on this page, insert it below. Else insert it at the top of the list.
    136 			if ( parent > 0 && $('#tag-' + parent ).length > 0 ) {
    137 				// As the parent exists, insert the version with - - - prefixed.
    138 				$( '.tags #tag-' + parent ).after( res.responses[0].supplemental.noparents );
    139 			} else {
    140 				// As the parent is not visible, insert the version with Parent - Child - ThisTerm.
    141 				$( '.tags' ).prepend( res.responses[0].supplemental.parents );
    142 			}
    143 
    144 			$('.tags .no-items').remove();
    145 
    146 			if ( form.find('select#parent') ) {
    147 				// Parents field exists, Add new term to the list.
    148 				term = res.responses[1].supplemental;
    149 
    150 				// Create an indent for the Parent field.
    151 				indent = '';
    152 				for ( i = 0; i < res.responses[1].position; i++ )
    153 					indent += '&nbsp;&nbsp;&nbsp;';
    154 
    155 				form.find( 'select#parent option:selected' ).after( '<option value="' + term.term_id + '">' + indent + term.name + '</option>' );
    156 			}
    157 
    158 			$('input[type="text"]:visible, textarea:visible', form).val('');
    159 		});
    160 
    161 		return false;
    162 	});
    163 
    164 });