VANA/VANA-php/resources/js/app.js

328 lines
8.8 KiB
JavaScript
Raw Normal View History

2024-10-29 21:27:29 +01:00
"use strict";
2024-10-11 08:02:45 +02:00
import './bootstrap';
2024-10-18 17:47:20 +02:00
import Pillarbox from '@srgssr/pillarbox-web';
import { gsap } from "gsap";
import { Draggable } from "gsap/Draggable";
import * as echarts from 'echarts';
// Tabs
2024-11-01 22:55:38 +01:00
function changeTab(el){
2024-10-18 17:47:20 +02:00
2024-11-01 22:55:38 +01:00
let tabId = el.target.getAttribute('id');
let panels = document.querySelectorAll('.panel');
2024-10-18 17:47:20 +02:00
2024-11-01 22:55:38 +01:00
panels.forEach(p => {
if(p.getAttribute('id') === tabId+'_panel'){
p.hidden = false;
}else{
p.hidden = true;
}
})
}
2024-10-18 17:47:20 +02:00
2024-11-01 22:55:38 +01:00
const tabs = document.querySelectorAll('input[type="radio"]');
tabs.forEach(tab => {
tab.addEventListener('click', e => {
changeTab(e);
2024-10-18 17:47:20 +02:00
})
})
2024-11-23 16:29:44 +01:00
// Chart word count over time
const wordCountTrack = document.getElementById('words-count-track');
2024-11-30 12:45:02 +01:00
const wordTime = wordCountTrack.getAttribute('data-time').split(',');
const wordWords = wordCountTrack.getAttribute('data-words').split(',');
2024-11-23 16:29:44 +01:00
const chartWordCount = echarts.init(wordCountTrack);
2024-11-30 12:45:02 +01:00
const wordCountCtrl = document.getElementById('topic-track-worte-ctrl');
function setWordCountChart(){
let grid = [];
let xAxis = [];
let yAxis = [];
let series = [];
let totalWordCount = wordWords.slice(-1);
let count = wordCountCtrl.value;
// let max = totalWordCount / count;
let data = [];
let wordWordsLen = wordWords.length;
let chunks = Math.floor(wordWordsLen / count);
for(let i = 0; i < wordWordsLen; i += chunks){
let chunk = wordWords.slice(i, i + chunks );
let firstItem = chunk[0];
let dataChunk = chunk.map((c) => c - firstItem);
data.push(dataChunk)
}
let max = 0;
data.forEach((d) => {
if(parseInt(d.slice(-1)) > max){
console.log(d.slice(-1));
max = d.slice(-1);
2024-11-23 16:29:44 +01:00
}
2024-11-30 12:45:02 +01:00
})
2024-10-29 21:27:29 +01:00
2024-11-30 12:45:02 +01:00
console.log(max)
for(let i = 0; i < count; i++){
grid.push({
left: `${i * (100 / count)}%`,
top: '0',
width: `${100 / count}%`,
height: '100%'
});
xAxis.push({
type: 'category',
gridIndex: i,
min: 0,
axisLabel: {
show: false,
},
axisTick: {
show: false
},
axisLine: {
show: false
}
});
yAxis.push({
type: 'value',
gridIndex: i,
min: 0,
max: max,
splitLine: {
lineStyle: {
color: '#aaa'
}
},
axisLabel: {
show: false
}
});
series.push({
type: 'line',
name: `${i}`,
xAxisIndex: i,
yAxisIndex: i,
data: data[i],
showSymbol: false,
2024-11-23 16:29:44 +01:00
lineStyle: {
2024-11-30 12:45:02 +01:00
color: '#fff',
2024-11-23 16:29:44 +01:00
}
2024-11-30 12:45:02 +01:00
});
}
const wordCountChartOptions = {
tooltip: {
trigger: 'axis',
valueFormatter: (value) => 'Gesprochene Worte ' + new Intl.NumberFormat('de-CH').format(value)
2024-11-23 16:29:44 +01:00
},
2024-11-30 12:45:02 +01:00
grid: grid,
xAxis: xAxis,
yAxis: yAxis,
series: series
}
2024-10-29 21:27:29 +01:00
2024-11-30 12:45:02 +01:00
chartWordCount.setOption(wordCountChartOptions);
}
wordCountCtrl.addEventListener('change', (e) => {
chartWordCount.clear();
setWordCountChart();
})
setWordCountChart();
2024-10-29 21:27:29 +01:00
2024-11-23 16:29:44 +01:00
// Chart sentiments over time
const sentimentsTrack = document.getElementById('sentiment-track');
2024-11-30 12:45:02 +01:00
const sentiTime = sentimentsTrack.getAttribute('data-time').split(',');
const sentiPol = sentimentsTrack.getAttribute('data-sentiments').split(',');
const sentiWeights = sentimentsTrack.getAttribute('data-weights').split(',');
2024-11-23 16:29:44 +01:00
const chartSentiments = echarts.init(sentimentsTrack);
2024-11-30 12:45:02 +01:00
2024-11-23 16:29:44 +01:00
const chartSentimentsOptions = {
2024-11-30 12:45:02 +01:00
tooltip: {
trigger: 'axis',
position: "top",
formatter: 'Sekunde: {b0}<br>Sentiment: {c0}'
},
2024-10-29 21:27:29 +01:00
grid: {
2024-11-30 12:45:02 +01:00
show: false,
2024-10-29 21:27:29 +01:00
top: 0,
bottom: 0,
right: 0,
left: 0
},
2024-10-18 17:47:20 +02:00
xAxis: {
2024-11-30 12:45:02 +01:00
type: "category",
data: sentiTime
2024-10-18 17:47:20 +02:00
},
yAxis: {
2024-11-30 12:45:02 +01:00
type: "category",
data: ["-1", "0", "1"]
2024-10-18 17:47:20 +02:00
},
2024-11-30 12:45:02 +01:00
color: '#fff',
2024-10-18 17:47:20 +02:00
series: [
{
2024-11-30 12:45:02 +01:00
symbolSize: sentiWeights,
data: sentiPol,
type: 'scatter'
2024-10-18 17:47:20 +02:00
}
]
};
2024-11-23 16:29:44 +01:00
chartSentiments.setOption(chartSentimentsOptions);
2024-10-18 17:47:20 +02:00
// VIDEO
2024-10-29 21:27:29 +01:00
2024-10-18 17:47:20 +02:00
const video = document.getElementById('video');
// Timeline draggable
gsap.registerPlugin(Draggable);
const player = new Pillarbox('my-player', {
controls: false,
muted: true,
srgOptions: {
liveui: false
}
});
player.src({ src: video.getAttribute('data-urn'), type: 'srgssr/urn' });
2024-10-29 21:27:29 +01:00
const timelineIndicator = document.getElementById('timeline-bar-ctrl');
2024-10-18 17:47:20 +02:00
// Play / Pause video
video.addEventListener('click', _ => {
player.paused() ? player.play() : player.pause();
2024-11-01 09:45:32 +01:00
timelineTimelabels();
2024-10-18 17:47:20 +02:00
})
2024-10-29 21:27:29 +01:00
// Transcript
const transcript = document.getElementById('transcript');
const transcript_cues = transcript.querySelectorAll('[data-start]');
transcript_cues.forEach(c => {
c.addEventListener('click', (e) => {
player.currentTime(e.target.getAttribute('data-start'));
2024-11-01 09:45:32 +01:00
player.play();
2024-10-29 21:27:29 +01:00
});
});
/************
* Timeline *
************/
const status = {
timelineIndicatorIsDragged: false
}
2024-11-01 09:45:32 +01:00
function timelineTimelabels(){
let duration = player.duration()
let step = duration / 10;
let html = '<span>00:00:00</span>'
for(let i = 1; i <= 10; i++){
html += `<span>${secondsToTimecode(i * step)}</span>`;
}
document.querySelector('#timeline-bar').insertAdjacentHTML('beforeend', html);
}
2024-10-29 21:27:29 +01:00
/**
* calcCurrenttimeByPosition
* Calculate time to seek to, based on the position of the timelineIndicator.
**/
function calcCurrenttimeByPosition(x){
return (player.duration() / document.getElementById('timeline-bar').offsetWidth) * x
}
/**
* Updates the position of the timeline indicator with every timeupdate of the video.
**/
player.on('timeupdate', e =>{
if(!status.timelineIndicatorIsDragged){
let timelineIndicatorPosition = (document.getElementById('timeline-bar').offsetWidth / player.duration()) * player.currentTime();
timelineIndicator.style.transform = `translate3d(${timelineIndicatorPosition}px, 0px, 0px)`;
2024-10-18 17:47:20 +02:00
}
})
2024-10-29 21:27:29 +01:00
/**
* Initialize the timelineindicator
**/
document.addEventListener("DOMContentLoaded", (event) => {
gsap.registerPlugin(Draggable)
Draggable.create("#timeline-bar-ctrl", {
type: "x",
bounds: document.getElementById("timeline-bar"),
onDragStart: function () {
status.timelineIndicatorIsDragged = true;
},
onDragEnd: function () {
player.currentTime(calcCurrenttimeByPosition(this.x));
2024-11-01 17:50:06 +01:00
player.play();
2024-10-29 21:27:29 +01:00
status.timelineIndicatorIsDragged = false;
},
});
});
2024-10-18 17:47:20 +02:00
2024-10-29 21:27:29 +01:00
/*
document.querySelector('#timeline-bar').addEventListener('click', e => {
console.log(e.clientX - e.target.offsetLeft);
let x = e.clientX - e.target.offsetLeft;
player.currentTime(calcCurrenttimeByPosition(x));
})
*/
2024-11-01 22:55:38 +01:00
const topics = [
["Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic6 Topic7 Topic8"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
["Topic1 Topic2 Topic3", "Topic3 Topic4 Topic5", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3", "Topic1 Topic2 Topic3"],
];
2024-11-01 17:50:06 +01:00
function setTopicSegments(count){
let html = '';
2024-11-01 22:55:38 +01:00
topics[count - 1].forEach(t => {
html += `<li>${t}</li>`;
})
2024-11-01 17:50:06 +01:00
let list = document.querySelector('#topic-segement-list');
list.innerHTML = html;
list.style.gridTemplate = `1fr / repeat(${count}, 1fr)`;
}
2024-11-01 22:55:38 +01:00
2024-11-01 17:50:06 +01:00
let topicSegmentCtrl = document.getElementById('topic-track-segment-ctrl');
topicSegmentCtrl.addEventListener('change', e => {
setTopicSegments(e.target.value);
})
setTopicSegments(topicSegmentCtrl.value);