library.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <br>
  2. <div class="ui container" style="margin-top: 20px;">
  3. <h2 class="ui header">Image Library</h2>
  4. <p>The table below shows the screenshots pasted into the editor</p>
  5. <table class="ui celled table">
  6. <thead>
  7. <tr>
  8. <th>Image Name</th>
  9. <th>Upload Time</th>
  10. <th>Preview</th>
  11. <th>Delete</th>
  12. </tr>
  13. </thead>
  14. <tbody id="libraryTableBody">
  15. <tr>
  16. <td colspan="4" style="text-align: center;">
  17. <div class="ui active inline loader"></div>
  18. <span>Loading...</span>
  19. </td>
  20. </tr>
  21. </tbody>
  22. </table>
  23. <button class="ui green basic button" onclick="initImageLibrary();">
  24. <i class="refresh icon"></i> Refresh
  25. </button>
  26. <div class="ui divider"></div>
  27. <!-- Image Preview Section -->
  28. <h3>Image Preview</h3>
  29. <p>Click on the preview button to see the image</p>
  30. <div class="ui segment">
  31. <img id="library_image_preview_element" class="ui centered big image" src="">
  32. </div>
  33. <br><br>
  34. </div>
  35. <script>
  36. function initImageLibrary(){
  37. // Fetch the list of posts from the server
  38. $("#libraryTableBody").html(`<tr><td colspan="4" style="text-align: center;"><div class="ui active inline loader"></div><span>Loading...</span></td></tr>`);
  39. $.get("/api/fs/list?dir=/site/img/", function(data){
  40. console.log(data);
  41. let imageList = data.filter(file => (file.Filename.endsWith(".jpg") || file.Filename.endsWith(".png")) && !file.IsDir);
  42. imageList.sort((a, b) => {
  43. const getTimestamp = filename => parseInt(filename.split('_')[0]);
  44. return getTimestamp(b.Filename) - getTimestamp(a.Filename); // Sort by timestamp in filename
  45. });
  46. //Render the images in the table
  47. let tableBody = $("#libraryTableBody");
  48. tableBody.empty(); // Clear existing rows
  49. imageList.forEach(image => {
  50. const imageName = image.Filename.split('/').pop(); // Extract the image name
  51. const imagePath = "/site/img/" + image.Filename;
  52. const uploadTimestamp = parseInt(imageName.split('.')[0]); // Extract the timestamp from the filename
  53. const uploadDate = new Date(uploadTimestamp).toLocaleString(); // Convert timestamp to local time
  54. tableBody.append(`
  55. <tr>
  56. <td>${imageName.replace(/^\d+_/, '')}</td>
  57. <td>${uploadDate}</td>
  58. <td><button class="ui basic button" onclick="previewImage('${imagePath}');">Preview</button></td>
  59. <td><button class="ui basic red button" onclick="deleteImage('${imagePath}');">Delete</button></td>
  60. </tr>
  61. `);
  62. });
  63. if (imageList.length == 0) {
  64. tableBody.append(`
  65. <tr id="noimage">
  66. <td colspan="4" style="text-align: center;"><i class="ui green circle check icon"></i> No images / screenshots</td>
  67. </tr>
  68. `);
  69. }
  70. });
  71. }
  72. function previewImage(imagePath) {
  73. // Set the image source to the selected image path
  74. $("#library_image_preview_element").attr("src", imagePath);
  75. $("#library_image_preview_element").show(); // Show the image preview element
  76. }
  77. function deleteImage(imagePath) {
  78. // Confirm deletion
  79. if (confirm("Are you sure you want to delete this image?")) {
  80. $.ajax({
  81. url: "/api/fs/del?target=" + imagePath,
  82. method: "POST",
  83. success: function(data) {
  84. if (data.error !== undefined) {
  85. msgbox(`<i class="ui red times icon"></i> Error deleting image: ` + data.error);
  86. } else {
  87. msgbox(`<i class="ui green check icon"></i> Image deleted`);
  88. initImageLibrary(); // Refresh the image library
  89. $("#library_image_preview_element").attr("src", "").hide(); // Clear and hide the image preview
  90. }
  91. },
  92. error: function() {
  93. alert("Error deleting image.");
  94. }
  95. });
  96. }
  97. }
  98. initImageLibrary();
  99. </script>