From ffc9352f91702ba6b1e14e1ecd960a78b1f48fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Z=C3=A1le=C5=A1=C3=A1k?= Date: Wed, 27 Jan 2021 00:17:59 +0100 Subject: [PATCH] display comments from xml --- js/controller.js | 21 +++++++++++++++++++++ js/model.js | 6 +++--- js/view.js | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/js/controller.js b/js/controller.js index 89ef6d2..3ce6db4 100644 --- a/js/controller.js +++ b/js/controller.js @@ -51,6 +51,7 @@ class Controller { "timeupdate", function () { this.view.videoPlayer.updateProgress(); + this.getActiveComment(); }.bind(this) ); video.addEventListener( @@ -85,4 +86,24 @@ class Controller { return this.model.parseXML(TheDocument); } + + getActiveComment() { + const video = document.getElementById("video"); + let currentTime = Math.round(video.currentTime); + + for (let i = 0; i < this.model.comments.length; i++) { + if ( + currentTime > this.model.comments[i].start && + currentTime < this.model.comments[i].end + ) { + console.log("active: " + i); + this.view.activateComment(i); + this.view.activateCommentText(this.model.comments[i].fulltext); + return + } else { + this.view.activateComment(null); + this.view.activateCommentText(null); + } + } + } } diff --git a/js/model.js b/js/model.js index 2bb153a..92a9196 100644 --- a/js/model.js +++ b/js/model.js @@ -5,7 +5,7 @@ class Model { let parser = new DOMParser(); let xmlDoc = parser.parseFromString(xml, "text/xml"); - let comments = []; + this.comments = []; let comment = xmlDoc.getElementsByTagName("comment"); for (let i = 0; i < comment.length; i++) { @@ -14,9 +14,9 @@ class Model { let text = comment[i].getElementsByTagName("text")[0].innerHTML; let fulltext = comment[i].getElementsByTagName("fulltext")[0].innerHTML; - comments.push(new Comment(start, end, text, fulltext)); + this.comments.push(new Comment(start, end, text, fulltext)); } - return comments; + return this.comments; } } diff --git a/js/view.js b/js/view.js index 55549f8..56e9d90 100644 --- a/js/view.js +++ b/js/view.js @@ -45,7 +45,7 @@ class View { time.innerHTML = minutes.toString().padStart(2, "0") + ":" + - seconds.toString().padStart(2, "0");; + seconds.toString().padStart(2, "0"); comment.appendChild(time); @@ -65,9 +65,13 @@ class View { for (let i = 0; i < comments.length; i++) { let videoComment = document.createElement("div"); videoComment.setAttribute("class", "videoComment"); - let percentLeft = (comments[i].start / this.videoPlayer.videoDuration) * 100; + let percentLeft = + (comments[i].start / this.videoPlayer.videoDuration) * 100; videoComment.style.left = percentLeft + "%"; - let percentWidth = ((comments[i].end - comments[i].start) / this.videoPlayer.videoDuration) * 100; + let percentWidth = + ((comments[i].end - comments[i].start) / + this.videoPlayer.videoDuration) * + 100; videoComment.style.width = percentWidth + "%"; videoComment.setAttribute("id", "v" + i); @@ -79,6 +83,32 @@ class View { } } + activateComment(id) { + if (id != null) { + let activeComment = document.getElementById("c" + id); + activeComment.classList.add("activeComment"); + + activeComment.firstChild.contentDocument.getElementById( + "path4" + ).style.fill = "#ffa800"; + } else { + let comments = document.getElementsByClassName("comment"); + + for (let i = 0; i < comments.length; i++) { + comments[i].classList.remove("activeComment"); + comments[i].firstChild.contentDocument.getElementById( + "path4" + ).style.fill = "#c1c1c1"; + } + } + } + + activateCommentText(text) { + let textArea = document.getElementsByTagName("article")[0]; + + textArea.innerHTML = text; + } + /* colorTogglePlaySVG() { var playSVG = document.getElementById("togglePlayIcon"); var svgDoc;