all.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <br>
  2. <div class="ui container">
  3. <div class="ui basic segment">
  4. <h2 class="ui header">All Posts</h2>
  5. <table class="ui single line table">
  6. <thead>
  7. <tr>
  8. <th>Post Name</th>
  9. <th>Creation Time</th>
  10. <th>Edit</th>
  11. <th>Delete</th>
  12. </tr>
  13. </thead>
  14. <tbody id="postTableBody">
  15. <tr id="loadingRow">
  16. <td colspan="4" style="text-align: center;">
  17. <div class="ui active inline loader"></div>
  18. </td>
  19. </tr>
  20. <!-- Rows will be dynamically loaded here -->
  21. </tbody>
  22. </table>
  23. <button class="ui green basic button" id="refreshButton">
  24. <i class="ui refresh icon"></i> Refresh
  25. </button>
  26. </div>
  27. <div class="ui basic small modal" id="deleteConfirmModal">
  28. <div class="ui icon header">
  29. <i class="trash alternate outline icon"></i>
  30. Confirm Delete
  31. </div>
  32. <div class="content" align="center">
  33. <p>Are you sure you want to delete the post <span id="deletePostName"></span>?</p>
  34. </div>
  35. <div class="actions">
  36. <div class="ui red ok inverted button" onclick="confirmDelete()" deleteingPostID="">
  37. <i class="trash icon"></i>
  38. Yes
  39. </div>
  40. <div class="ui basic cancel inverted button">
  41. <i class="remove icon"></i>
  42. Cancel
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. <script>
  48. /*
  49. Post List
  50. This script will fetch the list of posts from /www/site/posts/*.md
  51. and display them in a table format.
  52. */
  53. function getPosts(){
  54. // Show loading animation
  55. $("#postTableBody").html(`<tr id="loadingRow">
  56. <td colspan="4" style="text-align: center;">
  57. <div class="ui active inline loader"></div>
  58. </td>
  59. </tr>`);
  60. // Fetch the list of posts from the server
  61. $.get("/api/fs/list?dir=/site/posts/", function(data){
  62. console.log(data);
  63. let postList = data.filter(file => file.Filename.endsWith(".md") && !file.IsDir);
  64. postList.sort((a, b) => {
  65. const getTimestamp = filename => parseInt(filename.split('_')[0]);
  66. return getTimestamp(b.Filename) - getTimestamp(a.Filename); // Sort by timestamp in filename
  67. });
  68. //Render the posts in the table
  69. console.log("Post list loaded: ", postList);
  70. $("#postTableBody").empty(); // Clear the table body
  71. postList.forEach(post => {
  72. const postName = post.Filename.split('/').pop().replace('.md', ''); // Extract the post name
  73. const postTime = new Date(post.Filename.split('_')[0] * 1000).toLocaleString(); // Convert timestamp to readable format
  74. $("#postTableBody").append(`
  75. <tr>
  76. <td>${postName.replace(/^\d+_/, '')}</td>
  77. <td>${postTime}</td>
  78. <td><button class="ui basic button edit-button" onclick="editPost('${post.Filename}');"><i class="ui blue edit icon"></i> Edit</button></td>
  79. <td><button class="ui red basic button delete-button" onclick="deletePost('${post.Filename}');"><i class="ui trash icon"></i> Delete</button></td>
  80. </tr>
  81. `);
  82. });
  83. if (postList.length == 0) {
  84. $("#postTableBody").append(`
  85. <tr id="nopost">
  86. <td colspan="4" style="text-align: center;">No posts available</td>
  87. </tr>
  88. `);
  89. }
  90. });
  91. }
  92. //Open post editor
  93. function editPost(postID){
  94. //TODO: Implement the post editor functionality
  95. }
  96. // Delete post
  97. function deletePost(postID) {
  98. const postName = postID.replace(/^\d+_/, ''); // Trim the timestamp prefix
  99. $('#deletePostName').text(postName); // Populate the post name in the modal
  100. $('#deleteConfirmModal .ok.button').attr('deleteingPostID', postID); // Store the postID in the confirm button
  101. $('#deleteConfirmModal').modal('show'); // Show the confirmation modal
  102. }
  103. function confirmDelete() {
  104. const postID = $('#deleteConfirmModal .ok.button').attr('deleteingPostID'); // Retrieve the postID
  105. if (!postID) return;
  106. // Send a delete request to the server
  107. $.ajax({
  108. url: `/api/fs/del?target=/site/posts/${postID}`,
  109. type: 'POSt',
  110. success: function(response) {
  111. console.log('Post deleted:', response);
  112. $('#deleteConfirmModal').modal('hide'); // Hide the modal
  113. getPosts(); // Refresh the post list
  114. msgbox(`Post "${postID.replace(/^\d+_/, '')}" deleted successfully`, 2000); // Show success message
  115. },
  116. error: function(error) {
  117. console.error('Error deleting post:', error);
  118. msgbox('Failed to delete the post', 2000); // Show error message
  119. }
  120. });
  121. }
  122. // Initial call to load posts
  123. getPosts();
  124. // Event listener for the Refresh button
  125. $('#refreshButton').on('click', function() {
  126. getPosts(); // Reload the table content
  127. msgbox("Post list refreshed", 2000); // Show a message box
  128. });
  129. </script>