# JSComponent

## Overview

The `JSComponent` class provides a set of methods for interacting with and managing components within a solution model. It offers functionality for setting and retrieving properties such as background, font type, borders, visibility, and positioning. Through various getter and setter methods, it allows for fine-tuned control over the component’s attributes, including handling design-time properties, setting Z-index values, and managing anchor configurations.

In addition to visual and layout properties, `JSComponent` supports grouping components together by `groupID`, enabling batch actions like enabling or disabling multiple elements at once. The class also supports retrieving and setting CSS position for components in NGClient, as well as managing event handlers and design-time properties. It provides utility methods for managing component attributes, including getting and setting attribute values, and clearing attributes when necessary.

## Properties Summarized

| Type                                                                                        | Name                        | Summary                                                                                                    |
| ------------------------------------------------------------------------------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------- |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [anchors](#anchors)         | Enables a component to stick to a specific side of form and/or to grow or shrink when a window is resized. |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [background](#background)   | The background color of the component.                                                                     |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [borderType](#bordertype)   | The type, color and style of border of the component.                                                      |
| [CSSPosition](https://docs.servoy.com/reference/servoycore/dev-api/application/cssposition) | [cssPosition](#cssposition) | CSS position is a replacement for anchoring system making it more intuitive to place a component.          |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)              | [enabled](#enabled)         | The enable state of the component, default true.                                                           |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [fontType](#fonttype)       | The font type of the component.                                                                            |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [foreground](#foreground)   | The foreground color of the component.                                                                     |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [formIndex](#formindex)     | The Z index of this component.                                                                             |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [height](#height)           | The height in pixels of the component.                                                                     |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [name](#name)               | The name of the component.                                                                                 |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                | [styleClass](#styleclass)   | The name of the style class that should be applied to this component.                                      |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)              | [transparent](#transparent) | Flag that tells if the component is transparent or not.                                                    |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)              | [visible](#visible)         | The visible property of the component, default true.                                                       |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [width](#width)             | The width in pixels of the component.                                                                      |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [x](#x)                     | The x coordinate of the component on the form.                                                             |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)                | [y](#y)                     | The y coordinate of the component on the form.                                                             |

## Methods Summarized

| Type                                                                          | Name                                                                  | Summary                                       |
| ----------------------------------------------------------------------------- | --------------------------------------------------------------------- | --------------------------------------------- |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)  | [getAttribute(name)](#getattribute-name)                              | Get the value of an attribute of the element. |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)    | [getAttributes()](#getattributes)                                     | Returns the attribute names of an element.    |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)  | [getComment()](#getcomment)                                           | Returns the comment of this component.        |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)  | [getDesignTimeProperty(key)](#getdesigntimeproperty-key)              | Get a design-time property of an element.     |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)    | [getDesignTimePropertyNames()](#getdesigntimepropertynames)           | Get the design-time properties of an element. |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)  | [getFormName()](#getformname)                                         | Returns the name of the form.                 |
| [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid) | [getUUID()](#getuuid)                                                 | Returns the UUID of this component.           |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)  | [putDesignTimeProperty(key, value)](#putdesigntimeproperty-key-value) | Set a design-time property of an element.     |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)  | [removeAttribute(name)](#removeattribute-name)                        | Remove the attribute of an element.           |
| [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object)  | [removeDesignTimeProperty(key)](#removedesigntimeproperty-key)        | Clear a design-time property of an element.   |
| void                                                                          | [setAttribute(name, value)](#setattribute-name-value)                 | Set the attribute value of an element.        |

## Properties Detailed

### anchors

Enables a component to stick to a specific side of form and/or to\
grow or shrink when a window is resized.

If opposite anchors are activated then the component with grow or\
shrink with the window. For example if Top and Bottom are activated,\
then the component will grow/shrink when the window is vertically\
resized. If Left and Right are activated then the component\
will grow/shrink when the window is horizontally resized.

If opposite anchors are not activated, then the component will\
keep a constant distance from the sides of the window which\
correspond to the activated anchors.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The anchor configuration for the component.

**Sample**

```js
var form = solutionModel.newForm('mediaForm', 'db:/example_data/parent_table', null, false, 400, 300);
var strechAllDirectionsLabel = form.newLabel('Strech all directions', 10, 10, 380, 280);
strechAllDirectionsLabel.background = 'red';
strechAllDirectionsLabel.anchors = SM_ANCHOR.ALL;
var strechVerticallyLabel = form.newLabel('Strech vertically', 10, 10, 190, 280);
strechVerticallyLabel.background = 'green';
strechVerticallyLabel.anchors = SM_ANCHOR.WEST | SM_ANCHOR.NORTH | SM_ANCHOR.SOUTH;
var strechHorizontallyLabel = form.newLabel('Strech horizontally', 10, 10, 380, 140);
strechHorizontallyLabel.background = 'blue';
strechHorizontallyLabel.anchors = SM_ANCHOR.NORTH | SM_ANCHOR.WEST | SM_ANCHOR.EAST;
var stickToTopLeftCornerLabel = form.newLabel('Stick to top-left corner', 10, 10, 200, 100);
stickToTopLeftCornerLabel.background = 'orange';
stickToTopLeftCornerLabel.anchors = SM_ANCHOR.NORTH | SM_ANCHOR.WEST; // This is equivalent to SM_ANCHOR.DEFAULT
var stickToBottomRightCornerLabel = form.newLabel('Stick to bottom-right corner', 190, 190, 200, 100);
stickToBottomRightCornerLabel.background = 'pink';
stickToBottomRightCornerLabel.anchors = SM_ANCHOR.SOUTH | SM_ANCHOR.EAST;
```

### background

The background color of the component.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The background color of the component.

**Sample**

```js
// This property can be used on all types of components.
// Here it is illustrated only for labels and fields.
var greenLabel = form.newLabel('Green',10,10,100,50);
greenLabel.background = 'green'; // Use generic names for colors.
var redField = form.newField('parent_table_text',JSField.TEXT_FIELD,10,110,100,30);
redField.background = '#FF0000'; // Use RGB codes for colors.
```

### borderType

The type, color and style of border of the component.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The border type of the component as a string.

**Sample**

```js
//HINT: To know exactly the notation of this property set it in the designer and then read it once out through the solution model.
var field = form.newField('my_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
field.borderType = solutionModel.createLineBorder(1,'#ff0000');
```

### cssPosition

CSS position is a replacement for anchoring system making it more intuitive to place a component.\
CSS position should be set on form, an absolute position form can either work with anchoring or with css position.\
This is only working in NGClient.

**Type**\
[CSSPosition](https://docs.servoy.com/reference/servoycore/dev-api/application/cssposition) The CSS position of the component.

**Sample**

```js
var label = form.newLabel('Label', -1);
label.cssPosition.r("10").b("10").w("20%").h("30px")
```

### enabled

The enable state of the component, default true.

**Type**\
[Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) True if the component is enabled; false otherwise.

**Sample**

```js
var form = solutionModel.newForm('printForm', 'db:/example_data/parent_table', null, false, 400, 300);
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
field.enabled = false;
```

### fontType

The font type of the component.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The font type of the component.

**Sample**

```js
var label = form.newLabel('Text here', 10, 50, 100, 20);
label.fontType = solutionModel.createFont('Times New Roman',1,14);
```

### foreground

The foreground color of the component.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The foreground color of the component.

**Sample**

```js
// This property can be used on all types of components.
// Here it is illustrated only for labels and fields.
var labelWithBlueText = form.newLabel('Blue text', 10, 10, 100, 30);
labelWithBlueText.foreground = 'blue'; // Use generic names for colors.
var fieldWithYellowText = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 50, 100, 20);
fieldWithYellowText.foreground = '#FFFF00'; // Use RGB codes for colors.
```

### formIndex

The Z index of this component. If two components overlap, then the component with higher Z index is displayed above the component with lower Z index.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The Z-index of the component.

**Sample**

```js
var labelBelow = form.newLabel('Green', 10, 10, 100, 50);
labelBelow.background = 'green';
labelBelow.formIndex = 10;
var fieldAbove = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 30);
fieldAbove.background = '#FF0000';
fieldAbove.formIndex = 20;
```

### height

The height in pixels of the component.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The width of the component in pixels.

**Sample**

```js
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
application.output('original width: ' + field.width);
application.output('original height: ' + field.height);
field.width = 200;
field.height = 100;
application.output('modified width: ' + field.width);
application.output('modified height: ' + field.height);
```

### name

The name of the component. Through this name it can also accessed in methods.\
Must be a valid javascript name. (no - in the name or start with number)

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The name of the component.

**Sample**

```js
var form = solutionModel.newForm('someForm', 'db:/example_data/parent_table', null, false, 620, 300);
var label = form.newLabel('Label', 10, 10, 150, 150);
label.name = 'myLabel'; // Give a name to the component.
forms['someForm'].controller.show()
// Now use the name to access the component.
forms['someForm'].elements['myLabel'].text = 'Updated text';
```

### styleClass

The name of the style class that should be applied to this component.

When defining style classes for specific component types, their names\
must be prefixed according to the type of the component. For example\
in order to define a class names 'fancy' for fields, in the style\
definition the class must be named 'field.fancy'. If it would be\
intended for labels, then it would be named 'label.fancy'. When specifying\
the class name for a component, the prefix is dropped however. Thus the\
field or the label will have its styleClass property set to 'fancy' only.

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The CSS style class assigned to the component.

**Sample**

```js
var form = solutionModel.newForm('printForm', 'db:/example_data/parent_table', null, false, 400, 300);
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
var style = solutionModel.newStyle('myStyle','field.fancy { background-color: yellow; }');
form.styleName = 'myStyle'; // First set the style on the form.
field.styleClass = 'fancy'; // Then set the style class on the field.
```

### transparent

Flag that tells if the component is transparent or not.

The default value is "false", that is the components\
are not transparent.

**Type**\
[Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) True if the component is transparent; false otherwise.

**Sample**

```js
// Load an image from disk an create a Media object based on it.
var imageBytes = plugins.file.readFile('d:/ball.jpg');
var media = solutionModel.newMedia('ball.jpg', imageBytes);
// Put on the form a label with the image.
var image = form.newLabel('', 10, 10, 100, 100);
image.imageMedia = media;
// Put two fields over the image. The second one will be transparent and the
// image will shine through.
var nonTransparentField = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 20, 100, 20);
var transparentField = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 50, 100, 20);
transparentField.transparent = true;
```

### visible

The visible property of the component, default true.

**Type**\
[Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) True if the component is visible; false otherwise.

**Sample**

```js
var form = solutionModel.newForm('printForm', 'db:/example_data/parent_table', null, false, 400, 300);
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
field.visible = false;
```

### width

The width in pixels of the component.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The width of the component in pixels.

**Sample**

```js
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
application.output('original width: ' + field.width);
application.output('original height: ' + field.height);
field.width = 200;
field.height = 100;
application.output('modified width: ' + field.width);
application.output('modified height: ' + field.height);
```

### x

The x coordinate of the component on the form.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number)

**Sample**

```js
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
application.output('original location: ' + field.x + ', ' + field.y);
field.x = 90;
field.y = 90;
application.output('changed location: ' + field.x + ', ' + field.y);
```

### y

The y coordinate of the component on the form.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The Y-coordinate position of the component.

**Sample**

```js
var field = form.newField('parent_table_text', JSField.TEXT_FIELD, 10, 10, 100, 20);
application.output('original location: ' + field.x + ', ' + field.y);
field.x = 90;
field.y = 90;
application.output('changed location: ' + field.x + ', ' + field.y);
```

## Methods Detailed

### getAttribute(name)

Get the value of an attribute of the element.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** the name of the attribute

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) The value of the specified attribute.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
var attributes = fld.getAttributes();
for (var i = 0; i < attributes.length; i++)
{
		application.output(fld.getAttribute(attributes[i]));
}
```

### getAttributes()

Returns the attribute names of an element.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) An array of attribute names for the component.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
var attributes = fld.getAttributes();
for (var i = 0; i < attributes.length; i++)
{
		application.output(fld.getAttribute(attributes[i]));
}
```

### getComment()

Returns the comment of this component.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the comment associated with this component.

**Sample**

```js
var comment = solutionModel.getForm("my_form").getButton("my_button").getComment();
application.output(comment);
```

### getDesignTimeProperty(key)

Get a design-time property of an element.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **key** the name of the property

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) The value of the specified design-time property.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
var prop = fld.getDesignTimeProperty('myprop')
```

### getDesignTimePropertyNames()

Get the design-time properties of an element.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) An array of design-time property names for the component.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
var propNames = fld.getDesignTimePropertyNames()
```

### getFormName()

Returns the name of the form. (may be empty string as well)

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The name of the form containing the component.

**Sample**

```js
var name = myElement.getFormName();
```

### getUUID()

Returns the UUID of this component.

**Returns:** [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid) the UUID of this component.

**Sample**

```js
var button_uuid = solutionModel.getForm("my_form").getButton("my_button").getUUID();
application.output(button_uuid.toString());
```

### putDesignTimeProperty(key, value)

Set a design-time property of an element. Value should be primitive data (string, boolean or number).\
&#x20;   Complex values should be stringified before call.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **key** the name of the property
* [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) **value** the value to store

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) The previous value of the specified design-time property, or null if none existed.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
fld.putDesignTimeProperty('myprop', 'strawberry')
```

### removeAttribute(name)

Remove the attribute of an element.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** the name of the attribute

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the deleted attribute value

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
fld.removeAttribute('keylistener')
```

### removeDesignTimeProperty(key)

Clear a design-time property of an element.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **key** the name of the property

**Returns:** [Object](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/object) The removed value of the specified design-time property.

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
fld.removeDesignTimeProperty('myprop')
```

### setAttribute(name, value)

Set the attribute value of an element.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **name** the name of the attribute
* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **value** the value of the attribute

**Returns:** void

**Sample**

```js
var frm = solutionModel.getForm('orders')
var fld = frm.getField('fld')
fld.setAttribute('keylistener', 'callback')
```

***
