File size: 4,330 Bytes
a637d5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
(function () {
    if (!globalThis.posex) globalThis.posex = {};
    const posex = globalThis.posex;

    function load(cont) {
        function load_() {
            if (posex.script_loading || posex.script_loaded) return;
            posex.script_loading = true;
            
            const scripts = cont.textContent.trim().split('\n');
            const base_path = `/file=${scripts.shift()}/js`;
            cont.textContent = '';

            const df = document.createDocumentFragment();
            for (let src of scripts) {
                console.log(`[Posex] loading ${src}`);
                const script = document.createElement('script');
                script.async = true;
                script.type = 'module';
                script.src = `file=${src}`;
                df.appendChild(script);
            }

            globalThis.posex.import = async () => {
                const THREE = await import(`${base_path}/three.module.js`);
                const { TrackballControls } = await import(`${base_path}/TrackballControls.js`);
                const { DragControls } = await import(`${base_path}/DragControls.js`);
                const { MeshLine, MeshLineMaterial } = await import(`${base_path}/THREE.MeshLine.Module.min.js`);
                return { THREE, TrackballControls, DragControls, MeshLine, MeshLineMaterial };
            };
            if (!globalThis.posex.imports) globalThis.posex.imports = {};
            if (!globalThis.posex.imports.three) globalThis.posex.imports.three = async () => await import(`${base_path}/three.module.js`);
            if (!globalThis.posex.imports.posex) globalThis.posex.imports.posex = async () => await import(`${base_path}/posex.js`);
            cont.appendChild(df);
        }

        return posex.script_loaded ? Promise.resolve() : new Promise(resolve => {
            document.addEventListener('posexscriptloaded', () => resolve(), false);
            load_();
        });
    }

    function lazy(fn, timeout) {
        if (timeout === undefined) timeout = 500;
        return new Promise(function callback(resolve) {
            const result = fn();
            if (result) {
                resolve(result);
            } else {
                setTimeout(() => callback(resolve), timeout);
            }
        });
    }

    function hook_acc(acc, fn) {
        const observer = new MutationObserver(list => {
            for (let mut of list) {
                if (mut.type === 'childList') {
                    if (mut.addedNodes.length != 0) {
                        // closed -> opened
                        fn();
                    } else {
                        // opened -> closed
                        // do nothing
                    }
                }
            }
        });
        observer.observe(acc, { childList: true, attributes: false, subtree: false });
    }

    function launch1(type) {
        return lazy(() => gradioApp()?.querySelector(`#posex-${type}-tab`)).
            then(async acc => {
                const cont = Array.from(acc.querySelectorAll(`#posex-${type}-js`)).at(-1); // !
                const enabled = acc.querySelector(`#posex-${type}-enabled input[type=checkbox]`);
                await load(cont);
                await posex[`init_${type}`]();
                    console.log(`[Posex] ${type} initialized`);
        });
    }

    // function launch(type) {
    //     return lazy(() => gradioApp()?.querySelector(`#posex-${type}-accordion`)).
    //         then(acc => hook_acc(acc, async () => {
    //             const cont = Array.from(acc.querySelectorAll(`#posex-${type}-js`)).at(-1); // !
    //             const enabled = acc.querySelector(`#posex-${type}-enabled input[type=checkbox]`);
    //             await load(cont);
    //             if (enabled.checked) {
    //                 await posex[`init_${type}`]();
    //                 console.log(`[Posex] ${type} initialized`);
    //             } else {
    //                 enabled.addEventListener('change', async () => {
    //                     await posex[`init_${type}`]();
    //                     console.log(`[Posex] ${type} initialized`);
    //                 }, { once: true });
    //             }
    //         }));
    // }
    
    launch1('t2i');
    // launch('i2i');

})();