Johnny/out/js/pc.js
2021-05-09 19:09:08 +02:00

75 lines
2.5 KiB
JavaScript

// Init Elm
var localStuff = localStorage.getItem("pc_data");
if (typeof localStuff !== "string") localStuff = "";
var app = Elm.Main.init({
node: document.getElementById("elm"),
flags: localStuff
});
// ###################################################################
// ###################################################################
// Dom Objects
let pc = document.getElementsByClassName("pc")[0];
let pc_ram = pc.getElementsByClassName("ram")[0];
let pc_cu = pc.getElementsByClassName("cu")[0];
let pc_alu = pc.getElementsByClassName("alu")[0];
let pc_ram_scroller = pc_ram.getElementsByClassName("scroller")[0];
let pc_cu_scroller = pc_cu.getElementsByClassName("scroller")[0];
let control_autoscroll = document.getElementById("enableScrolling");
// Methods
function scrollToCurrent(){
if( control_autoscroll.checked == false ) return;
let current_ram = pc_ram.getElementsByClassName("current")[0];
let current_uCode = pc_cu.getElementsByClassName("current")[0];
if( pc.scrollIntoViewIfNeeded == undefined ){
if( typeof current_ram != "undefined"){
current_ram.scrollIntoView({behavior: "smooth", block: "nearest"});
}
if( typeof current_uCode != "undefined"){
current_uCode.scrollIntoView({behavior: "smooth", block: "nearest"});
}
}else{
if( typeof current_ram != "undefined"){
current_ram.scrollIntoViewIfNeeded({behavior: "smooth", block: "nearest"});
}
if( typeof current_uCode != "undefined"){
current_uCode.scrollIntoViewIfNeeded({behavior: "smooth", block: "nearest"});
}
}
// pc.scrollIntoView();
}
// ###################################################################
// ###################################################################
// EVENT LISTENERS
// Recieve Elm updates via ports
app.ports.sendUUpdate.subscribe( (message) => {
// console.log("Update: ", message);
scrollToCurrent();
// Make sure that even when the calculation takes longer it will scroll correctly
setTimeout( scrollToCurrent, 100 );
} );
// Recieve LocalSession Updates via Ports
app.ports.localStorageSend.subscribe( (message) => {
// console.log("localSessionSend: ", message);
if( cookieAccepted() )
localStorage.setItem("pc_data", message);
} );
// ###################################################################
// ###################################################################
// Done.