Skip to content

Widgets: Options

A widget can receive options. For every option the widget can easely define default value by defining a static variable named defaults.

Let's start pass the options myWidgetArg1 to the widget in our application:

src/apps/first-app/index.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import { Application } from "@codecoupler/cc-ui";
import TestingWidget from "../../widgets/testing-features/index.js";
export default class extends Application {
  async init() {
    return {
      panelSize: "500 400",
      headerTitle: "My First Title"
    };
  }
  async start() {
    this.widget(TestingWidget, {
      myWidgetArg1: "Value 1"
    });
  }
}

Now we define two options with default values in our widget. Furthermore we use template strings to put the options in a string:

src/widgets/testing-features/index.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { Widget } from "@codecoupler/cc-ui";
import template from "./index.ejs.html";
import styles from "./index.module.css";
export default class extends Widget {
  myHeader = "JS Widget Base";
  static defaults = {
    myWidgetArg1: "Default 1",
    myWidgetArg2: "Default 2"
  };
  async init() {
    this.buildStructure();
    this.addStyles();
    this.attachHandler();
  }
  buildStructure() {
    this.env.$element.html(
      template({
        myHeader: this.myHeader,
        myTemplateArg: `(Arg1: ${this.options.myWidgetArg1}, Arg2: ${this.options.myWidgetArg2})`
      })
    );
  }
  addStyles() {
    this.env.$element.addClass(styles.widget);
  }
  attachHandler() {
    this.env.$element.find("[data-role=text-plus]").on("click", () => {
      this.env.$element
        .find("div.content")
        .append(
          "<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy</p>"
        );
    });
    this.env.$element.find("[data-role=text-minus]").on("click", () => {
      this.env.$element.find("div.content p").last().remove();
    });
    this.env.$element.find("[data-role=block-info]").on("click", async () => {
      let blockHandle = await this.block(
        "info",
        "This is an info message<br>Please wait 2 seconds"
      ).then((e) => e.handle);
      setTimeout(() => {
        this.unblock(blockHandle);
      }, 2000);
    });
    this.env.$element.find("[data-role=block-error]").on("click", async () => {
      let blockHandle = await this.block(
        "error",
        "This is an error message<br>Please wait 2 seconds"
      ).then((e) => e.handle);
      setTimeout(() => {
        this.unblock(blockHandle);
      }, 2000);
    });
    this.env.$element.find("[data-role=hint-error]").on("click", () => {
      this.hint("error", "An error hint");
    });
    this.env.$element.find("[data-role=hint-info]").on("click", () => {
      this.hint("info", "An info hint");
    });
    this.env.$element.find("[data-role=hint-success]").on("click", () => {
      this.hint("success", "An success hint");
    });
  }
}

As you can see you can access all options of a widget over this.options. Options that will not be passed to the widget get the values from the defaults.