xref: /plugin/aichat/script/AIChatButton.js (revision a107768c42fa9f48f2e9b98b62ceaa6150b8c1a9)
1class AIChatButton extends HTMLElement {
2    #root = null;
3    #dialog = null;
4
5
6    constructor() {
7        super();
8        this.#root = this.attachShadow({mode: 'open'});
9        this.#root.innerHTML = `
10            <button class="toggle">
11                <svg viewBox="0 0 24 24"><path d="M12,3C17.5,3 22,6.58 22,11C22,15.42 17.5,19 12,19C10.76,19 9.57,18.82 8.47,18.5C5.55,21 2,21 2,21C4.33,18.67 4.7,17.1 4.75,16.5C3.05,15.07 2,13.13 2,11C2,6.58 6.5,3 12,3Z" /></svg>
12            </button>
13            <dialog>
14                <div>
15                    <header>
16                        <button class="fs" title="Fullscreen">
17                            <svg viewBox="0 0 24 24"><path d="M12 5.5L10 8H14L12 5.5M18 10V14L20.5 12L18 10M6 10L3.5 12L6 14V10M14 16H10L12 18.5L14 16M21 3H3C1.9 3 1 3.9 1 5V19C1 20.1 1.9 21 3 21H21C22.1 21 23 20.1 23 19V5C23 3.9 22.1 3 21 3M21 19H3V5H21V19Z" /></svg>
18                        </button>
19                        <h1>AI Chat</h1>
20                        <button class="toggle" title="Close">
21                            <svg viewBox="0 0 24 24"><path d="M13.46,12L19,17.54V19H17.54L12,13.46L6.46,19H5V17.54L10.54,12L5,6.46V5H6.46L12,10.54L17.54,5H19V6.46L13.46,12Z" /></svg>
22                        </button>
23                    </header>
24                    <main>
25                        <slot></slot>
26                    </main>
27                </div>
28            </dialog>
29        `;
30
31        this.#root.appendChild(this.getStyle());
32        this.#dialog = this.#root.querySelector('dialog');
33
34        const toggleButtons = this.#root.querySelectorAll('button.toggle');
35        toggleButtons.forEach(function (button) {
36            button.addEventListener('click', this.toggleDialog.bind(this))
37        }.bind(this));
38
39        this.#dialog.querySelector('button.fs').addEventListener('click', function() {
40            this.#dialog.classList.toggle('fullscreen');
41        }.bind(this));
42    }
43
44    /**
45     * Called when the DOM has been connected
46     *
47     * We initialize the attribute based states here
48     */
49    connectedCallback() {
50        this.#dialog.querySelector('header h1').textContent = this.getAttribute('title') || 'AI Chat';
51    }
52
53    /**
54     * Define the web component's internal styles
55     *
56     * @returns {HTMLStyleElement}
57     */
58    getStyle() {
59        const style = document.createElement('style');
60        style.textContent = `
61            :host {
62                --color-chat-icon: #4881bf;
63            }
64            button {
65                background: none;
66                border: none;
67                cursor: pointer;
68            }
69            :host > button svg {
70                fill: var(--color-chat-icon);
71            }
72            svg {
73                width: 2em;
74                height: 2em;
75
76            }
77            dialog {
78                width: 500px;
79                max-width: 90vw;
80                height: 800px;
81                max-height: 90vh;
82
83                position: fixed;
84                top: 1em;
85                right: 1em;
86                left: auto;
87
88                padding: 0.25em;
89
90                box-shadow: 0 4px 5px rgb(0 0 0 / 30%);
91                border-radius: 8px;
92                border: 1px solid #fff;
93            }
94            dialog.fullscreen {
95                width: 100%;
96                height: 100%;
97                left: 1em;
98                right: 1em;
99
100            }
101            dialog > div {
102                display: flex;
103                flex-direction: column;
104                height: 100%;
105            }
106            dialog header {
107                display: flex;
108                justify-content: space-between;
109                align-items: flex-start;
110            }
111            dialog main {
112                overflow: auto;
113                flex-grow: 1;
114            }
115        `;
116        return style;
117    }
118
119    toggleDialog() {
120        if (this.#dialog.open) {
121            this.#dialog.close();
122        } else {
123            this.#dialog.show();
124        }
125    }
126}
127
128window.customElements.define('aichat-button', AIChatButton);
129