Files
OSIT-AE-App-Native-Electron/app/js/app_ui_files.js
2020-03-04 16:36:49 -05:00

222 lines
10 KiB
JavaScript

// This function is used to render all event, location, session, presentation, and presenter file records to the UI.
//exports.render_event_file_records = async function () {
async function render_event_file_records() {
console.log('Rendering all event, location, session, presentation, and presenter file records...');
console.log('****************** Files ******************');
if (looping_tbl_event_file) {
console.log('Already looping through the tbl_event_file table. Not starting until finished.');
return false;
} else {
}
looping_tbl_event_file = true;
console.log('Iterating through the tbl_event_file table...');
await tbl_event_file.iterate(function(value, key, iteration) {
let tbl_file_id = value.id;
let tbl_hosted_file_id = value.hosted_file_id;
let tbl_event_id = value.event_id;
let tbl_location_id = value.event_location_id;
let tbl_session_id = value.event_session_id;
let tbl_presentation_id = value.event_presentation_id;
let tbl_presenter_id = value.event_presenter_id;
let tbl_for_type = value.for_type;
let tbl_for_id = value.for_id;
let tbl_hash_sha256 = value.hosted_file_hash_sha256;
let tbl_filename = value.filename; // This could also be event_file_filename, internal_filename, private_filename, public_filename, or hosted_file_filename
let tbl_size = value.hosted_file_size;
let tbl_created_on = value.created_on;
let tbl_updated_on = value.created_on;
let tbl_internal_os = value.internal_os;
console.log('tbl_event_file iteration='+iteration+' | tbl_file_id='+tbl_file_id+' for tbl_event_id='+tbl_event_id+' at location tbl_location_id='+tbl_location_id+'.');
if (tbl_event_id == event_id && tbl_for_type == 'event') {
console.log('EVENT FILE **************************');
document.getElementById('event_files_menu').classList.remove('d-none');
document.getElementById('event_files_menu').classList.add('d-block');
}
if (tbl_location_id == event_location_id && tbl_for_type == 'location') {
console.log('LOCATION FILE **************************');
document.getElementById('location_files_menu').classList.remove('d-none');
document.getElementById('location_files_menu').classList.add('d-block');
}
//add_file_id = false;
remove_file_id = true;
if (tbl_event_id == event_id/* && tbl_location_id == event_location_id*/) {
console.log('Match for event_id='+event_id+' and event_location_id='+event_location_id);
file_li_node = document.getElementById('event_file_'+tbl_file_id);
if (file_li_node) {
console.log('event_file ('+tbl_file_id+') node found... check and remove/update.');
if (file_li_node.getAttribute('data-for_type') == tbl_for_type && file_li_node.getAttribute('data-for_id') == tbl_for_id) {
console.log('This file is still for_type='+tbl_for_type+' and for_id='+tbl_for_id+'.');
file_li_node.setAttribute('data-filename', tbl_filename);
let new_filename = shorten_filename(tbl_filename);
file_li_node.getElementsByClassName('filename')[0].innerHTML = new_filename;
try {
file_li_node.getElementsByClassName('file_meta')[0].innerHTML = format_bytes(tbl_size, 2)+'; '+dateFns.format(tbl_created_on, 'MMM M h:mm A')+'; '+tbl_internal_os;
} catch(err) {
console.log('file_meta span not found. This is ok for event and location specific files.');
}
file_li_node.setAttribute('data-size', tbl_size);
file_li_node.setAttribute('data-created_on', dateFns.format(tbl_created_on, 'YYYY-MM-DD HH:mm:ss A'));
file_li_node.setAttribute('data-updated_on', dateFns.format(tbl_updated_on, 'YYYY-MM-DD HH:mm:ss A'));
file_li_node.setAttribute('data-internal_os', tbl_internal_os);
remove_file_id = false;
} else {
console.log('This file no longer matching for_type='+tbl_for_type+' and for_id='+tbl_for_id+'. Removing...');
file_li_node.remove();
remove_file_id = false;
}
} else if (!file_li_node) {
console.log('event_file ('+tbl_file_id+') node NOT found... check and add.');
let node_id = tbl_for_type+'_files_list_'+tbl_for_id;
console.log(node_id);
let parent_ul_node = document.getElementById(node_id);
console.log(parent_ul_node);
if (parent_ul_node) {
// Trying to remove old ID in case there is one already rendered
console.log('Trying to remove an old file LI node if it exists...');
try {
document.getElementById('event_file_'+tbl_file_id).remove();
} catch(err) {
//console.log('A node with the ID of event_file_'+tbl_file_id+' was not found.');
console.log('This event file list item node was not found. In most cases this is expected.');
console.log(err.message);
}
let file_li_node = document.createElement('LI');
file_li_node.id = 'event_file_'+tbl_file_id;
if (tbl_for_type != 'event' && tbl_for_type != 'location') {
file_li_node.className = 'list-group-item btn btn-primary d-flex justify-content-between align-items-center open_local_file event_file';
//file_li_node.className = 'list-group-item btn btn-primary justify-content-between align-items-center open_local_file event_file';
} else {
file_li_node.className = 'list-group-item btn btn-sm btn-secondary d-flex justify-content-between align-items-center open_local_file event_file';
//file_li_node.className = 'list-group-item btn btn-sm btn-secondary justify-content-between align-items-center open_local_file event_file';
}
file_li_node.setAttribute('data-file_id', tbl_file_id);
file_li_node.setAttribute('data-event_id', tbl_event_id);
file_li_node.setAttribute('data-location_id', tbl_location_id);
file_li_node.setAttribute('data-session_id', tbl_session_id);
file_li_node.setAttribute('data-presentation_id', tbl_presentation_id);
file_li_node.setAttribute('data-presenter_id', tbl_presenter_id);
file_li_node.setAttribute('data-for_type', tbl_for_type);
file_li_node.setAttribute('data-for_id', tbl_for_id);
file_li_node.setAttribute('data-hash_sha256', tbl_hash_sha256+'.file');
file_li_node.setAttribute('data-filename', tbl_filename);
file_li_node.setAttribute('data-size', tbl_size);
file_li_node.setAttribute('data-created_on', dateFns.format(tbl_created_on, 'YYYY-MM-DD HH:mm:ss A'));
file_li_node.setAttribute('data-updated_on', dateFns.format(tbl_updated_on, 'YYYY-MM-DD HH:mm:ss A'));
file_li_node.setAttribute('data-internal_os', tbl_internal_os);
file_li_node.title = 'Click to open "'+tbl_filename+'" | id='+tbl_file_id+' | for_type='+tbl_for_type+' | for_id='+tbl_for_id+' | updated_on='+dateFns.format(tbl_updated_on, 'YYYY-MM-DD HH:mm:ss A');
let file_fa_span_node = document.createElement('SPAN');
file_fa_span_node.className = 'fas fa-external-link-alt';
let file_filename_span_node = document.createElement('SPAN');
file_filename_span_node.className = 'filename';
let new_filename = shorten_filename(tbl_filename);
let filename_text_node = document.createTextNode(new_filename);
file_filename_span_node.appendChild(filename_text_node);
file_li_node.appendChild(file_fa_span_node);
file_li_node.appendChild(file_filename_span_node);
// We do not want the badge to show in the left menu. Not enough space.
if (tbl_for_type != 'event' && tbl_for_type != 'location') {
let file_badge_span_node = document.createElement('SPAN');
file_badge_span_node.className = 'badge badge-pill badge-light float-right file_meta';
let badge_text_node = document.createTextNode(format_bytes(tbl_size, 2)+'; '+dateFns.format(tbl_created_on, 'MMM M h:mm A')+'; '+tbl_internal_os);
file_badge_span_node.appendChild(badge_text_node);
file_li_node.appendChild(file_badge_span_node);
}
console.log(file_li_node);
parent_ul_node.appendChild(file_li_node);
remove_file_id = false;
}
}
// NOTE: This is probably not needed?
if (remove_file_id) {
// Trying to remove old ID in case there is one already rendered
console.log('Trying to remove an old file LI node if it exists...');
try {
document.getElementById('event_file_'+tbl_file_id).remove();
} catch(err) {
//console.log('A node with the ID of event_file_'+tbl_file_id+' was not found.');
console.log('This event file list item node was not found. In most cases this is expected.');
console.log(err.message);
}
}
console.log('XXXXX ******** STARTING SORT ******** XXXXX');
let node_id = tbl_for_type+'_files_list_'+tbl_for_id;
console.log(node_id);
try {
var categoryItems = document.getElementById(node_id).childNodes;
console.log(categoryItems);
var categoryItemsArray = Array.from(categoryItems);
function sorter(a, b) {
if (a.dataset.updated_on > b.dataset.updated_on) return -1;
if (a.dataset.updated_on < b.dataset.updated_on) return 1;
}
let sorted = categoryItemsArray.sort(sorter);
function update_li_order(item, index) {
document.getElementById(node_id).appendChild(item);
}
sorted.forEach(update_li_order);
} catch(err) {
console.log(err);
}
console.log('******** FINISHED SORT ********');
} else {
console.log('This file ('+tbl_file_id+') is not part of this event and or location');
console.log('tbl_event_id='+tbl_event_id+' ?= event_id='+event_id);
console.log('tbl_location_id='+tbl_location_id+' ?= event_location_id='+event_location_id);
}
}).then(function() {
console.log('idb_to_ui: Iterate tbl_event_file looking for files to add is complete');
looping_tbl_event_file = false;
});
index_open_file_buttons('open_local_file');
return true;
}