Skip to content

Commit

Permalink
less keys
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Aug 24, 2023
1 parent b5bd8f6 commit 4f6b57f
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 79 deletions.
169 changes: 120 additions & 49 deletions application/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ export let settings = {

const style_calendar_cell = (targetYear, targetMonth) => {
if (events.length == 0) return;

/*
const eventsForMonth = events.filter((event) => {
const eventDate = dayjs(event.dateStart);
return eventDate.year() === targetYear && eventDate.month() === targetMonth;
});
*/

document.querySelectorAll("div.calendar-cell").forEach((e) => {
const p = e.getAttribute("data-date");
const eventInfo = event_check(p, eventsForMonth);
const rruleInfo = rrule_check(p, eventsForMonth);
const eventInfo = event_check(p);
const rruleInfo = rrule_check(p);

e.classList.toggle("event", eventInfo.event || rruleInfo.rrule);
e.classList.toggle(
Expand Down Expand Up @@ -797,6 +798,51 @@ const find_closest_date = function (date) {
}
};

function unixTimestampToDate(unixTimestamp) {
const date = new Date(unixTimestamp * 1000); // Convert Unix timestamp to milliseconds
date.setUTCHours(0, 0, 0, 0); // Set time to midnight UTC

const dateWithoutTimeUnix = Math.floor(date.getTime() / 1000); // Convert back to Unix timestamp (in seconds)
return dateWithoutTimeUnix;
}
/*
const event_check = function (date) {
let feedback = {
event: false,
multidayevent: false,
};
const targetTime = new Date(date).getTime();
let eventFound = false;
let multidayEventCount = 0;
for (const event of events) {
const eventStartTime = unixTimestampToDate(event.dateStartUnix * 1000);
const eventEndTime = unixTimestampToDate(event.dateEndUnix * 1000);
if (
eventStartTime <= targetTime &&
eventEndTime >= targetTime &&
event.RRULE === undefined
) {
eventFound = true;
if (multidayEventCount > 0) {
feedback.multidayevent = true;
break; // No need to continue checking
}
multidayEventCount++;
}
}
feedback.event = eventFound;
return feedback;
};
*/
const event_check = function (date) {
let feedback = {
event: false,
Expand All @@ -812,7 +858,7 @@ const event_check = function (date) {
eventStartTime === targetTime ||
(eventStartTime <= targetTime &&
eventEndTime >= targetTime &&
event.rrule_json.freq === undefined)
!event.RRULE)
);
});

Expand All @@ -823,6 +869,7 @@ const event_check = function (date) {
};

// check if has recur event

let rrule_check = function (date) {
let feedback = {
date: "",
Expand All @@ -842,18 +889,18 @@ let rrule_check = function (date) {
let a = new Date(events[t].dateStart).getTime();
let b = new Date(events[t].dateEnd).getTime();
let c = new Date(date).getTime();
let d = events[t].rrule_json.freq;
let d = events[t].RRULE.freq;
let e = events[t].RRULE;

if (typeof e !== "undefined" && e !== undefined && e != null) {
//recurrences

if (events[t].rrule_json != null) {
if (events[t].RRULE != null) {
//endless || with end
if (events[t].rrule_json.until == null) {
if (events[t].RRULE.until == null) {
b = new Date("3000-01-01").getTime();
} else {
b = new Date(events[t].rrule_json.until).getTime();
b = new Date(events[t].RRULE.until).getTime();
}
}

Expand Down Expand Up @@ -962,7 +1009,7 @@ let event_slider = function (date) {
let a = new Date(events[i].dateStart).getTime();
let b = new Date(events[i].dateEnd).getTime();
let c = new Date(date).getTime();
let d = events[i].rrule_json.freq;
let d = events[i].RRULE.freq;

if (d === "none" || d === "" || d === undefined || d === null) {
if (a === c || (a <= c && b >= c)) {
Expand All @@ -973,8 +1020,8 @@ let event_slider = function (date) {
//workaround if enddate is not set
//AKA infinity

if (events[i].rrule_json != null) {
if (events[i].rrule_json.until == null) {
if (events[i].RRULE != null) {
if (events[i].RRULE.until == null) {
b = new Date("3000-01-01").getTime();
}
}
Expand Down Expand Up @@ -1233,7 +1280,6 @@ let showCalendar = function (month, year) {
let p = year + "-" + mmonth + "-" + day;

const d = new Date(p);
//cell.setAttribute("data-day", d.getDay());

moon.classList.add("moon-phase-" + getMoonPhase(year, month, date));
cell.appendChild(moon);
Expand Down Expand Up @@ -1512,6 +1558,7 @@ var page_events = {
},
oncreate: function () {
find_closest_date();
sort_array(events, "DTSTART", "date");

bottom_bar(
"<img src='assets/image/pencil.svg'>",
Expand Down Expand Up @@ -1566,7 +1613,8 @@ var page_events = {
class: "item events " + u + " " + a,
tabindex: index + 1,
"data-id": item.UID,
"data-date": item.dateStart,
// "data-date": item.dateStart,
"data-date": dayjs.unix(item.dateStartUnix).format("YYYY-MM-DD"),
"data-category": (item.CATEGORIES || "").toUpperCase(),
"data-summary": (item.SUMMARY || "").toUpperCase(),
},
Expand Down Expand Up @@ -1634,11 +1682,11 @@ var page_events_filtered = {
//date
if (item.dateStart != item.dateEnd && !item.allDay) {
de =
dayjs(item.dateStart).format(settings.dateformat) +
dayjs.unix(item.dateStartUnix).format(settings.dateformat) +
" - " +
dayjs(item.dateEnd).format(settings.dateformat);
dayjs.unix(item.dateEndUnix).format(settings.dateformat);
} else {
de = dayjs(item.dateStart).format(settings.dateformat);
de = dayjs.unix(item.dateStartUnix).format(settings.dateformat);
}

let u = item.isSubscription ? "subscription" : "";
Expand All @@ -1649,7 +1697,9 @@ var page_events_filtered = {
class: "item events " + u + " " + a,
tabindex: tindex,
"data-id": item.UID,
"data-date": item.dateStart,
"data-date": dayjs
.unix(item.dateStartUnix)
.format("YYYY-MM-DD"),
"data-category": (item.CATEGORIES || "").toUpperCase(),
"data-summary": (item.SUMMARY || "").toUpperCase(),
},
Expand Down Expand Up @@ -2784,7 +2834,11 @@ var page_edit_event = {
class: "select-box",

oncreate: function ({ dom }) {
dom.value = update_event_date.dateStart;
dom.value = update_event_date.dateStartUnix;

dom.value = dayjs
.unix(update_event_date.dateStartUnix)
.format("YYYY-MM-DD");
},
}),
]),
Expand All @@ -2798,7 +2852,10 @@ var page_edit_event = {
class: "select-box",

oncreate: function ({ dom }) {
dom.value = update_event_date.dateEnd;
// dom.value = update_event_date.dateEnd;
dom.value = dayjs
.unix(update_event_date.dateEndUnix)
.format("YYYY-MM-DD");
},
}),
]),
Expand Down Expand Up @@ -2862,23 +2919,17 @@ var page_edit_event = {
type: "time",
id: "event-time-start",
class: "select-box",
value:
update_event_date.time_start.length == 8
? update_event_date.time_start.slice(0, -3)
: update_event_date.time_start,
value: dayjs.unix(update_event_date.dateStartUnix).format("HH:mm"),
}),
]),
m("div", { class: "item input-parent time", tabindex: "5" }, [
m("label", { for: "event-time-end" }, "End Time"),
m("input", {
placeholder: "hh:mm",
placeholder: "HH:mm",
type: "time",
id: "event-time-end",
class: "select-box",
value:
update_event_date.time_end.length == 8
? update_event_date.time_end.slice(0, -3)
: update_event_date.time_end,
value: dayjs.unix(update_event_date.dateEndUnix).format("HH:mm"),
}),
]),
m("div", { class: "item input-parent", tabindex: "6" }, [
Expand Down Expand Up @@ -2986,7 +3037,7 @@ var page_edit_event = {
"select",
{
id: "event-recur",
value: update_event_date.rrule_json.freq ?? "none",
value: update_event_date.RRULE.freq ?? "none",
class: "select-box",
},
[
Expand Down Expand Up @@ -3099,7 +3150,7 @@ let callback_getfile = function (result) {
localforage
.setItem("events", only_local_events)
.then(function () {
backup_events();
// backup_events();
side_toaster("<img src='assets/image/E25C.svg'", 2000);
setTimeout(function () {
m.route.set("/page_calendar");
Expand Down Expand Up @@ -3699,34 +3750,54 @@ let remove_alarm = function (id) {
// /////////////
// /////////////

const convert_ics_date = function (t, ful) {
let nn = dayjs(t).format("YYYYMMDDTHHmmss");
let convert_ics_date = function (t, ful, s) {
// Replace hyphens and colons with nothing, and replace space with 'T'
let nn = t.replace(/[-:]/g, "").replace(" ", "T");

// If ful is true, extract the date part and format it
if (ful) {
nn = ";VALUE=DATE:" + dayjs(t).format("YYYYMMDD");
} else {
let w = nn.split("T");
nn = ";VALUE=DATE:" + w[0];
}

// If ful is false, add a colon at the beginning
if (!ful) {
nn = ":" + nn;
}

// If s is true, remove colons
if (s) {
nn = nn.replace(/:/g, "");
}

return nn;
};

const days = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"];

const rrule_convert = function (val, date_end, date_start) {
if (val === "none") {
let rrule_convert = function (val, date_end, date_start) {
let r = "";
let f = days[new Date(date_start).getDay()];

if (val == "none") {
return "";
}
if (val != "none") {
r = "FREQ=" + val + ";UNTIL=" + convert_ics_date(date_end, false, true);

if (val == "WEEKLY") {
r =
"FREQ=" +
val +
";INTERVAL=1;BYDAY=" +
f +
";UNTIL=" +
convert_ics_date(date_end, false, true);
}
console.log(r);

const f = days[new Date(date_start).getDay()];

if (val === "WEEKLY") {
return `FREQ=${val};INTERVAL=1;BYDAY=${f};UNTIL=${convert_ics_date(
date_end
)}`;
return r;
}

return `FREQ=${val};UNTIL=${convert_ics_date(date_end)}`;
};

//todo if is rrule enddate == startdate
Expand Down Expand Up @@ -3911,7 +3982,7 @@ let store_event = function (db_id, cal_name) {
.setItem("events", without_subscription)
.then(function () {
clear_form();
backup_events();
// backup_events();
side_toaster("<img src='assets/image/E25C.svg'", 2000);
setTimeout(function () {
m.route.set("/page_calendar");
Expand All @@ -3925,7 +3996,7 @@ let store_event = function (db_id, cal_name) {
//caldav
//rrule event should end on the same day, but rrule.until should set the end date

if (event.RRULE != "") {
if (event.RRULE && event.RRULE != "") {
event.DTEND =
";TZID=" + settings.timezone + convert_ics_date(rrule_dt_end, allDay);
}
Expand Down Expand Up @@ -4150,7 +4221,7 @@ let update_event = function (etag, url, id, db_id, uid) {
.setItem("events", without_subscription)
.then(function () {
clear_form();
backup_events();
//backup_events();
side_toaster("<img src='assets/image/E25C.svg'", 2000);
setTimeout(function () {
m.route.set("/page_calendar");
Expand All @@ -4171,7 +4242,7 @@ let update_event = function (etag, url, id, db_id, uid) {
//rrule event should end on the same day, but rrule.until should set the end date
const alarm = `\nBEGIN:VALARM\nTRIGGER;VALUE=DATE-TIME:${notification_time}\nACTION:AUDIO\nEND:VALARM`;

if (event.RRULE !== "") {
if (event.RRULE && event.RRULE !== "") {
event.DTEND =
";TZID=" + settings.timezone + convert_ics_date(rrule_dt_end, allDay);
}
Expand Down Expand Up @@ -4230,7 +4301,7 @@ let delete_event = function (etag, url, account_id, uid) {
localforage
.setItem("events", without_subscription)
.then(function (value) {
backup_events();
//backup_events();
side_toaster("event deleted", 2000);
if (events.length != 0) {
m.route.set("/page_events");
Expand Down
Loading

0 comments on commit 4f6b57f

Please sign in to comment.