Skip to content

Commit

Permalink
Merge pull request #1 from Fanny-Leicht-Gymnasium/scrolling-text
Browse files Browse the repository at this point in the history
added Scrolling text
  • Loading branch information
Mr-Comand authored Sep 17, 2024
2 parents 4fcf4e8 + e4567b3 commit 6501744
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 6 deletions.
24 changes: 19 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ type ConfigStruct struct {
SwitchingTime uint
RefetchTime uint
ReloadIframeOnSizeChange bool
FixedHight bool
FixedHight bool
}
ScrollText struct {
Path string
Texts []string
RefetchTime uint
}
WebServer struct {
ServerAddress string
Expand All @@ -46,13 +51,22 @@ var Config ConfigStruct = ConfigStruct{
SwitchingTime uint
RefetchTime uint
ReloadIframeOnSizeChange bool
FixedHight bool
FixedHight bool
}{
Path: "",
SwitchingTime: 10,
RefetchTime: 10,
RefetchTime: 60,
ReloadIframeOnSizeChange: false,
FixedHight: false,
FixedHight: false,
},
ScrollText: struct {
Path string
Texts []string
RefetchTime uint
}{
Path: "",
Texts: []string{"Hello", "World"},
RefetchTime: 60,
},
WebServer: struct {
ServerAddress string
Expand Down Expand Up @@ -99,7 +113,7 @@ func LoadConfig() error {
}

viper.SetConfigFile(ConfigPath)

Config.ScrollText.Texts = nil
// Read the config file if it exists
if _, err := os.Stat(ConfigPath); err == nil {
if err := viper.ReadInConfig(); err != nil {
Expand Down
46 changes: 45 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='/static/main.css'>
<script src='/static/main.js' defer></script>
<script src='/static/scrole.js' defer></script>
<style>
body{
--scrolling-font-size: 4vh;
}
/* Ensure iframes are displayed next to each other */
.iframe-container {
display: flex;
Expand Down Expand Up @@ -47,14 +51,54 @@
object-fit: contain;
position: relative;
}

.fixedHight .bottom-right img{
height: 50vh;
}


#scrolling-text {
position: absolute;
top: 0;
left: 0;
width: 100%;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px;
white-space: nowrap;
overflow: hidden;
box-sizing: border-box;
font-size: var(--scrolling-font-size);
height: 1.5em;
}

#scrolling-text span {
display: inline-block;
padding-left: 5em;
padding-right: 5em;
white-space: nowarp;
position: absolute;
left: 0;
top: 0;
transition: left 1s linear;
}

.scrolling-text-content::after,
.scrolling-text-content::before {
content: ' *** ';
/* Separator content */
color: white;
/* Adjust color as needed */
padding-left: 1em;
/* Space around the separator */
padding-right: 1em;
}
</style>
</head>

<body>
<div id="scrolling-text">

</div>
<div class="iframe-container">
<iframe id="iframe1" class="timetable"></iframe>
<iframe id="iframe2" class="timetable"></iframe>
Expand Down
31 changes: 31 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/Fanny-Leicht-Gymnasium/untisDSB/config"
)
Expand Down Expand Up @@ -91,11 +93,40 @@ func main() {

// Serve files from the advertisement directory
http.Handle("/ad/", http.StripPrefix("/ad/", http.FileServer(http.Dir(config.Config.Advertisement.Path))))
http.HandleFunc("/scrolling-text", func(w http.ResponseWriter, r *http.Request) {
var texts []string
texts = config.Config.ScrollText.Texts
if texts == nil {
texts = []string{}
}
if config.Config.ScrollText.Path != "" {
file, err := os.ReadFile(config.Config.ScrollText.Path)
if err != nil {
log.Printf("Error reading scrolling text file: %v", err)
} else {
for _, line := range strings.Split(string(file), "\n") {
line = strings.TrimSpace(line) // Trim spaces or newlines
if line != "" {
texts = append(texts, line)
}
}
}
}
res := struct {
Texts []string `json:"texts"`
RefetchTime uint `json:"refetchTime"`
}{
Texts: texts,
RefetchTime: config.Config.ScrollText.RefetchTime,
}
json.NewEncoder(w).Encode(res)
})

// Start the server
log.Printf("Server starting on %s...", config.Config.WebServer.ServerAddress)
err = http.ListenAndServe(config.Config.WebServer.ServerAddress, nil)
if err != nil {
log.Fatalf("Error starting server: %v", err)
}

}
1 change: 1 addition & 0 deletions static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ document.addEventListener('DOMContentLoaded', function () {
.catch(error => {
console.error('Error fetching URLs:', error);
});

});
var refetchTime = 60
var reloadIframeOnSizeChange = false
Expand Down
88 changes: 88 additions & 0 deletions static/scrole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const scrollTextElement = document.getElementById('scrolling-text');
var currentIndex = 0
var texts = []
var textComponents = [];
var refetchTime = 60;
let refetchInterval;
function newEleement(position) {

// Create a new text component
const newTextComponent = document.createElement('span');
newTextComponent.className = 'scrolling-text-content';
currentIndex = (currentIndex + 1) % texts.length; // Cycle through texts
newTextComponent.textContent = texts[currentIndex];
newTextComponent.style.position = 'absolute';
newTextComponent.style.left = position + "px";

// Add the new component to scrollTextElement
scrollTextElement.appendChild(newTextComponent);

// Update textComponents to include the new element
textComponents = Array.from(scrollTextElement.querySelectorAll(".scrolling-text-content"));
}
function updateText() {
if (texts.length > 0) {
if (textComponents.length > 0) {

scrollTextElement.style.display = ""
textComponents.forEach(textComponent => {
const currentLeft = parseInt(getComputedStyle(textComponent).left, 10);
textComponent.style.left = (currentLeft - 20) + "px";
const textRect = textComponent.getBoundingClientRect();
if (textRect.right < 0) {
textComponent.remove()
textComponents.splice(textComponents.indexOf(textComponent), 1)
}

});
const lastTextComponent = textComponents[textComponents.length - 1];
const lastTextRect = lastTextComponent.getBoundingClientRect();

if (lastTextRect.right < window.innerWidth) {
newEleement((lastTextRect.left) + (lastTextRect.width))
}
} else {
newEleement(0)
}

// scrollTextElement.textContent = texts[currentIndex];
} else {
scrollTextElement.style.display = "none"

}

}
document.addEventListener('DOMContentLoaded', function () {
fetchAndUpdateData();
// Initial update
updateText();
setInterval(updateText, 900);

});
function fetchAndUpdateData() {
// Fetch scrolling text content from /scrolling-text
fetch('/scrolling-text')
.then(response => response.json())
.then(data => {

if (data.texts) {
texts = data.texts
}
if (data.refetchTime){
refetchTime = data.refetchTime
}
console.log("get" + texts)
textComponents = document.querySelectorAll(".scrolling-text-content");

// Clear the existing interval if any
if (refetchInterval) {
clearInterval(refetchInterval);
}
refetchInterval = setInterval(fetchAndUpdateData, refetchTime * 1000);


})
.catch(error => {
console.error('Error fetching scrolling text:', error);
});
}

0 comments on commit 6501744

Please sign in to comment.