‘liusuyi’
2023-11-24 bf75d92d753bcd3cf871d6d1201f8dd359b11bc6
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
53
54
55
56
57
58
59
60
61
 
class WebRTCStreamerOptionsElement extends HTMLElement {    
    constructor() {
        super(); 
        this.shadowDOM = this.attachShadow({mode: 'open'});
        this.shadowDOM.innerHTML = `
                    <style>@import "styles.css"</style>
                    <h2>Options</h2>
                    <div id="options"></div>
                    <button id="apply">Apply</button>
                    `;
        this.button = this.shadowDOM.getElementById("apply");
        this.button.onclick = () => this.notifyOptions();
        this.params = new URLSearchParams();
    }
 
    static get observedAttributes() {
        return ['options'];
    }  
 
    attributeChangedCallback(attrName, oldVal, newVal) {
        if (attrName === "options") {
            this.fillOptions();
        }
    }
 
    connectedCallback() {
        this.fillOptions();
    }
 
    fillOptions() {
        const options = this.getAttribute("options") || "";
        this.params = new URLSearchParams(options);
 
        let optElement = this.shadowDOM.getElementById("options");
        optElement.innerHTML = "";
 
        for (let [k,v] of this.params.entries()) {
            let label = document.createTextNode(k+":");
            optElement.appendChild(label);            
            let input = document.createElement("input");
            input.type = "text";
            input.value = v;
            input.onchange = () => this.params.set(k, input.value);
            optElement.appendChild(input);        
            optElement.appendChild(document.createElement("br"));        
        }    
    }
 
    notifyOptions()
    {
        this.setAttribute("options", this.params.toString());
        this.dispatchEvent(new CustomEvent('change', {
            detail: {
                  options: this.params.toString(),
            }
          }));
    }
}
 
customElements.define('webrtc-streamer-options', WebRTCStreamerOptionsElement);