common.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // common.js - 通用功能和初始化函数
  2. // 计时器相关变量
  3. let timerInterval = null;
  4. let timerTotalSeconds = 600; // 默认10分钟
  5. let timerCurrentSeconds = 600;
  6. let timerPaused = false;
  7. // 在页面加载完成后初始化
  8. document.addEventListener('DOMContentLoaded', function () {
  9. // 初始化页面
  10. initializePage();
  11. });
  12. // 标签页切换功能
  13. document.querySelectorAll('.tab').forEach(tab => {
  14. tab.addEventListener('click', function () {
  15. // 切换标签页
  16. const tabId = this.getAttribute('data-tab');
  17. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  18. document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
  19. this.classList.add('active');
  20. document.getElementById(tabId).classList.add('active');
  21. });
  22. });
  23. function initializePage() {
  24. showToast('加载主架构....', 'success');
  25. // 初始化时启动时间更新
  26. updateCurrentTime();
  27. loadTimerSettings();
  28. showToast('初始化页面....', 'success');
  29. // 各页面初始化
  30. initializeRepeatabilityPage();
  31. initializeStabilityPage();
  32. initializeErrorPage();
  33. initializeDataPage();
  34. showToast('加载完成', 'success');
  35. }
  36. function updateProjectTitle() {
  37. const titleInput = document.getElementById('project-title');
  38. const title = titleInput.value.trim() || '无标题';
  39. document.title = `${title} - LabStatistics`;
  40. setLocalStorage('projectTitle', title);
  41. }
  42. // 更新当前时间显示
  43. function updateCurrentTime() {
  44. const now = new Date();
  45. const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
  46. document.getElementById('current-time').textContent = timeString;
  47. setTimeout(updateCurrentTime, 1000);
  48. }
  49. // 记录时间并执行计算
  50. function recordTimeAndCalculate(type, index, timesArray, calculateFunction) {
  51. const now = new Date();
  52. const timestamp = now.getTime(); // 获取时间戳
  53. const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
  54. // 更新时间显示
  55. document.getElementById(`${type}-time-text-${index}`).textContent = `${timeString}: `;
  56. // 记录测试时间戳
  57. timesArray[index - 1] = timestamp;
  58. // 执行计算
  59. if (typeof calculateFunction === 'function') {
  60. calculateFunction();
  61. }
  62. }
  63. // 加载计时器设置
  64. function loadTimerSettings() {
  65. const savedMinutes = getLocalStorage('timerMinutes');
  66. const savedSeconds = getLocalStorage('timerSeconds');
  67. if (savedMinutes !== null && savedSeconds !== null) {
  68. const minutes = parseInt(savedMinutes);
  69. const seconds = parseInt(savedSeconds);
  70. document.getElementById('timer-minutes').value = minutes;
  71. document.getElementById('timer-seconds').value = seconds;
  72. timerTotalSeconds = minutes * 60 + seconds;
  73. timerCurrentSeconds = timerTotalSeconds;
  74. updateTimerDisplay();
  75. }
  76. }
  77. // 保存计时器设置
  78. function saveTimerSettings(minutes, seconds) {
  79. setLocalStorage('timerMinutes', minutes, 30);
  80. setLocalStorage('timerSeconds', seconds, 30);
  81. }
  82. // 打开计时器对话框
  83. function openTimerDialog() {
  84. document.getElementById('timer-dialog').style.display = 'flex';
  85. loadTimerSettings();
  86. }
  87. // 关闭计时器对话框
  88. function closeTimerDialog() {
  89. document.getElementById('timer-dialog').style.display = 'none';
  90. }
  91. // 开始计时器
  92. function startTimer() {
  93. if (timerCurrentSeconds <= 0) {
  94. return;
  95. }
  96. // 如果已暂停,则继续计时
  97. if (timerPaused) {
  98. timerPaused = false;
  99. document.querySelector('#timer-dialog button:nth-child(1)').textContent = '暂停';
  100. } else {
  101. if (document.querySelector('#timer-dialog button:nth-child(1)').textContent == '开始') {
  102. // 获取设置的时间
  103. const minutes = parseInt(document.getElementById('timer-minutes').value) || 0;
  104. const seconds = parseInt(document.getElementById('timer-seconds').value) || 0;
  105. timerTotalSeconds = minutes * 60 + seconds;
  106. timerCurrentSeconds = timerTotalSeconds;
  107. // 保存设置到localStorage
  108. saveTimerSettings(minutes, seconds);
  109. // 停止警报铃声
  110. document.getElementById('alertSound').pause();
  111. document.getElementById('alertSound').currentTime = 0;
  112. // 更新暂停按钮文本为暂停
  113. document.querySelector('#timer-dialog button:nth-child(1)').textContent = '暂停';
  114. } else {
  115. if (timerInterval) {
  116. clearInterval(timerInterval);
  117. timerInterval = null;
  118. timerPaused = true;
  119. // 停止警报铃声
  120. document.getElementById('alertSound').pause();
  121. document.getElementById('alertSound').currentTime = 0;
  122. // 更新暂停按钮文本为继续
  123. document.querySelector('#timer-dialog button:nth-child(1)').textContent = '继续';
  124. }
  125. }
  126. }
  127. closeTimerDialog();
  128. // 更新显示
  129. updateTimerDisplay();
  130. // 开始倒计时
  131. if (!timerInterval && !timerPaused) {
  132. timerInterval = setInterval(updateTimer, 1000);
  133. }
  134. }
  135. // 重置计时器
  136. function resetTimer() {
  137. if (timerInterval) {
  138. clearInterval(timerInterval);
  139. timerInterval = null;
  140. }
  141. timerPaused = false;
  142. // 获取设置的时间
  143. const minutes = parseInt(document.getElementById('timer-minutes').value) || 0;
  144. const seconds = parseInt(document.getElementById('timer-seconds').value) || 0;
  145. timerTotalSeconds = minutes * 60 + seconds;
  146. timerCurrentSeconds = timerTotalSeconds;
  147. // 更新显示
  148. updateTimerDisplay();
  149. // 停止警报铃声
  150. document.getElementById('alertSound').pause();
  151. document.getElementById('alertSound').currentTime = 0;
  152. // 更新暂停按钮文本为开始
  153. document.querySelector('#timer-dialog button:nth-child(1)').textContent = '开始';
  154. }
  155. // 更新计时器
  156. function updateTimer() {
  157. if (timerCurrentSeconds > 0) {
  158. timerCurrentSeconds--;
  159. updateTimerDisplay();
  160. // 当倒计时接近结束时播放警报
  161. if (timerCurrentSeconds <= 3 && timerCurrentSeconds > 0) {
  162. document.getElementById('alertSound').play();
  163. }
  164. } else {
  165. // 倒计时结束
  166. clearInterval(timerInterval);
  167. timerInterval = null;
  168. // 播放警报
  169. document.getElementById('alertSound').play();
  170. }
  171. }
  172. // 更新计时器显示
  173. function updateTimerDisplay() {
  174. const minutes = Math.floor(timerCurrentSeconds / 60);
  175. const seconds = timerCurrentSeconds % 60;
  176. const displayText = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  177. document.getElementById('countdown').textContent = displayText;
  178. }
  179. // 自定义参数输入行创建函数
  180. function createInputRow(index, type, onInputFunction, onDeleteFunction) {
  181. const row = document.createElement('div');
  182. row.className = 'input-row';
  183. const timeSpan = document.createElement('span');
  184. timeSpan.id = `${type}-time-text-${index}`;
  185. timeSpan.textContent = `${index}: `;
  186. const input = document.createElement('input');
  187. input.type = 'number';
  188. input.placeholder = '输入值';
  189. input.id = `${type}-value-${index}`;
  190. input.oninput = function () { onInputFunction(index); };
  191. const deleteBtn = document.createElement('div');
  192. deleteBtn.className = 'delete-btn';
  193. deleteBtn.innerHTML = '×';
  194. deleteBtn.onclick = function () { onDeleteFunction(this); };
  195. row.appendChild(timeSpan);
  196. row.appendChild(input);
  197. row.appendChild(deleteBtn);
  198. return row;
  199. }
  200. function getNumericInputs(selector) {
  201. return Array.from(document.querySelectorAll(selector))
  202. .map(input => parseFloat(input.value))
  203. .filter(value => !isNaN(value));
  204. }
  205. function updateResultDisplay(id, value, precision = 6) {
  206. const element = document.getElementById(id);
  207. if (element) {
  208. if (value === null || value === undefined || isNaN(value)) {
  209. element.innerText = '-';
  210. } else {
  211. // 格式化数字,去除尾随的0
  212. const formatted = parseFloat(value.toFixed(precision)).toString();
  213. element.innerText = formatted;
  214. }
  215. }
  216. }
  217. function createOrUpdateChart(chartInstance, ctx, labels, data, label) {
  218. if (chartInstance) {
  219. chartInstance.data.labels = labels;
  220. chartInstance.data.datasets[0].data = data;
  221. chartInstance.update();
  222. return chartInstance;
  223. } else {
  224. return new Chart(ctx, {
  225. type: 'line',
  226. data: {
  227. labels: labels,
  228. datasets: [{
  229. label: label,
  230. data: data,
  231. borderColor: 'rgba(75, 192, 192, 1)',
  232. backgroundColor: 'rgba(75, 192, 192, 0.2)',
  233. borderWidth: 2,
  234. pointRadius: 5,
  235. pointBackgroundColor: 'rgba(75, 192, 192, 1)',
  236. tension: 0.1
  237. }]
  238. },
  239. options: {
  240. responsive: true,
  241. maintainAspectRatio: false,
  242. scales: {
  243. y: {
  244. beginAtZero: false
  245. }
  246. }
  247. }
  248. });
  249. }
  250. }
  251. // 收集并保存数据到localStorage
  252. function collectAndSaveData(selector, times, resultIds, dataName) {
  253. // 收集输入数据
  254. const inputs = Array.from(document.querySelectorAll(selector)).map((input, index) => {
  255. return {
  256. time: times[index] ? times[index] : null,
  257. value: input.value
  258. };
  259. });
  260. // 收集结果数据
  261. const results = {};
  262. if (resultIds) {
  263. Object.keys(resultIds).forEach(key => {
  264. const element = document.getElementById(resultIds[key]);
  265. if (element) {
  266. results[key] = element.innerText;
  267. }
  268. });
  269. }
  270. // 保存到localStorage
  271. const data = {
  272. inputs: inputs,
  273. results: results
  274. };
  275. console.log(dataName, data);
  276. setLocalStorage(dataName, JSON.stringify(data));
  277. }