diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java new file mode 100644 index 0000000..d610ece --- /dev/null +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java @@ -0,0 +1,105 @@ +package uk.gov.laa.ccms.springboot.dialect; + +import java.util.Map; +import org.thymeleaf.context.ITemplateContext; +import org.thymeleaf.model.IModel; +import org.thymeleaf.model.IModelFactory; +import org.thymeleaf.model.IProcessableElementTag; +import org.thymeleaf.processor.element.AbstractElementTagProcessor; +import org.thymeleaf.processor.element.IElementTagStructureHandler; +import org.thymeleaf.templatemode.TemplateMode; + +/** + * hello. + */ +public class DatePickerElementTagProcessor extends AbstractElementTagProcessor { + + private static final String TAG_NAME = "datepicker"; + private static final int PRECEDENCE = 900; + + public DatePickerElementTagProcessor() { + super(TemplateMode.HTML, "moj", TAG_NAME, true, null, false, PRECEDENCE); + } + + @Override + protected void doProcess(ITemplateContext context, IProcessableElementTag tag, + IElementTagStructureHandler structureHandler) { + + Map attributes = ProcessorUtils.parseAttributes(context, tag); + + String id = attributes.getOrDefault("id", "date"); + String name = attributes.getOrDefault("name", "date"); + String value = attributes.get("value"); + String label = attributes.getOrDefault("label", "Date"); + String hint = attributes.getOrDefault("hint", "For example, 17/5/2024."); + String errorMessage = attributes.getOrDefault("errorMessage", "Enter or select a date"); + String minDate = attributes.get("minDate"); + String maxDate = attributes.get("maxDate"); + + boolean hasError = !errorMessage.isEmpty(); + + String datePickerHtml = + buildDatePickerHtml(id, name, label, hint, errorMessage, hasError, minDate, maxDate); + + final IModelFactory modelFactory = context.getModelFactory(); + final IModel model = modelFactory.parse(context.getTemplateData(), datePickerHtml); + structureHandler.replaceWith(model, false); + + } + + private String buildDatePickerHtml(String id, String name, String label, String hint, + String errorMessage, boolean hasError, String minDate, + String maxDate) { + StringBuilder html = new StringBuilder(); + html.append("
") + .append("
") + .append("") + .append("
") + .append(hint) + .append("
"); + + if (hasError) { + html.append("

") + .append("Error: ") + .append(errorMessage) + .append("

"); + } + + html.append("") + .append("
") + .append("
"); + + return html.toString(); + } +} diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/GovUkThymeleafDialectConfig.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/GovUkThymeleafDialectConfig.java index dec055b..edfa956 100644 --- a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/GovUkThymeleafDialectConfig.java +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/GovUkThymeleafDialectConfig.java @@ -19,4 +19,9 @@ public GovUkDialect govUkDialect() { return new GovUkDialect(); } + @Bean + public MojCustomDialect mojCustomDialect() { + return new MojCustomDialect(); + } + } diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/MojCustomDialect.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/MojCustomDialect.java new file mode 100644 index 0000000..039129a --- /dev/null +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/MojCustomDialect.java @@ -0,0 +1,17 @@ +package uk.gov.laa.ccms.springboot.dialect; + +import java.util.Set; +import org.thymeleaf.dialect.AbstractProcessorDialect; +import org.thymeleaf.processor.IProcessor; + +public class MojCustomDialect extends AbstractProcessorDialect { + + public MojCustomDialect() { + super("MOJ Custom Dialect", "moj", 1000); + } + + @Override + public Set getProcessors(String dialectPrefix) { + return Set.of(new DatePickerElementTagProcessor()); + } +} diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/test/java/uk/gov/laa/ccms/springboot/dialect/MoJDatePickerElementTagProcessorTest.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/test/java/uk/gov/laa/ccms/springboot/dialect/MoJDatePickerElementTagProcessorTest.java new file mode 100644 index 0000000..09340b1 --- /dev/null +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/test/java/uk/gov/laa/ccms/springboot/dialect/MoJDatePickerElementTagProcessorTest.java @@ -0,0 +1,7 @@ +package uk.gov.laa.ccms.springboot.dialect; + +import static org.junit.jupiter.api.Assertions.*; + +class MoJDatePickerElementTagProcessorTest { + +} \ No newline at end of file