1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.fckeditor.dialogs;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.gui.control.ControlImpl;
38 import info.magnolia.cms.gui.dialog.DialogBox;
39 import info.magnolia.cms.link.LinkHelper;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.link.EditorLinkTransformer;
42 import info.magnolia.link.LinkException;
43 import info.magnolia.link.LinkTransformerManager;
44 import info.magnolia.link.LinkUtil;
45 import org.apache.commons.lang.StringEscapeUtils;
46 import org.apache.commons.lang.StringUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import javax.jcr.RepositoryException;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import java.io.IOException;
54 import java.io.Writer;
55 import java.util.regex.Matcher;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 public class FckEditorDialog extends DialogBox {
140 private static final Logger log = LoggerFactory.getLogger(FckEditorDialog.class);
141
142
143
144
145 public static final String FCKEDIT_PATH = "/.resources/fckeditor/";
146
147
148
149
150 private static final String ATTRIBUTE_FCKED_LOADED = "info.magnolia.cms.gui.dialog.fckedit.loaded";
151
152
153
154
155 public static final String PARAM_JS_INIT_FILE = "jsInitFile";
156
157
158
159
160 public static final String PARAM_CUSTOM_CONFIGURATION_PATH = "jsConfigFile";
161
162 public static final String PARAM_CSS = "css";
163
164 public static final String PARAM_HEIGHT = "height";
165
166 public static final String PARAM_WIDTH = "width";
167
168 public static final String PARAM_TABLES = "tables";
169
170 private static final String PARAM_LISTS = "lists";
171
172 private static final String PARAM_ALIGNMENT = "alignment";
173
174 public static final String PARAM_IMAGES = "images";
175
176 public static final String PARAM_STYLES = "styles";
177
178 public static final String PARAM_TEMPLATES = "templates";
179
180 public static final String PARAM_FONTS = "fonts";
181
182 public static final String PARAM_FONT_SIZES = "fontSizes";
183
184 private static final String PARAM_COLORS = "colors";
185
186 public static final String PARAM_SOURCE = "source";
187
188 private static final String PARAM_ENTER_MODE = "enterMode";
189
190 private static final String PARAM_SHIFT_ENTER_MODE = "shiftEnterMode";
191
192
193
194
195 public static final String PARAM_JS_INIT_FILE_DEFAULT = "/.resources/fckeditor/custom/init/magnoliaStandard.js";
196
197 public static final String PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT = "/.resources/fckeditor/custom/config/magnoliaStandard.js";
198
199 public static final String PARAM_CSS_DEFAULT = "/.resources/fckeditor/custom/css/magnoliaStandard.css";
200
201 public static final String PARAM_HEIGHT_DEFAULT = "";
202
203 public static final String PARAM_WIDTH_DEFAULT = "";
204
205 public static final String PARAM_TABLES_DEFAULT = "false";
206
207 public static final String PARAM_IMAGES_DEFAULT = "false";
208
209 public static final String PARAM_STYLES_DEFAULT = "";
210
211 public static final String PARAM_TEMPLATES_DEFAULT = "";
212
213 public static final String PARAM_FONTS_DEFAULT = "";
214
215 public static final String PARAM_FONT_SIZES_DEFAULT = "";
216
217 public static final String PARAM_SOURCE_DEFAULT = "false";
218
219 private static final String PARAM_COLORS_DEFAULT = "";
220
221 private static final String PARAM_LISTS_DEFAULT = "true";
222
223 private static final String PARAM_ALIGNMENT_DEFAULT = "false";
224
225 private static final String PARAM_ENTER_MODE_DEFAULT = "p";
226
227 private static final String PARAM_SHIFT_ENTER_MODE_DEFAULT = "br";
228
229
230
231
232
233 public FckEditorDialog() {
234 }
235
236
237
238
239 public String getVarName() {
240 String id = getId();
241 if (id == null) {
242 id = getName();
243 }
244 return "fck_" + id.replace('-', '_');
245 }
246
247
248
249
250 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode)
251 throws RepositoryException {
252 super.init(request, response, websiteNode, configNode);
253 }
254
255
256
257
258 public void drawHtml(Writer out) throws IOException {
259
260 String jsInitFile = this.getConfigValue(PARAM_JS_INIT_FILE, PARAM_JS_INIT_FILE_DEFAULT);
261 String customConfigurationPath = this.getConfigValue(PARAM_CUSTOM_CONFIGURATION_PATH, this.getConfigValue(
262 "customConfigurationPath",
263 PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT));
264 String height = this.getConfigValue(PARAM_HEIGHT, PARAM_HEIGHT_DEFAULT);
265 String width = this.getConfigValue(PARAM_WIDTH, PARAM_WIDTH_DEFAULT);
266
267 this.drawHtmlPre(out);
268
269
270 if (getRequest().getAttribute(ATTRIBUTE_FCKED_LOADED) == null) {
271 out.write("<script type=\"text/javascript\" src=\""
272 + this.getRequest().getContextPath()
273 + "/.resources/fckeditor/fckeditor.js\"></script>");
274 getRequest().setAttribute(ATTRIBUTE_FCKED_LOADED, "true");
275 }
276
277 String id = getName();
278
279 if (id == null) {
280 log.error("Missing id for fckEditor instance");
281 }
282
283 String var = getVarName();
284 String value = convertToView(getValue());
285 out.write("<script type=\"text/javascript\">");
286 out.write("// <![CDATA[\n"); //$NON-NLS-1$
287
288
289 writeMgnlFCKConfig(out, id);
290
291 out.write("var " + var + " = null;");
292 out.write("fckInstance = new FCKeditor( '" + id + "' );");
293 out.write("fckInstance.Value = '" + escapeJsValue(value) + "';");
294 out.write("fckInstance.BasePath = '" + this.getRequest().getContextPath() + FCKEDIT_PATH + "';");
295
296 if (StringUtils.isNotEmpty(height)) {
297 out.write("fckInstance.Height = '" + this.getConfigValue("height") + "';");
298 }
299
300 if (StringUtils.isNotEmpty(width)) {
301 out.write("fckInstance.Width = '" + this.getConfigValue("width") + "';");
302 }
303
304
305 if (StringUtils.isNotEmpty(customConfigurationPath)) {
306 out.write("fckInstance.Config['CustomConfigurationsPath'] = '"
307 + this.getRequest().getContextPath()
308 + customConfigurationPath
309 + "';");
310 }
311
312
313
314
315
316 if (jsInitFile.length() > 0) {
317 out.write("// ]]>\n"); //$NON-NLS-1$
318 out.write("</script>");
319 out.write("<script type=\"text/javascript\" src=\""
320 + this.getRequest().getContextPath()
321 + jsInitFile
322 + "\"></script>\n");
323 out.write("<script type=\"text/javascript\">");
324 out.write("// <![CDATA[\n"); //$NON-NLS-1$
325 }
326
327
328 out.write("fckInstance.Create();");
329 out.write(var + " = fckInstance;");
330 out.write("// ]]>\n"); //$NON-NLS-1$
331 out.write("</script>");
332
333
334 out.write("<input type='hidden' name='mgnlSaveInfo' value='"
335 + id
336 + ",String,"
337 + ControlImpl.VALUETYPE_SINGLE
338 + ","
339 + ControlImpl.RICHEDIT_FCK
340 + ","
341 + ControlImpl.ENCODING_NO
342 + "' />");
343
344 this.drawHtmlPost(out);
345 }
346
347 private void writeMgnlFCKConfig(Writer out, String id) throws IOException {
348 String css = this.getConfigValue(PARAM_CSS, PARAM_CSS_DEFAULT);
349 String fonts = this.getConfigValue(PARAM_FONTS, PARAM_FONTS_DEFAULT);
350 String fontSizes = this.getConfigValue(PARAM_FONT_SIZES, PARAM_FONT_SIZES_DEFAULT);
351 String colors = this.getConfigValue(PARAM_COLORS, PARAM_COLORS_DEFAULT);
352 String styles = this.getConfigValue(PARAM_STYLES, PARAM_STYLES_DEFAULT);
353 String templates = this.getConfigValue(PARAM_TEMPLATES, PARAM_TEMPLATES_DEFAULT);
354
355 String lists = this.getConfigValue(PARAM_LISTS, PARAM_LISTS_DEFAULT);
356 String alignment = this.getConfigValue(PARAM_ALIGNMENT, PARAM_ALIGNMENT_DEFAULT);
357 String tables = this.getConfigValue(PARAM_TABLES, PARAM_TABLES_DEFAULT);
358 String images = this.getConfigValue(PARAM_IMAGES, PARAM_IMAGES_DEFAULT);
359 String source = this.getConfigValue(PARAM_SOURCE, PARAM_SOURCE_DEFAULT);
360 String enterMode = this.getConfigValue(PARAM_ENTER_MODE, PARAM_ENTER_MODE_DEFAULT);
361 String shiftEnterMode = this.getConfigValue(PARAM_SHIFT_ENTER_MODE, PARAM_SHIFT_ENTER_MODE_DEFAULT);
362
363
364 out.write("if( window.MgnlFCKConfigs == null)\n");
365 out.write(" window.MgnlFCKConfigs = new Object();\n");
366
367
368
369 out.write("MgnlFCKConfigs." + id + " = new Object();\n");
370
371 out.write("MgnlFCKConfigs." + id + ".language = '" + MgnlContext.getUser().getLanguage() + "';\n");
372 out.write("MgnlFCKConfigs." + id + ".contextPath = '" + getRequest().getContextPath() + "';\n");
373
374 out.write("MgnlFCKConfigs." + id + ".repository = '" + getTopParent().getConfigValue("repository") + "';\n");
375 out.write("MgnlFCKConfigs." + id + ".path = '" + getTopParent().getConfigValue("path") + "';\n");
376 out.write("MgnlFCKConfigs."
377 + id
378 + ".nodeCollection = '"
379 + getTopParent().getConfigValue("nodeCollection")
380 + "';\n");
381 out.write("MgnlFCKConfigs." + id + ".node = '" + getTopParent().getConfigValue("node") + "';\n");
382
383 out.write("MgnlFCKConfigs." + id + ".css = '" + css + "';\n");
384 out.write("MgnlFCKConfigs." + id + ".fonts = '" + fonts + "';\n");
385 out.write("MgnlFCKConfigs." + id + ".fontSizes = '" + fontSizes + "';\n");
386 out.write("MgnlFCKConfigs." + id + ".colors = '" + colors + "';\n");
387 out.write("MgnlFCKConfigs." + id + ".styles = '" + styles + "';\n");
388 out.write("MgnlFCKConfigs." + id + ".templates = '" + templates + "';\n");
389 out.write("MgnlFCKConfigs." + id + ".enterMode = '" + enterMode + "';\n");
390 out.write("MgnlFCKConfigs." + id + ".shiftEnterMode = '" + shiftEnterMode + "';\n");
391
392
393 out.write("MgnlFCKConfigs." + id + ".lists = " + lists + ";\n");
394 out.write("MgnlFCKConfigs." + id + ".alignment = " + alignment + ";\n");
395 out.write("MgnlFCKConfigs." + id + ".tables = " + tables + ";\n");
396 out.write("MgnlFCKConfigs." + id + ".images = " + images + ";\n");
397 out.write("MgnlFCKConfigs." + id + ".source = " + source + ";\n");
398 }
399
400
401
402
403
404 public String convertToView(String value) {
405 if (value != null) {
406 final EditorLinkTransformer transformer = LinkTransformerManager.getInstance().getEditorLink();
407 try {
408 value = LinkUtil.convertLinksFromUUIDPattern(value, transformer);
409 } catch (LinkException e) {
410
411 log.warn(e.getMessage());
412 throw new RuntimeException(e);
413 }
414
415
416 final Matcher matcher = LinkHelper.LINK_OR_IMAGE_PATTERN.matcher(value);
417 final StringBuffer res = new StringBuffer();
418 while (matcher.find()) {
419 final String src = matcher.group(4);
420
421
422 if (LinkUtil.isInternalRelativeLink(src)) {
423 final String link = MgnlContext.getContextPath()
424 + this.getTopParent().getConfigValue("path")
425 + "/"
426
427 + StringUtils.substringAfter(src, "/");
428
429 matcher.appendReplacement(res, "$1" + link + "$5");
430 }
431 }
432 matcher.appendTail(res);
433 return res.toString();
434 }
435
436 return StringUtils.EMPTY;
437 }
438
439
440
441
442
443
444
445
446 public String escapeJsValue(String src) {
447 return StringEscapeUtils.escapeJavaScript(src);
448 }
449
450 }