Files
OSIT-AE-App-Native-Electron/app/js/app_ui_presenters.js
2020-02-19 14:37:40 -05:00

211 lines
11 KiB
JavaScript

//exports.render_event_presenter_records = async function (events) {
async function render_event_presenter_records(events) {
console.log('Rendering event presenter records...');
console.log('****************** Presenters ******************');
if (looping_tbl_event_presenter) {
console.log('Already looping through the tbl_event_presenter table. Not starting until finished.');
return false;
} else {
}
looping_tbl_event_presenter = true;
let presentations_presenters_lists = document.getElementsByClassName('presentation_presenters'); // from the UL nodes for presenters
//console.log(presentations_presenters_lists);
// First: Loop through the presenters table looking for presenters for each presentation list
await tbl_event_presenter.iterate(function(value, key, iteration) {
let tbl_session_id = value.event_session_id;
let tbl_presentation_id = value.event_presentation_id;
let tbl_presenter_id = value.id;
console.log('tbl_event_presenter iteration='+iteration+' | tbl_presenter_id='+tbl_presenter_id+' for tbl_presentation_id='+tbl_presentation_id);
// Second: Loop through the presentations presenter UL placeholder (assumes there is a ul for presenters under each presentation)
if (presentations_presenters_lists.length) {
for (var i = 0; i < presentations_presenters_lists.length; i++) {
let ul_session_id = presentations_presenters_lists[i].getAttribute('data-session_id');
let ul_presentation_id = presentations_presenters_lists[i].getAttribute('data-presentation_id');
//console.log('presentation i='+i+' | ul_presentation_id='+ul_presentation_id);
// Check if this presenter is part of this presentation
let add_presenter_id = true; // Set flag to true. Update if found. Add if not found.
let remove_presenter_id = true; // Set flag to true. Update if found. Add if not found.
if (tbl_presentation_id == ul_presentation_id) {
console.log('This tbl_presentation_id '+tbl_presentation_id+' matches this ul_presentation_id '+ul_presentation_id+'. Trying to loop through list of presenters to see if it needs to be updated or added...');
// Get a list of list items under the UL presenters node for a presentation
let presentation_presenters_list_items = presentations_presenters_lists[i].children;
// Third: Loop through the presenters (LI) in a specific list under a presentation.
if (presentation_presenters_list_items.length) {
for (let j=0, len=presentation_presenters_list_items.length; j < len; j++) {
console.log('Checking li_presenter_id='+presentation_presenters_list_items[j].getAttribute('data-presenter_id'));
let li_presentation_id = presentation_presenters_list_items[j].getAttribute('data-presentation_id');
let li_presenter_id = presentation_presenters_list_items[j].getAttribute('data-presenter_id');
if ( tbl_presentation_id == li_presentation_id && tbl_presenter_id == li_presenter_id ) {
console.log('Updating presenter ID '+li_presenter_id);
// Update the presenter information
document.getElementById('event_presenter_'+tbl_presenter_id).setAttribute('data-given_name', value.given_name);
document.getElementById('event_presenter_'+tbl_presenter_id).setAttribute('data-family_name', value.family_name);
document.getElementById('event_presenter_'+tbl_presenter_id).getElementsByClassName('presenter_name')[0].innerHTML = value.given_name+' '+value.family_name;
// Could have used getElementsByTagName('strong') instead
console.log('XXXXX ******** STARTING SORT ******** XXXXX');
var categoryItems = document.getElementById('event_presentation_presenters_'+tbl_presentation_id).childNodes;
console.log(categoryItems);
var categoryItemsArray = Array.from(categoryItems);
function sorter(a, b) {
if (a.dataset.family_name < b.dataset.family_name) return -1;
if (a.dataset.family_name > b.dataset.family_name) return 1;
}
let sorted = categoryItemsArray.sort(sorter);
function update_li_order(item, index) {
document.getElementById('event_presentation_presenters_'+tbl_presentation_id).appendChild(item);
}
sorted.forEach(update_li_order);
console.log('******** FINISHED SORT ********');
add_presenter_id = false;
remove_presenter_id = false;
} else if (tbl_presentation_id != li_presentation_id ) {
console.log('Set flag to add presenter ID '+li_presenter_id+' to false. Set flag to remove presenter ID '+li_presenter_id+' to true.');
add_presenter_id = false;
remove_presenter_id = true; // The default is already set to true. Is this needed?
} else {
// NOTE: This section is not finished???
}
}
} else {
// No need to remove anything if the list is empty.
console.log('Ignoring this presentation\'s list ('+ul_presentation_id+') of presenters because it is empty. Adding this presenter ('+tbl_presenter_id+') to the list...');
add_presenter_id = true; // Technically this line is not needed since the value was initialized to true.
remove_presenter_id = false;
}
} else {
//NOTE: This probably needs to be reviewed again.
//console.log('This tbl_presentation_id '+tbl_presentation_id+' does not match this ul_presentation_id '+ul_presentation_id+'. Do not add presenter ID. ***But leave remove presenter ID as true???');
add_presenter_id = false;
remove_presenter_id = false;
}
// The presenter ID was not found. Need to add.
if ( add_presenter_id ) {
console.log('Adding this presenter ('+tbl_presenter_id+') to the list of presenters for this presentation ('+ul_presentation_id+').');
// Trying to remove old ID in case there is one already rendered
console.log('Trying to remove an old presenter LI node if it exists...');
try {
document.getElementById('event_presenter_'+tbl_presenter_id).remove();
}
catch(err) {
//console.log('A node with the ID of event_presenter_'+tbl_presenter_id+' was not found.');
console.log('This presenter list item node was not found. In most cases this is expected.');
console.log(err.message);
}
let presenter_li_node = document.createElement('LI');
presenter_li_node.id = 'event_presenter_'+tbl_presenter_id;
presenter_li_node.className = 'list-group-item event_presenter';
presenter_li_node.setAttribute('data-session_id', value.event_session_id);
presenter_li_node.setAttribute('data-presentation_id', ul_presentation_id);
presenter_li_node.setAttribute('data-presenter_id', tbl_presenter_id);
presenter_li_node.setAttribute('data-given_name', value.given_name);
presenter_li_node.setAttribute('data-family_name', value.family_name);
presenter_li_node.title = 'id='+tbl_presenter_id+' session_id='+value.event_session_id+' presentation_id='+ul_presentation_id+' presenter_id='+tbl_presenter_id;
let presenter_name_strong_node = document.createElement('STRONG');
presenter_name_strong_node.className = 'list-group-item-heading presenter_name';
let presenter_name_text_node= document.createTextNode(value.given_name+' '+value.family_name);
presenter_name_strong_node.appendChild(presenter_name_text_node);
presenter_li_node.appendChild(presenter_name_strong_node);
let presentation_presenters_ul_group_node = document.createElement('UL');
presentation_presenters_ul_group_node.id = 'presenter_files_list_'+tbl_presenter_id;
presentation_presenters_ul_group_node.className = 'list-group list-group-flush list-group-item-text presenter_files presenter_files_list event_files_list';
presentation_presenters_ul_group_node.setAttribute('data-presenter_id', tbl_presenter_id);
presenter_li_node.appendChild(presentation_presenters_ul_group_node);
document.getElementById('event_presentation_presenters_'+tbl_presentation_id).appendChild(presenter_li_node);
remove_presenter_id = false;
console.log('XXXXX ******** STARTING SORT ******** XXXXX');
var categoryItems = document.getElementById('event_presentation_presenters_'+tbl_presentation_id).childNodes;
console.log(categoryItems);
var categoryItemsArray = Array.from(categoryItems);
function sorter(a, b) {
if (a.dataset.family_name < b.dataset.family_name) return -1;
if (a.dataset.family_name > b.dataset.family_name) return 1;
}
let sorted = categoryItemsArray.sort(sorter);
function update_li_order(item, index) {
document.getElementById('event_presentation_presenters_'+tbl_presentation_id).appendChild(item);
}
sorted.forEach(update_li_order);
console.log('******** FINISHED SORT ********');
} else {
//console.log('Not going to add this presenter ('+tbl_presenter_id+') to the list of presenters for this presentation ('+ul_presentation_id+').');
}
// The presenter ID was found, but not part of this presentation. Need to remove.
if ( remove_presenter_id ) {
if ( ul_presentation_id != tbl_presentation_id ) {
console.log('Removing this presenter ('+tbl_presenter_id+') from the list of presenters for this presentation ('+ul_presentation_id+').');
// Trying to remove old ID in case there is one already rendered
console.log('Trying to remove a presenter LI node...');
try {
document.getElementById('event_presenter_'+tbl_presenter_id).remove();
}
catch(err) {
//console.log('A node with the ID of event_presenter_'+tbl_presenter_id+' was not found.');
console.log('This presenter list item node was not found. In some cases this is expected.');
console.log(err.message);
}
}
} else {
//console.log('This presenter ('+tbl_presenter_id+') is already part of the list of presenters for this presentation ('+ul_presentation_id+') and is correct.');
}
}
} else {
console.log('No UL placeholder list for presenters found under any presentations. Something might have gone wrong?');
}
}).then(function() {
console.log('idb_to_ui: Iterate tbl_event_presenter looking for presenters to update, add, or remove is complete');
looping_tbl_event_presenter = false;
});
return true;
}