Skip to content

Commit

Permalink
feat: add new js script Expandable.js
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTheHood committed Dec 30, 2023
1 parent bd10493 commit 1e2de91
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Templates/Scripts/Expandable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const expandables = document.querySelectorAll('.expandable');

expandables.forEach(function(expandable) {
const lines = 5;
expandable.style.maxHeight = "calc(1.5em * " + lines + ")";
expandable.style.overflow = "hidden";
if (hasOverflow(expandable, lines)) {
addExpandButton(expandable);
}
});

function hasOverflow(element, lines) {
if (countBRTags(element) > lines) {
return true;
}
return false;
}

function addExpandButton(expandable) {
var aTag = document.createElement('a');
aTag.href = "#"
aTag.addEventListener('click', function(event) {
event.preventDefault();
aTag.style.display = "none";
expandable.style.maxHeight = "inherit";
});
aTag.innerHTML = 'Alle Versionen anzeigen';
expandable.insertAdjacentElement('afterend', aTag);
}

function countBRTags(element) {
var brTags = element.innerHTML.match(/<br>/g);
return brTags ? brTags.length : 0;
}

0 comments on commit 1e2de91

Please sign in to comment.