LiDa_Search/js/minimap.js

146 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-02-06 12:51:34 +01:00
/**
*
*/
const displayCantoId = '#displaycanto';
const minimapviewerId = '#minimapviewer';
const displayminimapId = '#displayminimap';
const visCantoClass = '.visCanto';
2023-02-06 12:51:34 +01:00
let minimap = document.createElement('div');
let viewer = document.createElement('div');
let minimapContent = document.createElement('iframe');
let realScale;
let currentMinimap = '';
let initOffset = '';
2023-02-06 12:51:34 +01:00
function drawMinimap(name){
currentMinimap = name;
2023-02-17 16:11:39 +01:00
initOffset = parseInt($("body").css("padding-top").replace("px", ""))+63;
showMinimap();
$(displayCantoId).scrollTop(0);
$(minimapviewerId).offset({ top: initOffset});
var minimapTopPos = $(minimapviewerId).offset().top;
$(minimapviewerId).draggable({
2023-02-06 12:51:34 +01:00
axis: "y",
containment: $(displayminimapId),
2023-02-06 12:51:34 +01:00
scroll: false,
start: function(event, ui) { minimapScrolling = true; },
stop: function(event, ui) { minimapScrolling = false; },
drag: function(event, ui) {
minimapScrolling = true;
$(displayCantoId).scrollTop((ui.offset.top - minimapTopPos) / realScale);
2023-02-06 12:51:34 +01:00
}
});
}
function showMinimap() {
viewer.className = 'minimap__viewer';
viewer.id = 'minimapviewer';
minimapContent.className = 'minimap__content';
minimap.append(viewer, minimapContent);
$(displayminimapId).append(minimap);
2023-02-06 12:51:34 +01:00
let html = $(currentMinimap)[0].outerHTML;
2023-02-06 12:51:34 +01:00
if (html == null | html == '')
return;
let iFrameDoc = minimapContent.contentWindow.document;
2023-02-06 12:51:34 +01:00
var cssLinkb = document.createElement("link");
cssLinkb.href = $("#" + currStyle + "-Boot")[0].href;
2023-02-06 12:51:34 +01:00
cssLinkb.rel = "stylesheet";
cssLinkb.type = "text/css";
var cssLinkscroll = document.createElement("link");
cssLinkscroll.href = $("#" + currStyle + "-Custom")[0].href;
2023-02-06 12:51:34 +01:00
cssLinkscroll.rel = "stylesheet";
cssLinkscroll.type = "text/css";
iFrameDoc.open();
iFrameDoc.write('<div class="blog-main w-100 " style="display: d-flex;">'); // da pulire...
2023-02-06 12:51:34 +01:00
iFrameDoc.write(html);
iFrameDoc.write('</div>');
2023-02-06 12:51:34 +01:00
iFrameDoc.close();
iFrameDoc.head.appendChild(cssLinkb);
iFrameDoc.head.appendChild(cssLinkscroll);
let bgCanto = $(".visCanto").css('background-color');
$('iframe').contents().find('body').css('background-color', bgCanto + ' !important;');
2023-02-06 12:51:34 +01:00
getDimensionsDiv();
2023-02-06 12:51:34 +01:00
window.addEventListener('resize', getDimensionsDiv);
2023-02-06 12:51:34 +01:00
}
function getDimensionsDiv() {
var srcCantoHeight = $(currentMinimap)[0].clientHeight;
//var maxHeight = ($(displayminimapId)[0].clientHeight)-4-63; //h attuale dello spazio minimap -4 padding -63 top padding
var maxHeight = ($(displayminimapId)[0].clientHeight)-66; //h attuale dello spazio minimap; -63 top padding, -2 bordi minimapviewer, -1 bordo col, -4 bordi iframe
var maxWidth = $(displayminimapId)[0].clientWidth; //w attuale della minimap (css)
//var miniCantoHeight = Math.ceil(((srcCantoHeight-44)/25*26)+44+32); //h prevista del canto nella minimap
var miniCantoHeight = srcCantoHeight+32; //h prevista del canto nella minimap 16x2: padding py
2023-02-06 12:51:34 +01:00
var resizableWidth = $(visCantoClass)[0].clientWidth;
var resizableHeight = $(visCantoClass)[0].clientHeight;
var resizableRatio = resizableHeight/resizableWidth;
var heightRatio = resizableHeight/srcCantoHeight;
let viewerHeight = (maxHeight*heightRatio)-2; // -2 = bordi
let viewerWidth = Math.min(viewerHeight/resizableRatio, maxWidth-2); // -2 bordi
2023-02-17 16:11:39 +01:00
realScale = Math.min((maxHeight/miniCantoHeight), 1);
minimap.style.width = '100%';
//console.log("maxHeight: "+maxHeight)
//console.log("maxWidth: "+maxWidth)
//console.log("srcCantoHeight: "+$(currentMinimap)[0].clientHeight)
//console.log("realScale: "+realScale)
2023-02-06 12:51:34 +01:00
minimapContent.style.transform = `scale(${realScale})`;
//minimapContent.style.height = Math.trunc(miniCantoHeight) + "px";
//minimapContent.style.width = Math.trunc(maxWidth/realScale) + "px";
minimapContent.style.height = miniCantoHeight + "px";
minimapContent.style.width = maxWidth/realScale + "px";
//console.log("minimapContent.style.height: "+minimapContent.style.height)
//console.log("minimapContent.style.width: "+minimapContent.style.width)
viewer.style.height = viewerHeight + "px";
viewer.style.width = viewerWidth + "px";
trackScrollCanto();
};
2023-02-06 12:51:34 +01:00
getDimDiv = function(){
getDimensionsDiv();
}
function trackScrollCanto() {
var minimapHeight = $(displayminimapId)[0].clientHeight;
var posDbefore = $(displayCantoId).scrollTop();
//console.log(posDbefore);
var srcCantoHeight = $(currentMinimap)[0].clientHeight;
//var posAfter = (minimapHeight*posDbefore)/(srcCantoHeight*1.014);
var posAfter = (minimapHeight*posDbefore)/(srcCantoHeight*1.088);
$(minimapviewerId).offset({ top: initOffset + posAfter});
2023-02-06 12:51:34 +01:00
}
2023-02-06 12:51:34 +01:00
function hideMinimap() {
$('.canto').each(function() {
$(this).attr("style", 'display:none');
});
$(".minimap__container").remove();
currentMinimap = ''
}