Newer
Older
Daniel Kudwien
committed
Drupal.wysiwyg = Drupal.wysiwyg || { 'init': {}, 'attach': {}, 'detach': {} };
Daniel Kudwien
committed
Daniel Kudwien
committed
* Initialize editor libraries.
*
* Some editors need to be initialized before the DOM is fully loaded. The
* init hook gives them a chance to do so.
Daniel Kudwien
committed
Drupal.wysiwygInit = function() {
Daniel Kudwien
committed
jQuery.each(Drupal.wysiwyg.init, function(editor) {
this(Drupal.settings.wysiwygEditor.configs[editor]);
});
Daniel Kudwien
committed
* Attach editors to input formats and target elements (f.e. textareas).
*
* This behavior searches for input format selectors and formatting guidelines
* that have been preprocessed by Wysiwyg API. All CSS classes of those elements
* with the prefix 'wysiwyg-' are parsed into input format parameters, defining
* the configured editor, editor theme, target element id, and variable other
* properties, which are passed to the attach/detach hooks of the corresponding
* editor.
*
* Furthermore, an "enable/disable rich-text" toggle link is added after the
* target element to allow users to alter its contents in plain text.
Daniel Kudwien
committed
* This is executed once, while editor attach/detach hooks can be invoked
* multiple times.
*
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
Daniel Kudwien
committed
Drupal.behaviors.attachWysiwyg = function(context) {
Daniel Kudwien
committed
$('.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;
Daniel Kudwien
committed
}
Daniel Kudwien
committed
$this = $(this);
// Directly attach this editor, if the input format is enabled or there is
// only one input format at all.
if (($this.is(':input') && $this.is(':checked')) || $this.is('div')) {
Drupal.wysiwygEditorAttachToggleLink(context, params);
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);
});
}
$this.addClass('wysiwyg-processed');
Daniel Kudwien
committed
});
Daniel Kudwien
committed
* Attach an editor to a target element.
*
* This tests whether the passed in editor implements the attach hook and
* invokes it if available. Editor profile settings are cloned first, so they
* cannot be overridden. After attaching the editor, the toggle link is shown
* again, except in case we are attaching no editor.
Daniel Kudwien
committed
*
Daniel Kudwien
committed
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
Daniel Kudwien
committed
Drupal.wysiwygAttach = function(context, params) {
if (typeof Drupal.wysiwyg.attach[params.editor] == 'function') {
Drupal.wysiwyg.attach[params.editor](context, params, Drupal.wysiwyg.clone(Drupal.settings.wysiwygEditor.configs[params.editor]));
$('#wysiwyg-toggle-' + params.field).show();
}
if (params.editor == 'none') {
$('#wysiwyg-toggle-' + params.field).hide();
}
Daniel Kudwien
committed
* Detach all editors from a target element.
Daniel Kudwien
committed
*
Daniel Kudwien
committed
* 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.
Daniel Kudwien
committed
*
Daniel Kudwien
committed
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
Daniel Kudwien
committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Drupal.wysiwygDetach = function(context, params) {
jQuery.each(Drupal.wysiwyg.detach, function(editor) {
this(context, params);
});
}
/**
* Append a editor toggle link to a target element.
*
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
*/
Drupal.wysiwygEditorAttachToggleLink = function(context, params) {
var text = document.createTextNode(Drupal.settings.wysiwygEditor.status ? Drupal.settings.wysiwygEditor.disable : Drupal.settings.wysiwygEditor.enable);
var a = document.createElement('a');
$(a).toggle(
function() {
Drupal.wysiwygDetach(context, params);
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwygEditor.enable).blur();
// After disabling the editor, re-attach default behaviors.
Drupal.wysiwyg.attach.none(context, params);
},
function() {
// Before enabling the editor, detach default behaviors.
Drupal.wysiwyg.detach.none(context, params);
Drupal.wysiwygAttach(context, params);
$('#wysiwyg-toggle-' + params.field).html(Drupal.settings.wysiwygEditor.disable).blur();
})
.attr('id', 'wysiwyg-toggle-' + params.field)
.attr('href', 'javascript:void(0);')
.append(text);
var div = document.createElement('div');
$(div).append(a);
$('#' + params.field).after(div);
Daniel Kudwien
committed
/**
Daniel Kudwien
committed
* Clone a configuration object recursively.
Daniel Kudwien
committed
*
* @param obj
* The object to clone.
*
* @return
* A copy of the passed in object.
*/
Drupal.wysiwyg.clone = function(obj) {
var clone = {};
for (i in obj) {
if ((typeof obj[i] == 'object') || (typeof obj[i] == 'array')) {
Daniel Kudwien
committed
clone[i] = Drupal.wysiwyg.clone(obj[i]);
}
else {
clone[i] = obj[i];
}
}
return clone;
}
/**
Daniel Kudwien
committed
* Allow certain editor libraries to initialize before the DOM is loaded.
Daniel Kudwien
committed
Drupal.wysiwygInit();