001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.forms.element;
025:
026: import java.io.PrintWriter;
027: import java.util.HashMap;
028: import java.util.Map;
029:
030: import net.sf.json.JSONObject;
031:
032: import org.riotfamily.common.markup.Html;
033: import org.riotfamily.common.markup.TagWriter;
034: import org.riotfamily.forms.DHTMLElement;
035: import org.riotfamily.forms.resource.FormResource;
036: import org.riotfamily.forms.resource.ResourceElement;
037: import org.riotfamily.forms.resource.ScriptResource;
038:
039: /**
040: * A WYSIWYG richtext editor based on TinyMCE.
041: */
042: public class TinyMCE extends AbstractTextElement implements
043: ResourceElement, DHTMLElement {
044:
045: static Map defaults = new HashMap();
046: static {
047: defaults.put("skin", "riot");
048: defaults.put("theme", "advanced");
049: defaults
050: .put(
051: "valid_elements",
052: "+a[href|target|name],-strong/b,-em/i,h3/h2/h1,h4/h5/h6,p,br,hr,ul,ol,li,blockquote");
053: defaults.put("theme_advanced_containers", "buttons1,mceeditor");
054: defaults
055: .put(
056: "theme_advanced_container_buttons1",
057: "formatselect,bold,italic,sup,bullist,numlist,outdent,indent,hr,link,unlink,anchor,code,undo,redo,charmap");
058: defaults.put("theme_advanced_blockformats", "p,h3,h4");
059: }
060:
061: private int rows = 10;
062:
063: private Map config;
064:
065: private String initScript;
066:
067: public TinyMCE() {
068: setStyleClass("richtext");
069: }
070:
071: public void setRows(int rows) {
072: this .rows = rows;
073: }
074:
075: public void setConfig(Map config) {
076: this .config = config;
077: }
078:
079: public void renderInternal(PrintWriter writer) {
080: TagWriter tag = new TagWriter(writer);
081: tag.start(Html.TEXTAREA).attribute(Html.COMMON_CLASS,
082: getStyleClass()).attribute(Html.COMMON_ID, getId())
083: .attribute(Html.INPUT_NAME, getParamName()).attribute(
084: Html.TEXTAREA_ROWS, rows).body(getText()).end();
085: }
086:
087: public FormResource getResource() {
088: return new ScriptResource("tiny_mce/tiny_mce_src.js",
089: "tinymce.WindowManager", new ScriptResource(
090: "tiny_mce/lazy_load_fix.js", "tinyMCE_GZ"));
091: }
092:
093: private String getJsonConfig() {
094: JSONObject json = JSONObject.fromObject(defaults);
095: if (config != null) {
096: json.putAll(config);
097: }
098: json.element("mode", "exact");
099: json.element("elements", getId());
100: json.element("language", getFormContext().getLocale()
101: .getLanguage().toLowerCase());
102: json.element("add_unload_trigger", false);
103: json.element("submit_patch", false);
104: json.element("strict_loading_mode", true);
105: json.element("relative_urls", Boolean.FALSE);
106: json.element("theme_advanced_layout_manager", "RowLayout");
107: json.element("theme_advanced_containers_default_align", "left");
108: json.element("theme_advanced_container_mceeditor", "mceeditor");
109: return json.toString();
110: }
111:
112: public String getInitScript() {
113: if (initScript == null) {
114: StringBuffer sb = new StringBuffer();
115: sb.append("tinymce.dom.Event._pageInit();");
116: sb.append("tinyMCE.init(").append(getJsonConfig()).append(
117: ");");
118: initScript = sb.toString();
119: }
120: return initScript;
121: }
122:
123: }
|