Commit 36ac8b15 authored by Woonsan Ko's avatar Woonsan Ko

CMS7-8233: supporting readOnly in CodeMirrorEditor and some minor code cleanups

parent 90c576a4
......@@ -159,6 +159,7 @@ editor/repository/tm.out.1
editor/repository/tm.out.lck
editor/repository/tmlog.lck
editor/repository/wdbp-error-log.txt
editor/repository/workspaces
editor/target
editor/test/*.tmp
editor/test/.classpath
......@@ -258,6 +259,7 @@ google-analytics/target
gotolink/.classpath
gotolink/.project
gotolink/.settings
gotolink/WEB-INF
gotolink/hippo-cms-gotolink.iml
gotolink/target
/hippo-cms.iml
......
......@@ -59,12 +59,9 @@ public class CodeMirrorTextAreaWidget extends TextAreaWidget {
* @return
*/
protected CodeMirrorEditor createEditor(final IModel<String> model) {
return new CodeMirrorEditor("widget", "code-editor", model) {
@Override
protected boolean isChangeEventTriggeringEnabled() {
return true;
}
};
CodeMirrorEditor editor = new CodeMirrorEditor("widget", "code-editor", model);
editor.setChangeEventTriggeringEnabled(true);
return editor;
}
}
......@@ -51,6 +51,8 @@ public class FreemarkerTextAreaWidget extends CodeMirrorTextAreaWidget {
public FreemarkerCodeMirrorEditor(String id, String editorName, IModel<String> model) {
super(id, editorName, model);
setEditorMode(MIME_TYPE_HTML);
setChangeEventTriggeringEnabled(true);
}
@Override
......@@ -60,17 +62,6 @@ public class FreemarkerTextAreaWidget extends CodeMirrorTextAreaWidget {
response.render(JavaScriptHeaderItem.forReference(CSS_MODE_JS));
response.render(JavaScriptHeaderItem.forReference(HTML_MIXED_MODE_JS));
}
@Override
protected String getEditorMode() {
String editorMode = MIME_TYPE_HTML;
return editorMode;
}
@Override
protected boolean isChangeEventTriggeringEnabled() {
return true;
}
}
}
......@@ -74,6 +74,9 @@ public class CodeMirrorEditor extends TextArea<String> {
private static final CssResourceReference ECLIPSE_SKIN = new CssResourceReference(CodeMirrorEditor.class, "theme/eclipse.css");
private String editorName;
private String editorMode = MIME_TYPE_GROOVY;
private boolean readOnly;
private boolean changeEventTriggeringEnabled;
public CodeMirrorEditor(final String id, final String editorName, final IModel<String> model) {
super(id, model);
......@@ -93,38 +96,36 @@ public class CodeMirrorEditor extends TextArea<String> {
}
/**
* Renders head elements for custom CodeMirror skins.
* By default, it renders {@link #ECLIPSE_SKIN}.
* @param response
* Returns the CodeMirror editor name (in JavaScript context).
* @return
*/
protected void renderHeadForCustomSkins(final IHeaderResponse response) {
response.render(CssHeaderItem.forReference(ECLIPSE_SKIN));
public String getEditorName() {
return editorName;
}
/**
* Renders head elements for custom mode.
* By default, it renders {@link #GROOVY_MODE_JS}.
* @param response
* Returns the MIME Type to be associated with a CodeMirror editor mode.
* By default, it returns {@link #MIME_TYPE_GROOVY}.
* @return
*/
protected void renderHeadForCustomModes(final IHeaderResponse response) {
response.render(JavaScriptHeaderItem.forReference(GROOVY_MODE_JS));
public String getEditorMode() {
return editorMode;
}
/**
* Returns the CodeMirror editor name (in JavaScript context).
* @return
*/
protected String getEditorName() {
return editorName;
public void setEditorMode(String editorMode) {
this.editorMode = editorMode;
}
/**
* Returns the MIME Type to be associated with a CodeMirror editor mode.
* By default, it returns {@link #MIME_TYPE_GROOVY}.
* Returns true if the editor should be read-only.
* @return
*/
protected String getEditorMode() {
return MIME_TYPE_GROOVY;
public boolean isReadOnly() {
return readOnly;
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
......@@ -138,8 +139,30 @@ public class CodeMirrorEditor extends TextArea<String> {
* </P>
* @return
*/
protected boolean isChangeEventTriggeringEnabled() {
return false;
public boolean isChangeEventTriggeringEnabled() {
return changeEventTriggeringEnabled;
}
public void setChangeEventTriggeringEnabled(boolean changeEventTriggeringEnabled) {
this.changeEventTriggeringEnabled = changeEventTriggeringEnabled;
}
/**
* Renders head elements for custom CodeMirror skins.
* By default, it renders {@link #ECLIPSE_SKIN}.
* @param response
*/
protected void renderHeadForCustomSkins(final IHeaderResponse response) {
response.render(CssHeaderItem.forReference(ECLIPSE_SKIN));
}
/**
* Renders head elements for custom mode.
* By default, it renders {@link #GROOVY_MODE_JS}.
* @param response
*/
protected void renderHeadForCustomModes(final IHeaderResponse response) {
response.render(JavaScriptHeaderItem.forReference(GROOVY_MODE_JS));
}
/**
......@@ -149,9 +172,14 @@ public class CodeMirrorEditor extends TextArea<String> {
protected String getJavaScriptForEditor() {
StringBuilder sb = new StringBuilder();
sb.append("var cm = CodeMirror.fromTextArea(document.getElementById('" + getMarkupId() + "'), " +
"{ lineNumbers: true, matchBrackets: true, mode: \"" + getEditorMode() + "\", " +
"onChange: function(cm) { cm.save(); ");
sb.append("var cm = CodeMirror.fromTextArea(document.getElementById('" + getMarkupId() + "'), ");
sb.append("{ lineNumbers: true, matchBrackets: true, mode: \"" + getEditorMode() + "\", ");
if (isReadOnly()) {
sb.append("readOnly: true, ");
}
sb.append("onChange: function(cm) { cm.save(); ");
if (isChangeEventTriggeringEnabled()) {
sb.append("var ta = document.getElementById('" + getMarkupId() + "'); " +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment