Skip to content

Commit

Permalink
Custom server side event alump#10
Browse files Browse the repository at this point in the history
  • Loading branch information
athalay committed Nov 2, 2017
1 parent cacb9d4 commit d368dc1
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class AbstractCKEditorTextField extends AbstractField<String>
private boolean focusRequested = false;
private boolean immediate = false;
protected LinkedList<VaadinSaveListener> vaadinSaveListenerList;
protected LinkedList<CustomEventListener> customEventListenerList;

private boolean textIsDirty;
protected String value;
Expand Down Expand Up @@ -218,6 +219,11 @@ public void changeVariables(Object source, Map<String, Object> variables) {
if (variables.containsKey(VCKEditorTextField.VAR_VAADIN_SAVE_BUTTON_PRESSED) && ! isReadOnly()) {
notifyVaadinSaveListeners();
}

// See if customevent was fired
if (variables.containsKey(VCKEditorTextField.VAR_ON_CUSTOM_EVENT) && ! isReadOnly()) {
notifyCustomEventListeners((String)variables.get(VCKEditorTextField.VAR_ON_CUSTOM_EVENT));
}
}

@Override
Expand Down Expand Up @@ -315,6 +321,24 @@ synchronized void notifyVaadinSaveListeners() {
listener.vaadinSave(this);
}
}

public synchronized void addCustomEventListener(CustomEventListener listener) {
if ( customEventListenerList == null ) {
customEventListenerList = new LinkedList<CustomEventListener>();
}
customEventListenerList.add(listener);
}
public synchronized void removeCustomEventListener(CustomEventListener listener) {
if ( customEventListenerList != null ) {
customEventListenerList.remove(listener);
}
}
synchronized void notifyCustomEventListeners(String param) {
if ( customEventListenerList != null ) {
for( CustomEventListener listener : customEventListenerList )
listener.onCustomEvent(this, param);
}
}

public interface VaadinSaveListener extends Serializable {
/**
Expand All @@ -324,6 +348,15 @@ public interface VaadinSaveListener extends Serializable {
*/
void vaadinSave(AbstractCKEditorTextField editor);
}

public interface CustomEventListener extends Serializable {
/**
* Notifies this listener that a custom event has fired
*
* @param editor the CKEditorTextField that fired the event
*/
void onCustomEvent(AbstractCKEditorTextField editor, String param);
}


@SuppressWarnings("serial")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,16 @@ public final native void instanceReady(CKEditorService.CKEditorListener listener
this.on( 'focus', function( ev ) {
ev.listenerData.@org.vaadin.alump.ckeditor.client.CKEditorService.CKEditorListener::onFocus()();
}, null, listener);
this.on( 'vaadinsave', function( ev ) {
ev.listenerData.@org.vaadin.alump.ckeditor.client.CKEditorService.CKEditorListener::onSave()();
}, null, listener);
// hook into the change events for ckEditor
this.on( 'vaadinsave', function( ev ) {
ev.listenerData.@org.vaadin.alump.ckeditor.client.CKEditorService.CKEditorListener::onSave()();
}, null, listener);
this.on( 'customevent', function( ev ) {
ev.listenerData.@org.vaadin.alump.ckeditor.client.CKEditorService.CKEditorListener::onCustomEvent(Ljava/lang/String;)(ev.data);
}, null, listener);
// hook into the change events for ckEditor
this.on('change', function(ev) {
ev.listenerData.@org.vaadin.alump.ckeditor.client.CKEditorService.CKEditorListener::onChange()();
}, null, listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public interface CKEditorListener {
void onBlur();
void onFocus();
void onSave();
void onCustomEvent(String param);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class VCKEditorTextField extends Widget implements Paintable, CKEditorSer
public static final String ATTR_PROTECTED_BODY = "protected_body";
public static final String VAR_TEXT = "text";
public static final String VAR_VAADIN_SAVE_BUTTON_PRESSED = "vaadinsave";
public static final String VAR_ON_CUSTOM_EVENT = "customevent";
public static final String VAR_VERSION = "version";

public static final String EVENT_SELECTION_CHANGE = "selectionChange";
Expand Down Expand Up @@ -304,6 +305,14 @@ public void onSave() {
}
}

// Listener callback
@Override
public void onCustomEvent(String param) {
if ( ckEditorIsReady && ! readOnly ) {
clientToServer.updateVariable(paintableId, VAR_ON_CUSTOM_EVENT,param,true);
}
}

// Listener callback
@Override
public void onBlur() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Example of extending with plugins. In JavaScript annotation includes the plugin js file, that is available on
* same package (in maven project under resources).
*/
@JavaScript({"http://cdn.ckeditor.com/4.6.2/standard/ckeditor.js", "example_plugin.js"})
@JavaScript({"http://cdn.ckeditor.com/4.6.2/standard/ckeditor.js", "example_plugin.js", "customevent_plugin.js"})
public class CKEditorPluginExample extends AbstractCKEditorTextField {

public CKEditorPluginExample() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,22 @@ public void init(VaadinRequest request) {
config1.disableSpellChecker();
config1.setHeight("300px");
config1.addToExtraPlugins("exampleplugin");

config1.addToExtraPlugins("inserttextplugin");

//final CKEditorTextField ckEditorTextField1 = new CKEditorTextField(config1);
final AbstractCKEditorTextField ckEditorTextField1 = new CKEditorPluginExample(config1);
ckEditorTextField1.setImmediate(true);
ckEditorTextField1.setHeight("440px"); // account for 300px editor plus toolbars
mainView.addComponent(ckEditorTextField1);

ckEditorTextField1.addCustomEventListener(new AbstractCKEditorTextField.CustomEventListener() {
@Override
public void onCustomEvent(AbstractCKEditorTextField editor, String param) {
if(param!=null && param.equals("inserttext")){
editor.insertText("Text from vaadin");
}
}
});

ckEditorTextField1.setValue(editor1InitialValue);
ckEditorTextField1.addValueChangeListener(e -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

(function()
{
var insertTextPluginCmd =
{
modes: { wysiwyg: 1, source: 1 },
exec : function( editor )
{
editor.fire("customevent", "inserttext");
}
};

var pluginName = 'inserttextplugin';

// Register a plugin named "inserttext".
CKEDITOR.plugins.add( pluginName,
{
icons: pluginName,

init : function( editor )
{
var command = editor.addCommand( pluginName, insertTextPluginCmd );

editor.ui.addButton( 'InsertText',
{
label : 'Insert text Plugin',
command : pluginName,
toolbar : 'document',
icon : "link"
});
}
});
})();

0 comments on commit d368dc1

Please sign in to comment.