The code
tag is available in every field type and offers the option of implementing the output of a field as required using JavaScript.
You can find more examples of fields or an introduction to JavaScript in the Layouter manual.
Calls in code
Within the <Code>
element, two variants are possible for executing code.
Shortened call
If the code consists of only one statement, it can be written directly into the block.
The
return
statement is implicit.
<Text Name="Footer"> <Code>$.getText('Profile.User.FirstName')</Code> </Text>
Detailed call
If the code consists of several statements and/or contains variables, it must be written within the
function main()
.The
return
statement is explicit.
<Text Name="Footer"> <Code> function main() { let firstName = $.getString('Profile.User.FirstName'); if (firstName == 'Foo') { firstName += 'Bar'; } return firstName; } </Code> </Text>
API description
In a code
element $
initiates the use of the primedocs API.
Access to fields
Forms fields, fields and profile fields can be accessed with $('[field]')
. Below is an example for each field type:
Forms field |
|
Field |
|
Profile field: User |
|
Profile field: Organisation |
|
Function calls
Furthermore, $.
initiates the call of a primedocs function: $.myFunction()
It is possible to use general JavaScript functions (e.g. foreach()
on array
s (ObjectCollections) or replace()
on string
s). Consult Mozilla's JavaScript documentation for more information.
Return value of field types
The return value must always correspond to the field in terms of data type. For example, it is not possible to output a date
forms field in a text
field. Conversely, a date field cannot output string/text but only date
forms fields or date
fields.
Here is an overview:
Field type | Return type |
---|---|
Text |
|
FormattedText |
|
WordContent |
|
Date |
|
YesNo |
|
Picture | Image (z. B. |
Functions
The use of functions works according to JavaScript standards:
most functions require one or more arguments, which are specified using parameters. The parameters have a parameter type.
Each function has an explicit return type.
Overview of all primedocs functions
The following is a sortable table with all primedocs' own functions: which API they belong to, the target field type (in which field type they may be returned) and the respective return type.
API | Function | Return type | Target field type |
---|---|---|---|
getText() | String | Text-based (Text, FormattedText, WordContent) | |
getDateAsString() | String | Text-based | |
joinNonEmpty() | String | Text-based | |
formatDate() | String | Text-based | |
getDate() | Date | All except YesNo | |
setDate() | void | All except YesNo | |
formattedText | formattedText.fromText() | FormattedText | FormattedText |
wordContent | wordContent.fromFormattedText() | WordContent | WordContent |
wordContent | wordContent.fromText() | WordContent | WordContent |
translations | translations.getText() | String | Text |
translations | translations.getFormattedText() | FormattedText | FormattedText |
snippets | snippets.getFormattedText() | FormattedText | FormattedText |
snippets | snippets.getWordContent() | WordContent | WordContent |
formattedText | formattedText.getBuilder() | FormattedText | FormattedText |
wordContent | wordContent.getBuilder() | WordContent | WordContent |
builder | append() | void | Text, FormattedText, WordContent |
builder | build() | void | Text, FormattedText, WordContent |
Functions with named parameters
Some functions contain an object with a comma-separated list of key-value pairs as the second parameter. We call the key-value pairs ‘named parameters’.
The values of the key-value pairs can be any type of string. This includes
Calling profile data / Forms fields / Fields, see below Key
name
Fixed text, see below Key
info
References to variables, see below Key
function
In the following example, a global translation of type FormattedText
is returned in a field header
. The function call to getFormattedText
requires the ID of the global translation and, as a second parameter, a list of three key-value pairs with the keys name
, info
and function
.
<FormattedText Name="Header"> <Code> function main(){ let myVariable = $("Forms.SignerMain.User.Function"); return $.translations.getFormattedText("FormattedTexts.FooterBoldWithParams", { name: $("Forms.SignerMain.User.FirstName") + " " + $("SignerMainLastName"), info: "Fixtext ist auch möglich", function: myVariable }); } </Code> </FormattedText>
Explanation of all functions per API
Each function is described in detail below:
Function | Purpose |
---|---|
getText(String id) |
<Text Name="FirstName2"><!-- explicit--> <Code>$.getText('Profile.User.FirstName')</Code> </Text> <Text Name="FirstName1"><!-- implicit --> <Code>$('Profile.User.FirstName')</Code> </Text> |
getDateAsString(String id) |
<Text Name="Date"> <Code>$.getDateAsString("Forms.Date")</Code> </Text> |
joinNonEmpty(String separator, String item1, String item2, […]) |
More information here: https://primesoft-group.atlassian.net/wiki/spaces/PD/pages/edit-v2/395444225#Funktion-joinNonEmpty() (german) |
formatDate(Date date, String format) |
|
getDate() | Gets a date object so that you can calculate with it. Return type:
See example: https://primesoft-group.atlassian.net/wiki/spaces/PD/pages/edit-v2/395444225#Beispiel%3A-60-Tage-hinzuf%C3%BCgen (german) |
setDate(Date date) | Sets a new value for a date object. No return type.
See example: https://primesoft-group.atlassian.net/wiki/spaces/PD/pages/edit-v2/395444225#Beispiel%3A-60-Tage-hinzuf%C3%BCgen (german) |
formattedText API
Function | Purpose |
---|---|
formattedText.fromText(String text) |
Example with a string:
Example with reference to a field:
|
formattedText.getBuilder() | Creates a builder object in order to append FormattedText or Text to it using the
|
wordContent API
Function | Purpose |
---|---|
wordContent.fromFormattedText(FormattedText ft) |
Example with FormattedText from translation:
Example with FormattedText from snippets
Example with reference to field:
|
wordContent.fromText(String text) |
Example with a string:
Example with reference to a field:
|
wordContent.getBuilder() | Creates a builder object in order to append WordContent, FormattedText or Text to it using the
|
translations API
The following functions are available in the translations group:
Function | Purpose |
---|---|
translations.getText(String id) |
|
translations.getFormattedText(String id, Object parameters) | Retrieves a formatted translation from the global translations.
Example without
Example with
|
snippets API
The following functions are available in the snippets
group:
Function | Purpose |
---|---|
snippets.getWordContent(String key, Object placeholders) |
SnippetPlaceholder can only be filled with strings. Example without
Example with
|
snippets.getFormattedText(String key) |
|
Builder API
The Builder API can be used in a FormattedText
field or a WordContent
field and enables the assembly of Text
s, FormattedTexts
and WordContent
s in the sense of a modular system, in which each text, regardless of type, is strung together.
Use
The use of the Builder only makes sense if several text paragraphs have to be strung together conditionally.
Functions
Function | Purpose |
---|---|
formattedText.getBuilder() |
|
wordContent.getBuilder() |
|
append(WordContent/FormattedText content) |
See detailed example further below |
build() |
|
Application
The following rules apply when using the Builder:
A
FormattedText
field can only output FormattedText and Text.A
WordContent
field can output WordContent, FormattedText and Text.
The non-field type objects must first be converted to the target field type. For example, in a WordContent
field, a FormattedText
must be called from the conversion function fromFormattedText()
:
.append($.wordContent.fromFormattedText($.translations.getFormattedText('FormattedTexts.CopyTo')))
The same for WordContent
fields or FormattedText
fields and the output of text:
.append($.formattedText.fromText(' - with Builder'))
Example: Builder for WordContent field and FormattedText field
<FieldsConfiguration> <Fields> <WordContent Name="WCSnippetBuilder"> <Code>$.wordContent.getBuilder() // Create builder object .append($.snippets.getWordContent("Introduction")) // Attach WordContent .append($.wordContent.fromFormattedText($.translations.getFormattedText("FormattedTexts.CopyTo"))) // FormattedText as glob. Attach translation .append($.wordContent.fromText(" - mit Builder")) // Text anhängen .build() // Alles zusammenbauen </Code> </WordContent> <FormattedText Name="FTSnippetBuilder"> <Code>$.formattedText.getBuilder() // Create builder object .append($.snippets.getFormattedText("IntroductionFT")) // FormattedText Attach as snippet .append($.translations.getFormattedText("FormattedTexts.CopyTo")) // FormattedText as glob. Attach translation .append($.formattedText.fromText(" - mit Builder")) // Attach text .build() // Assemble everything </Code> </FormattedText> </Fields> </FieldsConfiguration>
Use customised functions
With GlobalCode
, specially defined functions can also be used in a code definition. For more information, read here: https://primesoft-group.atlassian.net/wiki/spaces/PDTEN/pages/61407710/Global+Configurations#GlobalCode .
CDATA-Tag
The CDATA tag (<![CDATA[My text]]>)
ensures that everything between the start and end tag does not go through the parser. This is called ‘escaping’.
Without a CDATA tag, for example, the logical AND operator must be output as an HTML entity: &
. By using the CDATA tag, you can only use &
, which results in more readable code.
We recommend the use of CDATA tags if the code in the field contains the following:
logical AND operators
&
Comparison operators (
<
,<=
,>
,>=
), e.g. in loopsArrow expressions (
=>
)
Example with CDATA tag and &
instead of &
<FieldsConfiguration> <Fields> <YesNo Name="IsPresident"> <Code><![CDATA[$("Profile.User.Function") === "President" && !$("Profile.Org.Unit")]]></Code> </YesNo> <YesNo Name="IsPresidentNoCDATA"> <Code>$("Profile.User.Function") === "President" && !$("Profile.Org.Unit")</Code> </YesNo> </Fields> </FieldsConfiguration>