Memento JavaScript UI

From Memento Database Wiki
Jump to navigation Jump to search

Overview

The ui() method is a JavaScript API provided by Memento Database that grants access to a framework of methods and objects for creating user interfaces (UIs) within Memento widgets. With this method, developers can create various UI elements such as text labels, buttons, images, layers, and checkboxes.

Example

ui().text('Hello, world!');

In this example, the ui() method is called to create a new UI object, which is then used to create a text label with the text "Hello, world!".

Notes

  • A script must return one UI object.
  • If you want to display multiple UI objects, you can use a layout object, which can hold multiple objects.

Example usage of a layout object:

ui().layout([
ui().text('This is a text label'),
ui().button('Click me!'),
ui().image('https://example.com/myimage.png')
]);

In this example, the layout object is created by calling the layout() method on a UI object and passing in multiple UI objects as arguments. The layout object contains a text label, a button, and an image, which are grouped together within a single view.

Base Methods

Define size of an element:

  • width(width: int): Sets the width of the UI element in pixels.
  • height(height: int): Sets the height of the UI element in pixels.
  • width_match_parent(): Sets the width of the UI element to match the parent container's width.
  • height_match_parent(): Sets the height of the UI element to match the parent container's height.
  • width_wrap_content(): Sets the width of the UI element to wrap the content.
  • height_wrap_content(): Sets the height of the UI element to wrap the content.

Weight:

  • weight(weight: int): Sets the weight of the UI element in a layout.

Tag:

  • tag(text: string): Sets the tag for the UI element. This tag can be used to find and manipulate the element later during events such as button clicks.

Example Usage:

ui().button('Button').width(100).height(50).tag('button1');

In this example, a button UI element is created with a width of 100 pixels, a height of 50 pixels, and the tag "button1" is set for later reference during events such as button clicks.


Text Element

The text object is used to create a text label.

Syntax

ui().text(text: string)
  • text (required): The text to be displayed in the label.

Example

ui().text('Hello');

In this example, the text() method is called on a UI object to create a new text label with the text "Hello".

Font Settings

Font settings can be applied to text objects using the font() method.

ui().text(text: string).font(fontOptions)
  • fontOptions (required): An object containing font settings.
    • size (optional): The font size in pixels.
    • color (optional): The font color as a string or hexadecimal value.
    • style (optional): The font style, such as "bold" or "italic".
ui().text('Hello').font({ size: 10, color: 'red', style: 'bold' });

In this example, a text object is created with the text "Hello" and the font settings are set to a size of 10 pixels, red color, and bold style.

Layout Element

The layout object is used to group multiple UI objects together within a single view.

Syntax

ui().layout(children: Object[])
  • children (required): An array of UI objects to be arranged either horizontally in a single row or vertically in a single column.

Example

ui().layout([
ui().text('This is a text label'),
ui().button('Click me!'),
ui().image('https://example.com/myimage.png')
]);

In this example, the layout object is created by calling the layout() method on a UI object and passing in an array of multiple UI objects as an argument. The layout object contains a text label, a button, and an image, which are grouped together within a single view.

Notes

  • By default, the layout object arranges its children vertically in a single column. However, it is possible to display the children horizontally by calling the horizontal() method.


Button Element

The button object is used to create a button within the app.

Syntax

ui().button(title: string)
  • title (required): The text to be displayed on the button.

Example

ui().button('Click me!');

In this example, the button() method is called on a UI object to create a new button with the title "Click me!".

Action The action() method can be used to set a callback function for the button. This function will be called when the button is clicked.

ui().button('Click me!').action(function() { message('Hello'); });

In this example, a button object is created with the title "Click me!" and a callback function is set to display a message when the button is clicked.

Icon The icon() method can be used to set an icon for the button. If no title is set for the button, a small button with an icon will be displayed.

ui().button().icon('nova:add-circle-1.png');

In this example, a small button with the "add" icon will be displayed.

Refreshing UI

If you want to refresh the libraries list or widgets UI after the button's action, you can simply return true in the action function.

ui().button('Add Entry').action(function() { lib().create({ 'Title': 'New record' }); return true; });

In this example, a new entry will be created in the library when the button is clicked, and the libraries list or widgets UI will be refreshed.

Editor Element

The editor object is used to create a text editor.

Syntax

ui().edit(text: string)
  • text (optional): The initial text to be displayed in the editor.

Example

ui().edit('Type your text here');

In this example, the edit() method is called on a UI object to create a new text editor with the initial text "Type your text here".

Properties

The editor object has two properties for retrieving and modifying the text in the editor, respectively.

var myEditor = ui().editor('Type your text here'); 
var currentText = myEditor.text; 
myEditor.text = 'New text';

In this example, a new editor object is created with the initial text "Type your text here". The current text in the editor is then retrieved and stored in the currentText variable using the text property. Finally, the text in the editor is changed to "New text" using the text property.

Tag

The tag() method can be used to assign a tag to the editor object. This tag can be used to identify and manipulate the editor object later on.

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

In this example, 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.

Checkbox Element

The checkbox object is used to create a checkbox UI element.

Syntax

ui().checkbox(title: string, value: boolean)
  • title (required): The title or label to be displayed next to the checkbox.
  • value (required): The initial value of the checkbox (true for checked, false for unchecked).

Example

ui().checkbox('Enable notifications', true);

In this example, the checkbox() method is called on a UI object to create a new checkbox with the title "Enable notifications" and an initial value of true (checked).

Properties

The checkbox object has one property for retrieving and modifying the value of the checkbox.

var myCheckbox = ui().checkbox('Enable notifications', true); 
var currentValue = myCheckbox.checked; 
myCheckbox.checked = false;

In this example, a new checkbox object is created with the title "Enable notifications" and an initial value of true (checked). The current value of the checkbox is then retrieved and stored in the currentValue variable using the checked property. Finally, the value of the checkbox is changed to false (unchecked) using the checked property.

onChange()

The onChange() method can be used to add a change listener to the checkbox object. This listener will be called whenever the value of the checkbox is changed by the user.

ui().layout([
    ui().checkbox('Enable notifications', true).onChange(function(value) { 
        if (value) { 
            message('Notifications enabled'); 
        } else { 
            message('Notifications disabled'); 
        } 
    })
]);

In this example, a layout object is created with a checkbox object that has the title "Enable notifications" and an initial value of true (checked). The onChange() method is then called to add a change listener to the checkbox object that displays a message depending on the new value of the checkbox.