Scripting: Difference between revisions

From Memento Database Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Stamp|2021-04-13-23|4.10.0|1.10.0}}
{{Stamp|2021-04-13-23|4.10.0|1.10.0}}


{{NoteLine| '''''THIS PAGE IS ACTIVELY UNDER DEVELOPMENT & WILL BE UPDATED DIRECTLY.''' Please be patient.''}}
== Native Memento Calculation scripting ==
== Native Memento Calculation scripting ==
Scripting for the Calculation field type is described in '''[[Calculation field]]'''.
Scripting for the Calculation field type is described in '''[[Calculation field]]'''.
Line 8: Line 9:


=== The '''[[JavaScript field]]''' type ===
=== The '''[[JavaScript field]]''' type ===
Like the Memento Calculation field type that game before it, the JavaScript field exists to produce ("return") a value calculated with an ''expression'', which is a formula consisting of ''constants'', ''variables'', ''operators'', and possibly ''statements''.
Like the Memento Calculation field type that came before it, the JavaScript field exists to produce ("return") a value calculated with an ''expression'' or formula. JavaScript fields use a somewhat simplified form of Memento JavaScript. For instance, it assumes the current entry for its context, while in full Memento JavaScript, the script must locate & provide the entry to operate on. While it can access any field in any library the current library is linked to, it doesn't have the complexity to access any field in any entry in any library, as you can in full Memento JavaScript. See [[How:Write scripts in JavaScript for Memento#JavaScript field]] for a detailed discussing of the JavaScript field.


==== Terminology ====
==== Example JavaScript field usage ====
:; Constant:a value that has a string (text) value or a numeric (number) value.
* The field() function gets the value of the field from Memento.
<small>
* Anything after "//" is ignored, so you can use it for comments.
::; String:"This is a string of textual characters"
* For numbers, x * y means the numerical ''product'' of x times y.
::; Number:123 or 3.14 or -28.5
* x = y means for Memento to assign to x the value of y.
::; Symbol:a name representing a constant value, like pi, x, tareWeight, or surname
* For numbers, x + y means the ''sum'' of x and y, while if x & y are strings of text, it means x ''concatenated'' with y.
</small>
:; Item price&nbsp;&nbsp;&nbsp;&nbsp;// ''on an invoice'':field("Unit price") * field("Quantity")
:; Variable:a name representing a value that may change over time, like ''sum'', ''currentValue'', or ''orderDate''
:; Amount for display&nbsp;&nbsp;&nbsp;&nbsp;// ''Final sum'': field("Total") + " widgets"
:; Operator: one or more characters that represent the value of other values that go together in some fashion to determine a result, like '''+''' for ''plus'' or '''-''' for ''minus'' or '''*''' for ''multiplied by'' or '''/''' for ''divided by''. :::Some operators are made up of other operators used in combination, like '''<=''' ''for less than or equal to'' or '''&&''' for ''and also''.
:; Limited&nbsp;&nbsp;&nbsp;&nbsp;// ''The capped amount'': value = field("Value"); if (value <= StandardLimit) value else StandardLimit
 
==== JavaScript field script examples ====
:123; any numeric constant


=== JavaScript scripting for [[Triggers|'''Memento Triggers''']] ===
=== JavaScript scripting for [[Triggers|'''Memento Triggers''']] ===

Revision as of 08:12, 24 April 2021

« Page as of 2021-04-13-23, editions Mobile 4.10.0, Desktop 1.10.0 »


« NOTE:  THIS PAGE IS ACTIVELY UNDER DEVELOPMENT & WILL BE UPDATED DIRECTLY. Please be patient. »


Native Memento Calculation scripting

Scripting for the Calculation field type is described in Calculation field.

Scripting Memento in JavaScript

Scripting in JavaScript is available in JavaScript field and Triggers.

The JavaScript field type

Like the Memento Calculation field type that came before it, the JavaScript field exists to produce ("return") a value calculated with an expression or formula. JavaScript fields use a somewhat simplified form of Memento JavaScript. For instance, it assumes the current entry for its context, while in full Memento JavaScript, the script must locate & provide the entry to operate on. While it can access any field in any library the current library is linked to, it doesn't have the complexity to access any field in any entry in any library, as you can in full Memento JavaScript. See How:Write scripts in JavaScript for Memento#JavaScript field for a detailed discussing of the JavaScript field.

Example JavaScript field usage

  • The field() function gets the value of the field from Memento.
  • Anything after "//" is ignored, so you can use it for comments.
  • For numbers, x * y means the numerical product of x times y.
  • x = y means for Memento to assign to x the value of y.
  • For numbers, x + y means the sum of x and y, while if x & y are strings of text, it means x concatenated with y.
Item price    // on an invoice
field("Unit price") * field("Quantity")
Amount for display    // Final sum
field("Total") + " widgets"
Limited    // The capped amount
value = field("Value"); if (value <= StandardLimit) value else StandardLimit

JavaScript scripting for Memento Triggers

TBD

moment.min.js — a possibly useful wrapper for JavaScript dates & times

See http://momentjs.com and its subordinate pages Guides and Docs.

Tidbit
To get the current date, use moment().toDate(). To get the current date & time, use moment().toDate().getTime().

TBD