File size: 3,873 Bytes
d6bdfb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 通用工具函数

// 创建表单组
export function createFormGroup(label, input) {
    const div = document.createElement('div');
    div.className = 'form-group';
    
    const labelEl = document.createElement('label');
    labelEl.textContent = label;
    if (input.id) {
        labelEl.setAttribute('for', input.id);
    }
    
    div.appendChild(labelEl);
    div.appendChild(input);
    
    return div;
}

// 创建滑块输入
export function createRangeInput(config) {
    const { id, min = 0, max = 100, step = 1, value = 50, onChange, label } = config;
    
    const container = document.createElement('div');
    container.style.display = 'flex';
    container.style.gap = '10px';
    container.style.alignItems = 'center';
    
    const input = document.createElement('input');
    input.type = 'range';
    input.id = id;
    input.min = min;
    input.max = max;
    input.step = step;
    input.value = value;
    
    // 添加可访问性属性
    if (label) {
        input.setAttribute('aria-label', label);
    } else {
        input.setAttribute('aria-label', `调整数值从 ${min} 到 ${max}`);
    }
    input.setAttribute('title', `当前值: ${value}, 范围: ${min}-${max}`);
    
    const valueSpan = document.createElement('span');
    valueSpan.id = `${id}-value`;
    valueSpan.textContent = value;
    valueSpan.setAttribute('aria-live', 'polite');
    
    input.addEventListener('input', (e) => {
        const newValue = e.target.value;
        valueSpan.textContent = newValue;
        input.setAttribute('title', `当前值: ${newValue}, 范围: ${min}-${max}`);
        if (onChange) onChange(newValue);
    });
    
    container.appendChild(input);
    container.appendChild(valueSpan);
    
    return container;
}

// 创建控制区域
export function createControlSection(title, icon = '') {
    const section = document.createElement('div');
    section.className = 'control-section';
    
    const heading = document.createElement('h3');
    heading.innerHTML = `${icon} ${title}`;
    
    section.appendChild(heading);
    
    return section;
}

// 创建按钮组
export function createButtonGroup(buttons) {
    const group = document.createElement('div');
    group.className = 'btn-group';
    
    buttons.forEach(btnConfig => {
        const button = document.createElement('button');
        button.className = btnConfig.primary ? 'btn btn-primary' : 'btn';
        button.id = btnConfig.id || '';
        button.innerHTML = `${btnConfig.icon || ''} ${btnConfig.text}`;
        
        if (btnConfig.onClick) {
            button.addEventListener('click', btnConfig.onClick);
        }
        
        group.appendChild(button);
    });
    
    return group;
}

// 获取后端端口
function getBackendPort() {
    // 从URL参数获取
    const urlParams = new URLSearchParams(window.location.search);
    const backendParam = urlParams.get('backend');
    if (backendParam) {
        return parseInt(backendParam);
    }
    
    // 从当前前端端口推断
    const currentPort = parseInt(window.location.port) || 8080;
    return currentPort === 8084 ? 8082 : (currentPort === 8080 ? 8081 : currentPort + 1);
}

// 与后端通信
export async function sendToBackend(endpoint, data) {
    const backendPort = getBackendPort();
    
    try {
        const response = await fetch(`http://localhost:${backendPort}${endpoint}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        return await response.json();
    } catch (error) {
        console.error(`后端通信错误 (端口 ${backendPort}):`, error);
        throw error;
    }
}