com.googlecode.wicket.jquery.ui.plugins.wysiwyg.WysiwygEditor Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.googlecode.wicket.jquery.ui.plugins.wysiwyg;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.OnLoadHeaderItem;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponentPanel;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.IModel;
import com.googlecode.wicket.jquery.core.IJQueryWidget;
import com.googlecode.wicket.jquery.core.JQueryBehavior;
import com.googlecode.wicket.jquery.ui.plugins.wysiwyg.toolbar.IWysiwygToolbar;
/**
* Provides an implementation of bootstrap-wysiwyg
* {@link WysiwygEditor} should be contained in a {@link Form} in order to work properly
*
* @author sebfz1
* @author solomax
* @author andunslg
*/
public class WysiwygEditor extends FormComponentPanel implements IJQueryWidget
{
private static final long serialVersionUID = 1L;
private TextArea textarea;
private final WebMarkupContainer container;
/**
* Constructor
*
* @param id the markup-id
*/
public WysiwygEditor(String id)
{
this(id, null, null);
}
/**
* Constructor
*
* @param id the markup-id
* @param toolbar the {@link IWysiwygToolbar}
*/
public WysiwygEditor(String id, IWysiwygToolbar toolbar)
{
this(id, null, toolbar);
}
/**
* Constructor
*
* @param id the markup-id
* @param model the {@link IModel}
*/
public WysiwygEditor(String id, IModel model)
{
this(id, model, null);
}
/**
* Constructor
*
* @param id the markup-id
* @param model the {@link IModel}
* @param toolbar the {@link IWysiwygToolbar}
*/
public WysiwygEditor(String id, IModel model, IWysiwygToolbar toolbar)
{
super(id, model);
this.container = this.newMarkupContainer();
this.add(this.container);
if (toolbar != null)
{
toolbar.attachToEditor(this.container);
}
this.setEscapeModelStrings(false); // fixes #121
}
// Properties //
/**
* Gets the editor markup-id
*
* @return the editor markup-id
*/
public String getEditorMarkupId()
{
return this.container.getMarkupId();
}
// Methods //
@Override
protected void convertInput()
{
this.setConvertedInput(this.textarea.getConvertedInput());
}
@Override
public void renderHead(IHeaderResponse response)
{
super.renderHead(response);
response.render(OnLoadHeaderItem.forScript(String.format("addTextAreaMapper('%s', '%s'); initToolbarBootstrapBindings();", this.container.getMarkupId(), this.textarea.getMarkupId())));
}
// Events //
@Override
protected void onInitialize()
{
super.onInitialize();
this.textarea = new TextArea("textarea", this.getModel());
this.textarea.setEscapeModelStrings(false);
this.add(this.textarea.setOutputMarkupId(true));
this.add(JQueryWidget.newWidgetBehavior(this, this.container));
}
@Override
public void onConfigure(JQueryBehavior behavior)
{
// noop
}
@Override
public void onBeforeRender(JQueryBehavior behavior)
{
// noop
}
// IJQueryWidget //
@Override
public JQueryBehavior newWidgetBehavior(String selector)
{
return new WysiwygBehavior(selector);
}
// Factories //
private WebMarkupContainer newMarkupContainer()
{
return new WebMarkupContainer("container") {
private static final long serialVersionUID = 1L;
@Override
protected void onComponentTag(ComponentTag tag)
{
super.onComponentTag(tag);
if (!WysiwygEditor.this.getForm().isEnabled())
{
tag.put("contenteditable", "false");
}
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy