diff --git a/selectedTime.json b/selectedTime.json new file mode 100644 index 0000000..01817f3 --- /dev/null +++ b/selectedTime.json @@ -0,0 +1,87 @@ +[ + { + "NameValue": { + "name": "da1" + }, + "timeValue": { + "time": "08:06" + }, + "daysValue": { + "Montag": false, + "Dienstag": false, + "Mittwoch": false, + "Donnerstag": false, + "Freitag": false, + "Samstag": false, + "Sonntag": false + } + }, + { + "NameValue": { + "name": "da2" + }, + "timeValue": { + "time": "08:06" + }, + "daysValue": { + "Montag": false, + "Dienstag": false, + "Mittwoch": false, + "Donnerstag": true, + "Freitag": false, + "Samstag": false, + "Sonntag": false + } + }, + { + "NameValue": { + "name": "daw" + }, + "timeValue": { + "time": "08:06" + }, + "daysValue": { + "Montag": false, + "Dienstag": false, + "Mittwoch": false, + "Donnerstag": true, + "Freitag": false, + "Samstag": false, + "Sonntag": false + } + }, + { + "NameValue": { + "name": "daa" + }, + "timeValue": { + "time": "08:06" + }, + "daysValue": { + "Montag": false, + "Dienstag": false, + "Mittwoch": false, + "Donnerstag": true, + "Freitag": false, + "Samstag": false, + "Sonntag": false + } + }, + { + "NameValue": { + "name": "daaa" + }, + "timeValue": { + "time": "07:07" + }, + "daysValue": { + "Montag": false, + "Dienstag": false, + "Mittwoch": false, + "Donnerstag": true, + "Freitag": true, + "Samstag": true, + "Sonntag": false + } + } +] \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..4a345c5 --- /dev/null +++ b/server.js @@ -0,0 +1,219 @@ +const http = require('http'); +const fs = require('fs'); +const path = require('path'); + +const PORT = process.env.PORT || 3000; + +let savedData = { + name: null, + time: null, + days: null +}; +/* +// Funktion zum Speichern der Auswahl +function saveSelection(NameValue, timeValue, daysValue) { + const data = { + NameValue, + timeValue, + daysValue + }; + const jsonString = JSON.stringify(data, null, 4); // Hier wird ein Einzug von 4 Leerzeichen verwendet + const filePath = 'selectedTime.json'; + fs.writeFile(filePath, jsonString, (err) => { + if (err) { + console.error('Fehler beim Speichern der Daten:', err); + } else { + console.log('Daten erfolgreich gespeichert.'); + readSelectedTime(); + } + }); +}*/ + +// Funktion zum Speichern der Auswahl +function saveSelection(nameValue, timeValue, daysValue) { + readSelectedTimeclb((err, data) => { + if (err) { + console.error('Fehler beim Lesen der vorhandenen Daten:', err); + return; + } + + // Überprüfen, ob ein Wecker mit dem gleichen Namen bereits existiert + const existingAlarm = data.find(alarm => alarm.NameValue.name === nameValue.name); + if (existingAlarm) { + console.log(`Ein Wecker mit dem Namen "${nameValue.name}" existiert bereits.`); + return; + } + + // Wenn der Name noch nicht existiert, neuen Wecker hinzufügen + data.push({ NameValue: nameValue, timeValue: timeValue, daysValue: daysValue }); + + const jsonString = JSON.stringify(data, null, 4); // Hier wird ein Einzug von 4 Leerzeichen verwendet + const filePath = 'selectedTime.json'; + fs.writeFile(filePath, jsonString, (err) => { + if (err) { + console.error('Fehler beim Speichern der Daten:', err); + } else { + console.log('Daten erfolgreich gespeichert.'); + readSelectedTime(); // Aktualisierte Daten lesen und ausgeben + } + }); + }); +} + + +// Funktion zum Verarbeiten der gespeicherten Daten +function processSavedData() { + // Überprüfen, ob alle Daten vorhanden sind + if (savedData.name && savedData.time && savedData.days) { + // Aufrufen von saveSelection nur, wenn alle Daten vorhanden sind + saveSelection(savedData.name, savedData.time, savedData.days); + // Daten zurücksetzen, um für die nächste Anfrage bereit zu sein + savedData = { + name: null, + time: null, + days: null + }; + } else { + // Nicht alle Daten sind verfügbar, daher warten wir auf weitere Anfragen + } +} + +// Funktion zum Lesen der ausgewählten Zeit +function readSelectedTime() { + const filePath = 'selectedTime.json'; + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error('Fehler beim Lesen der Datei:', err); + } else { + const parsedData = JSON.parse(data); + const formattedData = JSON.stringify(parsedData, null, 4); // Hier wird ein Einzug von 4 Leerzeichen verwendet + console.log('Inhalt von selectedTime.json:\n', formattedData); + } + }); +} + + +// Funktion zum Lesen der ausgewählten Zeit +function readSelectedTimeclb(callback) { + const filePath = 'selectedTime.json'; + fs.readFile(filePath, 'utf8', (err, data) => { + if (err) { + console.error('Fehler beim Lesen der Datei:', err); + callback(err, null); + } else { + let parsedData; + try { + parsedData = JSON.parse(data); + if (!Array.isArray(parsedData)) { + parsedData = []; // Wenn die Daten kein Array sind, initialisiere sie als leeres Array + } + } catch (parseError) { + console.error('Fehler beim Parsen der Daten:', parseError); + parsedData = []; // Bei einem Fehler beim Parsen initialisiere die Daten als leeres Array + } + callback(null, parsedData); + } + }); +} + + + + + + + + + + + + + +// Server erstellen +http.createServer((req, res) => { + if (req.method === 'POST' && req.url === '/savedays') { + let body = ''; + req.on('data', chunk => { + body += chunk.toString(); + }); + req.on('end', () => { + const daysValue = JSON.parse(body); + savedData.days = daysValue; // Zwischengespeicherte Daten aktualisieren + processSavedData(); // Prozess starten + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Daten erfolgreich gespeichert.'); + }); + } else if (req.method === 'POST' && req.url === '/saveTime') { + let body = ''; + req.on('data', chunk => { + body += chunk.toString(); + }); + req.on('end', () => { + const timeValue = JSON.parse(body); + savedData.time = timeValue; // Zwischengespeicherte Daten aktualisieren + processSavedData(); // Prozess starten + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Daten erfolgreich gespeichert.'); + }); + } else if (req.method === 'POST' && req.url === '/saveName') { + let body = ''; + req.on('data', chunk => { + body += chunk.toString(); + }); + req.on('end', () => { + const nameValue = JSON.parse(body); + savedData.name = nameValue; // Zwischengespeicherte Daten aktualisieren + processSavedData(); // Prozess starten + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end('Daten erfolgreich gespeichert.'); + }); + } else if (req.method === 'GET' && req.url === '/selectedTime') { + readSelectedTimeclb((err, data) => { + if (err) { + res.writeHead(500, { 'Content-Type': 'text/plain' }); + res.end('Internal Server Error'); + } else { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(data)); + } + }); + } else { + let filePath = '.' + req.url; + if (filePath === './') { + filePath = './index.html'; + } + const extname = String(path.extname(filePath)).toLowerCase(); + const contentType = { + '.html': 'text/html', + '.js': 'text/javascript', + '.css': 'text/css', + '.json': 'application/json', + '.png': 'image/png', + '.jpg': 'image/jpg', + '.gif': 'image/gif', + '.wav': 'audio/wav', + '.mp4': 'video/mp4', + '.woff': 'application/font-woff', + '.ttf': 'application/font-ttf', + '.eot': 'application/vnd.ms-fontobject', + '.otf': 'application/font-otf', + '.svg': 'application/image/svg+xml' + }[extname] || 'application/octet-stream'; + + fs.readFile(filePath, (err, content) => { + if (err) { + if (err.code == 'ENOENT') { + res.writeHead(404); + res.end('File not found'); + } else { + res.writeHead(500); + res.end('Sorry, check with the site admin for error: ' + err.code); + } + } else { + res.writeHead(200, { 'Content-Type': contentType }); + res.end(content, 'utf-8'); + } + }); + } +}).listen(PORT); + +console.log(`Server running at http://localhost:${PORT}/`); diff --git a/time-pick.js b/time-pick.js new file mode 100644 index 0000000..fd815e5 --- /dev/null +++ b/time-pick.js @@ -0,0 +1,169 @@ +var TimePick = (function () { + + 'use strict'; + + var Constructor = function (element) { + + var globalvar = {}; + globalvar.set_hour = {}; + globalvar.set_minute = {}; + + if(document.getElementById("TimePickStyleSheet") == null) { + MakeStyle(); + } + + var unique_id = RandomString(5); + + if(document.getElementById("TimePickBackgroundOverlay") == null) { + let overlaydiv = document.createElement("div"); + overlaydiv.id= "TimePickBackgroundOverlay"; + document.body.insertBefore(overlaydiv, document.body.firstChild); // Hinzufügen am Anfang des body-Elements + overlaydiv.style.zIndex = "-1"; + } + + globalvar.elemets = document.querySelectorAll(element); + + for (var i = 0; i < globalvar.elemets.length; i++) { + let timepickerbtn = globalvar.elemets[i]; + let TIMESARRAY = {}; + globalvar.elemets[i].setAttribute("TimePick", "input_" + unique_id + '_' + i); + timepickerbtn.insertAdjacentHTML("afterend", ``); + + if(globalvar.elemets[i].value){ + let previustimestr = globalvar.elemets[i].value; + let previustimes = previustimestr.split(":"); + let uniquecode = `${unique_id + '_' + i}`; + + document.getElementById('label_hour_' + uniquecode).innerText = previustimes[0]; + document.getElementById('label_minute_' + uniquecode).innerText = previustimes[1]; + } + else { + globalvar.elemets[i].setAttribute("value", "00:00:00"); + } + + } + + let timepickpopupbtn = document.getElementsByClassName("TimePick_ICON"); + for(let i = 0; i < timepickpopupbtn.length; i++) { + timepickpopupbtn[i].onclick = function () { + let currID = "popup_" + timepickpopupbtn[i].id; + if(document.getElementById(currID).style.display == "flex"){ + document.getElementById(currID).style.display = "none" + return; + } + document.getElementById(currID).style.display = "flex" + } + } + + document.onclick = function(e){ + if(e.target.id == 'TimePickBackgroundOverlay'){ + let TimePick_POPUP = document.getElementsByClassName("TimePick_POPUP"); + for(let i = 0; i < TimePick_POPUP.length; i++) { + TimePick_POPUP[i].style.display = "none"; + } + } + }; + + let adjustbtn = document.getElementsByClassName("adjustbtn"); + + for(let i = 0; i < adjustbtn.length; i++) { + + adjustbtn[i].onclick = function () { + let data = JSON.parse(adjustbtn[i].getAttribute("data")); + + let curr_hour = document.getElementById('label_hour_' + data.id).innerText; + let curr_minute = document.getElementById('label_minute_' + data.id).innerText; + + if (curr_hour != "00") { + globalvar.set_hour[data.id] = parseInt(curr_hour); + } + + if (curr_minute != "00") { + globalvar.set_minute[data.id] = parseInt(curr_minute); + } + + + if(data.type == 'hour' && data.action == 'up'){ + globalvar.set_hour[data.id] = (globalvar.set_hour[data.id]) ? (globalvar.set_hour[data.id] + 1) : 0 + 1; + globalvar.set_hour[data.id] = (globalvar.set_hour[data.id] == 24) ? 0 : globalvar.set_hour[data.id]; + } + if(data.type == 'hour' && data.action == 'down'){ + globalvar.set_hour[data.id] = (globalvar.set_hour[data.id]) ? (globalvar.set_hour[data.id] - 1) : -1; + globalvar.set_hour[data.id] = (globalvar.set_hour[data.id] == -1) ? 23 : globalvar.set_hour[data.id]; + } + + if(data.type == 'minute' && data.action == 'up'){ + globalvar.set_minute[data.id] = (globalvar.set_minute[data.id]) ? (globalvar.set_minute[data.id] + 1) : 0 + 1; + globalvar.set_minute[data.id] = (globalvar.set_minute[data.id] == 60) ? 0 : globalvar.set_minute[data.id]; + } + if(data.type == 'minute' && data.action == 'down'){ + globalvar.set_minute[data.id] = (globalvar.set_minute[data.id]) ? (globalvar.set_minute[data.id] - 1) : -1; + globalvar.set_minute[data.id] = (globalvar.set_minute[data.id] == -1) ? 59 : globalvar.set_minute[data.id]; + } + + + let hrview = (globalvar.set_hour[data.id] == undefined) ? '00' : (globalvar.set_hour[data.id] < 10) ? ("0" + globalvar.set_hour[data.id]) : globalvar.set_hour[data.id]; + let mnview = (globalvar.set_minute[data.id] == undefined) ? '00' : (globalvar.set_minute[data.id] < 10) ? ("0" + globalvar.set_minute[data.id]) : globalvar.set_minute[data.id]; + + document.getElementById('label_hour_' + data.id).innerText = hrview; + document.getElementById('label_minute_' + data.id).innerText = mnview; + + document.querySelectorAll('input[TimePick=input_' + data.id + ']')[0].value = hrview + ":" + mnview ; + } + } + + //private function + function RandomString(length) { + const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; + let result = ''; + const charactersLength = characters.length; + for ( let i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; + } + + function MakeStyle(){ + var styles = ".TimePick_POPUP{ position: absolute; height: 80px; width: 120px; background-color: #ffffff; border-radius: 3px; border: 1px solid #eeeeee; display: flex; justify-content: center; align-items: center; box-shadow: rgba(17, 17, 26, 0.05) 0px 4px 16px, rgba(17, 17, 26, 0.05) 0px 8px 32px; z-index:500; margin-left: 30px; margin-top: -32px; display: none;} svg:active{ fill: #0000; stroke: #0000;} .TimePick_BTN{ position: absolute; background-color: transparent; border: none; margin-left: -35px; margin-top: 7px; cursor: pointer;} .TimePick_ICON{ opacity: 0.5;} .TimePick_ICON:hover{ opacity: 1;} input{ padding: 10px; border-width: 1px; border-style: solid; border-color: lightgray; background-color: white;} #TimePickBackgroundOverlay{ background-color:transparent; position:fixed; top:0; left:0; right:0; bottom:0; display:block;} .label{ font-size: 20px;} .hour{ display: flex; flex-direction: column; width: 35px;} .minute{ display: flex; flex-direction: column; width: 35px;}"; + var styleSheet = document.createElement("style"); + styleSheet.type = "text/css"; + styleSheet.id = "TimePickStyleSheet"; + styleSheet.innerText = styles; + document.head.appendChild(styleSheet); + return; + } + + return globalvar; + }; + + return Constructor; + +})( +/********** + ************************************************************************* + *********** JAVASCRIPT MODULE TIME PICKER DONE BY KATHEESKUMAR ********** + ************************************************************************* + ***************************** Version: 1.0 ****************************** + ************************************************************************* + **********/ +); \ No newline at end of file