Remove matching form block when custom field is deleted

This commit is contained in:
Jonathan Labreuille
2016-01-18 17:46:42 +01:00
parent cabe2d61b7
commit 3b9821fbe1
3 changed files with 33 additions and 15 deletions

View File

@ -618,16 +618,27 @@ var WysijaForm = {
return encodeURI(str).replace(/[!'()*]/g, escape);
}
},
updateBlock: function(data) {
updateBlock: function(field) {
var hasUpdated = false;
WysijaForm.getBlocks().each(function(b) {
if(b.block.getData().id == data.id) {
if(b.block.getData().id === field.id) {
hasUpdated = true;
b.block.redraw(data);
b.block.redraw(field);
}
});
return hasUpdated;
},
removeBlock: function(field, callback) {
var hasRemoved = false;
WysijaForm.getBlocks().each(function(b) {
if(b.block.getData().id === field.id) {
hasRemoved = true;
b.block.removeBlock(callback);
}
});
return hasRemoved;
}
};
@ -836,10 +847,6 @@ WysijaForm.Block = Class.create({
Effect.Fade(this.element.identify(), {
duration: 0.2,
afterFinish: function(effect) {
if(effect.element.next('.mailpoet_form_block') !== undefined && callback !== false) {
// show controls of next block to allow mass delete
WysijaForm.get(effect.element.next('.mailpoet_form_block')).block.showControls();
}
// remove placeholder
if(effect.element.previous('.block_placeholder') !== undefined) {
effect.element.previous('.block_placeholder').remove();

View File

@ -18,15 +18,19 @@ class CustomFields {
}
function delete($id) {
$result = false;
$custom_field = CustomField::findOne($id);
if($custom_field !== false) {
if($custom_field === false or !$custom_field->id()) {
wp_send_json(array(
'result' => false
));
} else {
$custom_field->delete();
$result = true;
}
wp_send_json($result);
wp_send_json(array(
'result' => true,
'field' => $custom_field->asArray()
));
}
}
function save($data = array()) {

View File

@ -547,9 +547,16 @@
endpoint: 'customFields',
action: 'delete',
data: id
}).done(function(result) {
if(result === true) {
}).done(function(response) {
if(response.result === true) {
item.remove();
if(response.field !== undefined) {
WysijaForm.removeBlock(response.field, function() {
mailpoet_form_save(false);
});
}
mailpoet_form_fields();
MailPoet.Notice.success(
"<%= __('Removed custom field “"+name+"“') %>"