summaryrefslogtreecommitdiffstats
path: root/roles/reverseproxy/files/conversejs/src/shared/registry.js
blob: c1888daa02e8aac53daa7896471c64f2078f437a (plain)
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
import api from "@converse/headless/shared/api/index.js";

const registry = {};

/**
 * The "elements" namespace groups methods relevant to registering custom
 * HTML elements.
 * @namespace api.elements
 * @memberOf api
 */
api.elements = {
    registry,

    /**
     * Defines a new custom HTML element.
     *
     * By using this API instead of `customElements.define` from the DOM,
     * we can allow custom elements to be overwritten.
     *
     * Once `converse.initialize()` is called, `api.elements.register()`
     * will get called and all custom elements will be registered to the DOM,
     * from which point onward they cannot be overwritten.
     *
     * @method api.elements.define
     * @param { string } name
     * @param { object } constructor
     */
    define (name, constructor) {
        this.registry[name] = constructor;
    },

    /**
     * Registers all previously defined custom HTML elements
     * @method api.elements.register
     */
    register () {
        Object.keys(registry).forEach(name => {
            if (!customElements.get(name)) {
                customElements.define(name, registry[name])
            }
        });
    }
}