fileViewer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. let currentFileName = null; // Store the current file name
  2. let trajectoryDirectory = ''; // Global variable to store the directory
  3. let timeoutIds = []; // Store timeout IDs for pending operations
  4. function getBaseUrl() {
  5. const protocol = window.location.protocol;
  6. const host = window.location.hostname;
  7. const port = window.location.port;
  8. // Use the default port if the port number is empty (for standard HTTP/HTTPS)
  9. const defaultPort = (protocol === 'http:' && !port) ? '80' : (protocol === 'https:' && !port) ? '443' : port;
  10. return `${protocol}//${host}:${defaultPort}`;
  11. }
  12. function fetchFiles() {
  13. const baseUrl = getBaseUrl();
  14. fetch(`${baseUrl}/files`)
  15. .then(response => response.json())
  16. .then(files => {
  17. const fileList = document.getElementById('fileList');
  18. fileList.innerHTML = '';
  19. files.forEach(file => {
  20. const fileElement = document.createElement('li');
  21. fileElement.textContent = file;
  22. fileElement.onclick = () => viewFile(file.split(' ')[0]);
  23. fileList.appendChild(fileElement);
  24. });
  25. });
  26. }
  27. function viewFile(fileName) {
  28. // Clear any pending message loading from previous files
  29. timeoutIds.forEach(timeoutId => clearTimeout(timeoutId));
  30. timeoutIds = []; // Reset the list of timeout IDs
  31. const baseUrl = getBaseUrl();
  32. fetch(`${baseUrl}/trajectory/${fileName}`)
  33. .then(response => {
  34. if (!response.ok) {
  35. throw new Error('Network response was not ok');
  36. }
  37. return response.json();
  38. })
  39. .then(content => {
  40. const container = document.getElementById('fileContent');
  41. container.innerHTML = ''; // Clear existing content
  42. if (content.history && Array.isArray(content.history)) {
  43. let delay = 200; // Initial delay
  44. const delayIncrement = 50; // Delay between each message, in milliseconds
  45. content.history.forEach((item, index) => {
  46. const timeoutId = setTimeout(() => {
  47. const contentText = item.content ? item.content.replace(/</g, '&lt;').replace(/>/g, '&gt;') : '';
  48. let roleClass = item.agent && item.agent !== "primary" ? "subroutine" : item.role ? item.role.toLowerCase().replaceAll(' ', '-') : 'default';
  49. const elementId = 'historyItem' + index;
  50. const historyItem = document.createElement('div');
  51. historyItem.className = `history-item ${roleClass} fade-in`;
  52. historyItem.id = elementId;
  53. if (contentText.includes("--- DEMONSTRATION ---")) {
  54. item.role = "demo";
  55. }
  56. else if ('is_demo' in item && item.is_demo === true) {
  57. item.role += '[demo]';
  58. }
  59. historyItem.innerHTML = `
  60. <div class="role-bar ${roleClass}">
  61. <strong>
  62. <span>${item.role}</span>
  63. </strong>
  64. </div>
  65. <div class="content-container">
  66. <pre>${contentText}</pre>
  67. </div>
  68. <div class="shadow"></div>
  69. `;
  70. container.appendChild(historyItem);
  71. }, delay);
  72. delay += delayIncrement; // Increment delay for the next message
  73. timeoutIds.push(timeoutId); // Store the timeout ID
  74. });
  75. } else {
  76. container.textContent = 'No history content found.';
  77. }
  78. })
  79. .catch(error => {
  80. console.error('Error fetching file:', error);
  81. document.getElementById('fileContent').textContent = 'Error loading content. ' + error;
  82. });
  83. // Highlight the selected file in the list
  84. document.querySelectorAll('#fileList li').forEach(li => {
  85. li.classList.remove('selected');
  86. if (li.textContent.split(' ')[0] === fileName) {
  87. li.classList.add('selected');
  88. }
  89. });
  90. }
  91. function refreshCurrentFile() {
  92. if (currentFileName) {
  93. const currentScrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
  94. viewFile(currentFileName.split(' ')[0]); // Reload the current file
  95. // Restore the scroll position after the content is loaded
  96. setTimeout(() => {
  97. window.scrollTo(0, currentScrollPosition);
  98. }, 100);
  99. }
  100. }
  101. function fetchDirectoryInfo() {
  102. const baseUrl = getBaseUrl();
  103. fetch(`${baseUrl}/directory_info`)
  104. .then(response => response.json())
  105. .then(data => {
  106. if (data.directory) {
  107. trajectoryDirectory = data.directory; // Store the directory
  108. document.title = `Trajectory Viewer: ${data.directory}`;
  109. document.querySelector('h1').textContent = `Trajectory Viewer: ${data.directory}`;
  110. }
  111. })
  112. .catch(error => console.error('Error fetching directory info:', error));
  113. }
  114. window.onload = function() {
  115. fetchFiles();
  116. fetchDirectoryInfo();
  117. };