JavaScript field

Revision as of 14:59, 23 August 2016 by UnConnoisseur (talk | contribs) (Created page with "The value of the field will depend on the result of execution of the script preset in the settings of the field and executed in the entry context. '''JavaScript 1.7''' is use...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The value of the field will depend on the result of execution of the script preset in the settings of the field and executed in the entry context.

JavaScript 1.7 is used for scripts. The current version supports only core methods of the language.

A script may consist of multiple operations but only the result of the last operation will be entered into the field. Example:

var i = 1 + 1
i + 1
Result will be: 3

Fields of an entry

Function field("[field name]") is used to get access to the values of other entry fields. The type of the JavaScript object returned by the function depends on the field type.

Compatibility of Memento field types and JavaScript objects is shown in the table:

Field type JavaScript object
Text , Hyperlink, Password, Phone, Email, Calculation (string result) , Single-choice list , Radio buttons String
Integer, Real number, Rating, Currency, Calculation (number result) Number
Date , Date/Time, Time Date
Boolean Boolean
Multiple-choice list, Checkboxes Array of String
Link to entry Array of entries. Each array element has method field for access to values of linked entry.

Time of script execution

A script can be executed both when an entry is created or changed and when it is displayed.
The time of script execution is set by the toggle button Execute script real time on the field edit screen. It is reasonable to enable this option if your script contains functions that depend on the current time. Online execution in case of a large number of entries can significantly increase the time required to open the library.

Functions to work with the date

You can enable integrated library moment.js to work with the date and time. Click JavaScript Libraries and choose moment.min.js.

Some examples. If record contains the data field whose value is 18 March 2016 14:55

Example of Format dates

moment(field('Date')).format('MMMMM Do YYYYY') //March 18th 2016
moment(field('Date')).format('dddd') //Friday
moment(field('Date')).format('dddd [Escaped] YYYY') //Friday Escaped 2016
moment(field('Date')).format('HH:mm') //14:55

Example of Relative Time

moment(field('Date')).fromNow() //11 minutes ago
moment(field('Date')).startOf('day').fromNow() //15 hours ago

Example of Calendar Time

moment(field('Date')).calendar() //Today at 2:55 PM
moment(field('Date')).add(1 , 'days').calendar() //Tomorrow at 2:55 PM

More information about library features on http://momentjs.com

Examples

Example: Sum

Entries contain fields: Price, Count. We add a JavaScript field "Sum" by using the following script:

field('Price')*field('Count')

Example: Percentage

Entries contain fields: Count, Total. We add a JavaScript field "Percentage" by using the following script:

var total = field('Total')
if (total > 0)
field('Count')*100/total + ' %'
else 'none'

Example: Days

Entries contain fields: StartDateTime, EndDateTime. We add a calculating field "Days" by using the following script:

moment(field('End')).diff(field('Start') , 'days')

Example: References

There’s library Order – with orders and library Item – with items. Library Order contains a link to library Item. Library Item contains field Price. To calculate the cost of the order, let’s add script field Sum using the following script:

var sum = 0
var items = field('Item')
for(var i in items) {
sum = sum + items[i].field('Price')
}

Example: Birthday

We have birthday field. If a person younger than 18 years we want displaying string "youth", else "adult".

var years = moment().diff(field('Birthday') , 'years' )
if (years < 18) 'Youth'
else 'Adult'

References

You can find a full description of the language here. The implementation is based on Rhino library.