mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-10-04 22:57:20 +00:00
Sytling and improvements to inline editing, see #6815
git-svn-id: https://develop.svn.wordpress.org/trunk@8943 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
+157
-170
@@ -1,187 +1,174 @@
|
||||
var postType = null;
|
||||
var postView = null;
|
||||
var postsXml = null;
|
||||
var inlineRows = null;
|
||||
|
||||
jQuery(document).ready(function() {
|
||||
postType = window.location.href.indexOf('edit.php') == -1 ? 'page' : 'post';
|
||||
postView = window.location.href.indexOf('mode=excerpt') == -1 ? 'list' : 'excerpt';
|
||||
(function($) {
|
||||
inlineEdit = {
|
||||
type : '',
|
||||
rows : '',
|
||||
|
||||
// get IDs of all editable rows
|
||||
inlineRows = jQuery('table.widefat .check-column :checkbox[name="post[]"]').parents('tr');
|
||||
init : function() {
|
||||
var t = this, blankRow = $('#inline-edit');
|
||||
|
||||
// prepare the edit row
|
||||
var blankRow = jQuery('#inline-edit');
|
||||
jQuery('ul.categories *', blankRow).removeAttr('id');
|
||||
jQuery('ul.categories label', blankRow).removeAttr('for');
|
||||
blankRow.attr('title', 'Double-click to cancel')
|
||||
.dblclick(function() { toggleRow(this); })
|
||||
.keypress(function(event) { if(event.which == 13) return saveRow(this); });
|
||||
jQuery('span.cancel a', blankRow).click(function() { return revertRow(this); });
|
||||
jQuery('span.save a', blankRow).click(function() { return saveRow(this); });
|
||||
t.type = $('table.widefat').hasClass('page') ? 'page' : 'post';
|
||||
|
||||
// add events and get data
|
||||
inlineRows.dblclick(function() { toggleRow(this); });
|
||||
addEvents(inlineRows);
|
||||
// get all editable rows
|
||||
t.rows = $('tr.iedit');
|
||||
|
||||
// get data
|
||||
getInlineData('all');
|
||||
});
|
||||
// prepare the edit row
|
||||
blankRow.dblclick(function() { inlineEdit.toggle(this); })
|
||||
.keyup(function(e) { if(e.which == 27) return inlineEdit.revert(this); });
|
||||
|
||||
function toggleRow(el) {
|
||||
jQuery('#'+postType+'-'+getRowId(el)).css('display') == 'none' ? revertRow(el) : editRow(el);
|
||||
}
|
||||
$('a.cancel', blankRow).click(function() { return inlineEdit.revert(this); });
|
||||
$('a.save', blankRow).click(function() { return inlineEdit.save(this); });
|
||||
|
||||
// add events to links and make rows double-clickable
|
||||
function addEvents(rows) {
|
||||
rows.each(function() {
|
||||
var row = jQuery(this);
|
||||
jQuery('a.editinline', row).click(function() { editRow(this); return false; });
|
||||
row.attr('title', 'Double-click to edit');
|
||||
});
|
||||
}
|
||||
// add events
|
||||
t.rows.dblclick(function() { inlineEdit.toggle(this); });
|
||||
t.addEvents(t.rows);
|
||||
},
|
||||
|
||||
function getInlineData(id) {
|
||||
if(id == 'all') {
|
||||
var editable = [];
|
||||
inlineRows.each(function(i) { editable[i] = getRowId(this); });
|
||||
id = editable.join(',');
|
||||
}
|
||||
toggle : function(el) {
|
||||
var t = this;
|
||||
|
||||
$('#'+t.type+'-'+t.getId(el)).css('display') == 'none' ? t.revert(el) : t.edit(el);
|
||||
},
|
||||
|
||||
addEvents : function(r) {
|
||||
r.each(function() {
|
||||
var row = $(this);
|
||||
$('a.editinline', row).click(function() { inlineEdit.edit(this); return false; });
|
||||
row.attr('title', inlineEditL10n.edit);
|
||||
});
|
||||
},
|
||||
|
||||
edit : function(id) {
|
||||
var t = this, type = t.type, old = $('tr.inline-editor').attr('id');
|
||||
|
||||
if( typeof(id) == 'object' )
|
||||
id = t.getId(id);
|
||||
|
||||
if ( old ) {
|
||||
old = old.split('-')[1];
|
||||
t.revert(old);
|
||||
}
|
||||
|
||||
var fields = ['post_title', 'post_name', 'post_author', 'post_status', 'jj', 'mm', 'aa', 'hh', 'mn', 'post_password'];
|
||||
if ( type == 'page' ) fields.push('post_parent', 'menu_order', 'page_template');
|
||||
if ( type == 'post' ) fields.push('tags_input');
|
||||
|
||||
// add the new blank row
|
||||
var editRow = $('#inline-edit').clone(true);
|
||||
|
||||
if ( $('#'+type+'-'+id).hasClass('alternate') )
|
||||
$(editRow).addClass('alternate');
|
||||
$('#'+type+'-'+id).hide().after(editRow);
|
||||
|
||||
// populate the data
|
||||
var rowData = $('#inline_'+id);
|
||||
for ( var f = 0; f < fields.length; f++ ) {
|
||||
$(':input[name="'+fields[f]+'"]', editRow).val( $('.'+fields[f], rowData).val() );
|
||||
}
|
||||
|
||||
if ( $('.comment_status', rowData).val() == 'open' )
|
||||
$('input[name="comment_status"]', editRow).attr("checked", "checked");
|
||||
if ( $('.ping_status', rowData).val() == 'open' )
|
||||
$('input[name="ping_status"]', editRow).attr("checked", "checked");
|
||||
if ( $('.sticky', rowData).val() == 'sticky' )
|
||||
$('input[name="sticky"]', editRow).attr("checked", "checked");
|
||||
|
||||
// categories
|
||||
var cats;
|
||||
if ( cats = $('.post_category', rowData).val() )
|
||||
$('ul.cat-checklist :checkbox').val(cats.split(','));
|
||||
|
||||
// handle the post status
|
||||
var status = $('.post_status', rowData).val();
|
||||
if ( status != 'future' ) $('select[name="post_status"] option[value="future"]', editRow).remove();
|
||||
if ( status == 'private' ) $('input[name="keep_private"]', editRow).attr("checked", "checked");
|
||||
|
||||
// remove the current page and children from the parent dropdown
|
||||
var pageOpt = $('select[name="post_parent"] option[value="'+id+'"]', editRow);
|
||||
if ( pageOpt.length > 0 ) {
|
||||
var pageLevel = pageOpt[0].className.split('-')[1], nextPage = pageOpt, pageLoop = true;
|
||||
while ( pageLoop ) {
|
||||
var nextPage = nextPage.next('option'), nextLevel = nextPage[0].className.split('-')[1];
|
||||
if ( nextLevel <= pageLevel ) {
|
||||
pageLoop = false;
|
||||
} else {
|
||||
nextPage.remove();
|
||||
nextPage = pageOpt;
|
||||
}
|
||||
}
|
||||
pageOpt.remove();
|
||||
}
|
||||
|
||||
// categories expandable?
|
||||
$('span.catshow', editRow).click(function() {
|
||||
$('ul.cat-checklist', editRow).addClass("cat-hover");
|
||||
$('span.cathide', editRow).show();
|
||||
$(this).hide();
|
||||
});
|
||||
|
||||
$('span.cathide', editRow).click(function() {
|
||||
$('ul.cat-checklist', editRow).removeClass("cat-hover");
|
||||
$('span.catshow', editRow).show();
|
||||
$(this).hide();
|
||||
});
|
||||
|
||||
$(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
|
||||
$('.ptitle', editRow).focus();
|
||||
|
||||
// enable autocomplete for tags
|
||||
if ( type == 'post' )
|
||||
$('tr.inline-editor textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search', { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
|
||||
|
||||
if(id == '')
|
||||
return false;
|
||||
|
||||
jQuery.post('admin-ajax.php', {
|
||||
'inline_edit_nonce': jQuery('#inline_edit_nonce').val(),
|
||||
action: 'inline-data',
|
||||
posts: id
|
||||
},
|
||||
function(xml) {
|
||||
if(id.indexOf(',') == -1) {
|
||||
var newData = jQuery(xml).find('post[id="'+id+'"]');
|
||||
jQuery(postsXml).find('post[id="'+id+'"]').replaceWith(newData);
|
||||
} else {
|
||||
postsXml = xml;
|
||||
},
|
||||
|
||||
save : function(id) {
|
||||
if( typeof(id) == 'object' )
|
||||
id = this.getId(id);
|
||||
|
||||
$('#edit-'+id+' .check-column').html('<img src="images/loading.gif" alt="" />');
|
||||
|
||||
var params = {
|
||||
action: 'inline-save',
|
||||
post_type: this.type,
|
||||
post_ID: id,
|
||||
edit_date: 'true'
|
||||
};
|
||||
|
||||
var fields = $('#edit-'+id+' :input').fieldSerialize();
|
||||
params = fields + '&' + $.param(params);
|
||||
|
||||
// make ajax request
|
||||
$.post('admin-ajax.php', params,
|
||||
function(html) {
|
||||
var row = $('#'+inlineEdit.type+'-'+id);
|
||||
$('#edit-'+id).hide();
|
||||
html = $(html).html();
|
||||
row.html(html).show();
|
||||
row.animate( { backgroundColor: '#FFFBCC' }, 200)
|
||||
.animate( { backgroundColor: row.css('background-color') }, 500);
|
||||
inlineEdit.addEvents(row);
|
||||
}
|
||||
}, 'xml'
|
||||
);
|
||||
}
|
||||
);
|
||||
return false;
|
||||
},
|
||||
|
||||
function editRow(id) {
|
||||
if(typeof(id) == 'object')
|
||||
id = getRowId(id);
|
||||
revert : function(id) {
|
||||
if ( typeof(id) == 'object' )
|
||||
id = this.getId(id);
|
||||
|
||||
var blankRow = jQuery('#inline-edit');
|
||||
$('#edit-'+id).remove();
|
||||
$('#'+this.type+'-'+id).show();
|
||||
|
||||
var fields = ['post_title', 'post_name', 'post_author', 'post_status', 'jj', 'mm', 'aa', 'hh', 'mn'];
|
||||
if(postType == 'page') fields.push('post_parent', 'menu_order', 'page_template', 'post_password');
|
||||
if(postType == 'post') fields.push('tags_input');
|
||||
return false;
|
||||
},
|
||||
|
||||
// add the new blank row
|
||||
var editRow = blankRow.clone(true);
|
||||
jQuery(editRow).attr('id', 'edit-'+id).addClass('inline-editor').show();
|
||||
if(jQuery('#'+postType+'-'+id).hasClass('alternate'))
|
||||
jQuery(editRow).addClass('alternate');
|
||||
jQuery('#'+postType+'-'+id).hide().after(editRow);
|
||||
|
||||
// populate the data
|
||||
var rowData = jQuery(postsXml).find('post[id="'+id+'"]');
|
||||
for(var f = 0; f < fields.length; f++) {
|
||||
jQuery(':input[name="'+fields[f]+'"]', editRow).val(jQuery(fields[f], rowData).text());
|
||||
}
|
||||
|
||||
// ping, comments, and privacy
|
||||
if(jQuery('comment_status', rowData).text() == 'open')
|
||||
jQuery('input[name="comment_status"]', editRow).select();
|
||||
if(jQuery('ping_status', rowData).text() == 'open')
|
||||
jQuery('input[name="ping_status"]', editRow).select();
|
||||
if(jQuery('sticky', rowData).text() == 'sticky')
|
||||
jQuery('input[name="sticky"]', editRow).select();
|
||||
|
||||
// categories
|
||||
var categories = jQuery('post_category', rowData).text().split(',');
|
||||
jQuery(categories).each(function() {
|
||||
jQuery('ul.categories :checkbox[value="'+this+'"]', editRow).select();
|
||||
});
|
||||
|
||||
// handle the post status
|
||||
var status = jQuery('post_status', rowData).text();
|
||||
if(status != 'future') jQuery('select[name="post_status"] option[value="future"]', editRow).remove();
|
||||
if(status == 'private') jQuery('input[name="page_private"]', editRow).select();
|
||||
|
||||
// enable autocomplete for tags
|
||||
if(postType == 'post') {
|
||||
jQuery('tr.inline textarea[name="tags_input"]').suggest( 'admin-ajax.php?action=ajax-tag-search', { delay: 500, minchars: 2, multiple: true, multipleSep: ", " } );
|
||||
}
|
||||
|
||||
// remove the current page and children from the parent dropdown
|
||||
var pageOpt = jQuery('select[name="post_parent"] option[value="'+id+'"]', editRow);
|
||||
if(pageOpt.length > 0) {
|
||||
var pageLevel = pageOpt[0].className.split('-')[1];
|
||||
var nextPage = pageOpt; var pageLoop = true;
|
||||
while(pageLoop) {
|
||||
var nextPage = nextPage.next('option');
|
||||
var nextLevel = nextPage[0].className.split('-')[1];
|
||||
if(nextLevel <= pageLevel)
|
||||
pageLoop = false;
|
||||
else {
|
||||
nextPage.remove();
|
||||
nextPage = pageOpt;
|
||||
}
|
||||
}
|
||||
pageOpt.remove();
|
||||
getId : function(o) {
|
||||
var id = o.tagName == 'TR' ? o.id : $(o).parents('tr').attr('id');
|
||||
var parts = id.split('-');
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function saveRow(id) {
|
||||
if(typeof(id) == 'object')
|
||||
id = getRowId(id);
|
||||
|
||||
jQuery('#edit-'+id+' .check-column').html('<img src="images/loading.gif" alt="" />');
|
||||
|
||||
var params = {
|
||||
'inline_edit_nonce': jQuery('#inline_edit_nonce').val(),
|
||||
action: 'inline-save',
|
||||
post_type: postType,
|
||||
post_ID: id,
|
||||
edit_date: 'true',
|
||||
post_view: postView
|
||||
};
|
||||
|
||||
var fields = jQuery('#edit-'+id+' :input').fieldSerialize();
|
||||
params = fields + '&' + jQuery.param(params);
|
||||
|
||||
// make ajax request
|
||||
jQuery.post('admin-ajax.php', params,
|
||||
function(html) {
|
||||
var row = jQuery('#'+postType+'-'+id);
|
||||
jQuery('#edit-'+id).hide();
|
||||
html = jQuery(html).html();
|
||||
row.html(html).show();
|
||||
row.animate( { backgroundColor: '#FFFBCC' }, 200)
|
||||
.animate( { backgroundColor: row.css('background-color') }, 500);
|
||||
getInlineData(id);
|
||||
addEvents(row);
|
||||
}
|
||||
);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function revertRow(id) {
|
||||
if(typeof(id) == 'object')
|
||||
id = getRowId(id);
|
||||
|
||||
jQuery('#edit-'+id).remove();
|
||||
jQuery('#'+postType+'-'+id).show();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function getRowId(obj) {
|
||||
var id = obj.tagName == 'TR' ? obj.id : jQuery(obj).parents('tr').attr('id');
|
||||
var parts = id.split('-');
|
||||
return parts[parts.length - 1];
|
||||
}
|
||||
$(document).ready(function(){inlineEdit.init();});
|
||||
})(jQuery);
|
||||
|
||||
Reference in New Issue
Block a user