projectb/index.js
2024-02-10 10:12:33 +05:30

143 lines
3.9 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const xml2js = require('xml2js');
$("#overlay").show();
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 convertBaseToString =require('./envConvertBasetoString');
var configObject = convertBaseToString();
console.log("index");
const xmlPath=configObject.XML_FILE_PATH.trim();
// 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) {
try {
const titleText = await loadList(xmlFile); // Initially load in list view
const dynamicIdDownload = `download-button-${Date.now()}`;
const dynamicIdDuplicate = `duplicate-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><img src="assets/images/view.svg" alt=""></i>
</a>
<a class="icons" href="addform.html?filename=${xmlFile}" title="Edit">
<i><img src="assets/images/edit.svg" alt=""></i>
</a>
<a class="icons" href="#" id="${dynamicIdDownload}" data-filename="${xmlFile}" title="Download">
<i><img src="assets/images/download.svg" alt=""></i>
</a>
<a class="icons" href="#" id="${dynamicIdDuplicate}" data-filename="${xmlFile}" title="Duplicate">
<i><img src="assets/images/duplicate.svg" alt="" style="width: 25px;"></i>
</a>
</div>
<div class="resent-post-single-box">
<div class="resent-thunb">
<i class="fa fa-file-text-o" style="font-size: 25px; color:#457B9D; margin-left: 5px"></i>
</div>
<div class="xmlFileName">
<a style="font-size: 16px; font-weight: 300">${xmlFile}</a>
</div>
</div>`;
$('#list').append(html_for_list);
// Add event listener for download buttons
$('#'+dynamicIdDownload).on('click',function (event) {
event.preventDefault();
const filename =$(this).data('filename');
downloadFile(filename);
});
$('#'+dynamicIdDuplicate).on('click',function (event) {
event.preventDefault();
const filename =$(this).data('filename');
duplicateFile(filename);
});
} catch (error) {
console.error("Error loading XML:", error);
}
}
}
// Call the function to load and display XML files
loadMultipleXMLs();
$("#overlay").hide();
});