1/**
2 * Static methods to show GUI elements
3 */
4class GUI {
5    /**
6     * Display a simple toast message that vanishes after a short time
7     *
8     * @param {string} msg Text of the message
9     * @param {string} className Additional class for the message
10     */
11    static toast(msg, className) {
12        const div = document.createElement('div');
13        div.className = 'encryptedpasswords-toast ' + className;
14        div.innerText = msg;
15        document.body.appendChild(div);
16
17        window.setTimeout(function () {
18            document.body.removeChild(div);
19        }, 3500);
20    }
21
22    /**
23     * A jQuery UI replacement for prompt, but using a password field
24     *
25     * @param {string} msg
26     * @param {string} title
27     * @param {boolean} repeat
28     * @return {Promise<string>}
29     */
30    static async prompt(msg, title = '', repeat = false) {
31        return new Promise(function (resolve, reject) {
32            const repeatField = repeat ?
33                `<p>${LANG.plugins.encryptedpasswords.repeatKey}</p>` +
34                '<input name="repeat" type="password">' :
35                '';
36            let $dialog = jQuery('<div class="encryptedpasswords-prompt">' +
37                `<p>${msg}</p>` +
38                '<input name="pass" type="password">' +
39                repeatField +
40                '</div>'
41            );
42
43            $dialog.dialog({
44                modal: true,
45                title: title,
46                closeOnEscape: true,
47                closeText: LANG.plugins.encryptedpasswords.btn_no,
48                beforeClose: function (event, ui) {
49                    console.log(event);
50                    const pass = $dialog.find("input[name='pass']").val();
51                    if (repeat && pass !== $dialog.find("input[name='repeat']").val()) {
52                        $dialog.prepend(`<p class="error">${LANG.plugins.encryptedpasswords.repeatError}</p>`);
53                        return false;
54                    }
55                },
56                close: function () {
57                    resolve($dialog.find("input[name='pass']").val());
58                    jQuery(this).dialog('destroy');
59                },
60                buttons: {
61                    [LANG.plugins.encryptedpasswords.btn_ok]: function () {
62                        jQuery(this).dialog('close');
63                    },
64                    [LANG.plugins.encryptedpasswords.btn_no]: function () {
65                        $dialog.find("input[name='pass']").val('');
66                        $dialog.find("input[name='repeat']").val('');
67                        jQuery(this).dialog('close');
68                    }
69                }
70            });
71        });
72    }
73}
74