From 65be97c028458728098c3291f82c86c84dea9d7b Mon Sep 17 00:00:00 2001 From: strukturart Date: Mon, 6 Dec 2021 22:31:20 +0100 Subject: [PATCH] read only calendars --- application/assets/css/main.css | 10 ++ application/assets/js/eximport.js | 289 ++++++++++++++++-------------- application/index.html | 8 + application/index.js | 41 +++-- 4 files changed, 203 insertions(+), 145 deletions(-) diff --git a/application/assets/css/main.css b/application/assets/css/main.css index f537d873..8f6f16b4 100644 --- a/application/assets/css/main.css +++ b/application/assets/css/main.css @@ -333,6 +333,16 @@ div#options div.import-text { padding: 10px; } +div#subscription-form { + position: fixed; + z-index: 30; + bottom: 40px; + display: none; + height: 100px; + background: white; + border-top: 2px solid black; +} + /*///////////////// ///BOTTOM BAR//// ////////////////*/ diff --git a/application/assets/js/eximport.js b/application/assets/js/eximport.js index 0ae4310e..dd58c669 100644 --- a/application/assets/js/eximport.js +++ b/application/assets/js/eximport.js @@ -1,41 +1,4 @@ const eximport = (() => { - let months = [ - "Jan", - "Feb", - "Mar", - "Apr", - "May", - "Jun", - "Jul", - "Aug", - "Sep", - "Oct", - "Nov", - "Dec", - ]; - - let weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; - - function share(url, name) { - var activity = new MozActivity({ - name: "share", - data: { - type: "text/calendar", - number: 1, - blobs: [url], - filenames: [name], - }, - }); - - activity.onsuccess = function () { - alert("done"); - }; - - activity.onerror = function () { - console.log("The activity encounter en error: " + this.error); - }; - } - let export_json = function () { var sdcard = navigator.getDeviceStorage("sdcard"); @@ -112,6 +75,9 @@ const eximport = (() => { }; }, 2000); }; + //////////// + ///LIST ICS + ////////////// let list_ics = function () { //search .ics files @@ -147,6 +113,155 @@ const eximport = (() => { }); }; + /////////////// + ///PARSE ICS + /////////////// + + let parse_ics = function (data, callback, saveOnDevice) { + var data = data.split(/\r\n|\n/); + data = data.join("\r\n"); + //parse iCal data + var jcalData = ICAL.parse(data); + var vcalendar = new ICAL.Component(jcalData); + let last_uid = ""; + let last_date = ""; + + vcalendar.getAllSubcomponents("vevent").forEach(function (index) { + if ( + index.getFirstPropertyValue("dtstart") == "" && + index.getFirstPropertyValue("summary") == "" + ) + return false; + + let t = new Date(index.getFirstPropertyValue("dtstart")); + let f = new Date(index.getFirstPropertyValue("dtend")); + + //last modified + let g = new Date(index.getFirstPropertyValue("last-modified")).getTime(); + + let k = t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate(); + + let s_month = `0${t.getMonth() + 1}`.slice(-2); + let s_day = `0${t.getDate()}`.slice(-2); + let s_year = t.getFullYear(); + + let s_hour = t.getHours() ? t.getHours() : "00"; + let s_minutes = t.getMinutes() ? t.getMinutes() : "00"; + let s_seconds = t.getSeconds() ? t.getSeconds() : "00"; + + let e_month = `0${f.getMonth() + 1}`.slice(-2); + let e_day = `0${f.getDate()}`.slice(-2); + let e_year = f.getFullYear(); + + let e_hour = f.getHours() ? f.getHours() : "00"; + let e_minutes = f.getMinutes() ? f.getMinutes() : "00"; + let e_seconds = f.getSeconds() ? f.getSeconds() : "00"; + + let start_date = + s_year + + s_month + + s_day + + "T" + + s_hour + + s_minutes + + s_minutes + + s_seconds; + + let end_date = + e_year + + e_month + + e_day + + "T" + + e_hour + + e_minutes + + e_minutes + + e_seconds; + + let start_time = s_hour + ":" + s_minutes + ":" + s_seconds; + let end_time = e_hour + ":" + e_minutes + ":" + e_seconds; + if (start_time == end_time) { + start_time = ""; + end_time = ""; + } + + let date = s_year + "-" + s_month + "-" + s_day; + last_uid = ""; + last_date = ""; + + let imp = { + BEGIN: "VEVENT", + UID: index.getFirstPropertyValue("uid"), + SUMMARY: index.getFirstPropertyValue("summary"), + LOCATION: index.getFirstPropertyValue("location"), + DESCRIPTION: index.getFirstPropertyValue("description"), + "LAST-MODIFIED": g, + CLASS: "PRIVATE", + DTSTAMP: start_date, + DTSTART: start_date, + DTEND: end_date, + date: date, + time_start: start_time, + time_end: end_time, + notification: " ", + alarm: "none", + END: "VEVENT", + }; + last_uid = imp.UID; + last_date = imp.date; + events.push(imp); + console.log(imp); + }); + + callback(last_uid, last_date); + if (saveOnDevice) { + localStorage.setItem("events", JSON.stringify(events)); + } + }; + + ///////////// + ///FETCH ICS + /////////// + + let fetch_ics = function (url, cb) { + let xhttp = new XMLHttpRequest({ + mozSystem: true, + }); + + xhttp.open("GET", url + "?time=" + new Date().getTime(), true); + xhttp.timeout = 25000; + xhttp.onload = function () { + if (xhttp.readyState === xhttp.DONE && xhttp.status === 200) { + let data = xhttp.response; + console.log(cb); + parse_ics(data, cb, false); + } + }; + + xhttp.onerror = function () {}; + + xhttp.send(null); + }; + + function share(url, name) { + var activity = new MozActivity({ + name: "share", + data: { + type: "text/calendar", + number: 1, + blobs: [url], + filenames: [name], + }, + }); + + activity.onsuccess = function () { + alert("done"); + }; + + activity.onerror = function () { + console.log("The activity encounter en error: " + this.error); + }; + } + ///////////////////////// /////Load ICS/////////// /////////////////////// @@ -176,104 +291,9 @@ const eximport = (() => { reader.onloadend = function (event) { //text to array - var data = event.target.result.split(/\r\n|\n/); - data = data.join("\r\n"); - //parse iCal data - var jcalData = ICAL.parse(data); - var vcalendar = new ICAL.Component(jcalData); - let last_uid = ""; - let last_date = ""; - - vcalendar.getAllSubcomponents("vevent").forEach(function (index) { - if ( - index.getFirstPropertyValue("dtstart") == "" && - index.getFirstPropertyValue("summary") == "" - ) - return false; - - let t = new Date(index.getFirstPropertyValue("dtstart")); - let f = new Date(index.getFirstPropertyValue("dtend")); - - //last modified - let g = new Date( - index.getFirstPropertyValue("last-modified") - ).getTime(); - - let k = - t.getFullYear() + "-" + (t.getMonth() + 1) + "-" + t.getDate(); - - let s_month = `0${t.getMonth() + 1}`.slice(-2); - let s_day = `0${t.getDate()}`.slice(-2); - let s_year = t.getFullYear(); - - let s_hour = t.getHours() ? t.getHours() : "00"; - let s_minutes = t.getMinutes() ? t.getMinutes() : "00"; - let s_seconds = t.getSeconds() ? t.getSeconds() : "00"; - - let e_month = `0${f.getMonth() + 1}`.slice(-2); - let e_day = `0${f.getDate()}`.slice(-2); - let e_year = f.getFullYear(); - - let e_hour = f.getHours() ? f.getHours() : "00"; - let e_minutes = f.getMinutes() ? f.getMinutes() : "00"; - let e_seconds = f.getSeconds() ? f.getSeconds() : "00"; - - let start_date = - s_year + - s_month + - s_day + - "T" + - s_hour + - s_minutes + - s_minutes + - s_seconds; - - let end_date = - e_year + - e_month + - e_day + - "T" + - e_hour + - e_minutes + - e_minutes + - e_seconds; - - let start_time = s_hour + ":" + s_minutes + ":" + s_seconds; - let end_time = e_hour + ":" + e_minutes + ":" + e_seconds; - if (start_time == end_time) { - start_time = ""; - end_time = ""; - } - let date = s_year + "-" + s_month + "-" + s_day; - last_uid = ""; - last_date = ""; - - let imp = { - BEGIN: "VEVENT", - UID: index.getFirstPropertyValue("uid"), - SUMMARY: index.getFirstPropertyValue("summary"), - LOCATION: index.getFirstPropertyValue("location"), - DESCRIPTION: index.getFirstPropertyValue("description"), - "LAST-MODIFIED": g, - CLASS: "PRIVATE", - DTSTAMP: start_date, - DTSTART: start_date, - DTEND: end_date, - date: date, - time_start: start_time, - time_end: end_time, - notification: " ", - alarm: "none", - END: "VEVENT", - }; - last_uid = imp.UID; - last_date = imp.date; - events.push(imp); - }); - - callback(last_uid, last_date); - localStorage.setItem("events", JSON.stringify(events)); + let data = event.target.result; + parse_ics(data, callback, true); }; reader.readAsText(file); @@ -286,5 +306,6 @@ const eximport = (() => { list_ics, loadICS, share, + fetch_ics, }; })(); diff --git a/application/index.html b/application/index.html index 8d320816..ee6be459 100644 --- a/application/index.html +++ b/application/index.html @@ -143,9 +143,17 @@

{{SUMMARY}}

+
The following events can be imported
+
+
+ + +
+
+ diff --git a/application/index.js b/application/index.js index 93b07a77..3d54cfb9 100644 --- a/application/index.js +++ b/application/index.js @@ -657,7 +657,7 @@ document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll("div#options button")[0].focus(); } }; - //callback import single event + //callback import event let import_event = function (id, date) { console.log(date); status.selected_day = date; @@ -665,11 +665,20 @@ document.addEventListener("DOMContentLoaded", function () { helper.bottom_bar("edit", "", ""); //edit_event(); - status.view = "list-view"; - router(); - helper.toaster("events imported", 2000); + //status.view = "list-view"; + //router(); + //helper.toaster("events imported", 2000); }; + let load_subscriptions = function () { + eximport.fetch_ics( + "https://calendar.google.com/calendar/ical/de.christian%23holiday%40group.v.calendar.google.com/public/basic.ics", + import_event + ); + }; + + load_subscriptions(); + ////////////////////////////// ////KEYPAD HANDLER//////////// ////////////////////////////// @@ -756,7 +765,6 @@ document.addEventListener("DOMContentLoaded", function () { break; case "9": - test_alarm(); break; case "7": @@ -821,6 +829,22 @@ document.addEventListener("DOMContentLoaded", function () { return true; } + if ( + document.activeElement.getAttribute("data-function") == + "add-subscription" + ) { + document.getElementById("subscription-form").style.display = "block"; + + document + .querySelectorAll("div#subscription-form div.item input")[0] + .focus(); + + helper.bottom_bar("QR", "save", "chancel"); + status.view = "subscription"; + + return true; + } + if (document.activeElement.id == "save-event") { if (status.update_event_id != "") { update_event(); @@ -906,10 +930,7 @@ document.addEventListener("DOMContentLoaded", function () { } if (evt.repeat) { - if (evt.key == "Backspace") - // evt.preventDefault(); - - longpress = false; + if (evt.key == "Backspace") longpress = false; repeat_action(evt); } } @@ -917,8 +938,6 @@ document.addEventListener("DOMContentLoaded", function () { function handleKeyUp(evt) { if (status.visible === false) return false; - //evt.preventDefault(); - if (evt.key == "Backspace" && document.activeElement.tagName == "INPUT") { }