Skip to content
This repository was archived by the owner on Oct 7, 2023. It is now read-only.

Commit dc1dac6

Browse files
authored
filtering, eventCircle settings (#26)
* filter calendars, alldayevents with calCircles - allow filtering calendars - let disable all-day events from counting towards eventCircle color - change "flipped" to also use widget parameter
1 parent 2dface1 commit dc1dac6

File tree

7 files changed

+77
-9
lines changed

7 files changed

+77
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
calendar app.
2626
- `calendarApp` - Tapping on the widget launches a calendar app (as long as `debug: false`), by default it launches the iOS Calendar app, however it can be changed to anything as long as the app supports callback URLs. Changing the `calshow` to something else would open other apps. E.g. for Google Calendar it is `googlecalendar`, for Fantastical it is `x-fantastical3`.
2727
- `backgroundImage` - Image path to use as the widget background, which is taken either from the widget parameters, from the `params` variable at the top, or just replace `params.bg` with the image path. To get an image that can then be used to have a "transparent" widget background use [this](https://gist.github.com/mzeryck/3a97ccd1e059b3afa3c6666d27a496c9#gistcomment-3468585) script and save it to the _Scriptable_ folder on iCloud. Then set either the widget parameter (long press on the widget -> edit widget -> parameter) to `{ "bg": "my-image.jpg"}` where `my-image` is the name of your transparent background **OR** change the line which has `{ bg: "1121.jpg" }` to include your image name.
28+
- `calFilter` - Optionally an array of calendars to show, shows all calendars if empty. Can be supplied as a widget parameter to only affect that particular widget.
2829
- `widgetBackgroundColor` - In case of no background image, what color to use.
2930
- `todayTextColor` - color of today's date
3031
- `markToday` - show a circle around today or not
3132
- `todayCircleColor` - if we mark days, then in what color
3233
- `showEventCircles` - adds colored background for all days that have an event. The color intensity is based roughly on how many events take place that day.
34+
- `discountAllDayEvents` - if true, all-day events don't count towards eventCircle intensity value
3335
- `eventCircleColor` - if showing event circles, then in what color
3436
- `weekdayTextColor` - color of weekdays
3537
- `weekendLetters` - color of the letters in the top row
@@ -48,6 +50,7 @@
4850
- `showPrevMonth` - would show days from the previous month if they fit into the calendar view.
4951
- `showNextMonth` - would show days from the next month if they fit into the calendar view.
5052
- `individualDateTargets` - would allow tapping on a date to open that specific day in the calendar set by the `calendarApp` setting. (atm, supports default iOS calendar and Fantastical callback urls, should be possible to add more).
53+
- `flipped` - the layout for the medium-sized widget can be either the default, `events - calendar`, or a flipped, `calendar - events` layout. This setting can also be given as a widget parameter (something like: `{ "flipped": true }`) to just affect that particular widget.
5154

5255
## Small Widgets
5356

calendar.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ var settings = {
44
debug: false,
55
calendarApp: "calshow",
66
backgroundImage: params.bg ? params.bg : "transparent.jpg",
7+
calFilter: params.calFilter ? params.calFilter : [],
78
widgetBackgroundColor: "#000000",
89
todayTextColor: "#000000",
910
markToday: true,
1011
todayCircleColor: "#FFB800",
1112
showEventCircles: true,
13+
discountAllDayEvents: false,
1214
eventCircleColor: "#1E5C7B",
1315
weekdayTextColor: "#ffffff",
1416
weekendLetters: "#FFB800",
@@ -27,7 +29,7 @@ var settings = {
2729
showPrevMonth: true,
2830
showNextMonth: true,
2931
individualDateTargets: false,
30-
flipped: false,
32+
flipped: params.flipped ? params.flipped : false,
3133
};
3234
var settings_default = settings;
3335

@@ -196,14 +198,20 @@ function buildCalendar(
196198
var buildCalendar_default = buildCalendar;
197199

198200
// src/countEvents.ts
199-
async function countEvents(date, extendToPrev = 0, extendToNext = 0) {
201+
async function countEvents(
202+
date,
203+
extendToPrev = 0,
204+
extendToNext = 0,
205+
settings2
206+
) {
200207
const { firstOfMonth } = getMonthBoundaries_default(date);
201208
const { startDate, endDate } = extendBoundaries(
202209
firstOfMonth,
203210
extendToPrev,
204211
extendToNext
205212
);
206-
const events = await CalendarEvent.between(startDate, endDate);
213+
let events = await CalendarEvent.between(startDate, endDate);
214+
events = trimEvents(events, settings2);
207215
const eventCounts = new Map();
208216
events.forEach((event) => {
209217
if (event.isAllDay) {
@@ -219,6 +227,18 @@ async function countEvents(date, extendToPrev = 0, extendToNext = 0) {
219227
const intensity = calculateIntensity(eventCounts);
220228
return { eventCounts, intensity };
221229
}
230+
function trimEvents(events, settings2) {
231+
let trimmedEvents = events;
232+
if (settings2.calFilter.length) {
233+
trimmedEvents = events.filter((event) =>
234+
settings2.calFilter.includes(event.calendar.title)
235+
);
236+
}
237+
if (settings2.discountAllDayEvents || !settings2.showAllDayEvents) {
238+
trimmedEvents = trimmedEvents.filter((event) => !event.isAllDay);
239+
}
240+
return trimmedEvents;
241+
}
222242
function extendBoundaries(first, extendToPrev, extendToNext) {
223243
const startDate = new Date(
224244
first.getFullYear(),
@@ -368,7 +388,8 @@ async function buildCalendarView(date, stack, settings2) {
368388
const { eventCounts, intensity } = await countEvents_default(
369389
date,
370390
daysFromPrevMonth,
371-
daysFromNextMonth
391+
daysFromNextMonth,
392+
settings2
372393
);
373394
for (let i = 0; i < calendar.length; i += 1) {
374395
const weekdayStack = calendarStack.addStack();
@@ -571,6 +592,11 @@ async function getEvents(date, settings2) {
571592
dateLimit.setDate(dateLimit.getDate() + settings2.nextNumOfDays);
572593
events = await CalendarEvent.between(date, dateLimit);
573594
}
595+
if (settings2.calFilter.length) {
596+
events = events.filter((event) =>
597+
settings2.calFilter.includes(event.calendar.title)
598+
);
599+
}
574600
const futureEvents = [];
575601
for (const event of events) {
576602
if (

src/buildCalendarView.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ async function buildCalendarView(
4747
const { eventCounts, intensity } = await countEvents(
4848
date,
4949
daysFromPrevMonth,
50-
daysFromNextMonth
50+
daysFromNextMonth,
51+
settings
5152
);
5253

5354
for (let i = 0; i < calendar.length; i += 1) {

src/countEvents.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Settings } from "settings";
12
import getMonthBoundaries from "./getMonthBoundaries";
23

34
/**
@@ -8,16 +9,21 @@ import getMonthBoundaries from "./getMonthBoundaries";
89
async function countEvents(
910
date: Date,
1011
extendToPrev = 0,
11-
extendToNext = 0
12+
extendToNext = 0,
13+
settings: Settings
1214
): Promise<EventCountInfo> {
1315
const { firstOfMonth } = getMonthBoundaries(date);
1416
const { startDate, endDate } = extendBoundaries(
1517
firstOfMonth,
1618
extendToPrev,
1719
extendToNext
1820
);
19-
const events = await CalendarEvent.between(startDate, endDate);
21+
let events = await CalendarEvent.between(startDate, endDate);
22+
23+
events = trimEvents(events, settings);
24+
2025
const eventCounts: EventCounts = new Map();
26+
2127
events.forEach((event) => {
2228
if (event.isAllDay) {
2329
const date = event.startDate;
@@ -35,6 +41,26 @@ async function countEvents(
3541
return { eventCounts, intensity };
3642
}
3743

44+
/**
45+
* Remove events that we don't care about from the array, so that they won't
46+
* affect the intensity of the eventCircles
47+
*/
48+
function trimEvents(events: CalendarEvent[], settings: Settings) {
49+
let trimmedEvents = events;
50+
51+
if (settings.calFilter.length) {
52+
trimmedEvents = events.filter((event) =>
53+
settings.calFilter.includes(event.calendar.title)
54+
);
55+
}
56+
57+
if (settings.discountAllDayEvents || !settings.showAllDayEvents) {
58+
trimmedEvents = trimmedEvents.filter((event) => !event.isAllDay);
59+
}
60+
61+
return trimmedEvents;
62+
}
63+
3864
/**
3965
* Find the boundaries between which the events are counted, when showing the
4066
* previous and/or the next month then the boundaries are wider than just the

src/getEvents.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ async function getEvents(
1313
events = await CalendarEvent.between(date, dateLimit);
1414
}
1515

16+
if (settings.calFilter.length) {
17+
events = events.filter((event) =>
18+
settings.calFilter.includes(event.calendar.title)
19+
);
20+
}
21+
1622
const futureEvents: CalendarEvent[] = [];
1723

1824
// if we show events for the whole week, then we need to filter allDay events

src/settings.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const settings: Settings = {
1414
// parameter config would look like this:
1515
// { "bg": "2111.jpg", "view": "events" }
1616
backgroundImage: params.bg ? params.bg : "transparent.jpg",
17+
// what calendars to show, all if empty or something like: ["Work"]
18+
calFilter: params.calFilter ? params.calFilter : [],
1719
widgetBackgroundColor: "#000000",
1820
todayTextColor: "#000000",
1921
markToday: true,
@@ -22,6 +24,8 @@ const settings: Settings = {
2224
// background for all other days, only applicable if showEventCircles is true
2325
// show a circle behind each date that has an event then
2426
showEventCircles: true,
27+
// if true, all-day events don't count towards eventCircle intensity value
28+
discountAllDayEvents: false,
2529
eventCircleColor: "#1E5C7B",
2630
// color of all the other dates
2731
weekdayTextColor: "#ffffff",
@@ -59,19 +63,21 @@ const settings: Settings = {
5963
// tapping on a date opens that specific one
6064
individualDateTargets: false,
6165
// events-calendar OR a flipped calendar-events type of view for medium widget
62-
flipped: false,
66+
flipped: params.flipped ? params.flipped : false,
6367
};
6468

6569
export interface Settings {
6670
debug: boolean;
6771
calendarApp: string;
6872
backgroundImage: string;
73+
calFilter: string[];
6974
widgetBackgroundColor: string;
7075
todayTextColor: string;
7176
markToday: boolean;
7277
todayCircleColor: string;
7378
weekdayTextColor: string;
7479
showEventCircles: boolean;
80+
discountAllDayEvents: boolean;
7581
eventCircleColor: string;
7682
locale: string;
7783
weekendLetters: string;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"target": "es2017",
44
"module": "commonjs", // ts-node breaks if this is something else
55
"baseUrl": "src",
6-
"lib": ["ES6"]
6+
"lib": ["es2017"]
77
}
88
}

0 commit comments

Comments
 (0)