pages.html 5.6 KB

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