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