demo3/visa-scpi-example/index.html

254 lines
8.0 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SCPI命令参数编辑器</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
box-sizing: border-box;
}
.command-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 10px;
}
.command-row {
box-sizing: border-box;
height: inherit;
/* margin: 10px 0; */
padding: 10px ;
border: 1px solid #ddd;
border-radius: 4px;
display: flex;
flex: 1 1 30%;
flex-direction: column;
/* width: calc(30% - 20px); */
}
.comment-input {
display: inline-block;
width: 100%;
height: 100%;
padding: 5px;
border: none;
background: none;
font-size: 14px;
cursor: text;
}
.command-label {
display: inline-block;
width: auto;
font-family: monospace;
margin-top: 5px;
}
.value-input {
width: auto;
padding: 5px;
margin-top: 5px;
}
.button-container {
display: flex;
justify-content: space-between;
gap: 10px;
}
#update-btn {
width: 50%;
margin-top: 10px;
padding: 5px 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
#update-btn:hover {
background-color: #45a049;
}
#reset-btn {
width: 50%;
margin-top: 10px;
padding: 5px 10px;
background-color: #f44336;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
#reset-btn:hover {
background-color: #e53935;
}
.highlight {
background-color: yellow; /* 高亮更新后的值 */
}
/* 添加 toast 样式 */
.toast {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #333;
color: white;
padding: 12px 24px;
border-radius: 4px;
font-size: 14px;
opacity: 0;
transition: opacity 0.3s ease-in-out;
z-index: 1000;
}
.toast.show {
opacity: 1;
}
.toast.success {
background-color: #4CAF50;
}
.toast.error {
background-color: #f44336;
}
</style>
</head>
<body>
<h1>SCPI命令参数编辑器</h1>
<div class="command-container" id="commands-container"></div>
<div class="button-container">
<button id="reset-btn">复位</button>
<button id="update-btn">更新</button>
</div>
<script>
let commandsData = [];
async function loadCommands() {
const container = document.getElementById('commands-container');
container.innerHTML = '';
const response = await fetch('commands.json');
commandsData = await response.json();
const commentsResponse = await fetch('comments.json');
const comments = await commentsResponse.json();
commandsData.forEach(({ command, value }) => {
const row = document.createElement('div');
row.className = 'command-row';
const commentDisplay = document.createElement('span');
commentDisplay.className = 'comment-input';
commentDisplay.textContent = comments[command] || '';
commentDisplay.ondblclick = () => {
const commentInput = document.createElement('input');
commentInput.type = 'text';
commentInput.value = commentDisplay.textContent;
commentInput.onblur = async () => {
comments[command] = commentInput.value;
commentDisplay.textContent = commentInput.value;
row.replaceChild(commentDisplay, commentInput);
await fetch('/save_comments', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(comments)
});
};
row.replaceChild(commentInput, commentDisplay);
commentInput.focus();
};
const label = document.createElement('span');
label.className = 'command-label';
label.textContent = command;
const input = document.createElement('input');
input.className = 'value-input';
// 仅支持输入数字
input.type = 'number';
input.value = value;
input.onfocus = () => {
// 天蓝色高亮所在div
row.style.backgroundColor = 'lightblue';
};
input.onblur = () => {
row.style.backgroundColor = '';
};
// 与原值比较,变化则高亮
input.oninput = () => {
if (input.value !== value) {
input.classList.add('highlight');
}
else {
input.classList.remove('highlight');
}
};
row.appendChild(commentDisplay);
row.appendChild(label);
row.appendChild(input);
container.appendChild(row);
});
}
function showToast(message, type = 'success') {
// 移除现有的 toast
const existingToast = document.querySelector('.toast');
if (existingToast) {
existingToast.remove();
}
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = message;
document.body.appendChild(toast);
// 触发重排以显示动画
setTimeout(() => toast.classList.add('show'), 10);
// 3秒后自动消失
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
document.getElementById('update-btn').onclick = async () => {
const updates = commandsData.map(({ command }, index) => ({
command: command,
value: document.querySelectorAll('.value-input')[index].value
}));
// 根据回应判断是否更新成功
const response = await fetch('/update_command', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updates)
});
if (response.ok) {
const result = await response.json();
if (result.success) {
showToast('更新成功!');
} else {
showToast('更新失败!', 'error');
}
} else {
showToast('网络错误,无法更新!', 'error');
}
};
document.getElementById('reset-btn').onclick = () => {
loadCommands(); // 重新加载命令以复位
};
window.onload = loadCommands;
</script>
</body>
</html>