web/wp-admin/js/inline-edit-post.js
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
     1 (function(a){inlineEditPost={init:function(){var c=this,d=a("#inline-edit"),b=a("#bulk-edit");c.type=a("table.widefat").hasClass("pages")?"page":"post";c.what="#post-";d.keyup(function(f){if(f.which==27){return inlineEditPost.revert()}});b.keyup(function(f){if(f.which==27){return inlineEditPost.revert()}});a("a.cancel",d).click(function(){return inlineEditPost.revert()});a("a.save",d).click(function(){return inlineEditPost.save(this)});a("td",d).keydown(function(f){if(f.which==13){return inlineEditPost.save(this)}});a("a.cancel",b).click(function(){return inlineEditPost.revert()});a('#inline-edit .inline-edit-private input[value="private"]').click(function(){var e=a("input.inline-edit-password-input");if(a(this).prop("checked")){e.val("").prop("disabled",true)}else{e.prop("disabled",false)}});a("a.editinline").live("click",function(){inlineEditPost.edit(this);return false});a("#bulk-title-div").parents("fieldset").after(a("#inline-edit fieldset.inline-edit-categories").clone()).siblings("fieldset:last").prepend(a("#inline-edit label.inline-edit-tags").clone());a("span.catshow").click(function(){a(this).hide().next().show().parent().next().addClass("cat-hover")});a("span.cathide").click(function(){a(this).hide().prev().show().parent().next().removeClass("cat-hover")});a('select[name="_status"] option[value="future"]',b).remove();a("#doaction, #doaction2").click(function(f){var g=a(this).attr("id").substr(2);if(a('select[name="'+g+'"]').val()=="edit"){f.preventDefault();c.setBulk()}else{if(a("form#posts-filter tr.inline-editor").length>0){c.revert()}}});a("#post-query-submit").mousedown(function(f){c.revert();a('select[name^="action"]').val("-1")})},toggle:function(c){var b=this;a(b.what+b.getId(c)).css("display")=="none"?b.revert():b.edit(c)},setBulk:function(){var e="",d=this.type,b,f=true;this.revert();a("#bulk-edit td").attr("colspan",a(".widefat:first thead th:visible").length);a("table.widefat tbody").prepend(a("#bulk-edit"));a("#bulk-edit").addClass("inline-editor").show();a('tbody th.check-column input[type="checkbox"]').each(function(g){if(a(this).prop("checked")){f=false;var h=a(this).val(),c;c=a("#inline_"+h+" .post_title").text()||inlineEditL10n.notitle;e+='<div id="ttle'+h+'"><a id="_'+h+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+c+"</div>"}});if(f){return this.revert()}a("#bulk-titles").html(e);a("#bulk-titles a").click(function(){var c=a(this).attr("id").substr(1);a('table.widefat input[value="'+c+'"]').prop("checked",false);a("#ttle"+c).remove()});if("post"==d){b="post_tag";a('tr.inline-editor textarea[name="tax_input['+b+']"]').suggest(ajaxurl+"?action=ajax-tag-search&tax="+b,{delay:500,minchars:2,multiple:true,multipleSep:inlineEditL10n.comma+" "})}a("html, body").animate({scrollTop:0},"fast")},edit:function(c){var n=this,j,e,g,i,h,m,l,d=true,o,b,k;n.revert();if(typeof(c)=="object"){c=n.getId(c)}j=["post_title","post_name","post_author","_status","jj","mm","aa","hh","mn","ss","post_password","post_format","menu_order"];if(n.type=="page"){j.push("post_parent","page_template")}e=a("#inline-edit").clone(true);a("td",e).attr("colspan",a(".widefat:first thead th:visible").length);if(a(n.what+c).hasClass("alternate")){a(e).addClass("alternate")}a(n.what+c).hide().after(e);g=a("#inline_"+c);if(!a(':input[name="post_author"] option[value="'+a(".post_author",g).text()+'"]',e).val()){a(':input[name="post_author"]',e).prepend('<option value="'+a(".post_author",g).text()+'">'+a("#"+n.type+"-"+c+" .author").text()+"</option>")}if(a(':input[name="post_author"] option',e).length==1){a("label.inline-edit-author",e).hide()}b=a(".post_format",g).text();a("option.unsupported",e).each(function(){var f=a(this);if(f.val()!=b){f.remove()}});for(k=0;k<j.length;k++){a(':input[name="'+j[k]+'"]',e).val(a("."+j[k],g).text())}if(a(".comment_status",g).text()=="open"){a('input[name="comment_status"]',e).prop("checked",true)}if(a(".ping_status",g).text()=="open"){a('input[name="ping_status"]',e).prop("checked",true)}if(a(".sticky",g).text()=="sticky"){a('input[name="sticky"]',e).prop("checked",true)}a(".post_category",g).each(function(){var f=a(this).text();if(f){taxname=a(this).attr("id").replace("_"+c,"");a("ul."+taxname+"-checklist :checkbox",e).val(f.split(","))}});a(".tags_input",g).each(function(){var q=a(this).text(),r=a(this).attr("id").replace("_"+c,""),p=a("textarea.tax_input_"+r,e),f=inlineEditL10n.comma;if(q){if(","!==f){q=q.replace(/,/g,f)}p.val(q)}p.suggest(ajaxurl+"?action=ajax-tag-search&tax="+r,{delay:500,minchars:2,multiple:true,multipleSep:inlineEditL10n.comma+" "})});i=a("._status",g).text();if("future"!=i){a('select[name="_status"] option[value="future"]',e).remove()}if("private"==i){a('input[name="keep_private"]',e).prop("checked",true);a("input.inline-edit-password-input").val("").prop("disabled",true)}h=a('select[name="post_parent"] option[value="'+c+'"]',e);if(h.length>0){m=h[0].className.split("-")[1];l=h;while(d){l=l.next("option");if(l.length==0){break}o=l[0].className.split("-")[1];if(o<=m){d=false}else{l.remove();l=h}}h.remove()}a(e).attr("id","edit-"+c).addClass("inline-editor").show();a(".ptitle",e).focus();return false},save:function(e){var d,b,c=a(".post_status_page").val()||"";if(typeof(e)=="object"){e=this.getId(e)}a("table.widefat .inline-edit-save .waiting").show();d={action:"inline-save",post_type:typenow,post_ID:e,edit_date:"true",post_status:c};b=a("#edit-"+e+" :input").serialize();d=b+"&"+a.param(d);a.post(ajaxurl,d,function(f){a("table.widefat .inline-edit-save .waiting").hide();if(f){if(-1!=f.indexOf("<tr")){a(inlineEditPost.what+e).remove();a("#edit-"+e).before(f).remove();a(inlineEditPost.what+e).hide().fadeIn()}else{f=f.replace(/<.[^<>]*?>/g,"");a("#edit-"+e+" .inline-edit-save .error").html(f).show()}}else{a("#edit-"+e+" .inline-edit-save .error").html(inlineEditL10n.error).show()}},"html");return false},revert:function(){var b=a("table.widefat tr.inline-editor").attr("id");if(b){a("table.widefat .inline-edit-save .waiting").hide();if("bulk-edit"==b){a("table.widefat #bulk-edit").removeClass("inline-editor").hide();a("#bulk-titles").html("");a("#inlineedit").append(a("#bulk-edit"))}else{a("#"+b).remove();b=b.substr(b.lastIndexOf("-")+1);a(this.what+b).show()}}return false},getId:function(c){var d=a(c).closest("tr").attr("id"),b=d.split("-");return b[b.length-1]}};a(document).ready(function(){inlineEditPost.init()})})(jQuery);
     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 		$('a.editinline').live('click', 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 		// hiearchical taxonomies expandable?
       
    57 		$('span.catshow').click(function(){
       
    58 			$(this).hide().next().show().parent().next().addClass("cat-hover");
       
    59 		});
       
    60 
       
    61 		$('span.cathide').click(function(){
       
    62 			$(this).hide().prev().show().parent().next().removeClass("cat-hover");
       
    63 		});
       
    64 
       
    65 		$('select[name="_status"] option[value="future"]', bulkRow).remove();
       
    66 
       
    67 		$('#doaction, #doaction2').click(function(e){
       
    68 			var n = $(this).attr('id').substr(2);
       
    69 			if ( $('select[name="'+n+'"]').val() == 'edit' ) {
       
    70 				e.preventDefault();
       
    71 				t.setBulk();
       
    72 			} else if ( $('form#posts-filter tr.inline-editor').length > 0 ) {
       
    73 				t.revert();
       
    74 			}
       
    75 		});
       
    76 
       
    77 		$('#post-query-submit').mousedown(function(e){
       
    78 			t.revert();
       
    79 			$('select[name^="action"]').val('-1');
       
    80 		});
       
    81 	},
       
    82 
       
    83 	toggle : function(el){
       
    84 		var t = this;
       
    85 		$(t.what+t.getId(el)).css('display') == 'none' ? t.revert() : t.edit(el);
       
    86 	},
       
    87 
       
    88 	setBulk : function(){
       
    89 		var te = '', type = this.type, tax, c = true;
       
    90 		this.revert();
       
    91 
       
    92 		$('#bulk-edit td').attr('colspan', $('.widefat:first thead th:visible').length);
       
    93 		$('table.widefat tbody').prepend( $('#bulk-edit') );
       
    94 		$('#bulk-edit').addClass('inline-editor').show();
       
    95 
       
    96 		$('tbody th.check-column input[type="checkbox"]').each(function(i){
       
    97 			if ( $(this).prop('checked') ) {
       
    98 				c = false;
       
    99 				var id = $(this).val(), theTitle;
       
   100 				theTitle = $('#inline_'+id+' .post_title').html() || inlineEditL10n.notitle;
       
   101 				te += '<div id="ttle'+id+'"><a id="_'+id+'" class="ntdelbutton" title="'+inlineEditL10n.ntdeltitle+'">X</a>'+theTitle+'</div>';
       
   102 			}
       
   103 		});
       
   104 
       
   105 		if ( c )
       
   106 			return this.revert();
       
   107 
       
   108 		$('#bulk-titles').html(te);
       
   109 		$('#bulk-titles a').click(function(){
       
   110 			var id = $(this).attr('id').substr(1);
       
   111 
       
   112 			$('table.widefat input[value="' + id + '"]').prop('checked', false);
       
   113 			$('#ttle'+id).remove();
       
   114 		});
       
   115 
       
   116 		// enable autocomplete for tags
       
   117 		if ( 'post' == type ) {
       
   118 			// support multi taxonomies?
       
   119 			tax = 'post_tag';
       
   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 + ' ' } );
       
   121 		}
       
   122 		$('html, body').animate( { scrollTop: 0 }, 'fast' );
       
   123 	},
       
   124 
       
   125 	edit : function(id) {
       
   126 		var t = this, fields, editRow, rowData, status, pageOpt, pageLevel, nextPage, pageLoop = true, nextLevel, cur_format, f;
       
   127 		t.revert();
       
   128 
       
   129 		if ( typeof(id) == 'object' )
       
   130 			id = t.getId(id);
       
   131 
       
   132 		fields = ['post_title', 'post_name', 'post_author', '_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'ss', 'post_password', 'post_format', 'menu_order'];
       
   133 		if ( t.type == 'page' )
       
   134 			fields.push('post_parent', 'page_template');
       
   135 
       
   136 		// add the new blank row
       
   137 		editRow = $('#inline-edit').clone(true);
       
   138 		$('td', editRow).attr('colspan', $('.widefat:first thead th:visible').length);
       
   139 
       
   140 		if ( $(t.what+id).hasClass('alternate') )
       
   141 			$(editRow).addClass('alternate');
       
   142 		$(t.what+id).hide().after(editRow);
       
   143 
       
   144 		// populate the data
       
   145 		rowData = $('#inline_'+id);
       
   146 		if ( !$(':input[name="post_author"] option[value="' + $('.post_author', rowData).text() + '"]', editRow).val() ) {
       
   147 			// author no longer has edit caps, so we need to add them to the list of authors
       
   148 			$(':input[name="post_author"]', editRow).prepend('<option value="' + $('.post_author', rowData).text() + '">' + $('#' + t.type + '-' + id + ' .author').text() + '</option>');
       
   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 		});
       
   161 
       
   162 		for ( f = 0; f < fields.length; f++ ) {
       
   163 			$(':input[name="' + fields[f] + '"]', editRow).val( $('.'+fields[f], rowData).text() );
       
   164 		}
       
   165 
       
   166 		if ( $('.comment_status', rowData).text() == 'open' )
       
   167 			$('input[name="comment_status"]', editRow).prop("checked", true);
       
   168 		if ( $('.ping_status', rowData).text() == 'open' )
       
   169 			$('input[name="ping_status"]', editRow).prop("checked", true);
       
   170 		if ( $('.sticky', rowData).text() == 'sticky' )
       
   171 			$('input[name="sticky"]', editRow).prop("checked", true);
       
   172 
       
   173 		// hierarchical taxonomies
       
   174 		$('.post_category', rowData).each(function(){
       
   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 		});
       
   198 
       
   199 		// handle the post status
       
   200 		status = $('._status', rowData).text();
       
   201 		if ( 'future' != status )
       
   202 			$('select[name="_status"] option[value="future"]', editRow).remove();
       
   203 
       
   204 		if ( 'private' == status ) {
       
   205 			$('input[name="keep_private"]', editRow).prop("checked", true);
       
   206 			$('input.inline-edit-password-input').val('').prop('disabled', true);
       
   207 		}
       
   208 
       
   209 		// remove the current page and children from the parent dropdown
       
   210 		pageOpt = $('select[name="post_parent"] option[value="' + id + '"]', editRow);
       
   211 		if ( pageOpt.length > 0 ) {
       
   212 			pageLevel = pageOpt[0].className.split('-')[1];
       
   213 			nextPage = pageOpt;
       
   214 			while ( pageLoop ) {
       
   215 				nextPage = nextPage.next('option');
       
   216 				if (nextPage.length == 0) break;
       
   217 				nextLevel = nextPage[0].className.split('-')[1];
       
   218 				if ( nextLevel <= pageLevel ) {
       
   219 					pageLoop = false;
       
   220 				} else {
       
   221 					nextPage.remove();
       
   222 					nextPage = pageOpt;
       
   223 				}
       
   224 			}
       
   225 			pageOpt.remove();
       
   226 		}
       
   227 
       
   228 		$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
       
   229 		$('.ptitle', editRow).focus();
       
   230 
       
   231 		return false;
       
   232 	},
       
   233 
       
   234 	save : function(id) {
       
   235 		var params, fields, page = $('.post_status_page').val() || '';
       
   236 
       
   237 		if ( typeof(id) == 'object' )
       
   238 			id = this.getId(id);
       
   239 
       
   240 		$('table.widefat .spinner').show();
       
   241 
       
   242 		params = {
       
   243 			action: 'inline-save',
       
   244 			post_type: typenow,
       
   245 			post_ID: id,
       
   246 			edit_date: 'true',
       
   247 			post_status: page
       
   248 		};
       
   249 
       
   250 		fields = $('#edit-'+id+' :input').serialize();
       
   251 		params = fields + '&' + $.param(params);
       
   252 
       
   253 		// make ajax request
       
   254 		$.post( ajaxurl, params,
       
   255 			function(r) {
       
   256 				$('table.widefat .spinner').hide();
       
   257 
       
   258 				if (r) {
       
   259 					if ( -1 != r.indexOf('<tr') ) {
       
   260 						$(inlineEditPost.what+id).remove();
       
   261 						$('#edit-'+id).before(r).remove();
       
   262 						$(inlineEditPost.what+id).hide().fadeIn();
       
   263 					} else {
       
   264 						r = r.replace( /<.[^<>]*?>/g, '' );
       
   265 						$('#edit-'+id+' .inline-edit-save .error').html(r).show();
       
   266 					}
       
   267 				} else {
       
   268 					$('#edit-'+id+' .inline-edit-save .error').html(inlineEditL10n.error).show();
       
   269 				}
       
   270 			}
       
   271 		, 'html');
       
   272 		return false;
       
   273 	},
       
   274 
       
   275 	revert : function(){
       
   276 		var id = $('table.widefat tr.inline-editor').attr('id');
       
   277 
       
   278 		if ( id ) {
       
   279 			$('table.widefat .spinner').hide();
       
   280 
       
   281 			if ( 'bulk-edit' == id ) {
       
   282 				$('table.widefat #bulk-edit').removeClass('inline-editor').hide();
       
   283 				$('#bulk-titles').html('');
       
   284 				$('#inlineedit').append( $('#bulk-edit') );
       
   285 			} else {
       
   286 				$('#'+id).remove();
       
   287 				id = id.substr( id.lastIndexOf('-') + 1 );
       
   288 				$(this.what+id).show();
       
   289 			}
       
   290 		}
       
   291 
       
   292 		return false;
       
   293 	},
       
   294 
       
   295 	getId : function(o) {
       
   296 		var id = $(o).closest('tr').attr('id'),
       
   297 			parts = id.split('-');
       
   298 		return parts[parts.length - 1];
       
   299 	}
       
   300 };
       
   301 
       
   302 $(document).ready(function(){inlineEditPost.init();});
       
   303 })(jQuery);