| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- // common.js - 通用功能和初始化函数
- // 页面加载完成后初始化
- document.addEventListener('DOMContentLoaded', function () {
- // 添加初始示值误差测试点(因为已移除所有默认测试点)
- if (document.querySelectorAll('#error-inputs .error-point-container').length === 0) {
- addErrorPoint();
- }
- // 调用各个模块的占位符更新函数
- if (typeof updateRepeatabilityInputPlaceholders === 'function') {
- updateRepeatabilityInputPlaceholders();
- }
- if (typeof updateStabilityInputPlaceholders === 'function') {
- updateStabilityInputPlaceholders();
- }
- // 调用各个模块的删除按钮更新函数
- if (typeof updateRepeatabilityDeleteButtons === 'function') {
- updateRepeatabilityDeleteButtons();
- }
- if (typeof updateStabilityDeleteButtons === 'function') {
- updateStabilityDeleteButtons();
- }
- // 调用updateStabilityInputs以保持向后兼容性
- if (typeof updateStabilityInputs === 'function') {
- updateStabilityInputs();
- }
- // 初始化示值误差测试点的删除按钮
- updateErrorPoints();
- loadTimerSettings();
- // 初始化页面数据
- initializePage();
- });
- // 标签页切换功能
- document.querySelectorAll('.tab').forEach(tab => {
- tab.addEventListener('click', function() {
- // 保存当前自定义参数
- saveCustomParamsData();
- // 切换标签页
- const tabId = this.getAttribute('data-tab');
- document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
- document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
- this.classList.add('active');
- document.getElementById(tabId).classList.add('active');
- });
- });
- // 通用函数
- function createInputRow(index, type, onInputFunction, onDeleteFunction) {
- const row = document.createElement('div');
- row.className = 'input-row';
- const timeSpan = document.createElement('span');
- timeSpan.id = `${type}-time-text-${index}`;
- timeSpan.textContent = `${index}: `;
- const input = document.createElement('input');
- input.type = 'number';
- input.placeholder = '输入值';
- input.id = `${type}-value-${index}`;
- input.oninput = function() { onInputFunction(index); };
- const deleteBtn = document.createElement('span');
- deleteBtn.className = 'delete-btn';
- deleteBtn.innerHTML = '×';
- deleteBtn.onclick = function() { onDeleteFunction(this); };
- row.appendChild(timeSpan);
- row.appendChild(input);
- row.appendChild(deleteBtn);
- return row;
- }
- function updateInputRows(selector, minRows) {
- const rows = document.querySelectorAll(selector);
- rows.forEach((row, index) => {
- const deleteBtn = row.querySelector('.delete-btn');
- if (index < minRows - 1) {
- deleteBtn.style.visibility = 'hidden';
- } else {
- deleteBtn.style.visibility = 'visible';
- }
- });
- }
- function recordTimeAndCalculate(type, index, timesArray, calculateFunction) {
- const now = new Date();
- const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
- // 更新时间显示
- document.getElementById(`${type}-time-text-${index}`).textContent = `${timeString}: `;
- // 记录测试时间
- timesArray[index - 1] = timeString;
- // 执行计算
- calculateFunction();
- }
- function createOrUpdateChart(chartInstance, ctx, labels, data, label) {
- if (chartInstance) {
- chartInstance.data.labels = labels;
- chartInstance.data.datasets[0].data = data;
- chartInstance.update();
- return chartInstance;
- } else {
- return new Chart(ctx, {
- type: 'line',
- data: {
- labels: labels,
- datasets: [{
- label: label,
- data: data,
- borderColor: 'rgba(75, 192, 192, 1)',
- backgroundColor: 'rgba(75, 192, 192, 0.2)',
- borderWidth: 2,
- pointRadius: 5,
- pointBackgroundColor: 'rgba(75, 192, 192, 1)',
- tension: 0.1
- }]
- },
- options: {
- responsive: true,
- maintainAspectRatio: false,
- scales: {
- y: {
- beginAtZero: false
- }
- }
- }
- });
- }
- }
- function getNumericInputs(selector) {
- return Array.from(document.querySelectorAll(selector))
- .map(input => parseFloat(input.value))
- .filter(value => !isNaN(value));
- }
- function updateResultDisplay(id, value, precision = 6) {
- const element = document.getElementById(id);
- if (element) {
- if (value === null || value === undefined || isNaN(value)) {
- element.innerText = '-';
- } else {
- // 格式化数字,去除尾随的0
- const formatted = parseFloat(value.toFixed(precision)).toString();
- element.innerText = formatted;
- }
- }
- }
- function updateInputPlaceholders() {
- updateInputRows('#repeatability-inputs .input-row', 6);
- }
- function updateStabilityInputs() {
- updateInputRows('#stability-inputs .input-row', 6);
- }
- function updateErrorPoints() {
- const pointContainers = document.querySelectorAll('#error-inputs .error-point-container');
- pointContainers.forEach((container, index) => {
- const deleteBtn = container.querySelector('.delete-btn');
- if (pointContainers.length <= 1) {
- deleteBtn.style.visibility = 'hidden';
- } else {
- deleteBtn.style.visibility = 'visible';
- }
- });
- }
- function updateProjectTitle() {
- const titleInput = document.getElementById('project-title');
- const title = titleInput.value.trim() || '无标题';
- updateDocumentTitle();
- setCookie('projectTitle', title, 365);
- }
- function updateDocumentTitle() {
- const titleInput = document.getElementById('project-title');
- const title = titleInput.value.trim() || '无标题';
- document.title = `${title} - LabStatistics`;
- }
- function newProject() {
- if (confirm('确定要新建工程吗?这将清除所有当前数据!')) {
- // 清除所有数据
- clearAllData();
- // 重置标题
- document.getElementById('project-title').value = '';
- updateProjectTitle();
- // 重新初始化页面
- initializePage();
- }
- }
- function clearAllData() {
- // 清除重复性数据
- const repeatabilityInputs = document.querySelectorAll('#repeatability-inputs .input-row');
- for (let i = 6; i < repeatabilityInputs.length; i++) {
- repeatabilityInputs[i].remove();
- }
- document.querySelectorAll('#repeatability-inputs input[type="number"]').forEach(input => {
- input.value = '';
- });
- repeatabilityTimes = [];
- // 重置重复性时间标签
- for (let i = 1; i <= 6; i++) {
- const timeSpan = document.getElementById(`repeatability-time-text-${i}`);
- if (timeSpan) {
- timeSpan.textContent = `${i}: `;
- }
- }
- // 清除稳定性数据
- const stabilityInputs = document.querySelectorAll('#stability-inputs .input-row');
- for (let i = 6; i < stabilityInputs.length; i++) {
- stabilityInputs[i].remove();
- }
- document.querySelectorAll('#stability-inputs input[type="number"]').forEach(input => {
- input.value = '';
- });
- stabilityTimes = [];
- // 重置稳定性时间标签
- const stabilityTimes = [0, 10, 20, 30, 40, 50];
- for (let i = 1; i <= 6; i++) {
- const timeSpan = document.getElementById(`stability-time-text-${i}`);
- if (timeSpan) {
- timeSpan.textContent = `${stabilityTimes[i-1]}: `;
- }
- }
- // 清除示值误差数据
- const errorContainer = document.getElementById('error-inputs');
- while (errorContainer.firstChild) {
- errorContainer.removeChild(errorContainer.firstChild);
- }
- addErrorPoint();
- // 清除自定义参数
- const customParamsContainer = document.getElementById('custom-params-container');
- while (customParamsContainer.firstChild) {
- customParamsContainer.removeChild(customParamsContainer.firstChild);
- }
- // 重置图表
- if (chartInstance) {
- chartInstance.destroy();
- chartInstance = null;
- }
- if (stabilityChartInstance) {
- stabilityChartInstance.destroy();
- stabilityChartInstance = null;
- }
- if (errorChartInstance) {
- errorChartInstance.destroy();
- errorChartInstance = null;
- }
- // 清除结果显示
- document.querySelectorAll('.result-item span').forEach(span => {
- span.innerText = '-';
- });
- // 清除cookies
- deleteCookie('repeatabilityData');
- deleteCookie('stabilityData');
- deleteCookie('errorData');
- deleteCookie('customParamsData');
- }
- function initializePage() {
- // 加载项目标题
- const savedTitle = getCookie('projectTitle');
- if (savedTitle) {
- document.getElementById('project-title').value = savedTitle;
- updateDocumentTitle();
- }
- // 加载自定义参数
- loadCustomParamsFromCookies();
- // 加载重复性、稳定性和示值误差数据
- loadDataFromCookies();
- }
- // 更新当前时间显示
- function updateCurrentTime() {
- const now = new Date();
- const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
- document.getElementById('current-time').textContent = timeString;
- setTimeout(updateCurrentTime, 1000);
- }
- // 初始化时启动时间更新
- updateCurrentTime();
|