Commit 24f20b85 by Tim Krones

Make sure EDIT dialog for block fields always operates on up-to-date data.

To achieve this, remove TinyMCE from block fields on "Save" and
"Cancel" (previous version of the code was delaying this step until user
requested an EDIT dialog for another block).
parent 3b58c757
...@@ -14,6 +14,7 @@ function StudioEditableXBlockMixin(runtime, element) { ...@@ -14,6 +14,7 @@ function StudioEditableXBlockMixin(runtime, element) {
fields.push({ fields.push({
name: $wrapper.data('field-name'), name: $wrapper.data('field-name'),
isSet: function() { return $wrapper.hasClass('is-set'); }, isSet: function() { return $wrapper.hasClass('is-set'); },
hasEditor: function() { return tinyMceAvailable && $field.tinymce(); },
val: function() { val: function() {
var val = $field.val(); var val = $field.val();
// Cast values to the appropriate type so that we send nice clean JSON over the wire: // Cast values to the appropriate type so that we send nice clean JSON over the wire:
...@@ -31,6 +32,9 @@ function StudioEditableXBlockMixin(runtime, element) { ...@@ -31,6 +32,9 @@ function StudioEditableXBlockMixin(runtime, element) {
val = JSON.parse(val); // TODO: handle parse errors val = JSON.parse(val); // TODO: handle parse errors
} }
return val; return val;
},
removeEditor: function() {
$field.tinymce().remove();
} }
}); });
var fieldChanged = function() { var fieldChanged = function() {
...@@ -45,8 +49,6 @@ function StudioEditableXBlockMixin(runtime, element) { ...@@ -45,8 +49,6 @@ function StudioEditableXBlockMixin(runtime, element) {
$resetButton.removeClass('active').addClass('inactive'); $resetButton.removeClass('active').addClass('inactive');
}); });
if (type == 'html' && tinyMceAvailable) { if (type == 'html' && tinyMceAvailable) {
if ($field.tinymce())
$field.tinymce().remove(); // Stale instance from a previous dialog. Delete it to avoid interference.
tinyMCE.baseURL = baseUrl + "/js/vendor/tinymce/js/tinymce"; tinyMCE.baseURL = baseUrl + "/js/vendor/tinymce/js/tinymce";
$field.tinymce({ $field.tinymce({
theme: 'modern', theme: 'modern',
...@@ -82,6 +84,7 @@ function StudioEditableXBlockMixin(runtime, element) { ...@@ -82,6 +84,7 @@ function StudioEditableXBlockMixin(runtime, element) {
fields.push({ fields.push({
name: $wrapper.data('field-name'), name: $wrapper.data('field-name'),
isSet: function() { return $wrapper.hasClass('is-set'); }, isSet: function() { return $wrapper.hasClass('is-set'); },
hasEditor: function() { return false; },
val: function() { val: function() {
var val = []; var val = [];
$checkboxes.each(function() { $checkboxes.each(function() {
...@@ -146,11 +149,24 @@ function StudioEditableXBlockMixin(runtime, element) { ...@@ -146,11 +149,24 @@ function StudioEditableXBlockMixin(runtime, element) {
} else { } else {
notSet.push(field.name); notSet.push(field.name);
} }
// Remove TinyMCE instances to make sure jQuery does not try to access stale instances
// when loading editor for another block:
if (field.hasEditor()) {
field.removeEditor();
}
} }
studio_submit({values: values, defaults: notSet}); studio_submit({values: values, defaults: notSet});
}); });
$(element).find('.cancel-button').bind('click', function(e) { $(element).find('.cancel-button').bind('click', function(e) {
// Remove TinyMCE instances to make sure jQuery does not try to access stale instances
// when loading editor for another block:
for (var i in fields) {
var field = fields[i];
if (field.hasEditor()) {
field.removeEditor();
}
}
e.preventDefault(); e.preventDefault();
runtime.notify('cancel', {}); runtime.notify('cancel', {});
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment