Newer
Older
Daniel Kudwien
committed
// $Id$
/**
Daniel Kudwien
committed
* Initialize editor instances.
Daniel Kudwien
committed
*
* @todo Is the following note still valid for 3.x?
* This function needs to be called before the page is fully loaded, as
* calling tinyMCE.init() after the page is loaded breaks IE6.
*
* @param editorSettings
Daniel Kudwien
committed
* An object containing editor settings for each input format.
Daniel Kudwien
committed
*/
Daniel Kudwien
committed
Drupal.wysiwyg.editor.init.tinymce = function(settings) {
Daniel Kudwien
committed
// If JS compression is enabled, TinyMCE is unable to find its own base path
// and exec mode, hence we need to define it manually.
// @todo Move global library settings somewhere else.
Daniel Kudwien
committed
tinyMCE.baseURL = Drupal.settings.wysiwyg.editorBasePath;
tinyMCE.srcMode = (Drupal.settings.wysiwyg.execMode == 'src' ? '_src' : '');
tinyMCE.gzipMode = (Drupal.settings.wysiwyg.execMode == 'gzip');
Daniel Kudwien
committed
Daniel Kudwien
committed
// Initialize editor configurations.
Daniel Kudwien
committed
for (var format in settings) {
tinyMCE.init(settings[format]);
Daniel Kudwien
committed
}
Daniel Kudwien
committed
// Load native external plugins.
for (var plugin in Drupal.settings.wysiwyg.plugins.tinymce.native) {
tinymce.PluginManager.load(plugin, Drupal.settings.wysiwyg.plugins.tinymce.native[plugin]);
}
// Load Drupal plugins.
for (var plugin in Drupal.settings.wysiwyg.plugins.tinymce.drupal) {
Drupal.wysiwyg.editor.instance.tinymce.addPlugin(plugin, Drupal.settings.wysiwyg.plugins.tinymce.drupal[plugin], Drupal.settings.wysiwyg.plugins.drupal[plugin]);
Daniel Kudwien
committed
};
Daniel Kudwien
committed
/**
Daniel Kudwien
committed
* Attach this editor to a target element.
Daniel Kudwien
committed
*
Daniel Kudwien
committed
* See Drupal.wysiwyg.editor.attach.none() for a full desciption of this hook.
Daniel Kudwien
committed
*/
Daniel Kudwien
committed
Drupal.wysiwyg.editor.attach.tinymce = function(context, params, settings) {
// Configure editor settings for this input format.
Daniel Kudwien
committed
var ed = new tinymce.Editor(params.field, settings);
// Reset active instance id on any event.
ed.onEvent.add(function(ed, e) {
Drupal.wysiwyg.activeId = ed.id;
});
Daniel Kudwien
committed
// Attach editor.
Daniel Kudwien
committed
ed.render();
Daniel Kudwien
committed
};
Daniel Kudwien
committed
/**
Daniel Kudwien
committed
* Detach a single or all editors.
Daniel Kudwien
committed
*
Daniel Kudwien
committed
* See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook.
Daniel Kudwien
committed
*/
Daniel Kudwien
committed
Drupal.wysiwyg.editor.detach.tinymce = function(context, params) {
Daniel Kudwien
committed
if (typeof params != 'undefined') {
var editor = tinyMCE.get(params.field);
if (editor) {
editor.save();
editor.remove();
}
}
else {
// Save contents of all editors back into textareas.
Daniel Kudwien
committed
tinyMCE.triggerSave();
// Remove all editor instances.
for (var instanceId in tinyMCE.editors) {
tinyMCE.editors[instanceId].remove();
}
Daniel Kudwien
committed
}
Daniel Kudwien
committed
};
Daniel Kudwien
committed
Daniel Kudwien
committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
Drupal.wysiwyg.editor.instance.tinymce = {
addPlugin: function(plugin, settings, pluginSettings) {
if (typeof Drupal.wysiwyg.plugins[plugin] != 'object') {
return;
}
tinymce.create('tinymce.plugins.' + plugin, {
/**
* Initialize the plugin, executed after the plugin has been created.
*
* @param ed
* The tinymce.Editor instance the plugin is initialized in.
* @param url
* The absolute URL of the plugin location.
*/
init: function(ed, url) {
// Register an editor command for this plugin, invoked by the plugin's button.
ed.addCommand(plugin, function() {
if (typeof Drupal.wysiwyg.plugins[plugin].invoke == 'function') {
var data = { format: 'html', node: ed.selection.getNode(), content: ed.selection.getContent() };
Drupal.wysiwyg.plugins[plugin].invoke(data, pluginSettings, ed.id);
}
});
// Register the plugin button.
ed.addButton(plugin, {
title : settings.iconTitle,
cmd : plugin,
image : settings.icon
});
// Load custom CSS for editor contents on startup.
ed.onInit.add(function() {
if (settings.css) {
ed.dom.loadCSS(settings.css);
}
});
// Attach: Replace plain text with HTML representations.
ed.onBeforeSetContent.add(function(ed, data) {
if (typeof Drupal.wysiwyg.plugins[plugin].attach == 'function') {
data.content = Drupal.wysiwyg.plugins[plugin].attach(data.content, pluginSettings, ed.id);
data.content = Drupal.wysiwyg.editor.instance.tinymce.prepareContent(data.content);
}
});
// Detach: Replace HTML representations with plain text.
ed.onGetContent.add(function(ed, data) {
if (typeof Drupal.wysiwyg.plugins[plugin].detach == 'function') {
data.content = Drupal.wysiwyg.plugins[plugin].detach(data.content, pluginSettings, ed.id);
}
});
// isNode: Return whether the plugin button should be enabled for the
// current selection.
ed.onNodeChange.add(function(ed, command, node) {
if (typeof Drupal.wysiwyg.plugins[plugin].isNode == 'function') {
command.setActive(plugin, Drupal.wysiwyg.plugins[plugin].isNode(node));
}
});
},
/**
* Return information about the plugin as a name/value array.
*/
getInfo: function() {
return {
longname: settings.title
};
}
});
// Register plugin.
tinymce.PluginManager.add(plugin, tinymce.plugins[plugin]);
},
openDialog: function(dialog, params) {
var editor = tinyMCE.get(this.field);
editor.windowManager.open({
file: dialog.url + '/' + this.field,
width: dialog.width,
height: dialog.height,
inline: 1
}, params);
},
closeDialog: function(dialog) {
var editor = tinyMCE.get(this.field);
editor.windowManager.close(dialog);
},
prepareContent: function(content) {
// Certain content elements need to have additional DOM properties applied
// to prevent this editor from highlighting an internal button in addition
// to the button of a Drupal plugin.
var specialProperties = {
img: { class: 'mceItem' }
};
$content = $('<div>' + content + '</div>'); // No .outerHTML() in jQuery :(
jQuery.each(specialProperties, function(element, properties) {
$content.find(element).each(function() {
for (var property in properties) {
if (property == 'class') {
$(this).addClass(properties[property]);
}
else {
$(this).attr(property, properties[property]);
}
}
});
});
return $content.html();
},
insert: function(content) {
content = this.prepareContent(content);
tinyMCE.execInstanceCommand(this.field, 'mceInsertContent', false, content);
}
};