Skip to content

Commit

Permalink
feat(registrar): add list and map application form items
Browse files Browse the repository at this point in the history
* Added support for new applicatoin form items.
* Both items have default input and add button. With this button, user can add new values to the list or map. Each new value in the list or map has also a remove button, so user can also remove the previously added value.
* ListBox and MapBox have new own validators, ListBox has one more validator for ssh keys.
  • Loading branch information
HejdaJakub committed May 19, 2022
1 parent 2f85624 commit 6e48ade
Show file tree
Hide file tree
Showing 16 changed files with 750 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public enum ApplicationFormItemType {
USERNAME,
HEADING,
TIMEZONE,
EMBEDDED_GROUP_APPLICATION
EMBEDDED_GROUP_APPLICATION,
LIST_INPUT_BOX,
MAP_INPUT_BOX
}

public enum Hidden {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface PerunRegistrarCss extends CssResource {

String help();

String overflow();

}

@Source("css/PerunRegistrar.gss")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ public interface PerunRegistrarTranslation extends PerunTranslation {
@DefaultMessage("Incorrect input format!")
public String incorrectFormat();

@DefaultMessage("The following items have incorrect format:")
public String incorrectFormatItemList();

@DefaultMessage("Items with the following keys have incorrect format:")
public String incorrectFormatItemMap();

@DefaultMessage("Each key must be unique!")
public String duplicateKeys();

@DefaultMessage("Item <b>can`t be empty!</b>")
public String cantBeEmpty();

Expand Down Expand Up @@ -211,29 +220,35 @@ public interface PerunRegistrarTranslation extends PerunTranslation {
@DefaultMessage("Key \"<i>{0}</i>\" does not have the correct format.")
String sshKeyFormat(String key);

@DefaultMessage("Do not mix commas and new-lines as SSH keys separators.")
String mixingNewlinesWithCommas();

@DefaultMessage("Multiple consecutive commas are not allowed, use a single comma or newline as a separator between SSH keys.")
String tooMuchCommas();
@DefaultMessage("Newlines are not allowed, use a single comma as a separator between SSH keys.")
String newlinesNotAllowed();

@DefaultMessage("Multiple consecutive commas are not allowed, use a single comma as a separator between SSH keys.")
String tooMuchCommasTextField();

@DefaultMessage("Multiple consecutive newlines are not allowed, use one newline or comma as a separator between SSH keys.")
String tooMuchNewlines();
String tooMuchCommas();

@DefaultMessage("Missing separator between the SSH keys (comma or newline).")
String sshKeyMissingDelimiter();

@DefaultMessage("Missing comma as a separator between the SSH keys.")
String sshKeyMissingCommaDelimiterTextField();

@DefaultMessage("No spaces are allowed around the SSH key separator (comma or newline).")
@DefaultMessage("No spaces are allowed around the SSH key separator (comma).")
String sshKeyNoSpaceAroundKeySeparator();

@DefaultMessage("No spaces are allowed around the SSH key separator (comma).")
String sshKeyNoSpaceAroundCommasTextField();
@DefaultMessage("No separators (commas) are allowed. Add new value for the new SSH key.")
String sshKeySeparatorNotAllowed();

@DefaultMessage("Add new value")
public String addNewValue();

@DefaultMessage("Remove value")
String removeValue();

@DefaultMessage("Enter new key")
public String enterKey();

@DefaultMessage("Enter value")
String enterValue();

// -------------- SUBMITTED APPS PAGE ------------------------ //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
import cz.metacentrum.perun.wui.registrar.widgets.items.Undefined;
import cz.metacentrum.perun.wui.registrar.widgets.items.Username;
import cz.metacentrum.perun.wui.registrar.widgets.items.ValidatedEmail;
import cz.metacentrum.perun.wui.registrar.widgets.items.ListBox;
import cz.metacentrum.perun.wui.registrar.widgets.items.MapBox;
import org.gwtbootstrap3.client.ui.constants.ValidationState;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -235,6 +236,10 @@ private PerunFormItem generatePerunFormItem(ApplicationFormItemData data) {
return new SubmitButton(form, data, lang, true);
case EMBEDDED_GROUP_APPLICATION:
return new GroupCheckBox(form, data, lang);
case LIST_INPUT_BOX:
return new ListBox(form, data, lang);
case MAP_INPUT_BOX:
return new MapBox(form, data, lang);
default:
return new Undefined(form, data, lang);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package cz.metacentrum.perun.wui.registrar.widgets.items;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.VerticalPanel;
import cz.metacentrum.perun.wui.json.Events;
import cz.metacentrum.perun.wui.registrar.client.resources.PerunRegistrarResources;
import cz.metacentrum.perun.wui.registrar.widgets.items.validators.ListBoxValidator;
import cz.metacentrum.perun.wui.registrar.widgets.items.validators.SshKeysListBoxValidator;
import cz.metacentrum.perun.wui.widgets.boxes.ExtendedTextBox;
import cz.metacentrum.perun.wui.widgets.PerunButton;
import cz.metacentrum.perun.wui.model.beans.ApplicationFormItemData;
import cz.metacentrum.perun.wui.registrar.widgets.PerunForm;
import org.gwtbootstrap3.client.ui.html.Paragraph;

import java.util.ArrayList;
import java.util.List;

/**
* Represents ListBox form item.
*
* @author Jakub Hejda <Jakub.Hejda@cesnet.cz>
*/
public class ListBox extends WidgetBox {

private final ListBoxValidator validator;
List<ExtendedTextBox> inputList;

public ListBox(PerunForm form, ApplicationFormItemData item, String lang) {
super(form, item, lang);
if ("urn:perun:user:attribute-def:def:sshPublicKey".equals(item.getFormItem().getPerunDestinationAttribute())) {
this.validator = new SshKeysListBoxValidator();
} else {
this.validator = new ListBoxValidator();
}
}

@Override
public String getValue() {
StringBuilder value = new StringBuilder();
if (isOnlyPreview()) {
for(int i = 0; i < getPreview().getWidgetCount(); i++) {
Paragraph p = (Paragraph) getPreview().getWidget(i);
value.append((p.getText() == null || p.getText().isEmpty())
? ""
: p.getText().replace(",", "\\,") + ",");
}
} else {
for (ExtendedTextBox input : inputList) {
value.append((input.getValue() == null || input.getValue().isEmpty())
? ""
: input.getValue().replace(",", "\\,") + ",");
}
}

return value.toString();
}

public List<ExtendedTextBox> getListValue() {
return inputList;
}

@Override
public void validate(Events<Boolean> events) {
validator.validate(this, events);
}

@Override
public boolean validateLocal() {
return validator.validateLocal(this);
}

@Override
protected Widget initWidget() {
inputList = new ArrayList<>();
return super.initWidget();
}

@Override
protected Widget initWidgetOnlyPreview() {
widget = new Paragraph();
Paragraph p = new Paragraph();
p.addStyleName("form-control");
getPreview().add(p);
return widget;
}

@Override
protected void setValidationTriggers() {
if (isOnlyPreview()) {
return;
}
for (ExtendedTextBox input : inputList) {
input.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
validateLocal();
}
});
}
}

@Override
protected void setValueImpl(String value) {
int counter = 0;
if (isOnlyPreview()) {
// delete default empty widget if there is some value
getPreview().getWidget(0).removeFromParent();
}
for (String val : value.split("(?<!\\\\),+?")) {
if (isOnlyPreview()) {
Paragraph p = new Paragraph();
p.setText(val.trim().replace("\\,", ","));
p.addStyleName("form-control");
p.addStyleName(PerunRegistrarResources.INSTANCE.gss().overflow());
p.setHeight("auto");
getPreview().add(p);
} else {
if (counter != 0) {
generateItemWithRemoveButton((VerticalPanel) widget);
}
inputList.get(counter).setValue(val.trim().replace("\\,", ","));
}
counter++;
}
}

@Override
protected PerunButton generateAddButton(VerticalPanel vp) {
return new PerunButton("", new ClickHandler() {
public void onClick(ClickEvent event) {
generateItemWithRemoveButton(vp);
}
});
}

@Override
protected void generateItemWithRemoveButton(VerticalPanel vp) {
HorizontalPanel hp = new HorizontalPanel();
hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
hp.setWidth("100%");
ExtendedTextBox input = new ExtendedTextBox();

if (getItemData().getFormItem().getRegex() != null) {
input.setRegex(getItemData().getFormItem().getRegex());
}

inputList.add(input);
PerunButton removeButton = new PerunButton("", new ClickHandler() {
public void onClick(ClickEvent event) {
inputList.remove(input);
vp.remove(hp);
validateLocal();
}
});
setupRemoveButton(removeButton);
hp.add(input);
hp.add(removeButton);
setValidationTriggers();
setupHPandAddToVP(hp, vp, removeButton);
}

}
Loading

0 comments on commit 6e48ade

Please sign in to comment.