Script Widget

Requires Memento Database 5.0 or newer

Script widgets are a powerful feature in Memento Database that allow you to execute custom JavaScript code and display the result in a widget on your dashboard. These widgets contain a script that is executed before the widget is displayed and also after all refreshes of a dashboard. The result of this script as a string will be displayed in the widget. A script may consist of multiple operations, but only the result of the last operaion will be displayed. Also, you can use a limited amount of HTML tags to format the text that is displayed.

To add a script widget to your dashboard, simply click on the Add Widget menu item and then select Script from the available options. Once you have added the widget, you can then start writing your JavaScript code.

In script widgets, you can use all the methods and objects that are available for triggers and actions in Memento Database. Scripts run in the context of the library, just like actions for the library. You can find more information about these methods and objects in the Memento Database wiki.

In addition, Memento Database now provides a new JavaScript library that allows you to create interfaces for your scripts in widgets. This library provides a simple way to build custom user interfaces that can be used to display data and interact with users. To learn more about this library, you can refer to the Memento Database wiki for detailed documentation and examples.

In summary, script widgets in Memento Database provide a powerful way to execute custom JavaScript code and display the result in a widget on your dashboard. With the ability to use all the available methods and objects, as well as the new JavaScript library for creating interfaces, the possibilities are endless.

Examples

Example script to count the number of entries in a library and display it in a script widget:
var library = lib();
// get the number of entries in the library
var entryCount = library.entries().length;
// return the entry count as a string
"Total Entries: " + entryCount;

Display the Titles of the Last Five Entries in the Library:
var entries = lib().entries();
var result = '';
for (var i = 0; i < Math.min(entries.length, 5); i++) {
var entry = entries[i];
result += entry.title + '\n';
}
result;

This script will group the entries in the library by the “Category” field and display the number of entries for each category in an unordered list:

var entries = lib().entries();
var categories = {};
for (var i = 0; i < entries.length; i++) { var category = entries[i].field("Category"); if (category in categories) { categories[category]++; } else { categories[category] = 1; } } var result = "<ul>";
for (var category in categories) {
result += "<li>" + category + " - " + categories[category] + "</li>";
}
result += "</ul>";
result;

The following example utilizes the Memento JavaScript UI library. A layout object is created with an editor object assigned the tag name “name”, and a button object with an action function that retrieves the text from the editor object using the findByTag() method and creates a new entry in the library:

ui().layout([
    ui().edit('').tag('name'), 
    ui().button('Create').action(function() { 
        lib().create({ 'Name': ui().findByTag('name').text }); 
        return true; 
    })
]);

Script initialization.

The _initWidget global variable is a boolean variable that is available for use in script widgets. When the script runs for the first time, the value of _initWidget is true. On subsequent runs of the script, _initWidget will be false.

if (_initWidget) { // Code to be run only once goes here }

By using the _initWidget variable in your script, you can ensure that your script is only executed when it needs to be, and avoid unnecessary processing and delays in loading your dashboard.

Was this article helpful?

Related Articles