Newer
Older
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() {
// This breaks in Konqueror. Prevent it from running.
if (/KDE/.test(navigator.vendor)) {
return;
}
Daniel Kudwien
committed
jQuery.each(Drupal.wysiwyg.editor.init, function(editor) {
Daniel Kudwien
committed
// Clone, so original settings are not overwritten.
this(jQuery.extend(true, {}, Drupal.settings.wysiwyg.configs[editor]));
Daniel Kudwien
committed
});
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
Daniel Kudwien
committed
* the input format, configured editor, target element id, and variable other
Daniel Kudwien
committed
* 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) {
// This breaks in Konqueror. Prevent it from running.
if (/KDE/.test(navigator.vendor)) {
return;
}
Daniel Kudwien
committed
$('.wysiwyg:not(.wysiwyg-processed)', context).each(function() {
var params = Drupal.wysiwyg.getParams(this);
Daniel Kudwien
committed
var $this = $(this).addClass('wysiwyg-processed');
Daniel Kudwien
committed
// 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.wysiwygAttach(context, params);
}
// Attach onChange handlers to input format selector elements.
if ($this.is(':input')) {
$this.change(function() {
// If not disabled, detach the current and attach a new editor.
Daniel Kudwien
committed
Drupal.wysiwygDetach(context, params);
Drupal.wysiwygAttach(context, params);
Daniel Kudwien
committed
});
Daniel Kudwien
committed
// IE triggers onChange after blur only.
if ($.browser.msie) {
$this.click(function () {
this.blur();
});
}
Daniel Kudwien
committed
}
Daniel Kudwien
committed
// Detach any editor when the containing form is submitted.
Daniel Kudwien
committed
$('#' + params.field).parents('form').submit(function (event) {
// Do not detach if the event was cancelled.
if (event.originalEvent.returnValue === false) {
return;
}
Daniel Kudwien
committed
Drupal.wysiwygDetach(context, params);
});
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) {
Daniel Kudwien
committed
if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
// (Re-)initialize field instance.
Drupal.wysiwyg.instances[params.field] = {};
// Provide all input format parameters to editor instance.
jQuery.extend(Drupal.wysiwyg.instances[params.field], params);
Daniel Kudwien
committed
// Provide editor callbacks for plugins, if available.
if (typeof Drupal.wysiwyg.editor.instance[params.editor] == 'object') {
jQuery.extend(Drupal.wysiwyg.instances[params.field], Drupal.wysiwyg.editor.instance[params.editor]);
}
// Store this field id, so (external) plugins can use it.
// @todo Wrong point in time. Probably can only supported by editors which
// support an onFocus() or similar event.
Daniel Kudwien
committed
Drupal.wysiwyg.activeId = params.field;
// Attach or update toggle link, if enabled.
if (params.toggle) {
Drupal.wysiwygAttachToggleLink(context, params);
}
// Otherwise, ensure that toggle link is hidden.
else {
$('#wysiwyg-toggle-' + params.field).hide();
}
Daniel Kudwien
committed
// Attach editor, if enabled by default or last state was enabled.
if (params.status) {
Drupal.wysiwyg.editor.attach[params.editor](context, params, (Drupal.settings.wysiwyg.configs[params.editor] ? jQuery.extend(true, {}, Drupal.settings.wysiwyg.configs[params.editor][params.format]) : {}));
Daniel Kudwien
committed
}
// Otherwise, attach default behaviors.
else {
Drupal.wysiwyg.editor.attach.none(context, params);
Drupal.wysiwyg.instances[params.field].editor = 'none';
}
Daniel Kudwien
committed
}
Daniel Kudwien
committed
* Detach all editors from a 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
Drupal.wysiwygDetach = function(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);
}
Daniel Kudwien
committed
/**
Daniel Kudwien
committed
* Append or update an editor toggle link to a target element.
Daniel Kudwien
committed
*
* @param context
* A DOM element, supplied by Drupal.attachBehaviors().
* @param params
* An object containing input format parameters.
*/
Daniel Kudwien
committed
Drupal.wysiwygAttachToggleLink = function(context, params) {
if (!$('#wysiwyg-toggle-' + params.field).length) {
Daniel Kudwien
committed
var text = document.createTextNode(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
var a = document.createElement('a');
$(a).attr({ id: 'wysiwyg-toggle-' + params.field, href: 'javascript:void(0);' }).append(text);
Daniel Kudwien
committed
var div = document.createElement('div');
$(div).addClass('wysiwyg-toggle-wrapper').append(a);
Daniel Kudwien
committed
$('#' + params.field).after(div);
}
$('#wysiwyg-toggle-' + params.field)
.html(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable).show()
Daniel Kudwien
committed
.unbind('click.wysiwyg', Drupal.wysiwyg.toggleWysiwyg)
.bind('click.wysiwyg', { params: params, context: context }, Drupal.wysiwyg.toggleWysiwyg);
Daniel Kudwien
committed
// Hide toggle link in case no editor is attached.
if (params.editor == 'none') {
$('#wysiwyg-toggle-' + params.field).hide();
}
Daniel Kudwien
committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Callback for the Enable/Disable rich editor link.
*/
Drupal.wysiwyg.toggleWysiwyg = function (event) {
var context = event.data.context;
var params = event.data.params;
if (params.status) {
// Detach current editor.
params.status = false;
Drupal.wysiwygDetach(context, params);
// After disabling the editor, re-attach default behaviors.
// @todo We HAVE TO invoke Drupal.wysiwygAttach() here.
Drupal.wysiwyg.editor.attach.none(context, params);
Drupal.wysiwyg.instances[params.field] = Drupal.wysiwyg.editor.instance.none;
Drupal.wysiwyg.instances[params.field].editor = 'none';
$(this).html(Drupal.settings.wysiwyg.enable).blur();
}
else {
// Before enabling the editor, detach default behaviors.
Drupal.wysiwyg.editor.detach.none(context, params);
// Attach new editor using parameters of the currently selected input format.
Drupal.wysiwyg.getParams($('.wysiwyg-field-' + params.field + ':checked, div.wysiwyg-field-' + params.field, context).get(0), params);
params.status = true;
Drupal.wysiwygAttach(context, params);
$(this).html(Drupal.settings.wysiwyg.disable).blur();
}
}
/**
* Parse the CSS classes of an input format DOM element into parameters.
*
Daniel Kudwien
committed
* Syntax for CSS classes is "wysiwyg-name-value".
*
* @param element
* An input format DOM element containing CSS classes to parse.
* @param params
* (optional) An object containing input format parameters to update.
*/
Daniel Kudwien
committed
Drupal.wysiwyg.getParams = function(element, params) {
var classes = element.className.split(' ');
Daniel Kudwien
committed
var params = params || {};
for (var i = 0; i < classes.length; i++) {
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;
Daniel Kudwien
committed
// Convert numeric values.
Daniel Kudwien
committed
params.status = parseInt(params.status, 10);
params.toggle = parseInt(params.toggle, 10);
Daniel Kudwien
committed
params.resizable = parseInt(params.resizable, 10);
return params;
/**
* D5 only: Queue our attach behavior.
*/
$(document).ready(function () {
if (Drupal.attachBehaviors === undefined) {
Drupal.behaviors.attachWysiwyg(this);
}
});
Daniel Kudwien
committed
* Allow certain editor libraries to initialize before the DOM is loaded.
Daniel Kudwien
committed
Drupal.wysiwygInit();
Daniel Kudwien
committed