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