Triggers: Difference between revisions

246 bytes added ,  6 October 2016
m
no edit summary
mNo edit summary
mNo edit summary
Line 1: Line 1:
{{Stamp|2016-10-06|4.0.0|1.0.5}}
{{Stamp|2016-10-06|4.0.0|1.0.5}}


A Trigger is a script that defines the processing of an entry based on an Event that has taken place. Trigger scripts are written in [https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]. When a trigger script is executed, it may change an existing entry, create a new entry, execute an http request, create a file, perform data validation, etc.
A Trigger is a script that defines the processing of an entry based on an event that is takimg place. Trigger scripts are written in [https://developer.mozilla.org/en-US/docs/web/JavaScript JavaScript]. When a trigger script is executed, it may perform actions like changing an existing entry, creating a new entry, executing an http request, creating a file, performing data validation, etc.


=== Terminology ===
=== Terminology ===
Line 14: Line 14:
::* Removing an entry from Favorites
::* Removing an entry from Favorites


:; Event and Phase
:; Phase of an Event
:: One of a predefined set of moments (Event type & Phase) during entry processing during which the user can intervene via a trigger script. See the table of events below.<br/> The Event type and the Phase determine the trigger script to be run.
:: One of a predefined set of moments during entry processing during which the user can intervene via a trigger script. See the table of events and phases below.<br/> The Event type and the Phase determine the trigger script to be run.


:; Trigger or Trigger Script
:; Trigger or Trigger Script
Line 24: Line 24:


; Synchronous script execution mode
; Synchronous script execution mode
: implies the application suspends user interaction and then executes the script. It is not recommended to perform time-consuming operations in this case.
: The application suspends user interaction and then executes the script. Time-consuming operations are not recommended in this mode.


; Asynchronous script execution mode
; Asynchronous script execution mode
: results in the script running in the background; user interaction is not suspended. Usually, asynchronous scripts are used in the last phases of the action.
: The script runs in the background; user interaction is '''not''' suspended. Usually, asynchronous scripts are used in the last phases of the action.


== Creating a trigger ==
== Creating a trigger ==
Each library can have a number of triggers &mdash; up to one for each Event. To see the list of triggers, open the library, open the menu, and then select ''Triggers''.
Each library can have a number of triggers &mdash; up to one for each Event & Phase. To see the list of triggers, open the library, open the menu, and then select ''Triggers''.


To create a trigger, open the library's list of triggers and click <big>'''+'''</big>. You must then identify the Event and write a trigger script that performs the necessary actions.
To create a trigger, open the library's list of triggers and click <big>'''+'''</big>. You must then identify the Event & Phase and write a trigger script that performs the necessary actions.


== Events ==
== Events ==
These are the defined ''Event types'', ''Phases'', and their corresponding moves of execution.
These are the defined ''Event types'', ''Phases'', and their corresponding modes of execution.
{| class="wikitable" style="font-size:120%"
{| class="wikitable" style="font-size:120%"
|+ THE DEFINED EVENT TYPES & THEIR PHASES
|+ THE DEFINED EVENT TYPES & THEIR PHASES
Line 84: Line 84:


=== Permissions for scripts ===
=== Permissions for scripts ===
; Library permissions
; Library permission
: determines which other libraries can be affected by the script. You can grant access to all libraries or select only certain libraries. This authorization is required for the libByName() function.
: determines which other libraries can be affected by the script. You can grant access to all libraries or select only certain libraries. This authorization is required for the libByName() function.


; Read permissions
; Read permission
: grants the script read access to a file
: grants the script read access to a file


; Write permissions
; Write permission
: grants the script write access to a file
: grants the script write access to a file


; Network
; Network
: grants to the script the right to execute http requests
: grants to the script the right to execute HTTP requests


== Libraries and Entries ==
== Libraries and Entries ==
=== Global Library Functions ===
=== Library Global Functions ===


==== entry() ====  
==== entry() ====  
Line 195: Line 195:
</source>
</source>


This script should be used for the Event '''Creating an entry''' or '''Updating an entry''' in the Phase '''Before saving the entry'''.
This script should be used for the Event '''Creating an entry''' or '''Updating an entry''' in the Phase '''Before saving the entry'''. It will run synchronously.


==== Set default values ====
==== Set default values ====
Line 215: Line 215:
</source>
</source>


The script must be set for Event '''Creating an entry''' Phase '''Open the Entry Edit card for Add'''.
The script must be set for Event '''Creating an entry''' Phase '''Open the Entry Edit card for Add'''. It will run synchronously.


===== Beginning of the next day =====
===== Beginning of the next day =====
If you need to identify the beginning of a new day in the DateTime field, the script requires to connect the JavaScript library '''moment.js''' [http://momentjs.com/ moment.js].
Suppose you need to identify the beginning of a new day in the DateTime field. (The script requires connection of the JavaScript library '''moment.js''' [http://momentjs.com/ moment.js].)


<source lang="javascript">
<source lang="javascript">
Line 227: Line 227:
</source>
</source>


The script must be set for Event '''Creating an entry''' and Phase '''Opening an Entry Edit card for Add'''.
The script must be set for Event '''Creating an entry''' and Phase '''Opening an Entry Edit card for Add'''. It will run synchronously.


== Working with files ==
== Working with files ==
With scripts you can read or write files located in the device's internal memory or on the SD card. All file operations are performed by the ''File'' object, which is obtained through a global function called ''file()''.
With scripts, you can read or write files located in the device's internal memory or on the SD card. All file operations are performed by the ''File'' object, which is obtained via a global function called ''file()''.


To work with the files, the library should have read/write file access.
To work with the files, the library should have read/write file access.
Line 236: Line 236:
=== Global File Functions ===
=== Global File Functions ===
==== file(name) ====
==== file(name) ====
Open a file for read or write operations
Open a file for read or write operations. If the file with the specified name does not exist yet, it will be created.


:; Argument
:; Argument
Line 245: Line 245:


=== Object ''File'' ===
=== Object ''File'' ===
This object is returned by the global function ''file()'' and provides access to the requested file. If the file with the specified name does not exist yet, it will be created. After reading or writing, the file should be closed using the method ''close()''.
This object is returned by the global function ''file()'' and provides access to the requested file. After reading or writing, the file should be closed using the method ''close()''.


==== File Methods ====
==== File Methods ====
Line 294: Line 294:
</source>
</source>


====Save an entry into a file in XML format ====
====Save an entry to a file in XML format ====
The entry includes fields: id , title , date.
The entry includes fields: id , title , date.


Line 309: Line 309:
== Processing an HTTP request ==
== Processing an HTTP request ==
Scripts can send HTTP requests to Web services through their APIs.
Scripts can send HTTP requests to Web services through their APIs.
Processing for HTTP requests allows integration between Memento and the system. All file operations use the Http object, which works through global function http().
Processing for HTTP requests allows integration between Memento and the system. All file operations use the Http object, which works through global function ''http()''.


HTTP requests must fulfill two requirements:
HTTP requests must fulfill two requirements:
# Script execution should be asynchronous, because processing of the request on the network can take a long time. Therefore, HTTP requests of the last Event phase.
# Script execution should be asynchronous, because processing of the request on the network can take a long time &mdash; HTTP requests of the last Phase of an Event.
# The library should have permission - Network.
# The library should have the permission '''Network'''.
 
=== Object ''Http'' ===
Interface for processing HTTP requests


=== Object Http ===
Interface for processing HTTP requests.
==== Http Methods ====
==== Http Methods ====
===== get(url) =====
===== get(url) =====
:Execute get request
: Execute HTTP get request
:Argument: url - HTTP address, starting with http or https
:: Argument: url &mdash; HTTP address, starting with http or https
:Result: HttpResult - Object containing the result of the execution of the HTTP request.
:: Result: HttpResult &mdash; Object containing the result of the execution of the HTTP request.


=== Object HttpResult ===
=== Object ''HttpResult'' ===
Result of the execution of the HTTP request
Result of the execution of the HTTP request
==== Properties ====
==== Properties ====
;code : HTTP code of the response, if the request is successful (usually is 200).
: code &mdash; HTTP code of the response, if the request is successful (usually 200).
;body : The response in text form
: body &mdash; The response in text form


=== Examples ===
=== Examples ===
Line 333: Line 335:
Suppose the library contains two fields: PriceUSD and PriceEUR.
Suppose the library contains two fields: PriceUSD and PriceEUR.
The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.
The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.
We will create a Trigger on Event '''Create Entry to Add''' and phaase '''After Saving Entry'''.
 
We will create a Trigger on Event '''Create entry to Add''' and phase '''After saving the entry'''.


<source lang="javascript">
<source lang="javascript">
Line 365: Line 368:


== Interaction with the System ==
== Interaction with the System ==
=== Global System Functions ===
=== System Global Functions ===
==== message(text) ====
==== message(text) ====
:Shows the user a brief notifiction
: Shows the user a brief notification
:Argument: text &mdash; Text of the notification
: Argument: text &mdash; Text of the notification


==== cancel() ====
==== cancel() ====
: Stop the system operation that caused the event. Many triggers can be a result of entry manipulation (create, update, delete, etc). The cancel() function can be used during the phases that precede the system operation. For example, this function can be used during data validation before the entry is saved.
: Stop the system operation that caused the event. Many triggers can be a result of an entry manipulation (create, update, delete, etc). The ''cancel()'' function can be used during the phases that precede the system operation. For example, this function can be used during data validation before the entry is saved.


==== system() ====
==== system() ====
: Obtain information about the system
: Obtain information about the system
:Result: System object
: Result &mdash; System object


==== log(text) ====
==== log(text) ====
: Write a line to the log file
: Write a line to the log file
: Argument: Text - text to be written to the log
: Argument &mdash; Text - text to be written to the log


==== guid() ====
==== guid() ====
: Generates random text identifier
: Generates random text identifier
: Result: Random string identifier
: Result &mdash; Random string identifier


==== intent(action) ====
==== intent(action) ====
Line 393: Line 396:
: Android has many built-in actions. A list of these actions can be found at [https://developer.android.com/reference/android/content/Intent.html here].
: Android has many built-in actions. A list of these actions can be found at [https://developer.android.com/reference/android/content/Intent.html here].


=== Object System ===
=== Object ''System'' ===
This object contains information about the system.
This object contains information about the system.


==== Properties ====
==== Properties ====
;os : Name of the operating system executing the script
; os : Name of the operating system executing the script


=== Object Intent ===
=== Object ''Intent'' ===
Information exchange object. This object is created by using the global function intent().
Information exchange object. This object is created by using the global function intent().


Line 405: Line 408:
===== data(uri) =====
===== data(uri) =====
: Define URI to reference the data
: Define URI to reference the data
: Argument:uri - URI referencing data to which the action will be applied. It can be contact ID, path to the file, phone number, etc.
:; Argument
:: uri &mdash; URI referencing data to which the action will be applied. It can be contact ID, path to the file, phone number, etc.


===== mimeType(mime) =====
===== mimeType(mime) =====
: Define MIME type of the data
: Define MIME type of the data
: Argument:mime &mdash; MIME type of the data on which the operation will be performed
:; Argument
:: mime &mdash; MIME type of the data on which the operation will be performed


===== extra(key, value) =====
===== extra(key, value) =====
: Define additional data as key-value pairs, as necessary for execution of the required action. Similar to how URI data can be required for certain actions, other actions may require extra data in this format.
: Define additional data as key-value pairs, as necessary for execution of the required action. Similar to how URI data can be required for certain actions, other actions may require extra data in this format.
: Arguments: key and value
:; Arguments
:: key and value


===== extraLong(key, value) =====
===== extraLong(key, value) =====
: Define additional data as key-value pairs, where data type needs to be Long
: Define additional data as key-value pairs, where data type needs to be Long
:; Arguments
:: key and value


=====send()=====
=====send()=====
Line 446: Line 454:


==== Script to insert an appointment into Google Calendar ====
==== Script to insert an appointment into Google Calendar ====
Suppose a library contains time and name of an appointment.
Suppose a library contains the time and name of an appointment.


<source lang="javascript">
<source lang="javascript">