-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform-modal.js
More file actions
440 lines (379 loc) · 15.3 KB
/
form-modal.js
File metadata and controls
440 lines (379 loc) · 15.3 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// 预设表单数据
const presetFormData = {
token: {
input: 1769,
output: 246
},
services: [
{
name: "A",
recharge_amount: 1,
currency: "USD",
balance: 1,
input_price: 12.5,
output_price: 5,
unified_io: false,
token_unit: "M"
},
{
name: "B",
recharge_amount: 1.5,
currency: "CNY",
balance: 1,
input_price: 0.1,
output_price: 0.1,
unified_io: true,
token_unit: "K"
},
{
name: "C",
recharge_amount: 5.49,
currency: "USD",
balance: 5,
input_price: 0.015,
output_price: 0.075,
unified_io: false,
token_unit: "K"
}
]
};
// 表单模态框功能实现
// 只暴露弹窗相关函数,不直接绑定按钮事件
window.openSaveFormModal = openSaveFormModal;
window.openHistoryFormModal = openHistoryFormModal;
// 初始化模态框和预设表单
initFormModal();
addPresetFormToStorage();
// 初始化表单模态框
function initFormModal() {
// 直接绑定事件(DOM已在index.html中)
setupSaveFormModalEvents();
setupHistoryFormModalEvents();
}
// 设置保存表单模态框事件
function setupSaveFormModalEvents() {
const modal = document.getElementById('saveFormModal');
const closeBtn = modal.querySelector('.close-btn');
const saveBtn = document.getElementById('saveFormConfirm');
// 关闭按钮事件
closeBtn.addEventListener('click', () => {
console.log('点击保存表单弹窗关闭按钮');
closeSaveFormModal();
});
// 点击模态框外部关闭
window.addEventListener('click', (event) => {
if (event.target === modal) {
console.log('点击保存表单弹窗外部关闭');
closeSaveFormModal();
}
});
// 保存表单按钮事件
saveBtn.addEventListener('click', () => {
console.log('点击保存表单按钮');
saveFormWithName();
});
// 表单名称输入框支持Enter触发保存
const formNameInput = document.getElementById('formName');
if (formNameInput) {
formNameInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
console.log('回车保存表单');
saveFormWithName();
}
});
}
}
// 设置历史记录模态框事件
function setupHistoryFormModalEvents() {
const modal = document.getElementById('historyFormModal');
const closeBtn = modal.querySelector('.close-btn');
// 关闭按钮事件
closeBtn.addEventListener('click', () => {
console.log('点击历史记录弹窗关闭按钮');
closeHistoryFormModal();
});
// 点击模态框外部关闭
window.addEventListener('click', (event) => {
if (event.target === modal) {
console.log('点击历史记录弹窗外部关闭');
closeHistoryFormModal();
}
});
}
// 打开保存表单模态框
function openSaveFormModal() {
const modal = document.getElementById('saveFormModal');
document.getElementById('formName').value = '';
modal.style.display = 'flex';
}
// 关闭保存表单模态框
function closeSaveFormModal() {
const modal = document.getElementById('saveFormModal');
modal.style.display = 'none';
document.getElementById('formName').value = '';
}
// 打开历史记录模态框
function openHistoryFormModal() {
const modal = document.getElementById('historyFormModal');
const formList = document.getElementById('formList');
const noFormsMessage = document.getElementById('noFormsMessage');
// 清空表单列表
formList.innerHTML = '';
// 加载已保存的表单
const forms = JSON.parse(localStorage.getItem('formHistory') || '[]');
if (forms.length > 0) {
forms.forEach((form, index) => {
const li = document.createElement('li');
li.innerHTML = `
<div class="form-name">${form.name}</div>
<button class="delete-btn" data-index="${index}">
<svg viewBox="0 0 24 24" width="16" height="16">
<path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/>
</svg>
</button>
`;
// 添加点击加载事件
li.addEventListener('click', (e) => {
// 如果点击的是删除按钮或其子元素,不触发加载
if (e.target.closest('.delete-btn')) {
return;
}
loadForm(index);
});
formList.appendChild(li);
});
// 绑定删除按钮事件
formList.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation(); // 阻止事件冒泡到列表项
const index = e.currentTarget.getAttribute('data-index');
deleteForm(index);
});
});
// 显示表单列表,隐藏无表单消息
formList.style.display = 'block';
noFormsMessage.style.display = 'none';
} else {
// 隐藏表单列表,显示无表单消息
formList.style.display = 'none';
noFormsMessage.style.display = 'block';
}
// 显示模态框
modal.style.display = 'flex';
}
// 关闭历史记录模态框
function closeHistoryFormModal() {
const modal = document.getElementById('historyFormModal');
modal.style.display = 'none';
}
// 保存表单
function saveFormWithName() {
const formName = document.getElementById('formName').value.trim();
if (!formName) {
// 只高亮输入框,不显示红色提示字
const input = document.getElementById('formName');
if (input) {
input.classList.add('error');
const tip = document.getElementById('formName-error-tip');
if (tip) tip.remove();
}
return;
}
// 获取当前表单数据
const formData = getCurrentFormData();
// 获取已保存的表单历史
const forms = JSON.parse(localStorage.getItem('formHistory') || '[]');
// 添加新表单
forms.push({
name: formName,
data: formData
});
// 保存到本地存储
localStorage.setItem('formHistory', JSON.stringify(forms));
// 保存成功后移除高亮
const input = document.getElementById('formName');
if (input) {
input.classList.remove('error');
const tip = document.getElementById('formName-error-tip');
if (tip) tip.remove();
}
// 关闭模态框
closeSaveFormModal();
// 不再弹窗,直接关闭弹窗或刷新历史列表即可
}
// 加载表单
function loadForm(index) {
const forms = JSON.parse(localStorage.getItem('formHistory') || '[]');
if (index >= 0 && index < forms.length) {
// 使用script.js中的populateForm函数
const formData = forms[index].data;
document.getElementById('inputtokens').value = formData.inputTokens || '';
document.getElementById('outputtokens').value = formData.outputTokens || '';
// 清除当前所有行
const tbody = document.querySelector('#providersTable tbody');
tbody.innerHTML = '';
// 添加服务商行
if (formData.providers && formData.providers.length > 0) {
formData.providers.forEach(provider => {
const row = addProviderRow(false);
row.querySelector('.provider-name').value = provider.providerName || '';
row.querySelector('.recharge-amount').value = provider.recharge_amount || '';
row.querySelector('.currency').value = provider.currency || 'USD';
row.querySelector('.balance').value = provider.balance || '';
row.querySelector('.input-price').value = provider.input_price || '';
row.querySelector('.output-price').value = provider.output_price || '';
row.querySelector('.same-price').checked = !!provider.same_price_checked;
row.querySelector('.token-unit').value = provider.token_unit || '1000';
});
} else {
// 确保至少有一行
ensureMinimumRows();
}
// 更新导航数组
updateNavigationArray();
adjustFrameHeights();
closeHistoryFormModal();
}
}
// 删除表单
function deleteForm(index) {
const forms = JSON.parse(localStorage.getItem('formHistory') || '[]');
if (index >= 0 && index < forms.length) {
const formName = forms[index].name;
// 使用自定义弹窗确认删除
showCustomConfirm(`确定要删除表单 "${formName}" 吗?`, function() {
forms.splice(index, 1);
localStorage.setItem('formHistory', JSON.stringify(forms));
// 刷新表单列表
openHistoryFormModal();
});
}
}
// 显示自定义确认弹窗
function showCustomConfirm(message, confirmCallback) {
// 使用现有的自定义弹窗
const modal = document.getElementById('customAlert');
const alertMessage = document.getElementById('alertMessage');
const confirmBtn = document.getElementById('alertConfirm');
const cancelBtn = document.getElementById('alertCancel');
// 如果取消按钮不存在,创建一个
if (!cancelBtn) {
const newCancelBtn = document.createElement('button');
newCancelBtn.id = 'alertCancel';
newCancelBtn.textContent = '取消';
document.querySelector('.alert-buttons').appendChild(newCancelBtn);
}
// 设置消息
alertMessage.textContent = message;
// 显示弹窗
modal.style.display = 'flex';
// 确认按钮点击事件
const handleConfirm = function() {
modal.style.display = 'none';
confirmBtn.removeEventListener('click', handleConfirm);
document.getElementById('alertCancel').removeEventListener('click', handleCancel);
confirmCallback();
};
// 取消按钮点击事件
const handleCancel = function() {
modal.style.display = 'none';
confirmBtn.removeEventListener('click', handleConfirm);
document.getElementById('alertCancel').removeEventListener('click', handleCancel);
};
// 绑定事件
confirmBtn.addEventListener('click', handleConfirm);
document.getElementById('alertCancel').addEventListener('click', handleCancel);
}
// 获取当前表单数据
function getCurrentFormData() {
// 当前使用script.js中的getFormData函数格式
const providers = Array.from(document.querySelectorAll('#providersTable tbody tr')).map(row => {
const nameInput = row.querySelector('.provider-name');
if (!nameInput || !nameInput.value.trim()) return null;
return {
providerName: row.querySelector('.provider-name').value,
recharge_amount: row.querySelector('.recharge-amount').value,
currency: row.querySelector('.currency').value,
balance: row.querySelector('.balance').value,
input_price: row.querySelector('.input-price').value,
output_price: row.querySelector('.output-price').value,
same_price_checked: row.querySelector('.same-price').checked,
token_unit: row.querySelector('.token-unit').value
};
}).filter(item => item !== null);
const formData = {
inputTokens: document.getElementById('inputtokens').value,
outputTokens: document.getElementById('outputtokens').value,
providers: providers
};
console.log('获取当前表单数据:', formData);
return formData;
}
// 填充表单数据
function populateForm(formData) {
// 清除当前所有行
const tbody = document.querySelector('#providersTable tbody');
tbody.innerHTML = '';
// 填充输入和输出标记数
if (formData.token) {
document.getElementById('inputtokens').value = formData.token.input || '';
document.getElementById('outputtokens').value = formData.token.output || '';
}
// 添加服务商行
if (formData.providers && formData.providers.length > 0) {
formData.providers.forEach(provider => {
const row = addProviderRow(false);
const nameInput = row.querySelector('.provider-name');
const rechargeInput = row.querySelector('.recharge-amount');
const currencySelect = row.querySelector('.currency');
const balanceInput = row.querySelector('.balance');
const inputPriceInput = row.querySelector('.input-price');
const outputPriceInput = row.querySelector('.output-price');
const singlePriceInput = row.querySelector('.single-price');
const unitSelect = row.querySelector('.token-unit');
if (nameInput) nameInput.value = provider.name || '';
if (rechargeInput) rechargeInput.value = provider.recharge || '';
if (currencySelect) currencySelect.value = provider.currency || 'USD';
if (balanceInput) balanceInput.value = provider.balance || '';
if (inputPriceInput) inputPriceInput.value = provider.inputPrice || '';
if (outputPriceInput) outputPriceInput.value = provider.outputPrice || '';
if (singlePriceInput) singlePriceInput.value = provider.singlePrice || '';
if (unitSelect) unitSelect.value = provider.unit || '1';
});
} else {
// 确保至少有一行
ensureMinimumRows();
}
// 更新导航数组
updateNavigationArray();
}
// 添加预设表单到本地存储
function addPresetFormToStorage() {
console.log('添加预设表单到本地存储');
const forms = JSON.parse(localStorage.getItem('formHistory') || '[]');
// 检查是否已存在预设表单
const presetExists = forms.some(form => form.name === '预设表单示例');
console.log('预设表单是否存在:', presetExists);
if (!presetExists) {
// 转换预设数据格式以适应应用程序
const presetData = {
inputTokens: presetFormData.token.input.toString(),
outputTokens: presetFormData.token.output.toString(),
providers: presetFormData.services.map(service => ({
providerName: service.name,
recharge_amount: service.recharge_amount.toString(),
currency: service.currency,
balance: service.balance.toString(),
input_price: service.input_price.toString(),
output_price: service.output_price.toString(),
same_price_checked: service.unified_io,
token_unit: service.token_unit === 'K' ? '1000' : '1000000'
}))
};
// 添加预设表单
forms.push({ name: '预设表单示例', data: presetData });
localStorage.setItem('formHistory', JSON.stringify(forms));
console.log('预设表单已添加');
}
}