125 lines
3.3 KiB
JavaScript
125 lines
3.3 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs');
|
|
const xml2js = require('xml2js');
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
|
|
function getFilesInFolder(folderName) {
|
|
|
|
const xmlFilePath = path.join(__dirname, folderName);
|
|
try {
|
|
|
|
const fileNames = fs.readdirSync(xmlFilePath);
|
|
const xmlFiles = fileNames.filter((file) => file.endsWith('.xml'));
|
|
return xmlFiles;
|
|
|
|
} catch (error) {
|
|
console.error('Error reading folder:', error);
|
|
return [];
|
|
}
|
|
}
|
|
require('dotenv').config();
|
|
//XmlPath get from .env
|
|
const xmlPath=process.env.XML_FILE_PATH;
|
|
|
|
const folderName = `/${xmlPath}`;
|
|
const filePaths = [];
|
|
const files = getFilesInFolder(folderName);
|
|
files.forEach((file) => {
|
|
const filePath = file;
|
|
filePaths.push(filePath);
|
|
});
|
|
|
|
|
|
|
|
// Function to load XML data and populate the list view
|
|
function loadList(xmlFilePath) {
|
|
return fetch(`${xmlPath}`+xmlFilePath)
|
|
.then(response => response.text())
|
|
.then(xmlContent => {
|
|
const parser = new DOMParser();
|
|
const xmlDoc = parser.parseFromString(xmlContent, 'text/xml');
|
|
var titleText = $(xmlDoc).find('Title').text();
|
|
return titleText;
|
|
})
|
|
.catch(error => {
|
|
console.error("Error loading XML:", error);
|
|
throw error;
|
|
});
|
|
}
|
|
|
|
|
|
// Function to load and display XML files in a loop
|
|
async function loadMultipleXMLs() {
|
|
for (const xmlFile of filePaths) {
|
|
console.log(xmlFile)
|
|
try {
|
|
const titleText = await loadList(xmlFile); // Initially load in list view
|
|
|
|
const dynamicId = `download-button-${Date.now()}`;
|
|
|
|
// Handle list view
|
|
const html_for_list = `
|
|
<div class="landing-icons">
|
|
<a class="icons" href="QuestionPreview.html?filename=${xmlFile}" title="PreView">
|
|
<i class="fa fa-eye" aria-hidden="true" style="font-size: 24px"></i>
|
|
</a>
|
|
<a class="icons" href="addform.html?filename=${xmlFile}" title="Edit">
|
|
<i class="fa fa-pencil-square-o" aria-hidden="true" style="font-size: 24px"></i>
|
|
</a>
|
|
<a class="icons" href="#" id="${dynamicId}" data-filename="${xmlFile}" title="Export">
|
|
<i class="fa fa-upload" aria-hidden="true" style="font-size: 24px"></i>
|
|
</a>
|
|
</div>
|
|
<div class="resent-post-single-box">
|
|
<div class="resent-thunb">
|
|
<i class="fa fa-file-text-o" aria-hidden="true" style="font-size: 50px; color:#533dfd;"></i>
|
|
</div>
|
|
<div class="xmlFileName">
|
|
<a href="#">${xmlFile}</a>
|
|
</div>
|
|
</div>`;
|
|
$('#list').append(html_for_list);
|
|
|
|
// Add event listener for download buttons
|
|
$('#'+dynamicId).on('click',function (event) {
|
|
event.preventDefault();
|
|
const filename =$(this).data('filename');
|
|
downloadFile(filename);
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error("Error loading XML:", error);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Call the function to load and display XML files
|
|
loadMultipleXMLs();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|