1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.gui.control.ControlSuper; 17 import info.magnolia.cms.util.LinkUtil; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 22 import javax.jcr.RepositoryException; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.apache.log4j.Logger; 28 29 30 37 public class DialogFckEdit extends DialogBox { 38 39 42 private static Logger log = Logger.getLogger(DialogFckEdit.class); 43 44 47 public static final String FCKEDIT_PATH = "/admindocroot/fckeditor/"; 49 52 public static final String PARAM_JS_INIT_FILE = "jsInitFile"; 54 57 public static final String PARAM_CUSTOM_CONFIGURATION_PATH = "customConfigurationPath"; 59 62 public static final String PARAM_JS_INIT_FILE_DEFAULT = "/admindocroot/fckeditor/custom/init/magnoliaStandard.js"; 64 67 public static final String PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT = "/admindocroot/fckeditor/custom/config/magnoliaStandard.js"; 69 72 private String customConfigurationsPath = PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT; 73 74 77 private String jsInitFile = PARAM_JS_INIT_FILE_DEFAULT; 78 79 82 protected DialogFckEdit() { 83 } 84 85 88 public String getVarName() { 89 String id = getId(); 90 if (id == null) { 91 id = getName(); 92 } 93 return "fck_" + id.replace('-', '_'); } 95 96 99 public void setCustomConfigurationPath(String name) { 100 if (name != null) { 101 customConfigurationsPath = name; 102 } 103 } 104 105 108 public void setJSInitFile(String name) { 109 if (name != null) { 110 jsInitFile = name; 111 } 112 } 113 114 117 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) 118 throws RepositoryException { 119 super.init(request, response, websiteNode, configNode); 120 String jsInitFile = this.getConfigValue(PARAM_JS_INIT_FILE, PARAM_JS_INIT_FILE_DEFAULT); 121 String customConfigurationPath = this.getConfigValue( 122 PARAM_CUSTOM_CONFIGURATION_PATH, 123 PARAM_CUSTOM_CONFIGURATION_PATH_DEFAULT); 124 this.setJSInitFile(jsInitFile); 125 this.setCustomConfigurationPath(customConfigurationPath); 126 } 127 128 131 public void drawHtml(Writer out) throws IOException { 132 this.drawHtmlPre(out); 133 134 if (getRequest().getAttribute("__fcked_loaded") == null) { out.write("<script type=\"text/javascript\" SRC=\"" + this.getRequest().getContextPath() + "/admindocroot/fckeditor/fckeditor.js\"></script>"); getRequest().setAttribute("__fcked_loaded", "true"); } 140 141 String id = getName(); 142 143 if (id == null) { 144 log.error("Missing id for fckEditor instance"); } 146 147 String var = getVarName(); 148 String value = convertToView(getValue()); 149 value = LinkUtil.convertUUIDsToAbsoluteLinks(value); 150 out.write("<script type=\"text/javascript\">"); out.write("var " + var + " = null;"); out.write("fckInstance = new FCKeditor( '" + id + "' );"); out.write("fckInstance.Value = '" + escapeJsValue(value) + "';"); out.write("fckInstance.BasePath = '" + this.getRequest().getContextPath() + FCKEDIT_PATH + "';"); if (customConfigurationsPath.length() > 0) { 156 out.write("fckInstance.Config['CustomConfigurationsPath'] = '" + this.getRequest().getContextPath() + customConfigurationsPath + "';"); } 159 if (jsInitFile.length() > 0) { 160 out.write("</script>"); out.write("<script type=\"text/javascript\" SRC=\"" + this.getRequest().getContextPath() + jsInitFile + "\"></script>\n"); out.write("<script type=\"text/javascript\">"); } 165 out.write("fckInstance.Create();"); out.write(var + " = fckInstance;"); out.write("</script>"); 169 out.write("<input type='hidden' name='mgnlSaveInfo' value='" + id + ",String," + ControlSuper.VALUETYPE_SINGLE + "," + ControlSuper.RICHEDIT_FCK + "," + ControlSuper.ENCODING_NO + "' />"); 176 this.drawHtmlPost(out); 177 178 } 181 182 186 private static String convertToView(String value) { 187 String tmp = value; 188 if (tmp != null) { 189 tmp = tmp.replaceAll("\r\n", "<br />"); tmp = tmp.replaceAll("\n", "<br />"); return tmp; 192 } 193 return StringUtils.EMPTY; 194 } 195 196 210 public static String escapeJsValue(String src) { 211 if (src == null) { 212 return null; 213 } 214 String escapedSrc = src.replaceAll("'", "\\\\'"); escapedSrc = escapedSrc.replaceAll("\"", "\\\""); escapedSrc = escapedSrc.replaceAll("\\r\\n", "\\\\r\\\\n"); escapedSrc = escapedSrc.replaceAll("\\n", "\\\\n"); 219 return escapedSrc; 220 } 221 } | Popular Tags |