Skip to content

Commit

Permalink
feat: value change listener
Browse files Browse the repository at this point in the history
fixes #308
  • Loading branch information
F0rce committed Feb 27, 2023
1 parent a110d3f commit b77392c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/de/f0rce/signaturepad/SignaturePad.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ private void updateImage(ImageEncode event) {
this.imageUri = event.getImage();
this.type = event.getType();
this.isEmpty = event.isEmpty();

if (!this.imageUri.equals(event.getImage())) {
this.fireEvent(
new ValueChangedEvent(
event.getSource(), true, event.getImage(), event.getType(), event.isEmpty()));
}
}

/** Clears the widget. */
Expand Down Expand Up @@ -363,7 +369,11 @@ public boolean isReadOnly() {
*/
public void setImage(String uri) {
this.getElement().setProperty("img", uri);
if (!this.imageUri.equals(uri)) {
this.fireEvent(new ValueChangedEvent(this, false, uri, this.type, false));
}
this.imageUri = uri;
this.isEmpty = false;
}

/**
Expand Down Expand Up @@ -426,4 +436,16 @@ public void setClearButtonVisible(boolean clearButtonVisible) {
public boolean isClearButtonVisible() {
return this.clearButtonVisible;
}

/**
* Add a listener to the editor, which listens to when the value changed.
*
* <p>Check {@link ValueChangedEvent} for all available returned values.
*
* @param listener {@link ComponentEventListener}
* @return {@link Registration}
*/
public Registration addValueChangeListener(ComponentEventListener<ValueChangedEvent> listener) {
return this.addListener(ValueChangedEvent.class, listener);
}
}
64 changes: 64 additions & 0 deletions src/main/java/de/f0rce/signaturepad/ValueChangedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package de.f0rce.signaturepad;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import com.vaadin.flow.component.ComponentEvent;

/** @author David "F0rce" Dodlek */
public class ValueChangedEvent extends ComponentEvent<SignaturePad> {

/** */
private static final long serialVersionUID = 1280906391516162475L;

private String image;
private String type;
private boolean isEmpty;

public ValueChangedEvent(
SignaturePad source, boolean fromClient, String image, String type, boolean isEmpty) {
super(source, fromClient);
this.image = image;
this.type = type;
this.isEmpty = isEmpty;
}

/**
* Returns the dataUrl of the encoded image.
*
* @return {@link String}
*/
public String getImage() {
return this.image;
}

/**
* Returns the dataurl of the encoded image as Base64 decoded byte array.
*
* @return byte[]
*/
public byte[] getImageBase64() {
if (this.image.equals("") || this.isEmpty) {
return null;
}
String split = this.image.split(",")[1];
return Base64.getDecoder().decode(split.getBytes(StandardCharsets.UTF_8));
}

/**
* Returns the type, the image has been encoded with.
*
* @return {@link String}
*/
public String getType() {
return this.type;
}

/**
* Returns if the signature is empty.
*
* @return boolean
*/
public boolean isEmpty() {
return this.isEmpty;
}
}

0 comments on commit b77392c

Please sign in to comment.