wp/wp-admin/js/inline-edit-post.js
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 (function($) {
       
     2 inlineEditPost = {
       
     3 
       
     4 	init : function(){
       
     5 		var t = this, qeRow = $('#inline-edit'), bulkRow = $('#bulk-edit');
       
     6 
       
     7 		t.type = $('table.widefat').hasClass('pages') ? 'page' : 'post';
       
     8 		t.what = '#post-';
       
     9 
       
    10 		// prepare the edit rows
       
    11 		qeRow.keyup(function(e){
       
    12 			if (e.which == 27)
       
    13 				return inlineEditPost.revert();
       
    14 		});
       
    15 		bulkRow.keyup(function(e){
       
    16 			if (e.which == 27)
       
    17 				return inlineEditPost.revert();
       
    18 		});
       
    19 
       
    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(){
       
    36 			var pw = $('input.inline-edit-password-input');
       
    37 			if ( $(this).prop('checked') ) {
       
    38 				pw.val('').prop('disabled', true);
       
    39 			} else {
       
    40 				pw.prop('disabled', false);
       
    41 			}
       
    42 		});
       
    43 
       
    44 		// add events
       
    45 		$('#the-list').on('click', 'a.editinline', function(){
       
    46 			inlineEditPost.edit(this);
       
    47 			return false;
       
    48 		});
       
    49 
       
    50 		$('#bulk-title-div').parents('fieldset').after(
       
    51 			$('#inline-edit fieldset.inline-edit-categories').clone()
       
    52 		).siblings( 'fieldset:last' ).prepend(
       
    53 			$('#inline-edit label.inline-edit-tags').clone()
       
    54 		);
       
    55 
       
    56 		$('select[name="_status"] option[value="future"]', bulkRow).remove();
       
    57 
       
    58 		$('#doaction, #doaction2').click(function(e){
       
    59 			var n = $(this).attr('id').substr(2);
       
    60 			if ( $('select[name="'+n+'"]').val() == 'edit' ) {
       
    61 				e.preventDefault();
       
    62 				t.setBulk();
       
    63 			} else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
       
    64 				t.revert();
       
    65 			}
       
    66 		});
       
    67 	},
       
    68 
       
    69 	toggle : function(el){
       
    70 		var t = this;
       
    71 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
       
    72 	},
       
    73 
       
    74 	setBulk : function(){
       
    75 		var te = '', type = this.type, tax, c = true;
       
    76 		this.revert();
       
    77 
       
    78 		$('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
       
    79 		$('table.widefat tbody').prepend( $('#bulk-edit') );
       
    80 		$('#bulk-edit').addClass('inline-editor').show();
       
    81 
       
    82 		$('tbody th.check-column input[type="checkbox"]').each(function(i){
       
    83 			if ( $(this).prop('checked') ) {
       
    84 				c = false;
       
    85 				var id = $(this).val(), theTitle;
       
    86 				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>';
       
    88 			}
       
    89 		});
       
    90 
       
    91 		if ( c )
       
    92 			return this.revert();
       
    93 
       
    94 		$('#bulk-titles').html(te);
       
    95 		$('#bulk-titles a').click(function(){
       
    96 			var id = $(this).attr('id').substr(1);
       
    97 
       
    98 			$('table.widefat input[value="' + id + '"]').prop('checked', false);
       
    99 			$('#ttle'+id).remove();
       
   100 		});
       
   101 
       
   102 		// enable autocomplete for tags
       
   103 		if ( 'post' == type ) {
       
   104 			// support multi taxonomies?
       
   105 			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 + ' ' } );
       
   107 		}
       
   108 		$('html, body').animate( { scrollTop: 0 }, 'fast' );
       
   109 	},
       
   110 
       
   111 	edit : function(id) {
       
   112 		var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
       
   113 		t.revert();
       
   114 
       
   115 		if ( typeof(id) == 'object' )
       
   116 			id = t.getId(id);
       
   117 
       
   118 		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' )
       
   120 			fields.push('post_parent', 'page_template');
       
   121 
       
   122 		// add the new blank row
       
   123 		editRow = $('#inline-edit').clone(true);
       
   124 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
       
   125 
       
   126 		if ( $(t.what+id).hasClass('alternate') )
       
   127 			$(editRow).addClass('alternate');
       
   128 		$(t.what+id).hide().after(editRow);
       
   129 
       
   130 		// populate the data
       
   131 		rowData = $('#inline_'+id);
       
   132 		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
       
   134 			$(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
       
   135 		}
       
   136 		if ( $(':input[name="post_author"] option', editRow).length == 1 ) {
       
   137 			$('label.inline-edit-author', editRow).hide();
       
   138 		}
       
   139 
       
   140 		// hide unsupported formats, but leave the current format alone
       
   141 		cur_format = $('.post_format', rowData).text();
       
   142 		$('option.unsupported', editRow).each(function() {
       
   143 			var $this = $(this);
       
   144 			if ( $this.val() != cur_format )
       
   145 				$this.remove();
       
   146 		});
       
   147 
       
   148 		for ( f = 0; f < fields.length; f++ ) {
       
   149 			$(':input[name="' + fields[f] + '"]', editRow).val( $('.'+fields[f], rowData).text() );
       
   150 		}
       
   151 
       
   152 		if ( $('.comment_status', rowData).text() == 'open' )
       
   153 			$('input[name="comment_status"]', editRow).prop("checked", true);
       
   154 		if ( $('.ping_status', rowData).text() == 'open' )
       
   155 			$('input[name="ping_status"]', editRow).prop("checked", true);
       
   156 		if ( $('.sticky', rowData).text() == 'sticky' )
       
   157 			$('input[name="sticky"]', editRow).prop("checked", true);
       
   158 
       
   159 		// hierarchical taxonomies
       
   160 		$('.post_category', rowData).each(function(){
       
   161 			var term_ids = $(this).text();
       
   162 
       
   163 			if ( term_ids ) {
       
   164 				taxname = $(this).attr('id').replace('_'+id, '');
       
   165 				$('ul.'+taxname+'-checklist :checkbox', editRow).val(term_ids.split(','));
       
   166 			}
       
   167 		});
       
   168 
       
   169 		//flat taxonomies
       
   170 		$('.tags_input', rowData).each(function(){
       
   171 			var terms = $(this).text(),
       
   172 				taxname = $(this).attr('id').replace('_' + id, ''),
       
   173 				textarea = $('textarea.tax_input_' + taxname, editRow),
       
   174 				comma = inlineEditL10n.comma;
       
   175 
       
   176 			if ( terms ) {
       
   177 				if ( ',' !== comma )
       
   178 					terms = terms.replace(/,/g, comma);
       
   179 				textarea.val(terms);
       
   180 			}
       
   181 
       
   182 			textarea.suggest( ajaxurl + '?action=ajax-tag-search&tax=' + taxname, { delay: 500, minchars: 2, multiple: true, multipleSep: inlineEditL10n.comma + ' ' } );
       
   183 		});
       
   184 
       
   185 		// handle the post status
       
   186 		status = $('._status', rowData).text();
       
   187 		if ( 'future' != status )
       
   188 			$('select[name="_status"] option[value="future"]', editRow).remove();
       
   189 
       
   190 		if ( 'private' == status ) {
       
   191 			$('input[name="keep_private"]', editRow).prop("checked", true);
       
   192 			$('input.inline-edit-password-input').val('').prop('disabled', true);
       
   193 		}
       
   194 
       
   195 		// remove the current page and children from the parent dropdown
       
   196 		pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
       
   197 		if ( pageOpt.length > 0 ) {
       
   198 			pageLevel = pageOpt[0].className.split('-')[1];
       
   199 			nextPage = pageOpt;
       
   200 			while ( pageLoop ) {
       
   201 				nextPage = nextPage.next('option');
       
   202 				if (nextPage.length == 0) break;
       
   203 				nextLevel = nextPage[0].className.split('-')[1];
       
   204 				if ( nextLevel <= pageLevel ) {
       
   205 					pageLoop = false;
       
   206 				} else {
       
   207 					nextPage.remove();
       
   208 					nextPage = pageOpt;
       
   209 				}
       
   210 			}
       
   211 			pageOpt.remove();
       
   212 		}
       
   213 
       
   214 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
       
   215 		$('.ptitle', editRow).focus();
       
   216 
       
   217 		return false;
       
   218 	},
       
   219 
       
   220 	save : function(id) {
       
   221 		var params, fields, page = $('.post_status_page').val() || '';
       
   222 
       
   223 		if ( typeof(id) == 'object' )
       
   224 			id = this.getId(id);
       
   225 
       
   226 		$('table.widefat .spinner').show();
       
   227 
       
   228 		params = {
       
   229 			action: 'inline-save',
       
   230 			post_type: typenow,
       
   231 			post_ID: id,
       
   232 			edit_date: 'true',
       
   233 			post_status: page
       
   234 		};
       
   235 
       
   236 		fields = $('#edit-'+id+' :input').serialize();
       
   237 		params = fields + '&' + $.param(params);
       
   238 
       
   239 		// make ajax request
       
   240 		$.post( ajaxurl, params,
       
   241 			function(r) {
       
   242 				$('table.widefat .spinner').hide();
       
   243 
       
   244 				if (r) {
       
   245 					if ( -1 != r.indexOf('<tr') ) {
       
   246 						$(inlineEditPost.what+id).remove();
       
   247 						$('#edit-'+id).before(r).remove();
       
   248 						$(inlineEditPost.what+id).hide().fadeIn();
       
   249 					} else {
       
   250 						r = r.replace( /<.[^<>]*?>/g, '' );
       
   251 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
       
   252 					}
       
   253 				} else {
       
   254 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
       
   255 				}
       
   256 
       
   257 				if ( $('#post-'+id).prev().hasClass('alternate') ) {
       
   258 					$('#post-'+id).removeClass('alternate');
       
   259 				}
       
   260 			}
       
   261 		, 'html');
       
   262 		return false;
       
   263 	},
       
   264 
       
   265 	revert : function(){
       
   266 		var id = $('table.widefat tr.inline-editor').attr('id');
       
   267 
       
   268 		if ( id ) {
       
   269 			$('table.widefat .spinner').hide();
       
   270 
       
   271 			if ( 'bulk-edit' == id ) {
       
   272 				$('table.widefat #bulk-edit').removeClass('inline-editor').hide();
       
   273 				$('#bulk-titles').html('');
       
   274 				$('#inlineedit').append( $('#bulk-edit') );
       
   275 			} else {
       
   276 				$('#'+id).remove();
       
   277 				id = id.substr( id.lastIndexOf('-') + 1 );
       
   278 				$(this.what+id).show();
       
   279 			}
       
   280 		}
       
   281 
       
   282 		return false;
       
   283 	},
       
   284 
       
   285 	getId : function(o) {
       
   286 		var id = $(o).closest('tr').attr('id'),
       
   287 			parts = id.split('-');
       
   288 		return parts[parts.length - 1];
       
   289 	}
       
   290 };
       
   291 
       
   292 $( document ).ready( function(){ inlineEditPost.init(); } );
       
   293 
       
   294 // Show/hide locks on posts
       
   295 $( document ).on( 'heartbeat-tick.wp-check-locked-posts', function( e, data ) {
       
   296 	var locked = data['wp-check-locked-posts'] || {};
       
   297 
       
   298 	$('#the-list tr').each( function(i, el) {
       
   299 		var key = el.id, row = $(el), lock_data, avatar;
       
   300 
       
   301 		if ( locked.hasOwnProperty( key ) ) {
       
   302 			if ( ! row.hasClass('wp-locked') ) {
       
   303 				lock_data = locked[key];
       
   304 				row.find('.column-title .locked-text').text( lock_data.text );
       
   305 				row.find('.check-column checkbox').prop('checked', false);
       
   306 
       
   307 				if ( lock_data.avatar_src ) {
       
   308 					avatar = $('<img class="avatar avatar-18 photo" width="18" height="18" />').attr( 'src', lock_data.avatar_src.replace(/&amp;/g, '&') );
       
   309 					row.find('.column-title .locked-avatar').empty().append( avatar );
       
   310 				}
       
   311 				row.addClass('wp-locked');
       
   312 			}
       
   313 		} else if ( row.hasClass('wp-locked') ) {
       
   314 			// Make room for the CSS animation
       
   315 			row.removeClass('wp-locked').delay(1000).find('.locked-info span').empty();
       
   316 		}
       
   317 	});
       
   318 }).on( 'heartbeat-send.wp-check-locked-posts', function( e, data ) {
       
   319 	var check = [];
       
   320 
       
   321 	$('#the-list tr').each( function(i, el) {
       
   322 		if ( el.id )
       
   323 			check.push( el.id );
       
   324 	});
       
   325 
       
   326 	if ( check.length )
       
   327 		data['wp-check-locked-posts'] = check;
       
   328 });
       
   329 
       
   330 }(jQuery));