LiDa_Search/js/cesareBrowse.js

464 lines
13 KiB
JavaScript
Raw Normal View History

2022-11-06 17:12:56 +01:00
/**
*
*/
2022-11-08 10:44:20 +01:00
$(document).ready(function() {
const categoriegrammaticali={"v":"Verbo", "s":"Sostantivo", "a":"Aggettivo", "p":"Pronome", "r":"Articolo", "b":"Avverbio","e":"Preposizione",
"c":"Congiunzione", "i":"Interiezione", "n":"Onomastica", "f":"Citazione", "l":"Locuzione",
"r-e":"Articolo, Preposizione", "b-v":"Avverbio, Verbo", "p-v":"Pronome, Verbo",
"b-p-v":"Avverbio, Pronome, Verbo", "b-p":"Avverbio, Pronome",
"e-p":"Preposizione, Pronome", "a-s":"Aggettivo, Sostantivo"}
2022-11-08 10:44:20 +01:00
const cantobadgeclass = 'badge badge-outline-info badge-pill'
2022-11-08 10:44:20 +01:00
2022-11-06 17:12:56 +01:00
const sparqlGenerator = sparqljs.Generator;
const myEngine = new Comunica.QueryEngine();
const sparqlEndpoint = "https://hdnlab1.isti.cnr.it/fuseki/commediaontosintgra/query";
2022-12-06 11:25:04 +01:00
var listaVersi = new Set()
var currentMinimap = ''
var listarisultati = [];
var minimapScrolling = false;
var displayId = 'displaycanto'
var minimapTopPos;
//used in minimap
//
let minimap = document.createElement('div');
let minimapSize = document.createElement('div');
let viewer = document.createElement('div');
let minimapContent = document.createElement('iframe');
let scale = 0.1;
let realScale;
let mappedElement = '';
hideMinimap();
const start = performance.now();
showCantica('Inferno')
2022-12-06 11:25:04 +01:00
$('#cantica2').css({ 'opacity': 0.40 });
$('#cantica3').css({ 'opacity': 0.40 });
//$("#CantiInferno").attr('style', 'display:block')
$("#InfernoCanto_1").attr('style', 'display:block')
drawMinimap("InfernoCanto_1")
2022-12-06 11:25:04 +01:00
showCantica('Purgatorio')
showCantica('Paradiso')
const end = performance.now();
console.log(`Load cantica time: ${end - start} ms`);
2022-11-08 10:44:20 +01:00
2022-12-13 13:44:39 +01:00
$("[data-cg]").hover(function() {
var idspan = $(this);
var value = $(this).text();
var cg = $(this).data("cg");
// alert('fava '+ value + " "+cg);
$(this).popover({title: "Categoria", content: categoriegrammaticali[cg], trigger: "hover"});
2022-12-13 13:44:39 +01:00
});
// prima cantica
2022-11-06 17:12:56 +01:00
$('#cantica1').on('click', function() {
2022-11-08 10:44:20 +01:00
2022-12-12 13:30:59 +01:00
$('#cantica1').css({ 'opacity': 1 });
$('#cantica2').css({ 'opacity': 0.40 });
$('#cantica3').css({ 'opacity': 0.40 });
2022-11-07 11:52:54 +01:00
});
2022-11-08 10:44:20 +01:00
// seconda cantica
2022-11-07 11:52:54 +01:00
$('#cantica2').on('click', function() {
2022-12-12 13:30:59 +01:00
$('#cantica1').css({ 'opacity': 0.40 });
$('#cantica2').css({ 'opacity': 1 });
$('#cantica3').css({ 'opacity': 0.40 });
2022-12-06 11:25:04 +01:00
2022-11-07 11:52:54 +01:00
});
2022-11-08 10:44:20 +01:00
// terza cantica
2022-11-07 11:52:54 +01:00
$('#cantica3').on('click', function() {
2022-12-12 13:30:59 +01:00
$('#cantica1').css({ 'opacity': 0.40 });
$('#cantica2').css({ 'opacity': 0.40 });
$('#cantica3').css({ 'opacity': 1 });
});
var mmih = 0
var mmiw = 0
$("#" + displayId).scroll(function() {
if (minimapScrolling) {
minimapScrolling = false
console.log('no scroll')
2022-11-07 11:52:54 +01:00
}
2022-11-08 10:44:20 +01:00
else {
console.log('yes scrolling')
console.log("viewer top pos before: " + $('#minimapviewer').offset().top)
trackScrollCanto()
console.log("viewer top pos after: " + $('#minimapviewer').offset().top)
2022-12-06 11:25:04 +01:00
}
2022-11-06 17:12:56 +01:00
});
2022-11-06 17:12:56 +01:00
jQuery(document).delegate('.navig-canto', 'click', function(e) {
$('.canto').each(function() {
2022-11-08 10:44:20 +01:00
$(this).attr("style", 'display:none');
2022-11-06 17:12:56 +01:00
});
// Crea id per la div che contiene il canto
2022-11-08 10:44:20 +01:00
$("#" + $(this).attr('name').replace(" ", "_")).attr('style', 'display:block')
manageMiniMap(this);
2022-12-06 11:25:04 +01:00
2022-11-06 17:12:56 +01:00
});
2022-11-08 10:44:20 +01:00
function manageMiniMap(divElem) {
mmiw = $("#" + displayId).innerWidth();
mmih = $("#" + displayId).innerHeight();
drawMinimap($(divElem).attr('name').replace(" ", "_"))
}
2022-12-06 11:25:04 +01:00
//Query
2022-11-14 09:22:00 +01:00
2022-12-12 13:30:59 +01:00
jQuery(document).delegate('#searchcomm, #searchcomm2', 'click', function(e) {
e.preventDefault();
manageQuery()
});
2022-12-12 13:30:59 +01:00
//Reset
jQuery(document).delegate('#cleanresult', 'click', function(e) {
console.log('clean')
cleanSearchResult()
esprlogica = []
$('#queryText').val("")
});
//Utility functions
2022-12-06 11:25:04 +01:00
function drawMinimap(name){
showMinimap(name)
currentMinimap = name;
//mmviewer diventa draggable
minimapTopPos = $('#minimapviewer').offset().top
console.log("top viewer init " + minimapTopPos)
$('#minimapviewer').draggable({
axis: "y",
containment: $('#displayminimap'),
scroll: false,
start: function(event, ui) { minimapScrolling = true; },
stop: function(event, ui) { minimapScrolling = false; },
drag: function(event, ui) {
minimapScrolling = true;
trackScrollViewer(ui.offset.top - minimapTopPos);
console.log(ui);
}
});
2022-12-06 11:25:04 +01:00
}
2022-12-12 13:30:59 +01:00
function cleanSearchResult() {
2022-11-08 10:44:20 +01:00
for (verso of listarisultati) {
idverso = '#' + verso
$(idverso).attr('class', 'f6 text-secondary text-nowrap font-weight-lighter')
}
$(".badge").remove();
listarisultati = []
if (currentMinimap != '')
showMinimap(currentMinimap)
}
2022-12-12 13:30:59 +01:00
function manageQuery(){
cleanSearchResult()
esprlogica = []
jsonQuery=getJsonQuery()
console.log("got json query")
if(jsonQuery['QueryAvanzata']==0 && jsonQuery['Clausola0']['queryText']==''){
alert('Digitare una stringa da cercare')
return
}
2022-12-12 13:30:59 +01:00
jsonQuery = buildSQ(jsonQuery)
myquery = new sparqlGenerator().stringify(jsonQuery)
console.log(myquery)
executeQuerySearch(myquery)
}
2022-12-06 11:25:04 +01:00
//minimap management
2022-11-08 10:44:20 +01:00
function showMinimap(element) {
mmplace = document.getElementById('displayminimap')
minimap.className = 'minimap__container'
minimapSize.className = 'minimap__size'
viewer.className = 'minimap__viewer'
viewer.id = 'minimapviewer'
minimapContent.className = 'minimap__content'
2022-11-08 10:44:20 +01:00
minimap.append(minimapSize, viewer, minimapContent);
mmplace.appendChild(minimap)
2022-11-17 16:37:08 +01:00
let html = document.getElementById(element).outerHTML//innerHTML
if (html == null | html == '')
return;
2022-11-08 10:44:20 +01:00
let iFrameDoc = minimapContent.contentWindow.document;
2022-11-17 16:37:08 +01:00
var cssLinkb = document.createElement("link");
cssLinkb.href = "https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css";
cssLinkb.rel = "stylesheet";
2022-11-17 16:37:08 +01:00
cssLinkb.type = "text/css";
2022-11-18 14:55:06 +01:00
var cssLinkscroll = document.createElement("link");
cssLinkscroll.href = "./css/browsingldc.css";
cssLinkscroll.rel = "stylesheet";
2022-11-18 14:55:06 +01:00
cssLinkscroll.type = "text/css";
2022-11-08 10:44:20 +01:00
iFrameDoc.open();
iFrameDoc.write(html);
iFrameDoc.close();
2022-11-17 16:37:08 +01:00
iFrameDoc.head.appendChild(cssLinkb);
2022-11-18 14:55:06 +01:00
iFrameDoc.head.appendChild(cssLinkscroll);
mappedElement = element
2022-11-17 16:37:08 +01:00
getDimensionsDiv()
window.addEventListener('scroll', trackScroll)
2022-11-17 16:37:08 +01:00
window.addEventListener('resize', getDimensionsDiv)
}
function getInnerHeight(elm) {
var computed = getComputedStyle(elm),
padding = parseInt(computed.paddingTop) + parseInt(computed.paddingBottom);
return elm.clientHeight - padding
}
2022-12-06 11:25:04 +01:00
function getDimensionsDiv() {
cantoplace = document.getElementById(displayId)
elementplace = document.getElementById(mappedElement)
bodyWidth = elementplace.clientWidth;
bodyRatio = elementplace.clientHeight / bodyWidth;
let winRatio = getInnerHeight(cantoplace) / (cantoplace.clientWidth);
2022-12-06 11:25:04 +01:00
//minimap.style.width='15%';
minimap.style.width = '100%';
2022-12-06 11:25:04 +01:00
realScale = minimap.clientWidth / bodyWidth;
2022-12-06 11:25:04 +01:00
minimapSize.style.paddingTop = `${bodyRatio * 100}%`
viewer.style.paddingTop = `${winRatio * 100}%`;
minimapContent.style.transform = `scale(${realScale})`;
minimapContent.style.width = `${(100 / realScale)}%`;
minimapContent.style.height = `${(100 / realScale)}%`;
//minimapContent.style.height=`100%`;
minimapContent.style.float = 'right';
2022-11-07 11:52:54 +01:00
}
2022-11-17 10:15:29 +01:00
function trackScroll() {
//console.log(window.scrollY)
viewer.style.transform = `translateY(${window.scrollY * realScale}px)`
2022-11-17 10:15:29 +01:00
}
function trackScrollCanto() {
console.log('cantoscrollT*realscale ' + document.getElementById(displayId).scrollTop * realScale + ' top: ' + document.getElementById(displayId).scrollTop)
viewer.style.transform = `translateY(${document.getElementById(displayId).scrollTop * realScale}px)`
}
function trackScrollViewer(toppos) {
console.log('scrollMMView top*1/realScale: ' + toppos * (1 / realScale) + ' top pos: ' + toppos)
did = document.getElementById(displayId)
did.scrollTop = (toppos * (1 / realScale))
}
function hideMinimap() {
2022-11-11 22:11:22 +01:00
$('.canto').each(function() {
$(this).attr("style", 'display:none');
});
$(".minimap__container").remove();
currentMinimap = ''
2022-11-11 22:11:22 +01:00
}
2022-11-08 10:44:20 +01:00
2022-12-06 11:25:04 +01:00
//Cantica management functions
function showCantica(cantica) {
var listaCanti = new Set()
var setVersi = new Set()
$("#loader").show();
containerForme = formeprima
if (cantica == 'Paradiso')
containerForme = formeterza
if (cantica == 'Purgatorio')
containerForme = formeseconda
for (cmpItem of containerForme) {
formaItem = cmpItem.split("_")
listaCanti = createDivCanti(listaCanti, cantica, 'Canto ' + formaItem[1])
var forma = new Object()
setVersi = createDivVersi(setVersi, cantica, formaItem[3], 'Canto ' + formaItem[1])
forma.numverso = formaItem[3];
forma.canto = 'Canto ' + formaItem[1];
forma.pos = formaItem[2];
forma.form = formaItem[0];
forma.catgramm = formaItem[4];
forma.cantica = cantica;
createSpanForme(forma)
}
$("#loader").hide();
return
}
2022-12-06 15:48:49 +01:00
function createSpanForme(element) {
var spanoccorrenza = $('<span />')
var pos = parseInt(element.pos)
spanoccorrenza.attr('id', element.cantica + "_" + element.canto.replace(" ", "_") + "_" + element.numverso + "_" + pos)
2022-12-13 13:44:39 +01:00
spanoccorrenza.attr('data-cg', element.catgramm)
if (listarisultati.length > 0 && listarisultati.includes(spanoccorrenza.attr('id')))
spanoccorrenza.attr('class', "font-weight-bold mark ")
spanoccorrenza.append(element.form.replaceAll("%20", " "))
elementoverso = $('#' + element.cantica + "_" + element.canto.replace(" ", "_") + "_" + element.numverso)
spanoccorrenza.appendTo(elementoverso)
elementoverso.append('&nbsp;')
}
function createDivVersi(setVersi, cantica, num, canto) {
if (setVersi.has(cantica + "_" + num + "_" + canto))
return setVersi
else {
var elementoverso = $('<p />')
2022-12-06 11:25:04 +01:00
elementoverso.append(num + ". ")
elementoverso.attr("style", "line-height: 0.5em")
elementoverso.attr('class', 'f6 text-secondary text-nowrap font-weight-lighter')
elementoverso.attr('id', cantica + "_" + canto.replace(" ", "_") + "_" + num)
elementoverso.appendTo('#' + cantica + canto.replace(" ", "_"))
setVersi.add(cantica + "_" + num + "_" + canto)
return setVersi
}
}
function createDivCanti(orderedListaCanti, cantica, canto) {
if (orderedListaCanti.has(canto)) {
return orderedListaCanti
}
var divcanto = $('<div />')
var titolocanto = $('<h4 class="titolo-canto pb-2" />')
titolocanto.append(cantica + ", " + canto)
divcanto.attr('class', 'canto')
divcanto.attr('style', 'display:none')
divcanto.attr('id', (cantica + canto).replace(" ", "_"))
titolocanto.appendTo(divcanto)
divcanto.appendTo(' .blog-main')
orderedListaCanti.add(canto)
return orderedListaCanti
}
2022-12-12 13:30:59 +01:00
2022-12-06 11:25:04 +01:00
//execute query
2022-11-11 22:11:22 +01:00
async function executeQuerySearch(query) {
$("#loader").show();
2022-11-11 22:11:22 +01:00
seachbBindingsStream = await myEngine.queryBindings(query, { sources: [{ type: 'sparql', value: sparqlEndpoint },], });
//console.log(query)
2022-11-11 22:11:22 +01:00
alert(query)
try {
var result = new Set()
var resultsInCantica = []
var resultsInCanto = []
2022-11-11 22:11:22 +01:00
seachbBindingsStream.on('data', (binding) => {
//cantica
cantica = binding.get('Cantica').value;
2022-11-11 22:11:22 +01:00
//canto
canto = binding.get('Canto').value;
2022-11-11 22:11:22 +01:00
//numero del verso
numverso = binding.get('NumeroVerso').value;
//posizione del frammento
pos = binding.get('pos').value;
if (!result.has(cantica + "_" + canto.replace(" ", "_") + "_" + numverso + "_" + pos)){
result.add(cantica + "_" + canto.replace(" ", "_") + "_" + numverso + "_" + pos)
resultsInCantica.push(cantica);
resultsInCanto.push(cantica + canto)
}
2022-11-11 22:11:22 +01:00
});
seachbBindingsStream.on('end', () => {
listarisultati = Array.from(result)
cantiche = ["Inferno", "Purgatorio", "Paradiso"]
//evidenzia risultati
for (ris of listarisultati) {
idris = '#' + ris
$(idris).attr('class', "font-weight-bold mark ")
2022-11-12 19:45:14 +01:00
}
//aggiunge badge con totale risultati accanto alle cantiche
i = 0
for (cantica of cantiche) {
i += 1
let count = resultsInCantica.reduce((n, x) => n + (x === cantica), 0);
2022-12-12 13:30:59 +01:00
if (count>0){
var spanbadgecantica = $('<span />');
spanbadgecantica.attr('class', 'badge badge-primary badge-pill ml-1');
spanbadgecantica.append(count)
spanbadgecantica.appendTo($('#cantica' + i))
}
}
//aggiunge badge con totale risultati accanto ai canti
for (cantica of cantiche) {
for (k of Array(35).keys()) {
if (k == 0)
continue
let count = resultsInCanto.reduce((n, x) => n + (x === cantica + 'Canto ' + k), 0);
2022-12-12 13:30:59 +01:00
if (count>0){
var spanbadge = $('<span />');
spanbadge.attr('class', cantobadgeclass);
spanbadge.append(count)
//$("[name='nameofobject']")
spanbadge.appendTo($('[name="' + cantica + 'Canto ' + k + '"]'))
//spanbadge.insertAfter($('[name="'+cantica+'Canto '+k+'"]'))
}
}
}
//aggiorna minimap
if (currentMinimap != '') {
2022-11-12 19:45:14 +01:00
showMinimap(currentMinimap)
2022-11-06 17:12:56 +01:00
}
$("#loader").hide();
2022-11-08 10:44:20 +01:00
2022-11-06 17:12:56 +01:00
});
}
2022-11-08 10:44:20 +01:00
catch (err) {
console.log(err.message);
//$("#loader").hide();
2022-11-06 17:12:56 +01:00
};
2022-11-06 17:12:56 +01:00
};
2022-11-08 10:44:20 +01:00
2022-11-06 17:12:56 +01:00
});