• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 14804036802

02 May 2025 09:58PM UTC coverage: 5.927% (-83.4%) from 89.28%
14804036802

Pull #3404

github

web-flow
Merge 5c103d7f8 into 0f2ccaeb2
Pull Request #3404: feat: added Graph module to Math

234 of 8383 branches covered (2.79%)

229 of 246 new or added lines in 1 file covered. (93.09%)

13145 existing lines in 208 files now uncovered.

934 of 15759 relevant lines covered (5.93%)

4.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/engine/Util/Toaster.ts
1
import toasterCss from './Toaster.css';
2

3
/**
4
 * The Toaster is only meant to be called from inside Excalibur to display messages to players
5
 */
6
export class Toaster {
7
  private _styleBlock: HTMLStyleElement;
8
  private _container: HTMLDivElement;
UNCOV
9
  private _toasterCss: string = toasterCss.toString();
×
10

UNCOV
11
  private _isInitialized = false;
×
12
  private _initialize() {
UNCOV
13
    if (!this._isInitialized) {
×
UNCOV
14
      this._container = document.createElement('div');
×
UNCOV
15
      this._container.id = 'ex-toast-container';
×
UNCOV
16
      document.body.appendChild(this._container);
×
UNCOV
17
      this._isInitialized = true;
×
18

UNCOV
19
      this._styleBlock = document.createElement('style');
×
UNCOV
20
      this._styleBlock.textContent = this._toasterCss;
×
UNCOV
21
      document.head.appendChild(this._styleBlock);
×
22
    }
23
  }
24

25
  public dispose() {
UNCOV
26
    this._container.parentElement.removeChild(this._container);
×
27

UNCOV
28
    this._styleBlock.parentElement.removeChild(this._styleBlock);
×
29

UNCOV
30
    this._isInitialized = false;
×
31
  }
32

33
  private _createFragment(message: string) {
UNCOV
34
    const toastMessage = document.createElement('span');
×
UNCOV
35
    toastMessage.innerText = message;
×
UNCOV
36
    return toastMessage;
×
37
  }
38

39
  /**
40
   * Display a toast message to a player
41
   * @param message Text of the message, messages may have a single "[LINK]" to influence placement
42
   * @param linkTarget Optionally specify a link location
43
   * @param linkName Optionally specify a name for that link location
44
   */
45
  public toast(message: string, linkTarget?: string, linkName?: string) {
UNCOV
46
    this._initialize();
×
UNCOV
47
    const toast = document.createElement('div');
×
UNCOV
48
    toast.className = 'ex-toast-message';
×
49

UNCOV
50
    const messageFragments: HTMLElement[] = message.split('[LINK]').map((message) => this._createFragment(message));
×
51

UNCOV
52
    if (linkTarget) {
×
UNCOV
53
      const link = document.createElement('a');
×
UNCOV
54
      link.href = linkTarget;
×
UNCOV
55
      if (linkName) {
×
UNCOV
56
        link.innerText = linkName;
×
57
      } else {
UNCOV
58
        link.innerText = linkTarget;
×
59
      }
UNCOV
60
      messageFragments.splice(1, 0, link);
×
61
    }
62

63
    // Assembly message
UNCOV
64
    const finalMessage = document.createElement('div');
×
UNCOV
65
    messageFragments.forEach((message) => {
×
UNCOV
66
      finalMessage.appendChild(message);
×
67
    });
UNCOV
68
    toast.appendChild(finalMessage);
×
69

70
    // Dismiss button
UNCOV
71
    const dismissBtn = document.createElement('button');
×
UNCOV
72
    dismissBtn.innerText = 'x';
×
UNCOV
73
    dismissBtn.addEventListener('click', () => {
×
UNCOV
74
      this._container.removeChild(toast);
×
75
    });
UNCOV
76
    toast.appendChild(dismissBtn);
×
77

78
    // Escape to dismiss
UNCOV
79
    const keydownHandler = (evt: KeyboardEvent) => {
×
UNCOV
80
      if (evt.key === 'Escape') {
×
UNCOV
81
        try {
×
UNCOV
82
          this._container.removeChild(toast);
×
83
        } catch {
84
          // pass
85
        }
86
      }
UNCOV
87
      document.removeEventListener('keydown', keydownHandler);
×
88
    };
UNCOV
89
    document.addEventListener('keydown', keydownHandler);
×
90

91
    // Insert into container
UNCOV
92
    const first = this._container.firstChild;
×
UNCOV
93
    this._container.insertBefore(toast, first);
×
94
  }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc