wp/wp-admin/js/inline-edit-tax.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /* global inlineEditL10n, ajaxurl */
     1 
     2 
       
     3 var inlineEditTax;
     2 (function($) {
     4 (function($) {
     3 inlineEditTax = {
     5 inlineEditTax = {
     4 
     6 
     5 	init : function() {
     7 	init : function() {
     6 		var t = this, row = $('#inline-edit');
     8 		var t = this, row = $('#inline-edit');
    12 			inlineEditTax.edit(this);
    14 			inlineEditTax.edit(this);
    13 			return false;
    15 			return false;
    14 		});
    16 		});
    15 
    17 
    16 		// prepare the edit row
    18 		// prepare the edit row
    17 		row.keyup(function(e) { if(e.which == 27) return inlineEditTax.revert(); });
    19 		row.keyup( function( e ) {
       
    20 			if ( e.which === 27 ) {
       
    21 				return inlineEditTax.revert();
       
    22 			}
       
    23 		});
    18 
    24 
    19 		$('a.cancel', row).click(function() { return inlineEditTax.revert(); });
    25 		$( 'a.cancel', row ).click( function() {
    20 		$('a.save', row).click(function() { return inlineEditTax.save(this); });
    26 			return inlineEditTax.revert();
    21 		$('input, select', row).keydown(function(e) { if(e.which == 13) return inlineEditTax.save(this); });
    27 		});
       
    28 		$( 'a.save', row ).click( function() {
       
    29 			return inlineEditTax.save(this);
       
    30 		});
       
    31 		$( 'input, select', row ).keydown( function( e ) {
       
    32 			if ( e.which === 13 ) {
       
    33 				return inlineEditTax.save( this );
       
    34 			}
       
    35 		});
    22 
    36 
    23 		$('#posts-filter input[type="submit"]').mousedown(function(e){
    37 		$( '#posts-filter input[type="submit"]' ).mousedown( function() {
    24 			t.revert();
    38 			t.revert();
    25 		});
    39 		});
    26 	},
    40 	},
    27 
    41 
    28 	toggle : function(el) {
    42 	toggle : function(el) {
    29 		var t = this;
    43 		var t = this;
    30 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
    44 		$(t.what+t.getId(el)).css('display') === 'none' ? t.revert() : t.edit(el);
    31 	},
    45 	},
    32 
    46 
    33 	edit : function(id) {
    47 	edit : function(id) {
    34 		var t = this, editRow;
    48 		var editRow, rowData, val,
       
    49 			t = this;
    35 		t.revert();
    50 		t.revert();
    36 
    51 
    37 		if ( typeof(id) == 'object' )
    52 		if ( typeof(id) === 'object' ) {
    38 			id = t.getId(id);
    53 			id = t.getId(id);
       
    54 		}
    39 
    55 
    40 		editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
    56 		editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
    41 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
    57 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
    42 
    58 
    43 		if ( $(t.what+id).hasClass('alternate') )
    59 		$(t.what+id).hide().after(editRow).after('<tr class="hidden"></tr>');
    44 			$(editRow).addClass('alternate');
       
    45 
    60 
    46 		$(t.what+id).hide().after(editRow);
    61 		val = $('.name', rowData);
       
    62 		val.find( 'img' ).replaceWith( function() { return this.alt; } );
       
    63 		val = val.text();
       
    64 		$(':input[name="name"]', editRow).val( val );
    47 
    65 
    48 		$(':input[name="name"]', editRow).val( $('.name', rowData).text() );
    66 		val = $('.slug', rowData);
    49 		$(':input[name="slug"]', editRow).val( $('.slug', rowData).text() );
    67 		val.find( 'img' ).replaceWith( function() { return this.alt; } );
       
    68 		val = val.text();
       
    69 		$(':input[name="slug"]', editRow).val( val );
    50 
    70 
    51 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
    71 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
    52 		$('.ptitle', editRow).eq(0).focus();
    72 		$('.ptitle', editRow).eq(0).focus();
    53 
    73 
    54 		return false;
    74 		return false;
    55 	},
    75 	},
    56 
    76 
    57 	save : function(id) {
    77 	save : function(id) {
    58 		var params, fields, tax = $('input[name="taxonomy"]').val() || '';
    78 		var params, fields, tax = $('input[name="taxonomy"]').val() || '';
    59 
    79 
    60 		if( typeof(id) == 'object' )
    80 		if( typeof(id) === 'object' ) {
    61 			id = this.getId(id);
    81 			id = this.getId(id);
       
    82 		}
    62 
    83 
    63 		$('table.widefat .spinner').show();
    84 		$( 'table.widefat .spinner' ).addClass( 'is-active' );
    64 
    85 
    65 		params = {
    86 		params = {
    66 			action: 'inline-save-tax',
    87 			action: 'inline-save-tax',
    67 			tax_type: this.type,
    88 			tax_type: this.type,
    68 			tax_ID: id,
    89 			tax_ID: id,
    69 			taxonomy: tax
    90 			taxonomy: tax
    70 		};
    91 		};
    71 
    92 
    72 		fields = $('#edit-'+id+' :input').serialize();
    93 		fields = $('#edit-'+id).find(':input').serialize();
    73 		params = fields + '&' + $.param(params);
    94 		params = fields + '&' + $.param(params);
    74 
    95 
    75 		// make ajax request
    96 		// make ajax request
    76 		$.post( ajaxurl, params,
    97 		$.post( ajaxurl, params,
    77 			function(r) {
    98 			function(r) {
    78 				var row, new_id;
    99 				var row, new_id, option_value;
    79 				$('table.widefat .spinner').hide();
   100 				$( 'table.widefat .spinner' ).removeClass( 'is-active' );
    80 
   101 
    81 				if (r) {
   102 				if (r) {
    82 					if ( -1 != r.indexOf('<tr') ) {
   103 					if ( -1 !== r.indexOf( '<tr' ) ) {
    83 						$(inlineEditTax.what+id).remove();
   104 						$(inlineEditTax.what+id).siblings('tr.hidden').addBack().remove();
    84 						new_id = $(r).attr('id');
   105 						new_id = $(r).attr('id');
    85 
   106 
    86 						$('#edit-'+id).before(r).remove();
   107 						$('#edit-'+id).before(r).remove();
    87 						row = new_id ? $('#'+new_id) : $(inlineEditTax.what+id);
   108 
       
   109 						if ( new_id ) {
       
   110 							option_value = new_id.replace( inlineEditTax.type + '-', '' );
       
   111 							row = $( '#' + new_id );
       
   112 						} else {
       
   113 							option_value = id;
       
   114 							row = $( inlineEditTax.what + id );
       
   115 						}
       
   116 
       
   117 						// Update the value in the Parent dropdown.
       
   118 						$( '#parent' ).find( 'option[value=' + option_value + ']' ).text( row.find( '.row-title' ).text() );
       
   119 
    88 						row.hide().fadeIn();
   120 						row.hide().fadeIn();
    89 					} else
   121 					} else {
    90 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
   122 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
    91 				} else
   123 					}
       
   124 				} else {
    92 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
   125 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
    93 
   126 				}
    94 				if ($(row).prev('tr').hasClass('alternate'))
       
    95 					$(row).removeClass('alternate');
       
    96 			}
   127 			}
    97 		);
   128 		);
    98 		return false;
   129 		return false;
    99 	},
   130 	},
   100 
   131 
   101 	revert : function() {
   132 	revert : function() {
   102 		var id = $('table.widefat tr.inline-editor').attr('id');
   133 		var id = $('table.widefat tr.inline-editor').attr('id');
   103 
   134 
   104 		if ( id ) {
   135 		if ( id ) {
   105 			$('table.widefat .spinner').hide();
   136 			$( 'table.widefat .spinner' ).removeClass( 'is-active' );
   106 			$('#'+id).remove();
   137 			$('#'+id).siblings('tr.hidden').addBack().remove();
   107 			id = id.substr( id.lastIndexOf('-') + 1 );
   138 			id = id.substr( id.lastIndexOf('-') + 1 );
   108 			$(this.what+id).show();
   139 			$(this.what+id).show();
   109 		}
   140 		}
   110 
   141 
   111 		return false;
   142 		return false;
   112 	},
   143 	},
   113 
   144 
   114 	getId : function(o) {
   145 	getId : function(o) {
   115 		var id = o.tagName == 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
   146 		var id = o.tagName === 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
   116 		return parts[parts.length - 1];
   147 		return parts[parts.length - 1];
   117 	}
   148 	}
   118 };
   149 };
   119 
   150 
   120 $(document).ready(function(){inlineEditTax.init();});
   151 $(document).ready(function(){inlineEditTax.init();});