web/wp-admin/js/inline-edit-tax.dev.js
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 
       
     2 (function($) {
       
     3 inlineEditTax = {
       
     4 
       
     5 	init : function() {
       
     6 		var t = this, row = $('#inline-edit');
       
     7 
       
     8 		t.type = $('#the-list').attr('className').substr(5);
       
     9 		t.what = '#'+t.type+'-';
       
    10 
       
    11 		// get all editable rows
       
    12 		t.rows = $('tr.iedit');
       
    13 
       
    14 		// prepare the edit row
       
    15 		row.keyup(function(e) { if(e.which == 27) return inlineEditTax.revert(); });
       
    16 
       
    17 		$('a.cancel', row).click(function() { return inlineEditTax.revert(); });
       
    18 		$('a.save', row).click(function() { return inlineEditTax.save(this); });
       
    19 		$('input, select', row).keydown(function(e) { if(e.which == 13) return inlineEditTax.save(this); });
       
    20 
       
    21 		// add events
       
    22 		t.addEvents(t.rows);
       
    23 
       
    24 		$('#posts-filter input[type="submit"]').click(function(e){
       
    25 			if ( $('form#posts-filter tr.inline-editor').length > 0 )
       
    26 				t.revert();
       
    27 		});
       
    28 	},
       
    29 
       
    30 	toggle : function(el) {
       
    31 		var t = this;
       
    32 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
       
    33 	},
       
    34 
       
    35 	addEvents : function(r) {
       
    36 		r.each(function() {
       
    37 			$(this).find('a.editinline').click(function() { inlineEditTax.edit(this); return false; });
       
    38 		});
       
    39 	},
       
    40 
       
    41 	edit : function(id) {
       
    42 		var t = this, editRow;
       
    43 		t.revert();
       
    44 
       
    45 		if ( typeof(id) == 'object' )
       
    46 			id = t.getId(id);
       
    47 
       
    48 		editRow = $('#inline-edit').clone(true), rowData = $('#inline_'+id);
       
    49 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
       
    50 
       
    51 		if ( $(t.what+id).hasClass('alternate') )
       
    52 			$(editRow).addClass('alternate');
       
    53 
       
    54 		$(t.what+id).hide().after(editRow);
       
    55 
       
    56 		$(':input[name="name"]', editRow).val( $('.name', rowData).text() );
       
    57 		$(':input[name="slug"]', editRow).val( $('.slug', rowData).text() );
       
    58 
       
    59 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
       
    60 		$('.ptitle', editRow).eq(0).focus();
       
    61 
       
    62 		return false;
       
    63 	},
       
    64 
       
    65 	save : function(id) {
       
    66 		var params, fields, tax = $('input[name="taxonomy"]').val() || '';
       
    67 
       
    68 		if( typeof(id) == 'object' )
       
    69 			id = this.getId(id);
       
    70 
       
    71 		$('table.widefat .inline-edit-save .waiting').show();
       
    72 
       
    73 		params = {
       
    74 			action: 'inline-save-tax',
       
    75 			tax_type: this.type,
       
    76 			tax_ID: id,
       
    77 			taxonomy: tax
       
    78 		};
       
    79 
       
    80 		fields = $('#edit-'+id+' :input').fieldSerialize();
       
    81 		params = fields + '&' + $.param(params);
       
    82 
       
    83 		// make ajax request
       
    84 		$.post('admin-ajax.php', params,
       
    85 			function(r) {
       
    86 				var row, new_id;
       
    87 				$('table.widefat .inline-edit-save .waiting').hide();
       
    88 
       
    89 				if (r) {
       
    90 					if ( -1 != r.indexOf('<tr') ) {
       
    91 						$(inlineEditTax.what+id).remove();
       
    92 						new_id = $(r).attr('id');
       
    93 
       
    94 						$('#edit-'+id).before(r).remove();
       
    95 						row = new_id ? $('#'+new_id) : $(inlineEditTax.what+id);
       
    96 						row.hide();
       
    97 
       
    98 						inlineEditTax.addEvents(row);
       
    99 						row.fadeIn();
       
   100 					} else
       
   101 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
       
   102 				} else
       
   103 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
       
   104 			}
       
   105 		);
       
   106 		return false;
       
   107 	},
       
   108 
       
   109 	revert : function() {
       
   110 		var id = $('table.widefat tr.inline-editor').attr('id');
       
   111 
       
   112 		if ( id ) {
       
   113 			$('table.widefat .inline-edit-save .waiting').hide();
       
   114 			$('#'+id).remove();
       
   115 			id = id.substr( id.lastIndexOf('-') + 1 );
       
   116 			$(this.what+id).show();
       
   117 		}
       
   118 
       
   119 		return false;
       
   120 	},
       
   121 
       
   122 	getId : function(o) {
       
   123 		var id = o.tagName == 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
       
   124 		return parts[parts.length - 1];
       
   125 	}
       
   126 };
       
   127 
       
   128 $(document).ready(function(){inlineEditTax.init();});
       
   129 })(jQuery);