Commit a74955bf authored by sun's avatar sun

#328052 by sun: Fixed switching input formats leads to wrong editor/state.

parent e16a9972
......@@ -6,6 +6,7 @@ Wysiwyg x.x-x.x, xxxx-xx-xx
Wysiwyg 6.x-x.x, xxxx-xx-xx
---------------------------
#328052 by sun: Fixed switching input formats leads to wrong editor/state.
#337569 by sun: Fixed different profiles for same editor are not respected.
#340195 by sun: Fixed #after_build function not invoked on all forms.
#333521 by sun: Fixed TinyMCE version detection to look at the actual script.
......
// $Id$
Drupal.wysiwyg = Drupal.wysiwyg || {};
Drupal.wysiwyg = Drupal.wysiwyg || { 'instances': {} };
Drupal.wysiwyg.editor = Drupal.wysiwyg.editor || { 'init': {}, 'attach': {}, 'detach': {} };
......
......@@ -34,19 +34,7 @@ Drupal.wysiwygInit = function() {
*/
Drupal.behaviors.attachWysiwyg = function(context) {
$('.wysiwyg:not(.wysiwyg-processed)', context).each(function() {
// Parse the element's CSS classes into parameters.
// Format is wysiwyg-name-value.
var classes = this.className.split(' ');
var params = {};
for (var i in classes) {
if (classes[i].substr(0, 8) == 'wysiwyg-') {
var parts = classes[i].split('-');
var value = parts.slice(2).join('-');
params[parts[1]] = value;
}
}
// Convert format id to string.
params.format = 'format' + params.format;
var params = Drupal.wysiwyg.getParams(this);
$this = $(this);
// Directly attach this editor, if the input format is enabled or there is
// only one input format at all.
......@@ -55,12 +43,13 @@ Drupal.behaviors.attachWysiwyg = function(context) {
Drupal.wysiwygAttach(context, params);
}
// Attach onChange handlers to input format selector elements.
// @todo To support different editors on the same page, we need to store
// the last attached editor of each target element separately.
if ($this.is(':input')) {
$this.change(function() {
Drupal.wysiwygDetach(context, params);
Drupal.wysiwygAttach(context, params);
// If not disabled, detach the current and attach a new editor.
if (Drupal.wysiwyg.instances[params.field].status) {
Drupal.wysiwygDetach(context, params);
Drupal.wysiwygAttach(context, params);
}
});
}
$this.addClass('wysiwyg-processed');
......@@ -84,6 +73,11 @@ Drupal.wysiwygAttach = function(context, params) {
if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
// Attach editor.
Drupal.wysiwyg.editor.attach[params.editor](context, params, (Drupal.settings.wysiwyg.configs[params.editor] ? Drupal.wysiwyg.clone(Drupal.settings.wysiwyg.configs[params.editor][params.format]) : {}));
// (Re-)initialize field instance.
Drupal.wysiwyg.instances[params.field] = {};
// Store new editor name and status for this field.
Drupal.wysiwyg.instances[params.field].editor = params.editor;
Drupal.wysiwyg.instances[params.field].status = true;
// Display toggle link.
$('#wysiwyg-toggle-' + params.field).show();
}
......@@ -96,19 +90,16 @@ Drupal.wysiwygAttach = function(context, params) {
/**
* Detach all editors from a target element.
*
* Until there is a central registry of target elements storing the currently
* attached editor, we simply invoke the detach hook of all editors to ensure
* that no editor is attached to the target element.
*
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
*/
Drupal.wysiwygDetach = function(context, params) {
jQuery.each(Drupal.wysiwyg.editor.detach, function(editor) {
this(context, params);
});
var editor = Drupal.wysiwyg.instances[params.field].editor;
if (jQuery.isFunction(Drupal.wysiwyg.editor.detach[editor])) {
Drupal.wysiwyg.editor.detach[editor](context, params);
}
}
/**
......@@ -122,19 +113,22 @@ Drupal.wysiwygDetach = function(context, params) {
Drupal.wysiwygAttachToggleLink = function(context, params) {
var text = document.createTextNode(Drupal.settings.wysiwyg.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
var a = document.createElement('a');
$(a).toggle(
function() {
$(a).click(function() {
if (Drupal.wysiwyg.instances[params.field].status) {
Drupal.wysiwygDetach(context, params);
Drupal.wysiwyg.instances[params.field].status = false;
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.enable).blur();
// After disabling the editor, re-attach default behaviors.
Drupal.wysiwyg.editor.attach.none(context, params);
},
function() {
}
else {
// Before enabling the editor, detach default behaviors.
Drupal.wysiwyg.editor.detach.none(context, params);
Drupal.wysiwygAttach(context, params);
// Attach the editor using parameters of the currently selected input format.
Drupal.wysiwygAttach(context, Drupal.wysiwyg.getParams($('.wysiwyg-field-' + params.field + ':checked', context).get(0)));
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwyg.disable).blur();
})
}
})
.attr('id', 'wysiwyg-toggle-' + params.field)
.attr('href', 'javascript:void(0);')
.append(text);
......@@ -143,6 +137,26 @@ Drupal.wysiwygAttachToggleLink = function(context, params) {
$('#' + params.field).after(div);
}
/**
* Parse the CSS classes of an input format DOM element into parameters.
*
* Format is "wysiwyg-name-value".
*/
Drupal.wysiwyg.getParams = function(element) {
var classes = element.className.split(' ');
var params = {};
for (var i in classes) {
if (classes[i].substr(0, 8) == 'wysiwyg-') {
var parts = classes[i].split('-');
var value = parts.slice(2).join('-');
params[parts[1]] = value;
}
}
// Convert format id into string.
params.format = 'format' + params.format;
return params;
}
/**
* Clone a configuration object recursively.
*
......
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