Skip to content

Commit

Permalink
Implement custom OpenDialogAction
Browse files Browse the repository at this point in the history
 - Adds a nodeNameValidator to the field jcrName if it is present in the dialog (must be unique)
 - allows overriding commit/close actions
 - throw if dialogId is not a form dialog (instead of just not opening the dialog without any log)
  • Loading branch information
eschleb committed Apr 2, 2024
1 parent ff2983d commit 417f147
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,33 @@
import info.magnolia.ui.UIComponent;
import info.magnolia.ui.ValueContext;
import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
import info.magnolia.ui.contentapp.action.CloseActionDefinition;
import info.magnolia.ui.contentapp.action.CommitActionDefinition;
import info.magnolia.ui.dialog.DialogDefinition;
import info.magnolia.ui.dialog.DialogDefinitionRegistry;
import info.magnolia.ui.dialog.FormDialogDefinition;
import info.magnolia.ui.dialog.actions.OpenDialogAction;
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition;
import info.magnolia.ui.editor.LocaleContext;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;
import info.magnolia.ui.field.ConfiguredFieldDefinition;

import javax.inject.Inject;
import javax.jcr.Node;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.jcr.Node;

/**
* Adds a nodeNameValidator to the field jcrName if it is present in the dialog (must be unique).
* Adds a nodeNameValidator to the field jcrName if it is present in the dialog (must be unique) and allows overriding commit/close actions.
*/
public class JcrNameValidatingOpenDialogAction extends OpenDialogAction<Node> {
public class OpenDialogAction extends info.magnolia.ui.dialog.actions.OpenDialogAction<Node> {
private final NodeNameValidatorDefinition nodeNameValidatorDefinition;
private final Definition definition;

@Inject
public JcrNameValidatingOpenDialogAction(
public OpenDialogAction(
final Definition definition,
final LocaleContext localeContext,
final ValueContext<Node> valueContext,
Expand All @@ -36,15 +40,25 @@ public JcrNameValidatingOpenDialogAction(
final I18nizer i18nizer
) {
super(definition, localeContext, valueContext, parentView, i18NAuthoringSupport, dialogDefinitionRegistry, i18nizer);
this.definition = definition;
this.nodeNameValidatorDefinition = new NodeNameValidatorDefinition();
this.nodeNameValidatorDefinition.setMode(definition.getMode());
}

@Override
protected DialogDefinition getDialogDefinition(final DialogDefinitionRegistry dialogDefinitionRegistry, final I18nizer i18nizer) {
final DialogDefinition dialogDefinition = super.getDialogDefinition(dialogDefinitionRegistry, i18nizer);
final DialogDefinition dialogDefinition = dialogDefinitionRegistry.getProvider(getDefinition().getDialogId()).get();
if (dialogDefinition instanceof FormDialogDefinition) {
throw new IllegalArgumentException("Provided dialog id is not a form dialog!");
}
addNodeNameValidatorToJcrNameField(dialogDefinition);
return dialogDefinition;
Optional.ofNullable(definition.getCustomCommitAction()).ifPresent(commit ->
dialogDefinition.getActions().put(CommitActionDefinition.COMMIT_ACTION_NAME, commit) //we can't generate a new map due to byteBuddy (I18nizer), but since it is a mutable hash map this is fine
);
Optional.ofNullable(definition.getCustomCloseAction()).ifPresent(close ->
dialogDefinition.getActions().put(CloseActionDefinition.CLOSE_ACTION_NAME, close) //we can't generate a new map due to byteBuddy (I18nizer), but since it is a mutable hash map this is fine
);
return i18nizer.decorate(dialogDefinition);
}

protected void addNodeNameValidatorToJcrNameField(final DialogDefinition dialogDefinition) {
Expand All @@ -67,14 +81,36 @@ protected void addNodeNameValidatorToJcrNameField(final DialogDefinition dialogD

public static class Definition extends OpenDialogActionDefinition {
private final NodeNameValidatorDefinition.Mode mode;
@Nullable
private CommitActionDefinition customCommitAction;
@Nullable
private CloseActionDefinition customCloseAction;

public Definition(final NodeNameValidatorDefinition.Mode mode) {
setImplementationClass(JcrNameValidatingOpenDialogAction.class);
this.mode = mode;
setImplementationClass(OpenDialogAction.class);
}

public NodeNameValidatorDefinition.Mode getMode() {
return mode;
}

@Nullable //Optional doesn't work with byteBuddy
public CommitActionDefinition getCustomCommitAction() {
return customCommitAction;
}

public void setCustomCommitAction(final CommitActionDefinition customCommitAction) {
this.customCommitAction = customCommitAction;
}

@Nullable //Optional doesn't work with byteBuddy
public CloseActionDefinition getCustomCloseAction() {
return customCloseAction;
}

public void setCustomCloseAction(final CloseActionDefinition customCloseAction) {
this.customCloseAction = customCloseAction;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.namics.oss.magnolia.appbuilder.action.add;

import info.magnolia.cms.security.Permission;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;

import javax.annotation.Nullable;

import com.namics.oss.magnolia.appbuilder.MgnlIcon;
import com.namics.oss.magnolia.appbuilder.action.AppActionDefinition;
import com.namics.oss.magnolia.appbuilder.action.AvailabilityDefinitionBuilder;
import com.namics.oss.magnolia.appbuilder.action.OpenDialogAction;
import com.namics.oss.magnolia.appbuilder.action.rule.JcrIsNotDeletedRuleDefinition;
import com.namics.oss.magnolia.appbuilder.action.rule.PermissionRequiredRuleDefinition;
import info.magnolia.cms.security.Permission;
import info.magnolia.jcr.util.NodeTypes;
import info.magnolia.ui.api.action.ConfiguredActionDefinition;

import javax.annotation.Nullable;

public class AddAppActionDefinition implements AppActionDefinition {
public static final AddAppActionDefinition FOLDER = new AddAppActionDefinition(
Expand Down Expand Up @@ -49,9 +51,9 @@ public AddAppActionDefinition(
}

@Override
public ConfiguredActionDefinition action() {
final CreateNodeActionDefinition commitAction = new CreateNodeActionDefinition(nodeType);
final OpenCreateDialogActionDefinition definition = new OpenCreateDialogActionDefinition(commitAction);
public OpenDialogAction.Definition action() {
final OpenDialogAction.Definition definition = new OpenDialogAction.Definition(NodeNameValidatorDefinition.Mode.ADD);
definition.setCustomCommitAction(new CreateNodeActionDefinition(nodeType));
definition.setName(name);
definition.setDialogId(dialogId);
definition.setLabel(label);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.namics.oss.magnolia.appbuilder.action.edit;

import info.magnolia.cms.security.Permission;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;

import javax.annotation.Nullable;

import com.namics.oss.magnolia.appbuilder.MgnlIcon;
import com.namics.oss.magnolia.appbuilder.action.AppActionDefinition;
import com.namics.oss.magnolia.appbuilder.action.AvailabilityDefinitionBuilder;
import com.namics.oss.magnolia.appbuilder.action.JcrNameValidatingOpenDialogAction;
import com.namics.oss.magnolia.appbuilder.action.OpenDialogAction;
import com.namics.oss.magnolia.appbuilder.action.rule.JcrIsNotDeletedRuleDefinition;
import com.namics.oss.magnolia.appbuilder.action.rule.PermissionRequiredRuleDefinition;
import info.magnolia.cms.security.Permission;
import info.magnolia.ui.api.action.ConfiguredActionDefinition;
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;

import javax.annotation.Nullable;

public class EditAppActionDefinition implements AppActionDefinition {
public static final EditAppActionDefinition FOLDER = new EditAppActionDefinition(
Expand All @@ -38,8 +37,8 @@ public EditAppActionDefinition(final String name, final String dialogId, final S
}

@Override
public ConfiguredActionDefinition action() {
final OpenDialogActionDefinition definition = new JcrNameValidatingOpenDialogAction.Definition(NodeNameValidatorDefinition.Mode.EDIT);
public OpenDialogAction.Definition action() {
final OpenDialogAction.Definition definition = new OpenDialogAction.Definition(NodeNameValidatorDefinition.Mode.EDIT);
definition.setName(name);
definition.setDialogId(dialogId);
definition.setLabel(label);
Expand Down

0 comments on commit 417f147

Please sign in to comment.