wp/wp-admin/js/post.js
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
       
     2 
       
     3 // return an array with any duplicate, whitespace or values removed
       
     4 function array_unique_noempty(a) {
       
     5 	var out = [];
       
     6 	jQuery.each( a, function(key, val) {
       
     7 		val = jQuery.trim(val);
       
     8 		if ( val && jQuery.inArray(val, out) == -1 )
       
     9 			out.push(val);
       
    10 		} );
       
    11 	return out;
       
    12 }
       
    13 
       
    14 (function($){
       
    15 
       
    16 tagBox = {
       
    17 	clean : function(tags) {
       
    18 		var comma = postL10n.comma;
       
    19 		if ( ',' !== comma )
       
    20 			tags = tags.replace(new RegExp(comma, 'g'), ',');
       
    21 		tags = tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
       
    22 		if ( ',' !== comma )
       
    23 			tags = tags.replace(/,/g, comma);
       
    24 		return tags;
       
    25 	},
       
    26 
       
    27 	parseTags : function(el) {
       
    28 		var id = el.id, num = id.split('-check-num-')[1], taxbox = $(el).closest('.tagsdiv'),
       
    29 			thetags = taxbox.find('.the-tags'), comma = postL10n.comma,
       
    30 			current_tags = thetags.val().split(comma), new_tags = [];
       
    31 		delete current_tags[num];
       
    32 
       
    33 		$.each( current_tags, function(key, val) {
       
    34 			val = $.trim(val);
       
    35 			if ( val ) {
       
    36 				new_tags.push(val);
       
    37 			}
       
    38 		});
       
    39 
       
    40 		thetags.val( this.clean( new_tags.join(comma) ) );
       
    41 
       
    42 		this.quickClicks(taxbox);
       
    43 		return false;
       
    44 	},
       
    45 
       
    46 	quickClicks : function(el) {
       
    47 		var thetags = $('.the-tags', el),
       
    48 			tagchecklist = $('.tagchecklist', el),
       
    49 			id = $(el).attr('id'),
       
    50 			current_tags, disabled;
       
    51 
       
    52 		if ( !thetags.length )
       
    53 			return;
       
    54 
       
    55 		disabled = thetags.prop('disabled');
       
    56 
       
    57 		current_tags = thetags.val().split(postL10n.comma);
       
    58 		tagchecklist.empty();
       
    59 
       
    60 		$.each( current_tags, function( key, val ) {
       
    61 			var span, xbutton;
       
    62 
       
    63 			val = $.trim( val );
       
    64 
       
    65 			if ( ! val )
       
    66 				return;
       
    67 
       
    68 			// Create a new span, and ensure the text is properly escaped.
       
    69 			span = $('<span />').text( val );
       
    70 
       
    71 			// If tags editing isn't disabled, create the X button.
       
    72 			if ( ! disabled ) {
       
    73 				xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton">X</a>' );
       
    74 				xbutton.click( function(){ tagBox.parseTags(this); });
       
    75 				span.prepend('&nbsp;').prepend( xbutton );
       
    76 			}
       
    77 
       
    78 			// Append the span to the tag list.
       
    79 			tagchecklist.append( span );
       
    80 		});
       
    81 	},
       
    82 
       
    83 	flushTags : function(el, a, f) {
       
    84 		a = a || false;
       
    85 		var tags = $('.the-tags', el),
       
    86 			newtag = $('input.newtag', el),
       
    87 			comma = postL10n.comma,
       
    88 			newtags, text;
       
    89 
       
    90 		text = a ? $(a).text() : newtag.val();
       
    91 		tagsval = tags.val();
       
    92 		newtags = tagsval ? tagsval + comma + text : text;
       
    93 
       
    94 		newtags = this.clean( newtags );
       
    95 		newtags = array_unique_noempty( newtags.split(comma) ).join(comma);
       
    96 		tags.val(newtags);
       
    97 		this.quickClicks(el);
       
    98 
       
    99 		if ( !a )
       
   100 			newtag.val('');
       
   101 		if ( 'undefined' == typeof(f) )
       
   102 			newtag.focus();
       
   103 
       
   104 		return false;
       
   105 	},
       
   106 
       
   107 	get : function(id) {
       
   108 		var tax = id.substr(id.indexOf('-')+1);
       
   109 
       
   110 		$.post(ajaxurl, {'action':'get-tagcloud', 'tax':tax}, function(r, stat) {
       
   111 			if ( 0 == r || 'success' != stat )
       
   112 				r = wpAjax.broken;
       
   113 
       
   114 			r = $('<p id="tagcloud-'+tax+'" class="the-tagcloud">'+r+'</p>');
       
   115 			$('a', r).click(function(){
       
   116 				tagBox.flushTags( $(this).closest('.inside').children('.tagsdiv'), this);
       
   117 				return false;
       
   118 			});
       
   119 
       
   120 			$('#'+id).after(r);
       
   121 		});
       
   122 	},
       
   123 
       
   124 	init : function() {
       
   125 		var t = this, ajaxtag = $('div.ajaxtag');
       
   126 
       
   127 		$('.tagsdiv').each( function() {
       
   128 			tagBox.quickClicks(this);
       
   129 		});
       
   130 
       
   131 		$('input.tagadd', ajaxtag).click(function(){
       
   132 			t.flushTags( $(this).closest('.tagsdiv') );
       
   133 		});
       
   134 
       
   135 		$('div.taghint', ajaxtag).click(function(){
       
   136 			$(this).css('visibility', 'hidden').parent().siblings('.newtag').focus();
       
   137 		});
       
   138 
       
   139 		$('input.newtag', ajaxtag).blur(function() {
       
   140 			if ( this.value == '' )
       
   141 				$(this).parent().siblings('.taghint').css('visibility', '');
       
   142 		}).focus(function(){
       
   143 			$(this).parent().siblings('.taghint').css('visibility', 'hidden');
       
   144 		}).keyup(function(e){
       
   145 			if ( 13 == e.which ) {
       
   146 				tagBox.flushTags( $(this).closest('.tagsdiv') );
       
   147 				return false;
       
   148 			}
       
   149 		}).keypress(function(e){
       
   150 			if ( 13 == e.which ) {
       
   151 				e.preventDefault();
       
   152 				return false;
       
   153 			}
       
   154 		}).each(function(){
       
   155 			var tax = $(this).closest('div.tagsdiv').attr('id');
       
   156 			$(this).suggest( ajaxurl + '?action=ajax-tag-search&tax=' + tax, { delay: 500, minchars: 2, multiple: true, multipleSep: postL10n.comma + ' ' } );
       
   157 		});
       
   158 
       
   159 		// save tags on post save/publish
       
   160 		$('#post').submit(function(){
       
   161 			$('div.tagsdiv').each( function() {
       
   162 				tagBox.flushTags(this, false, 1);
       
   163 			});
       
   164 		});
       
   165 
       
   166 		// tag cloud
       
   167 		$('a.tagcloud-link').click(function(){
       
   168 			tagBox.get( $(this).attr('id') );
       
   169 			$(this).unbind().click(function(){
       
   170 				$(this).siblings('.the-tagcloud').toggle();
       
   171 				return false;
       
   172 			});
       
   173 			return false;
       
   174 		});
       
   175 	}
       
   176 };
       
   177 
       
   178 commentsBox = {
       
   179 	st : 0,
       
   180 
       
   181 	get : function(total, num) {
       
   182 		var st = this.st, data;
       
   183 		if ( ! num )
       
   184 			num = 20;
       
   185 
       
   186 		this.st += num;
       
   187 		this.total = total;
       
   188 		$('#commentsdiv .spinner').show();
       
   189 
       
   190 		data = {
       
   191 			'action' : 'get-comments',
       
   192 			'mode' : 'single',
       
   193 			'_ajax_nonce' : $('#add_comment_nonce').val(),
       
   194 			'p' : $('#post_ID').val(),
       
   195 			'start' : st,
       
   196 			'number' : num
       
   197 		};
       
   198 
       
   199 		$.post(ajaxurl, data,
       
   200 			function(r) {
       
   201 				r = wpAjax.parseAjaxResponse(r);
       
   202 				$('#commentsdiv .widefat').show();
       
   203 				$('#commentsdiv .spinner').hide();
       
   204 
       
   205 				if ( 'object' == typeof r && r.responses[0] ) {
       
   206 					$('#the-comment-list').append( r.responses[0].data );
       
   207 
       
   208 					theList = theExtraList = null;
       
   209 					$("a[className*=':']").unbind();
       
   210 
       
   211 					if ( commentsBox.st > commentsBox.total )
       
   212 						$('#show-comments').hide();
       
   213 					else
       
   214 						$('#show-comments').show().children('a').html(postL10n.showcomm);
       
   215 
       
   216 					return;
       
   217 				} else if ( 1 == r ) {
       
   218 					$('#show-comments').html(postL10n.endcomm);
       
   219 					return;
       
   220 				}
       
   221 
       
   222 				$('#the-comment-list').append('<tr><td colspan="2">'+wpAjax.broken+'</td></tr>');
       
   223 			}
       
   224 		);
       
   225 
       
   226 		return false;
       
   227 	}
       
   228 };
       
   229 
       
   230 WPSetThumbnailHTML = function(html){
       
   231 	$('.inside', '#postimagediv').html(html);
       
   232 };
       
   233 
       
   234 WPSetThumbnailID = function(id){
       
   235 	var field = $('input[value="_thumbnail_id"]', '#list-table');
       
   236 	if ( field.size() > 0 ) {
       
   237 		$('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
       
   238 	}
       
   239 };
       
   240 
       
   241 WPRemoveThumbnail = function(nonce){
       
   242 	$.post(ajaxurl, {
       
   243 		action:"set-post-thumbnail", post_id: $('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
       
   244 	}, function(str){
       
   245 		if ( str == '0' ) {
       
   246 			alert( setPostThumbnailL10n.error );
       
   247 		} else {
       
   248 			WPSetThumbnailHTML(str);
       
   249 		}
       
   250 	}
       
   251 	);
       
   252 };
       
   253 
       
   254 $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
       
   255 	var lock = $('#active_post_lock').val(),
       
   256 		post_id = $('#post_ID').val(),
       
   257 		send = {};
       
   258 
       
   259 	if ( ! post_id || ! $('#post-lock-dialog').length )
       
   260 		return;
       
   261 
       
   262 	send['post_id'] = post_id;
       
   263 
       
   264 	if ( lock )
       
   265 		send['lock'] = lock;
       
   266 
       
   267 	data['wp-refresh-post-lock'] = send;
       
   268 });
       
   269 
       
   270 // Post locks: update the lock string or show the dialog if somebody has taken over editing
       
   271 $(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
       
   272 	var received, wrap, avatar;
       
   273 
       
   274 	if ( data['wp-refresh-post-lock'] ) {
       
   275 		received = data['wp-refresh-post-lock'];
       
   276 
       
   277 		if ( received.lock_error ) {
       
   278 			// show "editing taken over" message
       
   279 			wrap = $('#post-lock-dialog');
       
   280 
       
   281 			if ( wrap.length && ! wrap.is(':visible') ) {
       
   282 				if ( typeof autosave == 'function' ) {
       
   283 					$(document).on('autosave-disable-buttons.post-lock', function() {
       
   284 						wrap.addClass('saving');
       
   285 					}).on('autosave-enable-buttons.post-lock', function() {
       
   286 						wrap.removeClass('saving').addClass('saved');
       
   287 						window.onbeforeunload = null;
       
   288 					});
       
   289 
       
   290 					// Save the latest changes and disable
       
   291 					if ( ! autosave() )
       
   292 						window.onbeforeunload = null;
       
   293 
       
   294 					autosave = function(){};
       
   295 				}
       
   296 
       
   297 				if ( received.lock_error.avatar_src ) {
       
   298 					avatar = $('<img class="avatar avatar-64 photo" width="64" height="64" />').attr( 'src', received.lock_error.avatar_src.replace(/&amp;/g, '&') );
       
   299 					wrap.find('div.post-locked-avatar').empty().append( avatar );
       
   300 				}
       
   301 
       
   302 				wrap.show().find('.currently-editing').text( received.lock_error.text );
       
   303 				wrap.find('.wp-tab-first').focus();
       
   304 			}
       
   305 		} else if ( received.new_lock ) {
       
   306 			$('#active_post_lock').val( received.new_lock );
       
   307 		}
       
   308 	}
       
   309 });
       
   310 
       
   311 }(jQuery));
       
   312 
       
   313 (function($) {
       
   314 	var check, timeout;
       
   315 
       
   316 	function schedule() {
       
   317 		check = false;
       
   318 		window.clearTimeout( timeout );
       
   319 		timeout = window.setTimeout( function(){ check = true; }, 300000 );
       
   320 	}
       
   321 
       
   322 	$(document).on( 'heartbeat-send.wp-refresh-nonces', function( e, data ) {
       
   323 		var nonce, post_id;
       
   324 
       
   325 		if ( check ) {
       
   326 			if ( ( post_id = $('#post_ID').val() ) && ( nonce = $('#_wpnonce').val() ) ) {
       
   327 				data['wp-refresh-post-nonces'] = {
       
   328 					post_id: post_id,
       
   329 					post_nonce: nonce
       
   330 				};
       
   331 			}
       
   332 		}
       
   333 	}).on( 'heartbeat-tick.wp-refresh-nonces', function( e, data ) {
       
   334 		var nonces = data['wp-refresh-post-nonces'];
       
   335 
       
   336 		if ( nonces ) {
       
   337 			schedule();
       
   338 
       
   339 			if ( nonces.replace ) {
       
   340 				$.each( nonces.replace, function( selector, value ) {
       
   341 					$( '#' + selector ).val( value );
       
   342 				});
       
   343 			}
       
   344 
       
   345 			if ( nonces.heartbeatNonce )
       
   346 				window.heartbeatSettings.nonce = nonces.heartbeatNonce;
       
   347 		}
       
   348 	}).ready( function() {
       
   349 		schedule();
       
   350 	});
       
   351 }(jQuery));
       
   352 
       
   353 jQuery(document).ready( function($) {
       
   354 	var stamp, visibility, sticky = '', last = 0, co = $('#content');
       
   355 
       
   356 	postboxes.add_postbox_toggles(pagenow);
       
   357 
       
   358 	// Post locks: contain focus inside the dialog. If the dialog is shown, focus the first item.
       
   359 	$('#post-lock-dialog .notification-dialog').on( 'keydown', function(e) {
       
   360 		if ( e.which != 9 )
       
   361 			return;
       
   362 
       
   363 		var target = $(e.target);
       
   364 
       
   365 		if ( target.hasClass('wp-tab-first') && e.shiftKey ) {
       
   366 			$(this).find('.wp-tab-last').focus();
       
   367 			e.preventDefault();
       
   368 		} else if ( target.hasClass('wp-tab-last') && ! e.shiftKey ) {
       
   369 			$(this).find('.wp-tab-first').focus();
       
   370 			e.preventDefault();
       
   371 		}
       
   372 	}).filter(':visible').find('.wp-tab-first').focus();
       
   373 
       
   374 	// multi-taxonomies
       
   375 	if ( $('#tagsdiv-post_tag').length ) {
       
   376 		tagBox.init();
       
   377 	} else {
       
   378 		$('#side-sortables, #normal-sortables, #advanced-sortables').children('div.postbox').each(function(){
       
   379 			if ( this.id.indexOf('tagsdiv-') === 0 ) {
       
   380 				tagBox.init();
       
   381 				return false;
       
   382 			}
       
   383 		});
       
   384 	}
       
   385 
       
   386 	// categories
       
   387 	$('.categorydiv').each( function(){
       
   388 		var this_id = $(this).attr('id'), catAddBefore, catAddAfter, taxonomyParts, taxonomy, settingName;
       
   389 
       
   390 		taxonomyParts = this_id.split('-');
       
   391 		taxonomyParts.shift();
       
   392 		taxonomy = taxonomyParts.join('-');
       
   393 		settingName = taxonomy + '_tab';
       
   394 		if ( taxonomy == 'category' )
       
   395 			settingName = 'cats';
       
   396 
       
   397 		// TODO: move to jQuery 1.3+, support for multiple hierarchical taxonomies, see wp-lists.js
       
   398 		$('a', '#' + taxonomy + '-tabs').click( function(){
       
   399 			var t = $(this).attr('href');
       
   400 			$(this).parent().addClass('tabs').siblings('li').removeClass('tabs');
       
   401 			$('#' + taxonomy + '-tabs').siblings('.tabs-panel').hide();
       
   402 			$(t).show();
       
   403 			if ( '#' + taxonomy + '-all' == t )
       
   404 				deleteUserSetting(settingName);
       
   405 			else
       
   406 				setUserSetting(settingName, 'pop');
       
   407 			return false;
       
   408 		});
       
   409 
       
   410 		if ( getUserSetting(settingName) )
       
   411 			$('a[href="#' + taxonomy + '-pop"]', '#' + taxonomy + '-tabs').click();
       
   412 
       
   413 		// Ajax Cat
       
   414 		$('#new' + taxonomy).one( 'focus', function() { $(this).val( '' ).removeClass( 'form-input-tip' ) } );
       
   415 
       
   416 		$('#new' + taxonomy).keypress( function(event){
       
   417 			if( 13 === event.keyCode ) {
       
   418 				event.preventDefault();
       
   419 				$('#' + taxonomy + '-add-submit').click();
       
   420 			}
       
   421 		});
       
   422 		$('#' + taxonomy + '-add-submit').click( function(){ $('#new' + taxonomy).focus(); });
       
   423 
       
   424 		catAddBefore = function( s ) {
       
   425 			if ( !$('#new'+taxonomy).val() )
       
   426 				return false;
       
   427 			s.data += '&' + $( ':checked', '#'+taxonomy+'checklist' ).serialize();
       
   428 			$( '#' + taxonomy + '-add-submit' ).prop( 'disabled', true );
       
   429 			return s;
       
   430 		};
       
   431 
       
   432 		catAddAfter = function( r, s ) {
       
   433 			var sup, drop = $('#new'+taxonomy+'_parent');
       
   434 
       
   435 			$( '#' + taxonomy + '-add-submit' ).prop( 'disabled', false );
       
   436 			if ( 'undefined' != s.parsed.responses[0] && (sup = s.parsed.responses[0].supplemental.newcat_parent) ) {
       
   437 				drop.before(sup);
       
   438 				drop.remove();
       
   439 			}
       
   440 		};
       
   441 
       
   442 		$('#' + taxonomy + 'checklist').wpList({
       
   443 			alt: '',
       
   444 			response: taxonomy + '-ajax-response',
       
   445 			addBefore: catAddBefore,
       
   446 			addAfter: catAddAfter
       
   447 		});
       
   448 
       
   449 		$('#' + taxonomy + '-add-toggle').click( function() {
       
   450 			$('#' + taxonomy + '-adder').toggleClass( 'wp-hidden-children' );
       
   451 			$('a[href="#' + taxonomy + '-all"]', '#' + taxonomy + '-tabs').click();
       
   452 			$('#new'+taxonomy).focus();
       
   453 			return false;
       
   454 		});
       
   455 
       
   456 		$('#' + taxonomy + 'checklist, #' + taxonomy + 'checklist-pop').on( 'click', 'li.popular-category > label input[type="checkbox"]', function() {
       
   457 			var t = $(this), c = t.is(':checked'), id = t.val();
       
   458 			if ( id && t.parents('#taxonomy-'+taxonomy).length )
       
   459 				$('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c );
       
   460 		});
       
   461 
       
   462 	}); // end cats
       
   463 
       
   464 	// Custom Fields
       
   465 	if ( $('#postcustom').length ) {
       
   466 		$('#the-list').wpList( { addAfter: function( xml, s ) {
       
   467 			$('table#list-table').show();
       
   468 		}, addBefore: function( s ) {
       
   469 			s.data += '&post_id=' + $('#post_ID').val();
       
   470 			return s;
       
   471 		}
       
   472 		});
       
   473 	}
       
   474 
       
   475 	// submitdiv
       
   476 	if ( $('#submitdiv').length ) {
       
   477 		stamp = $('#timestamp').html();
       
   478 		visibility = $('#post-visibility-display').html();
       
   479 
       
   480 		function updateVisibility() {
       
   481 			var pvSelect = $('#post-visibility-select');
       
   482 			if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
       
   483 				$('#sticky').prop('checked', false);
       
   484 				$('#sticky-span').hide();
       
   485 			} else {
       
   486 				$('#sticky-span').show();
       
   487 			}
       
   488 			if ( $('input:radio:checked', pvSelect).val() != 'password' ) {
       
   489 				$('#password-span').hide();
       
   490 			} else {
       
   491 				$('#password-span').show();
       
   492 			}
       
   493 		}
       
   494 
       
   495 		function updateText() {
       
   496 
       
   497 			if ( ! $('#timestampdiv').length )
       
   498 				return true;
       
   499 
       
   500 			var attemptedDate, originalDate, currentDate, publishOn, postStatus = $('#post_status'),
       
   501 				optPublish = $('option[value="publish"]', postStatus), aa = $('#aa').val(),
       
   502 				mm = $('#mm').val(), jj = $('#jj').val(), hh = $('#hh').val(), mn = $('#mn').val();
       
   503 
       
   504 			attemptedDate = new Date( aa, mm - 1, jj, hh, mn );
       
   505 			originalDate = new Date( $('#hidden_aa').val(), $('#hidden_mm').val() -1, $('#hidden_jj').val(), $('#hidden_hh').val(), $('#hidden_mn').val() );
       
   506 			currentDate = new Date( $('#cur_aa').val(), $('#cur_mm').val() -1, $('#cur_jj').val(), $('#cur_hh').val(), $('#cur_mn').val() );
       
   507 
       
   508 			if ( attemptedDate.getFullYear() != aa || (1 + attemptedDate.getMonth()) != mm || attemptedDate.getDate() != jj || attemptedDate.getMinutes() != mn ) {
       
   509 				$('.timestamp-wrap', '#timestampdiv').addClass('form-invalid');
       
   510 				return false;
       
   511 			} else {
       
   512 				$('.timestamp-wrap', '#timestampdiv').removeClass('form-invalid');
       
   513 			}
       
   514 
       
   515 			if ( attemptedDate > currentDate && $('#original_post_status').val() != 'future' ) {
       
   516 				publishOn = postL10n.publishOnFuture;
       
   517 				$('#publish').val( postL10n.schedule );
       
   518 			} else if ( attemptedDate <= currentDate && $('#original_post_status').val() != 'publish' ) {
       
   519 				publishOn = postL10n.publishOn;
       
   520 				$('#publish').val( postL10n.publish );
       
   521 			} else {
       
   522 				publishOn = postL10n.publishOnPast;
       
   523 				$('#publish').val( postL10n.update );
       
   524 			}
       
   525 			if ( originalDate.toUTCString() == attemptedDate.toUTCString() ) { //hack
       
   526 				$('#timestamp').html(stamp);
       
   527 			} else {
       
   528 				$('#timestamp').html(
       
   529 					publishOn + ' <b>' +
       
   530 					postL10n.dateFormat.replace( '%1$s', $('option[value="' + $('#mm').val() + '"]', '#mm').text() )
       
   531 						.replace( '%2$s', jj )
       
   532 						.replace( '%3$s', aa )
       
   533 						.replace( '%4$s', hh )
       
   534 						.replace( '%5$s', mn )
       
   535 					+ '</b> '
       
   536 				);
       
   537 			}
       
   538 
       
   539 			if ( $('input:radio:checked', '#post-visibility-select').val() == 'private' ) {
       
   540 				$('#publish').val( postL10n.update );
       
   541 				if ( optPublish.length == 0 ) {
       
   542 					postStatus.append('<option value="publish">' + postL10n.privatelyPublished + '</option>');
       
   543 				} else {
       
   544 					optPublish.html( postL10n.privatelyPublished );
       
   545 				}
       
   546 				$('option[value="publish"]', postStatus).prop('selected', true);
       
   547 				$('.edit-post-status', '#misc-publishing-actions').hide();
       
   548 			} else {
       
   549 				if ( $('#original_post_status').val() == 'future' || $('#original_post_status').val() == 'draft' ) {
       
   550 					if ( optPublish.length ) {
       
   551 						optPublish.remove();
       
   552 						postStatus.val($('#hidden_post_status').val());
       
   553 					}
       
   554 				} else {
       
   555 					optPublish.html( postL10n.published );
       
   556 				}
       
   557 				if ( postStatus.is(':hidden') )
       
   558 					$('.edit-post-status', '#misc-publishing-actions').show();
       
   559 			}
       
   560 			$('#post-status-display').html($('option:selected', postStatus).text());
       
   561 			if ( $('option:selected', postStatus).val() == 'private' || $('option:selected', postStatus).val() == 'publish' ) {
       
   562 				$('#save-post').hide();
       
   563 			} else {
       
   564 				$('#save-post').show();
       
   565 				if ( $('option:selected', postStatus).val() == 'pending' ) {
       
   566 					$('#save-post').show().val( postL10n.savePending );
       
   567 				} else {
       
   568 					$('#save-post').show().val( postL10n.saveDraft );
       
   569 				}
       
   570 			}
       
   571 			return true;
       
   572 		}
       
   573 
       
   574 		$('.edit-visibility', '#visibility').click(function () {
       
   575 			if ($('#post-visibility-select').is(":hidden")) {
       
   576 				updateVisibility();
       
   577 				$('#post-visibility-select').slideDown('fast');
       
   578 				$(this).hide();
       
   579 			}
       
   580 			return false;
       
   581 		});
       
   582 
       
   583 		$('.cancel-post-visibility', '#post-visibility-select').click(function () {
       
   584 			$('#post-visibility-select').slideUp('fast');
       
   585 			$('#visibility-radio-' + $('#hidden-post-visibility').val()).prop('checked', true);
       
   586 			$('#post_password').val($('#hidden-post-password').val());
       
   587 			$('#sticky').prop('checked', $('#hidden-post-sticky').prop('checked'));
       
   588 			$('#post-visibility-display').html(visibility);
       
   589 			$('.edit-visibility', '#visibility').show();
       
   590 			updateText();
       
   591 			return false;
       
   592 		});
       
   593 
       
   594 		$('.save-post-visibility', '#post-visibility-select').click(function () { // crazyhorse - multiple ok cancels
       
   595 			var pvSelect = $('#post-visibility-select');
       
   596 
       
   597 			pvSelect.slideUp('fast');
       
   598 			$('.edit-visibility', '#visibility').show();
       
   599 			updateText();
       
   600 
       
   601 			if ( $('input:radio:checked', pvSelect).val() != 'public' ) {
       
   602 				$('#sticky').prop('checked', false);
       
   603 			} // WEAPON LOCKED
       
   604 
       
   605 			if ( true == $('#sticky').prop('checked') ) {
       
   606 				sticky = 'Sticky';
       
   607 			} else {
       
   608 				sticky = '';
       
   609 			}
       
   610 
       
   611 			$('#post-visibility-display').html(	postL10n[$('input:radio:checked', pvSelect).val() + sticky]	);
       
   612 			return false;
       
   613 		});
       
   614 
       
   615 		$('input:radio', '#post-visibility-select').change(function() {
       
   616 			updateVisibility();
       
   617 		});
       
   618 
       
   619 		$('#timestampdiv').siblings('a.edit-timestamp').click(function() {
       
   620 			if ($('#timestampdiv').is(":hidden")) {
       
   621 				$('#timestampdiv').slideDown('fast');
       
   622 				$('#mm').focus();
       
   623 				$(this).hide();
       
   624 			}
       
   625 			return false;
       
   626 		});
       
   627 
       
   628 		$('.cancel-timestamp', '#timestampdiv').click(function() {
       
   629 			$('#timestampdiv').slideUp('fast');
       
   630 			$('#mm').val($('#hidden_mm').val());
       
   631 			$('#jj').val($('#hidden_jj').val());
       
   632 			$('#aa').val($('#hidden_aa').val());
       
   633 			$('#hh').val($('#hidden_hh').val());
       
   634 			$('#mn').val($('#hidden_mn').val());
       
   635 			$('#timestampdiv').siblings('a.edit-timestamp').show();
       
   636 			updateText();
       
   637 			return false;
       
   638 		});
       
   639 
       
   640 		$('.save-timestamp', '#timestampdiv').click(function () { // crazyhorse - multiple ok cancels
       
   641 			if ( updateText() ) {
       
   642 				$('#timestampdiv').slideUp('fast');
       
   643 				$('#timestampdiv').siblings('a.edit-timestamp').show();
       
   644 			}
       
   645 			return false;
       
   646 		});
       
   647 
       
   648 		$('#post').on( 'submit', function(e){
       
   649 			if ( ! updateText() ) {
       
   650 				e.preventDefault();
       
   651 				$('#timestampdiv').show();
       
   652 				$('#publishing-action .spinner').hide();
       
   653 				$('#publish').prop('disabled', false).removeClass('button-primary-disabled');
       
   654 				return false;
       
   655 			}
       
   656 		});
       
   657 
       
   658 		$('#post-status-select').siblings('a.edit-post-status').click(function() {
       
   659 			if ($('#post-status-select').is(":hidden")) {
       
   660 				$('#post-status-select').slideDown('fast');
       
   661 				$(this).hide();
       
   662 			}
       
   663 			return false;
       
   664 		});
       
   665 
       
   666 		$('.save-post-status', '#post-status-select').click(function() {
       
   667 			$('#post-status-select').slideUp('fast');
       
   668 			$('#post-status-select').siblings('a.edit-post-status').show();
       
   669 			updateText();
       
   670 			return false;
       
   671 		});
       
   672 
       
   673 		$('.cancel-post-status', '#post-status-select').click(function() {
       
   674 			$('#post-status-select').slideUp('fast');
       
   675 			$('#post_status').val($('#hidden_post_status').val());
       
   676 			$('#post-status-select').siblings('a.edit-post-status').show();
       
   677 			updateText();
       
   678 			return false;
       
   679 		});
       
   680 	} // end submitdiv
       
   681 
       
   682 	// permalink
       
   683 	if ( $('#edit-slug-box').length ) {
       
   684 		editPermalink = function(post_id) {
       
   685 			var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'), revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(), full = $('#editable-post-name-full').html();
       
   686 
       
   687 			$('#view-post-btn').hide();
       
   688 			b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
       
   689 			b.children('.save').click(function() {
       
   690 				var new_slug = e.children('input').val();
       
   691 				if ( new_slug == $('#editable-post-name-full').text() ) {
       
   692 					return $('.cancel', '#edit-slug-buttons').click();
       
   693 				}
       
   694 				$.post(ajaxurl, {
       
   695 					action: 'sample-permalink',
       
   696 					post_id: post_id,
       
   697 					new_slug: new_slug,
       
   698 					new_title: $('#title').val(),
       
   699 					samplepermalinknonce: $('#samplepermalinknonce').val()
       
   700 				}, function(data) {
       
   701 					var box = $('#edit-slug-box');
       
   702 					box.html(data);
       
   703 					if (box.hasClass('hidden')) {
       
   704 						box.fadeIn('fast', function () {
       
   705 							box.removeClass('hidden');
       
   706 						});
       
   707 					}
       
   708 					b.html(revert_b);
       
   709 					real_slug.val(new_slug);
       
   710 					makeSlugeditClickable();
       
   711 					$('#view-post-btn').show();
       
   712 				});
       
   713 				return false;
       
   714 			});
       
   715 
       
   716 			$('.cancel', '#edit-slug-buttons').click(function() {
       
   717 				$('#view-post-btn').show();
       
   718 				e.html(revert_e);
       
   719 				b.html(revert_b);
       
   720 				real_slug.val(revert_slug);
       
   721 				return false;
       
   722 			});
       
   723 
       
   724 			for ( i = 0; i < full.length; ++i ) {
       
   725 				if ( '%' == full.charAt(i) )
       
   726 					c++;
       
   727 			}
       
   728 
       
   729 			slug_value = ( c > full.length / 4 ) ? '' : full;
       
   730 			e.html('<input type="text" id="new-post-slug" value="'+slug_value+'" />').children('input').keypress(function(e) {
       
   731 				var key = e.keyCode || 0;
       
   732 				// on enter, just save the new slug, don't save the post
       
   733 				if ( 13 == key ) {
       
   734 					b.children('.save').click();
       
   735 					return false;
       
   736 				}
       
   737 				if ( 27 == key ) {
       
   738 					b.children('.cancel').click();
       
   739 					return false;
       
   740 				}
       
   741 			}).keyup(function(e) {
       
   742 				real_slug.val(this.value);
       
   743 			}).focus();
       
   744 		}
       
   745 
       
   746 		makeSlugeditClickable = function() {
       
   747 			$('#editable-post-name').click(function() {
       
   748 				$('#edit-slug-buttons').children('.edit-slug').click();
       
   749 			});
       
   750 		}
       
   751 		makeSlugeditClickable();
       
   752 	}
       
   753 
       
   754 	// word count
       
   755 	if ( typeof(wpWordCount) != 'undefined' ) {
       
   756 		$(document).triggerHandler('wpcountwords', [ co.val() ]);
       
   757 
       
   758 		co.keyup( function(e) {
       
   759 			var k = e.keyCode || e.charCode;
       
   760 
       
   761 			if ( k == last )
       
   762 				return true;
       
   763 
       
   764 			if ( 13 == k || 8 == last || 46 == last )
       
   765 				$(document).triggerHandler('wpcountwords', [ co.val() ]);
       
   766 
       
   767 			last = k;
       
   768 			return true;
       
   769 		});
       
   770 	}
       
   771 
       
   772 	wptitlehint = function(id) {
       
   773 		id = id || 'title';
       
   774 
       
   775 		var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');
       
   776 
       
   777 		if ( title.val() == '' )
       
   778 			titleprompt.removeClass('screen-reader-text');
       
   779 
       
   780 		titleprompt.click(function(){
       
   781 			$(this).addClass('screen-reader-text');
       
   782 			title.focus();
       
   783 		});
       
   784 
       
   785 		title.blur(function(){
       
   786 			if ( this.value == '' )
       
   787 				titleprompt.removeClass('screen-reader-text');
       
   788 		}).focus(function(){
       
   789 			titleprompt.addClass('screen-reader-text');
       
   790 		}).keydown(function(e){
       
   791 			titleprompt.addClass('screen-reader-text');
       
   792 			$(this).unbind(e);
       
   793 		});
       
   794 	}
       
   795 
       
   796 	wptitlehint();
       
   797 
       
   798 	// resizable textarea#content
       
   799 	(function() {
       
   800 		var textarea = $('textarea#content'), offset = null, el;
       
   801 		// No point for touch devices
       
   802 		if ( !textarea.length || 'ontouchstart' in window )
       
   803 			return;
       
   804 
       
   805 		function dragging(e) {
       
   806 			textarea.height( Math.max(50, offset + e.pageY) + 'px' );
       
   807 			return false;
       
   808 		}
       
   809 
       
   810 		function endDrag(e) {
       
   811 			var height;
       
   812 
       
   813 			textarea.focus();
       
   814 			$(document).unbind('mousemove', dragging).unbind('mouseup', endDrag);
       
   815 
       
   816 			height = parseInt( textarea.css('height'), 10 );
       
   817 
       
   818 			// sanity check
       
   819 			if ( height && height > 50 && height < 5000 )
       
   820 				setUserSetting( 'ed_size', height );
       
   821 		}
       
   822 
       
   823 		textarea.css('resize', 'none');
       
   824 		el = $('<div id="content-resize-handle"><br></div>');
       
   825 		$('#wp-content-wrap').append(el);
       
   826 		el.on('mousedown', function(e) {
       
   827 			offset = textarea.height() - e.pageY;
       
   828 			textarea.blur();
       
   829 			$(document).mousemove(dragging).mouseup(endDrag);
       
   830 			return false;
       
   831 		});
       
   832 	})();
       
   833 
       
   834 	if ( typeof(tinymce) != 'undefined' ) {
       
   835 		tinymce.onAddEditor.add(function(mce, ed){
       
   836 			// iOS expands the iframe to full height and the user cannot adjust it.
       
   837 			if ( ed.id != 'content' || tinymce.isIOS5 )
       
   838 				return;
       
   839 
       
   840 			function getHeight() {
       
   841 				var height, node = document.getElementById('content_ifr'),
       
   842 					ifr_height = node ? parseInt( node.style.height, 10 ) : 0,
       
   843 					tb_height = $('#content_tbl tr.mceFirst').height();
       
   844 
       
   845 				if ( !ifr_height || !tb_height )
       
   846 					return false;
       
   847 
       
   848 				// total height including toolbar and statusbar
       
   849 				height = ifr_height + tb_height + 21;
       
   850 				// textarea height = total height - 33px toolbar
       
   851 				height -= 33;
       
   852 
       
   853 				return height;
       
   854 			}
       
   855 
       
   856 			// resize TinyMCE to match the textarea height when switching Text -> Visual
       
   857 			ed.onLoadContent.add( function(ed, o) {
       
   858 				var ifr_height, node = document.getElementById('content'),
       
   859 					height = node ? parseInt( node.style.height, 10 ) : 0,
       
   860 					tb_height = $('#content_tbl tr.mceFirst').height() || 33;
       
   861 
       
   862 				// height cannot be under 50 or over 5000
       
   863 				if ( !height || height < 50 || height > 5000 )
       
   864 					height = 360; // default height for the main editor
       
   865 
       
   866 				if ( getUserSetting( 'ed_size' ) > 5000  )
       
   867 					setUserSetting( 'ed_size', 360 );
       
   868 
       
   869 				// compensate for padding and toolbars
       
   870 				ifr_height = ( height - tb_height ) + 12;
       
   871 
       
   872 				// sanity check
       
   873 				if ( ifr_height > 50 && ifr_height < 5000 ) {
       
   874 					$('#content_tbl').css('height', '' );
       
   875 					$('#content_ifr').css('height', ifr_height + 'px' );
       
   876 				}
       
   877 			});
       
   878 
       
   879 			// resize the textarea to match TinyMCE's height when switching Visual -> Text
       
   880 			ed.onSaveContent.add( function(ed, o) {
       
   881 				var height = getHeight();
       
   882 
       
   883 				if ( !height || height < 50 || height > 5000 )
       
   884 					return;
       
   885 
       
   886 				$('textarea#content').css( 'height', height + 'px' );
       
   887 			});
       
   888 
       
   889 			// save on resizing TinyMCE
       
   890 			ed.onPostRender.add(function() {
       
   891 				$('#content_resize').on('mousedown.wp-mce-resize', function(e){
       
   892 					$(document).on('mouseup.wp-mce-resize', function(e){
       
   893 						var height;
       
   894 
       
   895 						$(document).off('mouseup.wp-mce-resize');
       
   896 
       
   897 						height = getHeight();
       
   898 						// sanity check
       
   899 						if ( height && height > 50 && height < 5000 )
       
   900 							setUserSetting( 'ed_size', height );
       
   901 					});
       
   902 				});
       
   903 			});
       
   904 		});
       
   905 
       
   906 		// When changing post formats, change the editor body class
       
   907 		$('#post-formats-select input.post-format').on( 'change.set-editor-class', function( event ) {
       
   908 			var editor, body, format = this.id;
       
   909 
       
   910 			if ( format && $( this ).prop('checked') ) {
       
   911 				editor = tinymce.get( 'content' );
       
   912 
       
   913 				if ( editor ) {
       
   914 					body = editor.getBody();
       
   915 					body.className = body.className.replace( /\bpost-format-[^ ]+/, '' );
       
   916 					editor.dom.addClass( body, format == 'post-format-0' ? 'post-format-standard' : format );
       
   917 				}
       
   918 			}
       
   919 		});
       
   920 	}
       
   921 });