Skip to content

Widgets: Blocking

Every widget can be blocked to avoid user input. Let's explore this feature by adding two buttons:

src/widgets/testing-features/index.ejs.html

 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
<h2><%= locals.myHeader %></h2>
<h5><%= locals.myTemplateArg %></h5>
<div class="border rounded d-inline-block p-1 mr-2">
  Text
  <button class="btn btn-xs btn-success" data-role="text-plus">
    <i class="fas fa-plus"></i>
  </button>
  <button class="btn btn-xs btn-danger" data-role="text-minus">
    <i class="fas fa-minus"></i>
  </button>
</div>
<div class="border rounded d-inline-block p-1 mr-2">
  Block
  <button class="btn btn-xs btn-success" data-role="block-info">
    <i class="fas fa-info-circle"></i>
  </button>
  <button class="btn btn-xs btn-danger" data-role="block-error">
    <i class="fas fa-exclamation-circle"></i>
  </button>
</div>
<div class="content"><p>Hello World!</p></div>
<div class="arrow arrow-left"><i class="fas fa-chevron-circle-left"></i></div>
<div class="arrow arrow-right"><i class="fas fa-chevron-circle-right"></i></div>
<div class="arrow arrow-up"><i class="fas fa-chevron-circle-up"></i></div>
<div class="arrow arrow-down"><i class="fas fa-chevron-circle-down"></i></div>

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
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";
  async init() {
    this.buildStructure();
    this.addStyles();
    this.attachHandler();
  }
  buildStructure() {
    this.env.$element.html(
      template({
        myHeader: this.myHeader,
        myTemplateArg: "No Arguments here"
      })
    );
  }
  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);
    });
  }
}

Using this new buttons you can block the widget and show a message.