File size: 4,163 Bytes
0cc999a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112

// Fake gradio components!

// buttons
function newChatClick() {
    gradioApp().querySelector('#empty-btn').click();
}
function jsonDownloadClick() {
    gradioApp().querySelector('#gr-history-download-btn').click();
}
function mdDownloadClick() {
    gradioApp().querySelector('#gr-markdown-export-btn').click();
    gradioApp().querySelector('#gr-history-mardown-download-btn').click();

    // downloadHistory(username, currentChatName, ".md");
}

// index files
function setUploader() {
    transUpload();
    var uploaderObserver = new MutationObserver(function (mutations) {
        var fileInput = null;
        var fileCount = 0;
        fileInput = gradioApp().querySelector("#upload-index-file table.file-preview");
        var fileCountSpan = gradioApp().querySelector("#uploaded-files-count");
        if (fileInput) {
            chatbotArea.classList.add('with-file');
            fileCount = fileInput.querySelectorAll('tbody > tr.file').length;
            fileCountSpan.innerText = fileCount;
        } else {
            chatbotArea.classList.remove('with-file');
            fileCount = 0;
            transUpload();
        }
    });
    uploaderObserver.observe(uploaderIndicator, {attributes: true})
}
var grUploader;
var chatbotUploader;
var handleClick = function() {
    grUploader.click();

};
function transUpload() {
    chatbotUploader = gradioApp().querySelector("#upload-files-btn");
    chatbotUploader.removeEventListener('click', handleClick);
    grUploader = gradioApp().querySelector("#upload-index-file > .center.flex");

    // let uploaderEvents = ["click", "drag", "dragend", "dragenter", "dragleave", "dragover", "dragstart", "drop"];
    // transEventListeners(chatbotUploader, grUploader, uploaderEvents);

    chatbotUploader.addEventListener('click', handleClick);
}

// checkbox
var grSingleSessionCB;
var grOnlineSearchCB;
var chatbotSingleSessionCB;
var chatbotOnlineSearchCB;
function setCheckboxes() {
    chatbotSingleSessionCB = gradioApp().querySelector('input[name="single-session-cb"]');
    chatbotOnlineSearchCB = gradioApp().querySelector('input[name="online-search-cb"]');
    grSingleSessionCB = gradioApp().querySelector("#gr-single-session-cb > label > input");
    grOnlineSearchCB = gradioApp().querySelector("#gr-websearch-cb > label> input");

    chatbotSingleSessionCB.addEventListener('change', (e) => {
        grSingleSessionCB.checked = chatbotSingleSessionCB.checked;
        gradioApp().querySelector('#change-single-session-btn').click();
    });
    chatbotOnlineSearchCB.addEventListener('change', (e) => {
        grOnlineSearchCB.checked = chatbotOnlineSearchCB.checked;
        gradioApp().querySelector('#change-online-search-btn').click();
    });
    grSingleSessionCB.addEventListener('change', (e) => {
        chatbotSingleSessionCB.checked = grSingleSessionCB.checked;
    });
    grOnlineSearchCB.addEventListener('change', (e) => {
        chatbotOnlineSearchCB.checked = grOnlineSearchCB.checked;
    });
}

function bgChangeSingleSession() {
    // const grSingleSessionCB = gradioApp().querySelector("#gr-single-session-cb > label > input");
    let a = chatbotSingleSessionCB.checked;
    return [a];
}
function bgChangeOnlineSearch() {
    // const grOnlineSearchCB = gradioApp().querySelector("#gr-websearch-cb > label> input");
    let a = chatbotOnlineSearchCB.checked;
    return [a];
}

// UTILS
function transEventListeners(target, source, events) {
    events.forEach((sourceEvent) => {
        target.addEventListener(sourceEvent, function (targetEvent) {
            if(targetEvent.preventDefault) targetEvent.preventDefault();
            if(targetEvent.stopPropagation) targetEvent.stopPropagation();

            source.dispatchEvent(new Event(sourceEvent, {detail: targetEvent.detail}));
            // console.log(targetEvent.detail);
        });
    });
    /* 事实上,我发现这样写的大多数gradio组件并不适用。。所以。。。生气 */
}

function bgSelectHistory(a,b){
    const historySelectorInput = gradioApp().querySelector('#history-select-dropdown input');
    let file = historySelectorInput.value;
    return [a,file]
}