Skip to content

Commit

Permalink
Show header and/or footer
Browse files Browse the repository at this point in the history
  • Loading branch information
mgroeneweg committed Jun 28, 2023
1 parent 99ba057 commit 1a9ffc3
Show file tree
Hide file tree
Showing 150 changed files with 8,853 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Any content with fixed position will be repeated on every page in the generated
## Usage
- Place the widget on the page
- Put your content in the header, body and footer drop zones
- If you only need a header **or** a footer, leave the unused drop zone empty and set the property to only use what you need
- When the PDF is generated the header and footer are repeated on each page

76 changes: 53 additions & 23 deletions src/DocumentLayoutWidget.editorConfig.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { hidePropertyIn } from "@mendix/pluggable-widgets-tools";

/**
* @typedef Property
* @type {object}
Expand Down Expand Up @@ -48,33 +50,61 @@
*/
export function getProperties(values, defaultProperties, target) {
// Do the values manipulation here to control the visibility of properties in Studio and Studio Pro conditionally.
/* Example
if (values.myProperty === "custom") {
delete defaultProperties.properties.myOtherProperty;
if (values.headerFooter === "header") {
hidePropertyIn(defaultProperties, values, "footerContent");
hidePropertyIn(defaultProperties, values, "footerHeight");
}
if (values.headerFooter === "footer") {
hidePropertyIn(defaultProperties, values, "headerContent");
hidePropertyIn(defaultProperties, values, "headerHeight");
}
*/
return defaultProperties;
}

// /**
// * @param {Object} values
// * @returns {Problem[]} returns a list of problems.
// */
// export function check(values) {
// /** @type {Problem[]} */
// const errors = [];
// // Add errors to the above array to throw errors in Studio and Studio Pro.
// /* Example
// if (values.myProperty !== "custom") {
// errors.push({
// property: `myProperty`,
// message: `The value of 'myProperty' is different of 'custom'.`,
// url: "https://github.com/myrepo/mywidget"
// });
// }
// */
// return errors;
// }
/**
* @param {Object} values
* @returns {Problem[]} returns a list of problems.
*/
export function check(values) {
/** @type {Problem[]} */
const errors = [];

const headerHeightValue = values.headerHeight ? Number(values.headerHeight) : 0;
if (values.headerFooter === "both" || values.headerFooter === "header") {
if (!values.headerHeight || headerHeightValue <= 0) {
errors.push({
property: "headerHeight",
message: "Header height must be set and positive"
});
}

if (!values.headerContent || values.headerContent.widgetCount === 0) {
errors.push({
property: "headerContent",
message: "Place content in the header dropzone"
});
}
}

const footerHeightValue = values.footerHeight ? Number(values.footerHeight) : 0;
if (values.headerFooter === "both" || values.headerFooter === "footer") {
if (!values.footerHeight || footerHeightValue <= 0) {
errors.push({
property: "footerHeight",
message: "Footer height must be set and positive"
});
}

if (!values.footerContent || values.footerContent.widgetCount === 0) {
errors.push({
property: "footerContent",
message: "Place content in the footer dropzone"
});
}
}

return errors;
}

// /**
// * @param {object} values
Expand Down
58 changes: 33 additions & 25 deletions src/DocumentLayoutWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./ui/DocumentLayoutWidget.css";
import { createElement } from "react";

export function DocumentLayoutWidget(props) {
const { headerContent, bodyContent, footerContent, headerHeight, footerHeight } = props;
const { headerFooter, headerContent, bodyContent, footerContent, headerHeight, footerHeight } = props;

const className = "document-layout-widget " + props.class;

Expand All @@ -27,38 +27,46 @@ export function DocumentLayoutWidget(props) {
return (
<div className={className}>
<table className="layout-table">
<thead>
<tr>
<td>
<div className="header-space" style={headerStyle}>
&nbsp;
</div>
</td>
</tr>
</thead>
{(headerFooter === "header" || headerFooter === "both") && (
<thead>
<tr>
<td>
<div className="header-space" style={headerStyle}>
&nbsp;
</div>
</td>
</tr>
</thead>
)}
<tbody>
<tr>
<td>
<div className="content">{bodyContent}</div>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>
<div className="footer-space" style={footerStyle}>
&nbsp;
</div>
</td>
</tr>
</tfoot>
{(headerFooter === "footer" || headerFooter === "both") && (
<tfoot>
<tr>
<td>
<div className="footer-space" style={footerStyle}>
&nbsp;
</div>
</td>
</tr>
</tfoot>
)}
</table>
<div className="header" style={headerStyle}>
{headerContent}
</div>
<div className="footer" style={footerStyle}>
{footerContent}
</div>
{(headerFooter === "header" || headerFooter === "both") && (
<div className="header" style={headerStyle}>
{headerContent}
</div>
)}
{(headerFooter === "footer" || headerFooter === "both") && (
<div className="footer" style={footerStyle}>
{footerContent}
</div>
)}
</div>
);
}
13 changes: 11 additions & 2 deletions src/DocumentLayoutWidget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
<studioProCategory>Document generation</studioProCategory>
<properties>
<propertyGroup caption="General">
<property key="headerContent" type="widgets">
<property key="headerFooter" type="enumeration" defaultValue="both">
<caption>Header/Footer</caption>
<description></description>
<enumerationValues>
<enumerationValue key="both">Both</enumerationValue>
<enumerationValue key="header">Header</enumerationValue>
<enumerationValue key="footer">Footer</enumerationValue>
</enumerationValues>
</property>
<property key="headerContent" type="widgets" required="false">
<caption>Header content</caption>
<description></description>
</property>
<property key="bodyContent" type="widgets">
<caption>Body content</caption>
<description></description>
</property>
<property key="footerContent" type="widgets">
<property key="footerContent" type="widgets" required="false">
<caption>Footer content</caption>
<description></description>
</property>
Expand Down
Binary file modified test/TestDocLayoutWidget.mpr
Binary file not shown.
65 changes: 65 additions & 0 deletions test/javasource/communitycommons/DateTime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package communitycommons;

import communitycommons.proxies.DatePartSelector;
import static communitycommons.proxies.DatePartSelector.day;
import static communitycommons.proxies.DatePartSelector.month;
import static communitycommons.proxies.DatePartSelector.year;
import java.util.Date;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Calendar;

public class DateTime {

/**
* @author mwe
* @author res
* @param firstDate The begin of the period
* @param compareDate The end of the period
* @return The period between the firstDate in the system default timezone, and the compareDate in the system
* default timezone as a Java Period
*
* Code is based on http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java
*
* Adjusted to Java 8 APIs (April, 2019)
*/
public static Period periodBetween(Date firstDate, Date compareDate) {
return Period.between(toLocalDate(firstDate), toLocalDate(compareDate));
}

private static LocalDate toLocalDate(Date someDate) {
return someDate.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
}

public static long dateTimeToInteger(Date date, DatePartSelector selectorObj) {
Calendar newDate = Calendar.getInstance();
newDate.setTime(date);
int value = -1;
switch (selectorObj) {
case year:
value = newDate.get(Calendar.YEAR);
break;
case month:
value = newDate.get(Calendar.MONTH) + 1;
break; // Return starts at 0
case day:
value = newDate.get(Calendar.DAY_OF_MONTH);
break;
default:
break;
}
return value;
}

public static long dateTimeToLong(Date date) {
return date.getTime();
}

public static Date longToDateTime(Long value) {
return new Date(value);
}
}
58 changes: 58 additions & 0 deletions test/javasource/communitycommons/ImmutablePair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package communitycommons;

import org.apache.commons.lang3.builder.HashCodeBuilder;


public class ImmutablePair<T, U>
{
public static <T, U> ImmutablePair<T, U> of(T left, U right) {
return new ImmutablePair<T, U>(left, right);
}

private final T left;
private final U right;

private ImmutablePair(T left, U right) {
if (left == null)
throw new IllegalArgumentException("Left is NULL");
if (right == null)
throw new IllegalArgumentException("Right is NULL");

this.left = left;
this.right = right;
}

public T getLeft() {
return left;
}

public U getRight() {
return right;
}

@Override
public String toString() {
return "<" + left.toString()+ "," + right.toString() + ">";
}

@Override
public boolean equals(Object other) {
if (!(other instanceof ImmutablePair<?,?>))
return false;

if (this == other)
return true;

ImmutablePair<?,?> o = (ImmutablePair<?, ?>) other;
return left.equals(o.getLeft()) && right.equals(o.getRight());
}

@Override
public int hashCode() {
return new HashCodeBuilder(19, 85)
.append(left)
.append(right)
.toHashCode();
}

}
Loading

0 comments on commit 1a9ffc3

Please sign in to comment.