-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSperrbildschirm Kalender.js
More file actions
109 lines (92 loc) · 3.29 KB
/
Sperrbildschirm Kalender.js
File metadata and controls
109 lines (92 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: light-brown; icon-glyph: calendar-alt;
const VKal = importModule("Variables_Kal")
const V = new VKal()
// const TEST_MODE = true
const TEST_MODE = V.TEST_MODE
const CALENDAR_URL = V.CALENDAR_URL
const VISIBLE_CALENDARS = V.VISIBLE_CALENDARS
const NUM_ITEMS_TO_SHOW = 1 // 1 is the max without it being cramped
const NO_ITEMS_MESSAGE = V.NO_ITEMS_MESSAGE
const ITEM_NAME_SIZE = V.ITEM_NAME_SIZE
const ITEM_TIME_SIZE = V.ITEM_TIME_SIZE
const DATE_FORMATTER = new DateFormatter()
const NOW = new Date()
const MIDNIGHT = new Date()
MIDNIGHT.setHours(24,0,0,0)
if (!config.runsInWidget && !TEST_MODE) {
const appleDate = new Date('2001/01/01')
const timestamp = (NOW.getTime() - appleDate.getTime()) / 1000
const callback = new CallbackURL(CALENDAR_URL + timestamp)
callback.open()
Script.complete()
} else {
let itemsToShow = []
const events = await CalendarEvent.today([])
for (const event of events) {
if (event.endDate.getTime() > NOW.getTime() && event.calendar.title != "Ferien" && !event.isAllDay) {
let includesTime = true
itemsToShow.push({
id: event.identifier,
name: event.title,
startDate: event.startDate,
endDate: event.endDate,
dateIncludesTime: includesTime,
})
}
}
itemsToShow = itemsToShow.sort(sortItems).slice(0, NUM_ITEMS_TO_SHOW)
item = itemsToShow[0]
// Lay out the widget!
let widget = new ListWidget()
// If there is at least one item today
if (itemsToShow.length > 0) {
let itemName = widget.addText(item.name)
itemName.font = Font.semiboldSystemFont(ITEM_NAME_SIZE)
widget.addSpacer(5)
let itemDate = widget.addText(formatItemDate(item))
itemDate.font = Font.mediumSystemFont(ITEM_TIME_SIZE)
}
// Finalize widget settings
widget.setPadding(12, 12, 12, 0)
widget.spacing = -3
Script.setWidget(widget)
widget.presentAccessoryRectangular()
Script.complete()
}
// WIDGET TEXT HELPERS
function sortItems(first, second) {
if (first.dateIncludesTime === false && second.dateIncludesTime === false) {
return 0
} else if (first.dateIncludesTime === false) {
return -1
} else if (second.dateIncludesTime === false) {
return 1
} else {
return first.startDate - second.startDate
}
}
function formatItemDate(item) {
if (item.dateIncludesTime === true) {
if (item.startDate <= NOW && item.endDate <= MIDNIGHT){
DATE_FORMATTER.dateFormat = "hh:mma"
let endDate = DATE_FORMATTER.string(item.endDate)
return "▐ Bis " + endDate
} else if (item.startDate <= NOW && item.endDate >= MIDNIGHT) {
return "▐ Jetzt"
} else {
DATE_FORMATTER.dateFormat = "hh:mm"
let startDate = DATE_FORMATTER.string(item.startDate)
DATE_FORMATTER.dateFormat = "hh:mma"
let endDate = DATE_FORMATTER.string(item.endDate)
return "▐ " + startDate + " — " + endDate
}
}
else {
return "▐ Ganztägig"
}
}
function getItemUrl(item) {
return CALENDAR_URL + item.id
}