File size: 3,912 Bytes
92cd965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class PresetManagerDropdownComponent{
    constructor(component){
        this.component = gradioApp().getElementById(component)
    }
    updateDropdown(selection){
        //getElementById("preset-util_preset_ds_dd").querySelector("select").querySelectorAll("option")[1].selected = true;
        //Array.from(this.component.querySelector("select").querySelectorAll("option")).find( e => e.value.includes(selection)).selected = true;
        this.getDropDownOptions().find( e => e.value.includes(selection)).selected = true;
        this.component.querySelector("select").dispatchEvent(new Event("change"));
    }
    getDropDownOptions(asValues=false){
        if (asValues){
            let temp = new Array;
            Array.from(this.component.querySelector("select").querySelectorAll("option")).forEach( opt => temp.push(opt.value))
            return temp
        }
        else{
            return Array.from(this.component.querySelector("select").querySelectorAll("option"))
        }
    }
    getCurrentSelection(){
        return this.component.querySelector("select").value
    }
}

class PresetManagerCheckboxGroupComponent{
    constructor(component){
        this.component = gradioApp().getElementById(component);
        this.checkBoxes = new Object;
        this.setCheckBoxesContainer()
    }
    setCheckBoxesContainer(){
        Array.from(this.component.querySelectorAll("input")).forEach( _input => this.checkBoxes[_input.nextElementSibling.innerText] = _input)
    }
    getCheckBoxes(){
        let response = new Array;
        for (let _component in this.checkBoxes){
            response.push([_component, this.checkBoxes[_component]])
        }
        return response
    }
    setCheckBoxesValues(iterable){
        for (let _componentText in this.checkBoxes){
            this.conditionalToggle(false, this.checkBoxes[_componentText])
        }
        if (iterable instanceof Array){
            setTimeout( () =>
            iterable.forEach( _label => this.conditionalToggle(true, this.checkBoxes[_label])),
            2)
        }
    }
    conditionalToggle(desiredVal, _component){
        //This method behaves like 'set this value to this'
        //Using element.checked = true/false, does not register the change, even if you called change afterwards,
        //  it only sets what it looks like in our case, because there is no form submit, a person then has to click on it twice.
        //Options are to use .click() or dispatch an event
        if (desiredVal != _component.checked){
            _component.dispatchEvent(new Event("change"))//using change event instead of click, in case browser ad-blockers blocks the click method
        }
    }
}
class PresetManagerMover{
    constructor(self, target, onLoadHandler){
        this.presetManager = gradioApp().getElementById(self)
        this.target = gradioApp().getElementById(target)
        this.handler = gradioApp().getElementById(onLoadHandler)
    }
    move(adjacentAt='afterend'){
        /*
        'beforebegin'
            <ele>
            'afterbegin'
                <otherele>
            'beforeend'
            </ele>
        'afterend'
        */
         
        this.target.insertAdjacentElement(adjacentAt, this.presetManager)
    }
    updateTarget(new_target){
        this.target = gradioApp().getElementById(new_target)
    }
}
//function move(){
//        let src = gradioApp().getElementById("txt2img_preset_manager_accordion");
//        //let target = Array.from(gradioApp().querySelectorAll("div")).filter( d => d.hasAttribute("class") ? d.getAttribute("class").split(" ")[0] == "gradio-container": false)[0]
//        //target.insertAdjacentElement("afterbegin", src)
//        let target = gradioApp().getElementById("tab_txt2img");
//}
//document.addEventListener("DOMContentLoaded",() => () => {setTimeout(move, 10000)})

//onUiTabChange( function (...x) {console.log(x)})