Skip to content

Commit

Permalink
Merge pull request #350 from MichaelEbert/mapUpdate2
Browse files Browse the repository at this point in the history
refactor progress load system
  • Loading branch information
MichaelEbert authored Dec 21, 2024
2 parents d08a870 + 48e4f6e commit a20ad72
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 280 deletions.
79 changes: 2 additions & 77 deletions checklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Functions that generate the page
//===========================
function init(){
document.addEventListener("progressLoad",updateUIFromSaveData);
document.addEventListener("progressLoad",progress.updateProgressBar)
obliviondata.loadJsonData().then(()=>{
userdata.loadSettingsFromCookie();
//populate sections with json data.
Expand Down Expand Up @@ -167,81 +167,6 @@ function initMultiInternal(root, parentElement, depth, extraColumnName, leafCont
// Functions that deal with progress
//===========================

/**
* Recalculate the total progress, and update UI elements.
*/
function recalculateProgressAndUpdateProgressUI(){
let percentCompleteSoFar = localStorage.getItem("percentageDone");

try{
percentCompleteSoFar = window.progress.recalculateProgress();
} catch{

}

//round progress to 2 decimal places
let progress = Math.round((percentCompleteSoFar * 100)*100)/100;
Array.of(...document.getElementsByClassName("totalProgressPercent")).forEach(element => {
element.innerText = progress.toString();
if(element.parentElement.className == "topbarSection" || element.parentElement.className == "popoutTopBarSection"){
element.parentElement.style = `background: linear-gradient(to right, green ${progress.toString()}%, crimson ${progress.toString()}%);`;
}
});
}

/**
* helper function for updateUIFromSaveData
* @param {} cell
*/
function updateHtmlElementFromSaveData(cell){
const classname = cell.hive.classname
let usableId = cell.formId;
if(usableId == null){
usableId = cell.id;
}
let checkbox = document.getElementById(classname+usableId+"check");
if(checkbox == null){
if(usableId != null && window.debug){
//user doesn't really need to know if this happens; it is expected for elements that don't draw.
console.warn("unable to find checkbox element for modifiable cell '"+classname+usableId+"' (id "+cell.id+")");
}
return;
}
let newval = null;
if(cell.ref == null){
//we call updateChecklistProgress so indirect elements will update from this
if(cell.id != null){
if(savedata[classname] == null){
debugger;
}
newval = savedata[classname][cell.id];
progress.updateChecklistProgress(null, newval, null, cell, true);
}
}
}

/**
* When savedata is loaded, we need to bulk change all of the HTML to match the savedata state.
* This function does that.
*/
function updateUIFromSaveData(){
for(const klass of obliviondata.progressClasses){
const hive = obliviondata.jsondata[klass.name];
obliviondata.runOnTree(hive, updateHtmlElementFromSaveData);
}

recalculateProgressAndUpdateProgressUI();
}

function setParentChecked(checkbox){
if(checkbox.checked){
checkbox.parentElement.classList.add("checked");
}
else{
checkbox.parentElement.classList.remove("checked");
}
}

/**
* called when user inputs data
* @param {string} htmlRowId
Expand All @@ -257,7 +182,7 @@ function userInputData(htmlRowId, checkboxElement){
}
}

recalculateProgressAndUpdateProgressUI();
progress.updateProgressBar();
}

function checkboxClicked(event){
Expand Down
68 changes: 2 additions & 66 deletions guide.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ function init(){
checkIframeSize();

window.addEventListener("resize",onWindowResize);
document.addEventListener("progressLoad",progress.updateProgressBar)
loadJsonData().then(()=>{
replaceElements();
loadProgressFromCookie();
sharing.initSharingFeature();
replaceElements();
});
}

Expand Down Expand Up @@ -127,7 +128,6 @@ function replaceElements(){
}
}
}
updateUIFromSaveData();
}


Expand Down Expand Up @@ -161,75 +161,13 @@ function getNpcData(npcElement){
return {name:npcName};
}

/**
* Recalculate the total progress, and update UI elements.
*/
function recalculateProgressAndUpdateProgressUI(){
let percentCompleteSoFar;

try{
percentCompleteSoFar = window.progress.recalculateProgress();
} catch{
console.log("percentComplete got from localStorage");
percentCompleteSoFar = localStorage.getItem("percentageDone");
}

// //round progress to 2 decimal places
// let progress = (percentCompleteSoFar * 100).toFixed(2);
// Array.of(...document.getElementsByClassName("totalProgressPercent")).forEach(element => {
// element.innerText = progress.toString();
// if(element.parentElement.className == "topbarSection"){
// element.parentElement.style = `background: linear-gradient(to right, green ${progress.toString()}%, red ${progress.toString()}%);`;
// }
// });
}

/**
* helper function for updateUIFromSaveData. Does not call recalculateProgress().
* @param {} cell
*/
function updateHtmlElementFromSaveData(cell){
const classname = cell.hive.classname
let usableId = cell.formId;
if(usableId == null){
usableId = cell.id;
}
let newval = null;
if(cell.ref == null){
//we call updateChecklistProgress so indirect elements will update from this
if(cell.id != null){
if(savedata[classname] == null){
debugger;
}
newval = savedata[classname][cell.id];
updateChecklistProgress(null, newval, null, cell, true);
//don't call recalculateProgress() because we do that in bulk in the calling function.
}
}
}

/**
* When savedata is loaded, we need to bulk change all of the HTML to match the savedata state.
* This function does that.
*/
function updateUIFromSaveData(){
for(const klass of progressClasses){
const hive = jsondata[klass.name];
runOnTree(hive, updateHtmlElementFromSaveData);
}

recalculateProgressAndUpdateProgressUI();
}

function checkboxClicked(event){
const rowHtml = event.target.parentElement;

var parentid = rowHtml.getAttribute("clid");
var classname = rowHtml.classList[0];
updateChecklistProgressFromInputElement(parentid, event.target, classname);
event.stopPropagation();
// we need to update because there might be multiple instances of the same book on this page, and we want to check them all.
recalculateProgressAndUpdateProgressUI();
}

function rowClicked(event){
Expand Down Expand Up @@ -261,8 +199,6 @@ function userInputData(rowHtml, checkboxElement){
var classname = rowHtml.classList[0];
let rowid = rowHtml.getAttribute("clid");
progress.updateChecklistProgressFromInputElement(rowid, checkboxElement, classname);

recalculateProgressAndUpdateProgressUI();
}

//used to make sure we don't run a ton of refresh code constantly.
Expand Down
12 changes: 8 additions & 4 deletions js/map.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export {

import { Point } from "./map/point.mjs";
import { MapPOI } from "./map/mapObject.mjs";
import { sumCompletionItems } from "./progressCalculation.mjs";
import { sumCompletionItems, updateProgressBar } from "./progressCalculation.mjs";
import { saveProgressToCookie, initAutoSettings } from "./userdata.mjs"
import { Overlay, OVERLAY_LAYER_NONE, OVERLAY_LAYER_LOCATIONS, OVERLAY_LAYER_NIRNROOTS, OVERLAY_LAYER_WAYSHRINES, OVERLAY_LAYER_NEARBYGATES } from "./map/overlay.mjs";
import { findCell } from "./obliviondata.mjs";
Expand Down Expand Up @@ -523,15 +523,19 @@ function initListeners(){

document.getElementById("resetMapButton")?.addEventListener('click', (e)=>{
if(confirm("Delete saved map progress?")){
resetProgressForHive(jsondata.location);
resetProgressForHive(jsondata.nirnroot);
resetProgressForHive(savedata, jsondata.location);
resetProgressForHive(savedata, jsondata.nirnroot);
clearRandomGateCount();
saveProgressToCookie();
location.reload();
}
});

document.addEventListener("progressLoad",()=>overlay.recalculateBoundingBox());
document.addEventListener("progressLoad",()=>{
updateProgressBar();
overlay.recalculateBoundingBox();
drawFrame();
});
}

function updateZoom(deltaZ, zoomPoint){
Expand Down
18 changes: 18 additions & 0 deletions js/nirnroute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { saveCookie } from './userdata.mjs';
var prevNirnroot;
var thisNirnroot;
var nextNirnroot;
var thisNirnrootChecked;

/**
* Debugging function to get prev, this and next nirnroot.
Expand Down Expand Up @@ -58,6 +59,22 @@ function initEventListeners(){
activateNirnroot(prevNirnroot.cell.formId);
});


document.addEventListener("progressLoad",()=>{
// if after a progressLoad, the current nirn has
// been checked, advance to next unchecked nirn.
let newCheckedValue = savedata["nirnroot"][thisNirnroot.cell.id];
if(thisNirnrootChecked === false && newCheckedValue)
{
while(savedata["nirnroot"][thisNirnroot.cell.id] == true &&
nextNirnroot.cell.tspId != 0)
{
activateNirnroot(nextNirnroot.cell.formId);
}
newCheckedValue = savedata["nirnroot"][thisNirnroot.cell.id];
}
});

document.body.addEventListener('keyup', (evt)=>{
if(evt.code === "ArrowRight"){
nextButton.click();
Expand Down Expand Up @@ -171,6 +188,7 @@ function activateNirnroot(nirnFormId){
const nextToElement = document.getElementById("closeTo");
nextToElement.innerText = getFastTravelInstructions(thisNirnroot);

thisNirnrootChecked = savedata["nirnroot"][thisNirnroot.cell.id];

map.zoomToFormId(nirnFormId);
map.draw();
Expand Down
18 changes: 1 addition & 17 deletions js/popout.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,9 @@ function copyBrowserSourceToClipboard(){
}

function init(){
document.addEventListener("progressLoad",recalculateProgressAndUpdateProgressUI);
document.addEventListener("progressLoad",updateProgressBar);
obliviondata.loadJsonData().then(()=>{
userdata.loadSettingsFromCookie();
userdata.loadProgressFromCookie();
});
}

/**
* Recalculate the total progress, and update UI elements.
*/
function recalculateProgressAndUpdateProgressUI(){
let percentCompleteSoFar = recalculateProgress();

//round progress to 2 decimal places
let progress = Math.round((percentCompleteSoFar * 100)*100)/100;
Array.of(...document.getElementsByClassName("totalProgressPercent")).forEach(element => {
element.innerText = progress.toString();
if(element.parentElement.className == "topbarSection" || element.parentElement.className == "popoutTopBarSection"){
element.parentElement.style = `background: linear-gradient(to right, green ${progress.toString()}%, crimson ${progress.toString()}%);`;
}
});
}
Loading

0 comments on commit a20ad72

Please sign in to comment.