Browse Source

added on common based : Data encode Date Difference , filesize format and download file script

Base64 data encode : encodeBase64(value),
formatFileSize(bytes),
date_difference(fromdate,todate,type='datetime') and callDownload(fileUrl,fileName).
main
Balaji 3 years ago
parent
commit
d4e31e1509
  1. 106
      src/main/resources/templates/fragments/modules/base/script.html

106
src/main/resources/templates/fragments/modules/base/script.html

@ -33,7 +33,7 @@
<script src="node_modules/datatable/StateRestore-1.3.0/js/dataTables.stateRestore.min.js"></script> <script src="node_modules/datatable/StateRestore-1.3.0/js/dataTables.stateRestore.min.js"></script>
<script src="dist/js/theme.js"></script> <script src="dist/js/theme.js"></script>
<script>
<script th:inline="javascript">
// Get the current page URL path // Get the current page URL path
var currentPagePath = window.location.pathname; var currentPagePath = window.location.pathname;
// Remove the leading slash from the current page URL path // Remove the leading slash from the current page URL path
@ -50,15 +50,27 @@
} }
</script> </script>
<script>
<script th:inline="javascript">
$(document).ready(function() { $(document).ready(function() {
/*
// Get the current URL
var currentUrl = window.location.href;
// Remove the query parameter from the URL
var newUrl = currentUrl.split("?")[0];
// Replace the current URL with the modified URL without the query parameter
window.history.replaceState({}, document.title, newUrl);
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////
var currentPagePath = window.location.pathname; var currentPagePath = window.location.pathname;
//console.error(currentPagePath);
currentPagePath = "/"+currentPagePath.substr(1); currentPagePath = "/"+currentPagePath.substr(1);
//console.error(currentPagePath);
// Set active class for parent menu items // Set active class for parent menu items
$('.nav-item').each(function() { $('.nav-item').each(function() {
var link = $(this).find('a').attr('href'); var link = $(this).find('a').attr('href');
//console.log(link +" == "+ currentPagePath);
//console.log("Main: "+link +" == "+ currentPagePath);
if (link === currentPagePath || link === currentPagePath) { if (link === currentPagePath || link === currentPagePath) {
$(this).addClass('active'); $(this).addClass('active');
$(this).parents('.has-sub').addClass('active open'); $(this).parents('.has-sub').addClass('active open');
@ -68,7 +80,9 @@
// Set active class for submenu items // Set active class for submenu items
$('.submenu-content .menu-item').each(function() { $('.submenu-content .menu-item').each(function() {
var link = $(this).attr('href'); var link = $(this).attr('href');
//console.log("Sub: "+link +" == "+ currentPagePath);
if (link === currentPagePath || link === currentPagePath) { if (link === currentPagePath || link === currentPagePath) {
//console.error("Sub: "+link +" == "+ currentPagePath);
$(this).addClass('active'); $(this).addClass('active');
$(this).parents('.has-sub').addClass('active open'); $(this).parents('.has-sub').addClass('active open');
} }
@ -125,7 +139,7 @@ function convertdate(date_txt){
return formattedDateTime; return formattedDateTime;
} }
function date_difference(fromdate,todate,type='time'){
function date_difference(fromdate,todate,type='datetime'){
// Parse the date strings into moment objects with the specified format // Parse the date strings into moment objects with the specified format
var startDate = moment(fromdate, 'DD-MM-YYYY hh:mm A'); var startDate = moment(fromdate, 'DD-MM-YYYY hh:mm A');
@ -138,12 +152,34 @@ function date_difference(fromdate,todate,type='time'){
var duration = moment.duration(endDate - startDate); var duration = moment.duration(endDate - startDate);
var hoursDifference = duration.hours(); var hoursDifference = duration.hours();
var minutesDifference = duration.minutes(); var minutesDifference = duration.minutes();
var date_diff_txt ="";
var day_txt = "day";
var hour_txt = "hour";
var days_txt ="";
if(daysDifference>1){
day_txt = "days";
}
if(hoursDifference>1){
hour_txt = "hours";
}
// Display the result // Display the result
//1 day, 1 hour and 34 minutes.
if(type=='days'){ if(type=='days'){
return daysDifference + ' days';
}else{
return (hoursDifference) + ' hours and ' + (minutesDifference) + ' minutes';
date_diff_txt = daysDifference + ' '+day_txt;
}else if(type=='time'){
date_diff_txt = hoursDifference + ' '+ hour_txt +' and ' + minutesDifference + ' minutes';
}
else{
if(daysDifference>0){
days_txt = daysDifference + ' '+day_txt+', ';
}
date_diff_txt = days_txt + hoursDifference + ' '+ hour_txt +' and ' + minutesDifference + ' minutes';
} }
return date_diff_txt;
} }
function nodata(message="Data are not availble at this moment please check later"){ function nodata(message="Data are not availble at this moment please check later"){
@ -179,6 +215,60 @@ function getLoadingMessage() {
// Display the random message // Display the random message
return randomMessage; return randomMessage;
} }
//To convert the file size from bytes
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/* Check file Exits */
function checkFileExists(url) {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('File not found');
}
return true;
})
.catch(error => {
console.error('Error checking file existence:', error);
return false;
});
}
/* File Doanload script
* Send fileUrl - Full Path
* var fileUrl = "/gcc/files/m_petition/m_action_file/filename.jpg";
*/
function callDownload(fileUrl,fileName){
checkFileExists(fileUrl).then(exists => {
if (exists) {
//download file - fileUrl
// Create a temporary anchor element
var link = document.createElement("a");
link.href = fileUrl;
link.download = fileName; // Set the download attribute to the file name
link.click(); // Simulate a click on the anchor element to trigger the download
} else {
console.log('File does not exist');
}
});
}
/* encode a string to Base64 */
function encodeBase64(value) {
if (value && value.trim() !== '') {
// Encode the string to Base64
var encodedString = btoa(value);
return encodedString;
} else {
return '';
}
}
</script> </script>
<!-- <!--
<script th:inline="javascript"> <script th:inline="javascript">

Loading…
Cancel
Save