| 1 |
// $Id: wysiwyg.js,v 1.16 2009/07/23 16:36:33 sun Exp $
|
| 2 |
(function($) {
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Initialize editor libraries.
|
| 6 |
*
|
| 7 |
* Some editors need to be initialized before the DOM is fully loaded. The
|
| 8 |
* init hook gives them a chance to do so.
|
| 9 |
*/
|
| 10 |
Drupal.wysiwygInit = function() {
|
| 11 |
// This breaks in Konqueror. Prevent it from running.
|
| 12 |
if (/KDE/.test(navigator.vendor)) {
|
| 13 |
return;
|
| 14 |
}
|
| 15 |
|
| 16 |
jQuery.each(Drupal.wysiwyg.editor.init, function(editor) {
|
| 17 |
// Clone, so original settings are not overwritten.
|
| 18 |
this(jQuery.extend(true, {}, Drupal.settings.wysiwyg.configs[editor]));
|
| 19 |
});
|
| 20 |
};
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Attach editors to input formats and target elements (f.e. textareas).
|
| 24 |
*
|
| 25 |
* This behavior searches for input format selectors and formatting guidelines
|
| 26 |
* that have been preprocessed by Wysiwyg API. All CSS classes of those elements
|
| 27 |
* with the prefix 'wysiwyg-' are parsed into input format parameters, defining
|
| 28 |
* the input format, configured editor, target element id, and variable other
|
| 29 |
* properties, which are passed to the attach/detach hooks of the corresponding
|
| 30 |
* editor.
|
| 31 |
*
|
| 32 |
* Furthermore, an "enable/disable rich-text" toggle link is added after the
|
| 33 |
* target element to allow users to alter its contents in plain text.
|
| 34 |
*
|
| 35 |
* This is executed once, while editor attach/detach hooks can be invoked
|
| 36 |
* multiple times.
|
| 37 |
*
|
| 38 |
* @param context
|
| 39 |
* A DOM element, supplied by Drupal.attachBehaviors().
|
| 40 |
*/
|
| 41 |
Drupal.behaviors.attachWysiwyg = {
|
| 42 |
attach: function(context, settings) {
|
| 43 |
// This breaks in Konqueror. Prevent it from running.
|
| 44 |
if (/KDE/.test(navigator.vendor)) {
|
| 45 |
return;
|
| 46 |
}
|
| 47 |
|
| 48 |
$('.wysiwyg', context).once('wysiwyg', function() {
|
| 49 |
if (!this.id || !this.value || typeof Drupal.settings.wysiwyg.triggers[this.id] === undefined) {
|
| 50 |
return;
|
| 51 |
}
|
| 52 |
var $this = $(this);
|
| 53 |
var params = Drupal.settings.wysiwyg.triggers[this.id];
|
| 54 |
for (var format in params) {
|
| 55 |
params[format].format = format;
|
| 56 |
params[format].trigger = this.id;
|
| 57 |
params[format].field = params.field;
|
| 58 |
}
|
| 59 |
var format = 'format' + this.value;
|
| 60 |
// Directly attach this editor, if the input format is enabled or there is
|
| 61 |
// only one input format at all.
|
| 62 |
if ($this.is(':input')) {
|
| 63 |
Drupal.wysiwygAttach(context, params[format]);
|
| 64 |
}
|
| 65 |
// Attach onChange handlers to input format selector elements.
|
| 66 |
if ($this.is('select')) {
|
| 67 |
$this.change(function() {
|
| 68 |
// If not disabled, detach the current and attach a new editor.
|
| 69 |
Drupal.wysiwygDetach(context, params[format]);
|
| 70 |
format = 'format' + this.value;
|
| 71 |
Drupal.wysiwygAttach(context, params[format]);
|
| 72 |
});
|
| 73 |
}
|
| 74 |
// Detach any editor when the containing form is submitted.
|
| 75 |
$('#' + params.field).parents('form').submit(function () {
|
| 76 |
Drupal.wysiwygDetach(context, params[format]);
|
| 77 |
});
|
| 78 |
});
|
| 79 |
}
|
| 80 |
};
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Attach an editor to a target element.
|
| 84 |
*
|
| 85 |
* This tests whether the passed in editor implements the attach hook and
|
| 86 |
* invokes it if available. Editor profile settings are cloned first, so they
|
| 87 |
* cannot be overridden. After attaching the editor, the toggle link is shown
|
| 88 |
* again, except in case we are attaching no editor.
|
| 89 |
*
|
| 90 |
* @param context
|
| 91 |
* A DOM element, supplied by Drupal.attachBehaviors().
|
| 92 |
* @param params
|
| 93 |
* An object containing input format parameters.
|
| 94 |
*/
|
| 95 |
Drupal.wysiwygAttach = function(context, params) {
|
| 96 |
if (typeof Drupal.wysiwyg.editor.attach[params.editor] == 'function') {
|
| 97 |
// (Re-)initialize field instance.
|
| 98 |
Drupal.wysiwyg.instances[params.field] = {};
|
| 99 |
// Provide all input format parameters to editor instance.
|
| 100 |
jQuery.extend(Drupal.wysiwyg.instances[params.field], params);
|
| 101 |
// Provide editor callbacks for plugins, if available.
|
| 102 |
if (typeof Drupal.wysiwyg.editor.instance[params.editor] == 'object') {
|
| 103 |
jQuery.extend(Drupal.wysiwyg.instances[params.field], Drupal.wysiwyg.editor.instance[params.editor]);
|
| 104 |
}
|
| 105 |
// Store this field id, so (external) plugins can use it.
|
| 106 |
// @todo Wrong point in time. Probably can only supported by editors which
|
| 107 |
// support an onFocus() or similar event.
|
| 108 |
Drupal.wysiwyg.activeId = params.field;
|
| 109 |
// Attach or update toggle link, if enabled.
|
| 110 |
if (params.toggle) {
|
| 111 |
Drupal.wysiwygAttachToggleLink(context, params);
|
| 112 |
}
|
| 113 |
// Otherwise, ensure that toggle link is hidden.
|
| 114 |
else {
|
| 115 |
$('#wysiwyg-toggle-' + params.field).hide();
|
| 116 |
}
|
| 117 |
// Attach editor, if enabled by default or last state was enabled.
|
| 118 |
if (params.status) {
|
| 119 |
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]) : {}));
|
| 120 |
}
|
| 121 |
// Otherwise, attach default behaviors.
|
| 122 |
else {
|
| 123 |
Drupal.wysiwyg.editor.attach.none(context, params);
|
| 124 |
Drupal.wysiwyg.instances[params.field].editor = 'none';
|
| 125 |
}
|
| 126 |
}
|
| 127 |
};
|
| 128 |
|
| 129 |
/**
|
| 130 |
* Detach all editors from a target element.
|
| 131 |
*
|
| 132 |
* @param context
|
| 133 |
* A DOM element, supplied by Drupal.attachBehaviors().
|
| 134 |
* @param params
|
| 135 |
* An object containing input format parameters.
|
| 136 |
*/
|
| 137 |
Drupal.wysiwygDetach = function(context, params) {
|
| 138 |
var editor = Drupal.wysiwyg.instances[params.field].editor;
|
| 139 |
if (jQuery.isFunction(Drupal.wysiwyg.editor.detach[editor])) {
|
| 140 |
Drupal.wysiwyg.editor.detach[editor](context, params);
|
| 141 |
}
|
| 142 |
};
|
| 143 |
|
| 144 |
/**
|
| 145 |
* Append or update an editor toggle link to a target element.
|
| 146 |
*
|
| 147 |
* @param context
|
| 148 |
* A DOM element, supplied by Drupal.attachBehaviors().
|
| 149 |
* @param params
|
| 150 |
* An object containing input format parameters.
|
| 151 |
*/
|
| 152 |
Drupal.wysiwygAttachToggleLink = function(context, params) {
|
| 153 |
if (!$('#wysiwyg-toggle-' + params.field).length) {
|
| 154 |
var text = document.createTextNode(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable);
|
| 155 |
var a = document.createElement('a');
|
| 156 |
$(a).attr({ id: 'wysiwyg-toggle-' + params.field, href: 'javascript:void(0);' }).append(text);
|
| 157 |
var div = document.createElement('div');
|
| 158 |
$(div).addClass('wysiwyg-toggle-wrapper').append(a);
|
| 159 |
$('#' + params.field).after(div);
|
| 160 |
}
|
| 161 |
$('#wysiwyg-toggle-' + params.field)
|
| 162 |
.html(params.status ? Drupal.settings.wysiwyg.disable : Drupal.settings.wysiwyg.enable).show()
|
| 163 |
.unbind('click').click(function() {
|
| 164 |
if (params.status) {
|
| 165 |
// Detach current editor.
|
| 166 |
params.status = false;
|
| 167 |
Drupal.wysiwygDetach(context, params);
|
| 168 |
// After disabling the editor, re-attach default behaviors.
|
| 169 |
// @todo We HAVE TO invoke Drupal.wysiwygAttach() here.
|
| 170 |
Drupal.wysiwyg.editor.attach.none(context, params);
|
| 171 |
Drupal.wysiwyg.instances[params.field] = Drupal.wysiwyg.editor.instance.none;
|
| 172 |
Drupal.wysiwyg.instances[params.field].editor = 'none';
|
| 173 |
$(this).html(Drupal.settings.wysiwyg.enable).blur();
|
| 174 |
}
|
| 175 |
else {
|
| 176 |
// Before enabling the editor, detach default behaviors.
|
| 177 |
Drupal.wysiwyg.editor.detach.none(context, params);
|
| 178 |
// Attach new editor using parameters of the currently selected input format.
|
| 179 |
params = Drupal.settings.wysiwyg.triggers[params.trigger]['format' + $('#' + params.trigger).val()];
|
| 180 |
params.status = true;
|
| 181 |
Drupal.wysiwygAttach(context, params);
|
| 182 |
$(this).html(Drupal.settings.wysiwyg.disable).blur();
|
| 183 |
}
|
| 184 |
});
|
| 185 |
// Hide toggle link in case no editor is attached.
|
| 186 |
if (params.editor == 'none') {
|
| 187 |
$('#wysiwyg-toggle-' + params.field).hide();
|
| 188 |
}
|
| 189 |
};
|
| 190 |
|
| 191 |
/**
|
| 192 |
* Parse the CSS classes of an input format DOM element into parameters.
|
| 193 |
*
|
| 194 |
* Syntax for CSS classes is "wysiwyg-name-value".
|
| 195 |
*
|
| 196 |
* @param element
|
| 197 |
* An input format DOM element containing CSS classes to parse.
|
| 198 |
* @param params
|
| 199 |
* (optional) An object containing input format parameters to update.
|
| 200 |
*/
|
| 201 |
Drupal.wysiwyg.getParams = function(element, params) {
|
| 202 |
var classes = element.className.split(' ');
|
| 203 |
var params = params || {};
|
| 204 |
for (var i in classes) {
|
| 205 |
if (classes[i].substr(0, 8) == 'wysiwyg-') {
|
| 206 |
var parts = classes[i].split('-');
|
| 207 |
var value = parts.slice(2).join('-');
|
| 208 |
params[parts[1]] = value;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
// Convert format id into string.
|
| 212 |
params.format = 'format' + params.format;
|
| 213 |
// Convert numeric values.
|
| 214 |
params.status = parseInt(params.status, 10);
|
| 215 |
params.toggle = parseInt(params.toggle, 10);
|
| 216 |
params.resizable = parseInt(params.resizable, 10);
|
| 217 |
return params;
|
| 218 |
};
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Allow certain editor libraries to initialize before the DOM is loaded.
|
| 222 |
*/
|
| 223 |
Drupal.wysiwygInit();
|
| 224 |
|
| 225 |
})(jQuery);
|