Skip to content

Commit

Permalink
-New Plugins
Browse files Browse the repository at this point in the history
-Removed settings not applicable to TinyMCE 3.4.1
-Some TODO Notes about Toolbars; pretty sure ToolBar4 doesn't work
-Added some Generics
-Added a feature that 'removes' and 'adds' a TinyMCE instance in case the TextArea is redrawn via Ajax and, therefore, the behavior is run again.
  • Loading branch information
Jacob Brookover committed May 30, 2011
1 parent 6e6b673 commit d494b40
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 31 deletions.
Expand Up @@ -22,13 +22,10 @@ License, or (at your option) any later version.
import java.util.Collections;

import org.apache.wicket.Component;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;

import wicket.contrib.tinymce.settings.TinyMCESettings;
import wicket.contrib.tinymce.settings.TinyMCESettings.Mode;
Expand All @@ -41,6 +38,7 @@ public class TinyMceBehavior extends AbstractBehavior {

private Component component;
private TinyMCESettings settings;
private boolean rendered = false;

public TinyMceBehavior() {
this(new TinyMCESettings());
Expand Down Expand Up @@ -88,10 +86,24 @@ protected String getRenderJavascript(IHeaderResponse response) {
}

protected String getAddTinyMceSettingsScript(Mode mode, Collection<Component> components) {
return "" //
+ settings.getLoadPluginJavaScript() //
+ " tinyMCE.init({" + settings.toJavaScript(mode, components) + " });\n" //
+ settings.getAdditionalPluginJavaScript(); //

StringBuffer script = new StringBuffer();

// If this behavior is run a second time, it means we're redrawing this component via
// an ajax call. The tinyMCE javascript does not handle this scenario, so we must
// remove the old editor before initializing it again.
if (rendered) {
for(Component c : components) {
script.append("tinyMCE.remove(tinyMCE.get('" + c.getMarkupId() + "'));\n");
}
}

script.append(settings.getLoadPluginJavaScript());
script.append(" tinyMCE.init({" + settings.toJavaScript(mode, components) + " });\n");
script.append(settings.getAdditionalPluginJavaScript());
rendered = true;

return script.toString();
}

public void bind(Component component) {
Expand Down
@@ -0,0 +1,19 @@
package wicket.contrib.tinymce.settings;

/**
* Enables the Auto Resize plugin, which automatically
* resizes the editor to the content inside it.
*
* <a href="http://tinymce.moxiecode.com/wiki.php/Plugin:autoresize">http://tinymce.moxiecode.com/wiki.php/Plugin:autoresize</a>
* @author jbrookover
*
*/
public class AutoResizePlugin extends Plugin {

private static final long serialVersionUID = 1L;

public AutoResizePlugin() {
super("autoresize");
}

}
@@ -0,0 +1,15 @@
package wicket.contrib.tinymce.settings;
/**
* Adds the ability to tab in/out of a TinyMCE Editor.
*
* @author jbrookover
*
*/
public class TabFocusPlugin extends Plugin {

private static final long serialVersionUID = 1L;

public TabFocusPlugin() {
super("tabfocus");
}
}
Expand Up @@ -70,13 +70,16 @@ public class TinyMCESettings implements Serializable {
private Location statusbarLocation;
private Align toolbarAlign;
private Language language;
private EntityEncoding entityEncoding;
private boolean resizing = false;
private boolean horizontalResizing = true;
private boolean resizingUseCookie = true;
private boolean autoResize;
@Deprecated
private boolean autoResize = false;;
private boolean readOnly = false;

private Set<Plugin> plugins = new ListOrderedSet();
private List<Control> controls = new LinkedList();
private List<Control> controls = new LinkedList<Control>();
private Set<Button> disabledButtons = new ListOrderedSet();
private Map<Toolbar, List<Button>> toolbarButtons;
private Boolean convertUrls = null;
Expand Down Expand Up @@ -143,10 +146,17 @@ public void setContentCss(ResourceReference contentCss) {
this.contentCss = contentCss;
}

@Deprecated
public boolean getAutoResize() {
return autoResize;
}

/**
* Obsolete feature; replaced by autoResize plugin.
*
* @param auto_resize
*/
@Deprecated
public void setAutoResize(boolean auto_resize) {
this.autoResize = auto_resize;
}
Expand Down Expand Up @@ -182,6 +192,22 @@ public void setToolbarAlign(Align toolbarAlign) {
public Align getToolbarAlign() {
return toolbarAlign;
}

public void setEntityEncoding(EntityEncoding entityEncoding) {
this.entityEncoding = entityEncoding;
}

public EntityEncoding getEntityEncoding() {
return entityEncoding;
}

public void setReadOnly (boolean readOnly) {
this.readOnly = readOnly;
}

public boolean isReadOnly () {
return readOnly;
}

public void setResizing(boolean resizing) {
this.resizing = resizing;
Expand Down Expand Up @@ -265,6 +291,9 @@ public Boolean getRelativeUrls() {
* Add a default button to tinymce editor. These plugins are defined by
* tinymce editor and are ready to use.
*
* TODO: I'm not sure this works for anything except the fourth toolbar.
* TODO: Why doesn't it interact with the 'toolbars' variable?
*
* @param button
* - button to be added
* @param toolbar
Expand All @@ -273,22 +302,17 @@ public Boolean getRelativeUrls() {
* - position of this button
*/
public void add(Button button, Toolbar toolbar, Position position) {
if (button instanceof PluginButton)
register(((PluginButton) button).getPlugin());
controls.add(new Control(button, toolbar, position));
}

/**
* Allow users to add plugin button. Plugin buttons are defined by tinymce
* plugins and these plugins have to be registered. Fortunately, when new
* plugin button is added, the plugin that defines the button is
* automatically registered within tinymce editor.
* Use {@link #add(Button, Toolbar, Position)} instead. It will check for
* and register Plugin Buttons
*
* @param button
* - button to be added
* @param toolbar
* - the toolbar where to add this button to
* @param position
* - position of this button
*/
@Deprecated
public void add(PluginButton button, Toolbar toolbar, Position position) {
register(button.getPlugin());
controls.add(new Control(button, toolbar, position));
Expand All @@ -309,6 +333,8 @@ public void disableButton(Button button) {
* theme_advanced_layout_manager option is set to the default value of
* "SimpleLayout".
*
* TODO: Does this fail for the fourth toolbar? Seems like it will.
*
* @param toolbar
* the toolbar to define buttons for
* @param buttons
Expand Down Expand Up @@ -354,7 +380,7 @@ public void register(Plugin plugin) {
}

// used in testing
Set getPlugins() {
Set<Plugin> getPlugins() {
return plugins;
}

Expand Down Expand Up @@ -401,6 +427,9 @@ String toJavaScript() {

if (autoResize)
buffer.append(",\n\tauto_resize : true");

if (readOnly)
buffer.append(",\n\treadonly : true"); // Per Online Doc

if (contentCss != null)
buffer.append(",\n\t").append("content_css : \"").append(
Expand All @@ -409,6 +438,10 @@ String toJavaScript() {
if (documentBaseUrl != null)
buffer.append(",\n\t").append("document_base_url : \"").append(
documentBaseUrl).append("\"");

if (entityEncoding != null)
buffer.append(",\n\t").append("entity_encoding : \"").append(
entityEncoding).append("\"");

if (Theme.advanced.equals(theme))
appendAdvancedSettings(buffer);
Expand All @@ -430,9 +463,9 @@ private void addElements(Collection<Component> components,
StringBuffer buffer) {
if (components.size() > 0) {
buffer.append(",\n\telements : \"");
Iterator iterator = components.iterator();
Iterator<Component> iterator = components.iterator();
while (iterator.hasNext()) {
Component component = (Component) iterator.next();
Component component = iterator.next();
buffer.append(component.getMarkupId());
if (iterator.hasNext())
buffer.append(", ");
Expand All @@ -445,9 +478,9 @@ private void addElements(Collection<Component> components,

private void appendPluginSettings(StringBuffer buffer) {
if (plugins.size() > 0) {
Iterator iterator = plugins.iterator();
Iterator<Plugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
Plugin plugin = (Plugin) iterator.next();
Plugin plugin = iterator.next();
plugin.definePluginSettings(buffer);
}
}
Expand All @@ -457,9 +490,9 @@ public String getLoadPluginJavaScript() {
StringBuffer loadPluginJavaScript = new StringBuffer();

if (plugins.size() > 0) {
Iterator iterator = plugins.iterator();
Iterator<Plugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
Plugin plugin = (Plugin) iterator.next();
Plugin plugin = iterator.next();
String pluginPath = plugin.getPluginPath();

if (pluginPath != null && pluginPath.equals("") == false) {
Expand All @@ -484,9 +517,9 @@ public String getAdditionalPluginJavaScript() {
StringBuffer buffer = new StringBuffer();

if (plugins.size() > 0) {
Iterator iterator = plugins.iterator();
Iterator<Plugin> iterator = plugins.iterator();
while (iterator.hasNext()) {
Plugin plugin = (Plugin) iterator.next();
Plugin plugin = iterator.next();
plugin.definePluginExtensions(buffer);
}
}
Expand Down Expand Up @@ -577,7 +610,7 @@ else if (index == 4)
private void addButtons1_Before(StringBuffer buffer) {
ControlPredicate predicate = new ControlPredicate(Toolbar.first,
Position.before);
Collection result = CollectionUtils.select(controls, predicate);
Collection<Control> result = CollectionUtils.select(controls, predicate);
if (result.size() > 0) {
buffer.append(",\n\t").append(
"theme_advanced_buttons1_add_before : ").append("\"")
Expand Down Expand Up @@ -619,7 +652,7 @@ private void addButtons2_After(StringBuffer buffer) {
private void addButtons3_Before(StringBuffer buffer) {
ControlPredicate predicate = new ControlPredicate(Toolbar.third,
Position.before);
Collection result = CollectionUtils.select(controls, predicate);
Collection<?> result = CollectionUtils.select(controls, predicate);
if (result.size() > 0) {
buffer.append(",\n\t").append(
"theme_advanced_buttons3_add_before : ").append("\"")
Expand All @@ -630,7 +663,7 @@ private void addButtons3_Before(StringBuffer buffer) {
private void addButtons3_After(StringBuffer buffer) {
ControlPredicate predicate = new ControlPredicate(Toolbar.third,
Position.after);
Collection result = CollectionUtils.select(controls, predicate);
Collection<?> result = CollectionUtils.select(controls, predicate);
if (result.size() > 0) {
buffer.append(",\n\t").append("theme_advanced_buttons3_add : ")
.append("\"").append(controlsAsString(result)).append("\"");
Expand Down Expand Up @@ -873,4 +906,25 @@ public Toolbar(String name) {
super(name);
}
}

/**
* Controls how entities/characters get processed by TinyMCE.
*
* <a href="http://tinymce.moxiecode.com/wiki.php/Configuration:entity_encoding">http://tinymce.moxiecode.com/wiki.php/Configuration:entity_encoding</a>
*
* @author jbrookover
*
*/
public static class EntityEncoding extends Enum {

private static final long serialVersionUID = 1L;

public static final EntityEncoding named = new EntityEncoding("named");
public static final EntityEncoding numeric = new EntityEncoding("numeric");
public static final EntityEncoding raw = new EntityEncoding("raw");

protected EntityEncoding(String name) {
super(name);
}
}
}

0 comments on commit d494b40

Please sign in to comment.