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