web/wp-admin/js/inline-edit-post.dev.js
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 
       
     2 (function($) {
     1 (function($) {
     3 inlineEditPost = {
     2 inlineEditPost = {
     4 
     3 
     5 	init : function() {
     4 	init : function(){
     6 		var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
     5 		var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
     7 
     6 
     8 		t.type = $('table.widefat').hasClass('page') ? 'page' : 'post';
     7 		t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
     9 		t.what = '#'+t.type+'-';
     8 		t.what = '#post-';
    10 
     9 
    11 		// prepare the edit rows
    10 		// prepare the edit rows
    12 		qeRow.keyup(function(e) { if(e.which == 27) return inlineEditPost.revert(); });
    11 		qeRow.keyup(function(e){
    13 		bulkRow.keyup(function(e) { if (e.which == 27) return inlineEditPost.revert(); });
    12 			if (e.which == 27)
    14 
    13 				return inlineEditPost.revert();
    15 		$('a.cancel', qeRow).click(function() { return inlineEditPost.revert(); });
    14 		});
    16 		$('a.save', qeRow).click(function() { return inlineEditPost.save(this); });
    15 		bulkRow.keyup(function(e){
    17 		$('td', qeRow).keydown(function(e) { if ( e.which == 13 ) return inlineEditPost.save(this); });
    16 			if (e.which == 27)
    18 
    17 				return inlineEditPost.revert();
    19 		$('a.cancel', bulkRow).click(function() { return inlineEditPost.revert(); });
    18 		});
    20 
    19 
    21 		$('#inline-edit .inline-edit-private input[value=private]').click( function(){
    20 		$('a.cancel', qeRow).click(function(){
       
    21 			return inlineEditPost.revert();
       
    22 		});
       
    23 		$('a.save', qeRow).click(function(){
       
    24 			return inlineEditPost.save(this);
       
    25 		});
       
    26 		$('td', qeRow).keydown(function(e){
       
    27 			if ( e.which == 13 )
       
    28 				return inlineEditPost.save(this);
       
    29 		});
       
    30 
       
    31 		$('a.cancel', bulkRow).click(function(){
       
    32 			return inlineEditPost.revert();
       
    33 		});
       
    34 
       
    35 		$('#inline-edit .inline-edit-private input[value="private"]').click( function(){
    22 			var pw = $('input.inline-edit-password-input');
    36 			var pw = $('input.inline-edit-password-input');
    23 			if ( $(this).attr('checked') ) {
    37 			if ( $(this).prop('checked') ) {
    24 				pw.val('').attr('disabled', 'disabled');
    38 				pw.val('').prop('disabled', true);
    25 			} else {
    39 			} else {
    26 				pw.attr('disabled', '');
    40 				pw.prop('disabled', false);
    27 			}
    41 			}
    28 		});
    42 		});
    29 
    43 
    30 		// add events
    44 		// add events
    31 		$('a.editinline').live('click', function() { inlineEditPost.edit(this); return false; });
    45 		$('a.editinline').live('click', function(){
       
    46 			inlineEditPost.edit(this);
       
    47 			return false;
       
    48 		});
    32 
    49 
    33 		$('#bulk-title-div').parents('fieldset').after(
    50 		$('#bulk-title-div').parents('fieldset').after(
    34 			$('#inline-edit fieldset.inline-edit-categories').clone()
    51 			$('#inline-edit fieldset.inline-edit-categories').clone()
    35 		).siblings( 'fieldset:last' ).prepend(
    52 		).siblings( 'fieldset:last' ).prepend(
    36 			$('#inline-edit label.inline-edit-tags').clone()
    53 			$('#inline-edit label.inline-edit-tags').clone()
    37 		);
    54 		);
    38 
    55 
    39 		// categories expandable?
    56 		// hiearchical taxonomies expandable?
    40 		$('span.catshow').click(function() {
    57 		$('span.catshow').click(function(){
    41 			$('.inline-editor ul.cat-checklist').addClass("cat-hover");
    58 			$(this).hide().next().show().parent().next().addClass("cat-hover");
    42 			$('.inline-editor span.cathide').show();
    59 		});
    43 			$(this).hide();
    60 
    44 		});
    61 		$('span.cathide').click(function(){
    45 
    62 			$(this).hide().prev().show().parent().next().removeClass("cat-hover");
    46 		$('span.cathide').click(function() {
       
    47 			$('.inline-editor ul.cat-checklist').removeClass("cat-hover");
       
    48 			$('.inline-editor span.catshow').show();
       
    49 			$(this).hide();
       
    50 		});
    63 		});
    51 
    64 
    52 		$('select[name="_status"] option[value="future"]', bulkRow).remove();
    65 		$('select[name="_status"] option[value="future"]', bulkRow).remove();
    53 
    66 
    54 		$('#doaction, #doaction2').click(function(e){
    67 		$('#doaction, #doaction2').click(function(e){
    59 			} else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
    72 			} else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
    60 				t.revert();
    73 				t.revert();
    61 			}
    74 			}
    62 		});
    75 		});
    63 
    76 
    64 		$('#post-query-submit').click(function(e){
    77 		$('#post-query-submit').mousedown(function(e){
    65 			if ( $('form#posts-filter tr.inline-editor').length > 0 )
    78 			t.revert();
    66 				t.revert();
    79 			$('select[name^="action"]').val('-1');
    67 		});
    80 		});
    68 
    81 	},
    69 	},
    82 
    70 
    83 	toggle : function(el){
    71 	toggle : function(el) {
       
    72 		var t = this;
    84 		var t = this;
    73 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
    85 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
    74 	},
    86 	},
    75 
    87 
    76 	setBulk : function() {
    88 	setBulk : function(){
    77 		var te = '', type = this.type, tax, c = true;
    89 		var te = '', type = this.type, tax, c = true;
    78 		this.revert();
    90 		this.revert();
    79 
    91 
    80 		$('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
    92 		$('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
    81 		$('table.widefat tbody').prepend( $('#bulk-edit') );
    93 		$('table.widefat tbody').prepend( $('#bulk-edit') );
    82 		$('#bulk-edit').addClass('inline-editor').show();
    94 		$('#bulk-edit').addClass('inline-editor').show();
    83 
    95 
    84 		$('tbody th.check-column input[type="checkbox"]').each(function(i){
    96 		$('tbody th.check-column input[type="checkbox"]').each(function(i){
    85 			if ( $(this).attr('checked') ) {
    97 			if ( $(this).prop('checked') ) {
    86 				c = false;
    98 				c = false;
    87 				var id = $(this).val(), theTitle;
    99 				var id = $(this).val(), theTitle;
    88 				theTitle = $('#inline_'+id+' .post_title').text() || inlineEditL10n.notitle;
   100 				theTitle = $('#inline_'+id+' .post_title').text() || inlineEditL10n.notitle;
    89 				te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
   101 				te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
    90 			}
   102 			}
    92 
   104 
    93 		if ( c )
   105 		if ( c )
    94 			return this.revert();
   106 			return this.revert();
    95 
   107 
    96 		$('#bulk-titles').html(te);
   108 		$('#bulk-titles').html(te);
    97 		$('#bulk-titles a').click(function() {
   109 		$('#bulk-titles a').click(function(){
    98 			var id = $(this).attr('id').substr(1);
   110 			var id = $(this).attr('id').substr(1);
    99 
   111 
   100 			$('table.widefat input[value="'+id+'"]').attr('checked', '');
   112 			$('table.widefat input[value="' + id + '"]').prop('checked', false);
   101 			$('#ttle'+id).remove();
   113 			$('#ttle'+id).remove();
   102 		});
   114 		});
   103 
   115 
   104 		// enable autocomplete for tags
   116 		// enable autocomplete for tags
   105 		if ( type == 'post' ) {
   117 		if ( 'post' == type ) {
   106 			// support multi taxonomies?
   118 			// support multi taxonomies?
   107 			tax = 'post_tag';
   119 			tax = 'post_tag';
   108 			$('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
   120 			$('tr.inline-editor textarea[name="tax_input['+tax+']"]').suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
   109 		}
   121 		}
       
   122 		$('html, body').animate( { scrollTop: 0 }, 'fast' );
   110 	},
   123 	},
   111 
   124 
   112 	edit : function(id) {
   125 	edit : function(id) {
   113 		var t = this, fields, editRow, rowData, cats, status, pageOpt, f, pageLevel, nextPage, pageLoop = true, nextLevel, tax;
   126 		var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
   114 		t.revert();
   127 		t.revert();
   115 
   128 
   116 		if ( typeof(id) == 'object' )
   129 		if ( typeof(id) == 'object' )
   117 			id = t.getId(id);
   130 			id = t.getId(id);
   118 
   131 
   119 		fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password'];
   132 		fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order'];
   120 		if ( t.type == 'page' ) fields.push('post_parent', 'menu_order', 'page_template');
   133 		if ( t.type == 'page' )
   121 		if ( t.type == 'post' ) fields.push('tags_input');
   134 			fields.push('post_parent', 'page_template');
   122 
   135 
   123 		// add the new blank row
   136 		// add the new blank row
   124 		editRow = $('#inline-edit').clone(true);
   137 		editRow = $('#inline-edit').clone(true);
   125 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
   138 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
   126 
   139 
   128 			$(editRow).addClass('alternate');
   141 			$(editRow).addClass('alternate');
   129 		$(t.what+id).hide().after(editRow);
   142 		$(t.what+id).hide().after(editRow);
   130 
   143 
   131 		// populate the data
   144 		// populate the data
   132 		rowData = $('#inline_'+id);
   145 		rowData = $('#inline_'+id);
   133 		if ( !$(':input[name="post_author"] option[value=' + $('.post_author', rowData).text() + ']', editRow).val() ) {
   146 		if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
   134 			// author no longer has edit caps, so we need to add them to the list of authors
   147 			// author no longer has edit caps, so we need to add them to the list of authors
   135 			$(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
   148 			$(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
   136 		}
   149 		}
       
   150 		if ( $(':input[name="post_author"] option', editRow).length == 1 ) {
       
   151 			$('label.inline-edit-author', editRow).hide();
       
   152 		}
       
   153 
       
   154 		// hide unsupported formats, but leave the current format alone
       
   155 		cur_format = $('.post_format', rowData).text();
       
   156 		$('option.unsupported', editRow).each(function() {
       
   157 			var $this = $(this);
       
   158 			if ( $this.val() != cur_format )
       
   159 				$this.remove();
       
   160 		});
   137 
   161 
   138 		for ( f = 0; f < fields.length; f++ ) {
   162 		for ( f = 0; f < fields.length; f++ ) {
   139 			$(':input[name="'+fields[f]+'"]', editRow).val( $('.'+fields[f], rowData).text() );
   163 			$(':input[name="' + fields[f] + '"]', editRow).val( $('.'+fields[f], rowData).text() );
   140 		}
   164 		}
   141 
   165 
   142 		if ( $('.comment_status', rowData).text() == 'open' )
   166 		if ( $('.comment_status', rowData).text() == 'open' )
   143 			$('input[name="comment_status"]', editRow).attr("checked", "checked");
   167 			$('input[name="comment_status"]', editRow).prop("checked", true);
   144 		if ( $('.ping_status', rowData).text() == 'open' )
   168 		if ( $('.ping_status', rowData).text() == 'open' )
   145 			$('input[name="ping_status"]', editRow).attr("checked", "checked");
   169 			$('input[name="ping_status"]', editRow).prop("checked", true);
   146 		if ( $('.sticky', rowData).text() == 'sticky' )
   170 		if ( $('.sticky', rowData).text() == 'sticky' )
   147 			$('input[name="sticky"]', editRow).attr("checked", "checked");
   171 			$('input[name="sticky"]', editRow).prop("checked", true);
   148 
   172 
   149 		// categories
   173 		// hierarchical taxonomies
   150 		if ( cats = $('.post_category', rowData).text() )
   174 		$('.post_category', rowData).each(function(){
   151 			$('ul.cat-checklist :checkbox', editRow).val(cats.split(','));
   175 			var term_ids = $(this).text();
       
   176 
       
   177 			if ( term_ids ) {
       
   178 				taxname = $(this).attr('id').replace('_'+id, '');
       
   179 				$('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
       
   180 			}
       
   181 		});
       
   182 
       
   183 		//flat taxonomies
       
   184 		$('.tags_input', rowData).each(function(){
       
   185 			var terms = $(this).text(),
       
   186 				taxname = $(this).attr('id').replace('_' + id, ''),
       
   187 				textarea = $('textarea.tax_input_' + taxname, editRow),
       
   188 				comma = inlineEditL10n.comma;
       
   189 
       
   190 			if ( terms ) {
       
   191 				if ( ',' !== comma )
       
   192 					terms = terms.replace(/,/g, comma);
       
   193 				textarea.val(terms);
       
   194 			}
       
   195 
       
   196 			textarea.suggest( ajaxurl + '?action=ajax-tag-search&tax=' + taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
       
   197 		});
   152 
   198 
   153 		// handle the post status
   199 		// handle the post status
   154 		status = $('._status', rowData).text();
   200 		status = $('._status', rowData).text();
   155 		if ( status != 'future' ) $('select[name="_status"] option[value="future"]', editRow).remove();
   201 		if ( 'future' != status )
   156 		if ( status == 'private' ) {
   202 			$('select[name="_status"] option[value="future"]', editRow).remove();
   157 			$('input[name="keep_private"]', editRow).attr("checked", "checked");
   203 
   158 			$('input.inline-edit-password-input').val('').attr('disabled', 'disabled');
   204 		if ( 'private' == status ) {
       
   205 			$('input[name="keep_private"]', editRow).prop("checked", true);
       
   206 			$('input.inline-edit-password-input').val('').prop('disabled', true);
   159 		}
   207 		}
   160 
   208 
   161 		// remove the current page and children from the parent dropdown
   209 		// remove the current page and children from the parent dropdown
   162 		pageOpt = $('select[name="post_parent"] option[value="'+id+'"]', editRow);
   210 		pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
   163 		if ( pageOpt.length > 0 ) {
   211 		if ( pageOpt.length > 0 ) {
   164 			pageLevel = pageOpt[0].className.split('-')[1];
   212 			pageLevel = pageOpt[0].className.split('-')[1];
   165 			nextPage = pageOpt;
   213 			nextPage = pageOpt;
   166 			while ( pageLoop ) {
   214 			while ( pageLoop ) {
   167 				nextPage = nextPage.next('option');
   215 				nextPage = nextPage.next('option');
   178 		}
   226 		}
   179 
   227 
   180 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
   228 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
   181 		$('.ptitle', editRow).focus();
   229 		$('.ptitle', editRow).focus();
   182 
   230 
   183 		// enable autocomplete for tags
       
   184 		if ( t.type == 'post' ) {
       
   185 			tax = 'post_tag';
       
   186 			$('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search&tax='+tax, { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
       
   187 		}
       
   188 
       
   189 		return false;
   231 		return false;
   190 	},
   232 	},
   191 
   233 
   192 	save : function(id) {
   234 	save : function(id) {
   193 		var params, fields, page = $('.post_status_page').val() || '';
   235 		var params, fields, page = $('.post_status_page').val() || '';
   194 
   236 
   195 		if( typeof(id) == 'object' )
   237 		if ( typeof(id) == 'object' )
   196 			id = this.getId(id);
   238 			id = this.getId(id);
   197 
   239 
   198 		$('table.widefat .inline-edit-save .waiting').show();
   240 		$('table.widefat .inline-edit-save .waiting').show();
   199 
   241 
   200 		params = {
   242 		params = {
   201 			action: 'inline-save',
   243 			action: 'inline-save',
   202 			post_type: this.type,
   244 			post_type: typenow,
   203 			post_ID: id,
   245 			post_ID: id,
   204 			edit_date: 'true',
   246 			edit_date: 'true',
   205 			post_status: page
   247 			post_status: page
   206 		};
   248 		};
   207 
   249 
   208 		fields = $('#edit-'+id+' :input').serialize();
   250 		fields = $('#edit-'+id+' :input').serialize();
   209 		params = fields + '&' + $.param(params);
   251 		params = fields + '&' + $.param(params);
   210 
   252 
   211 		// make ajax request
   253 		// make ajax request
   212 		$.post('admin-ajax.php', params,
   254 		$.post( ajaxurl, params,
   213 			function(r) {
   255 			function(r) {
   214 				$('table.widefat .inline-edit-save .waiting').hide();
   256 				$('table.widefat .inline-edit-save .waiting').hide();
   215 
   257 
   216 				if (r) {
   258 				if (r) {
   217 					if ( -1 != r.indexOf('<tr') ) {
   259 					if ( -1 != r.indexOf('<tr') ) {
   218 						$(inlineEditPost.what+id).remove();
   260 						$(inlineEditPost.what+id).remove();
   219 						$('#edit-'+id).before(r).remove();
   261 						$('#edit-'+id).before(r).remove();
   220 						$(inlineEditPost.what+id).hide().fadeIn();
   262 						$(inlineEditPost.what+id).hide().fadeIn();
   221 					} else {
   263 					} else {
   222 						r = r.replace( /<.[^<>]*?>/g, '' );
   264 						r = r.replace( /<.[^<>]*?>/g, '' );
   223 						$('#edit-'+id+' .inline-edit-save').append('<span class="error">'+r+'</span>');
   265 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
   224 					}
   266 					}
   225 				} else {
   267 				} else {
   226 					$('#edit-'+id+' .inline-edit-save').append('<span class="error">'+inlineEditL10n.error+'</span>');
   268 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
   227 				}
   269 				}
   228 			}
   270 			}
   229 		, 'html');
   271 		, 'html');
   230 		return false;
   272 		return false;
   231 	},
   273 	},
   232 
   274 
   233 	revert : function() {
   275 	revert : function(){
   234 		var id;
   276 		var id = $('table.widefat tr.inline-editor').attr('id');
   235 
   277 
   236 		if ( id = $('table.widefat tr.inline-editor').attr('id') ) {
   278 		if ( id ) {
   237 			$('table.widefat .inline-edit-save .waiting').hide();
   279 			$('table.widefat .inline-edit-save .waiting').hide();
   238 
   280 
   239 			if ( 'bulk-edit' == id ) {
   281 			if ( 'bulk-edit' == id ) {
   240 				$('table.widefat #bulk-edit').removeClass('inline-editor').hide();
   282 				$('table.widefat #bulk-edit').removeClass('inline-editor').hide();
   241 				$('#bulk-titles').html('');
   283 				$('#bulk-titles').html('');
   242 				$('#inlineedit').append( $('#bulk-edit') );
   284 				$('#inlineedit').append( $('#bulk-edit') );
   243 			} else  {
   285 			} else {
   244 				$('#'+id).remove();
   286 				$('#'+id).remove();
   245 				id = id.substr( id.lastIndexOf('-') + 1 );
   287 				id = id.substr( id.lastIndexOf('-') + 1 );
   246 				$(this.what+id).show();
   288 				$(this.what+id).show();
   247 			}
   289 			}
   248 		}
   290 		}
   249 
   291 
   250 		return false;
   292 		return false;
   251 	},
   293 	},
   252 
   294 
   253 	getId : function(o) {
   295 	getId : function(o) {
   254 		var id = o.tagName == 'TR' ? o.id : $(o).parents('tr').attr('id'), parts = id.split('-');
   296 		var id = $(o).closest('tr').attr('id'),
       
   297 			parts = id.split('-');
   255 		return parts[parts.length - 1];
   298 		return parts[parts.length - 1];
   256 	}
   299 	}
   257 };
   300 };
   258 
   301 
   259 $(document).ready(function(){inlineEditPost.init();});
   302 $(document).ready(function(){inlineEditPost.init();});