-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandby Kalender(1).js
More file actions
163 lines (139 loc) · 4.96 KB
/
Standby Kalender(1).js
File metadata and controls
163 lines (139 loc) · 4.96 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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 = V.TEST_MODE
const CALENDAR_URL = V.CALENDAR_URL
const VISIBLE_CALENDARS = V.VISIBLE_CALENDARS
const NUM_ITEMS_TO_SHOW = 3 // 3 is the max without it being cramped
const NO_ITEMS_MESSAGE = V.NO_ITEMS_MESSAGE
const BACKGROUND_COLOR = Color.black()
const DATE_COLOR = V.DATE_COLOR
const ITEM_NAME_COLOR = V.ITEM_NAME_COLOR
const ITEM_TIME_COLOR = V.ITEM_TIME_COLOR
const CALENDAR_COLORS = V.CALENDAR_COLORS
const DATE_SIZE = V.DATE_SIZE
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()
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") {
let includesTime = false
if (!event.isAllDay) {
includesTime = true
}
let title = event.title
if (event.calendar.title == "Geburtstage") {
let index = title.lastIndexOf("(")
let name = title.slice(0, index - 1)
let age = title.slice(index + 1, -1)
if (age == "Geburtstag") {
title = name + " hat Geburtstag"
}
else {
age = age.slice(0, age.indexOf("Geburtstag") - 2)
title = name + " wird " + age
}
}
itemsToShow.push({
id: event.identifier,
name: title,
startDate: event.startDate,
endDate: event.endDate,
dateIncludesTime: includesTime,
calendarTitle: event.calendar.title
})
}
}
itemsToShow = itemsToShow.sort(sortItems).slice(0, NUM_ITEMS_TO_SHOW)
// Lay out the widget!
let widget = new ListWidget()
widget.backgroundColor = BACKGROUND_COLOR
// Add the top date
DATE_FORMATTER.dateFormat = "EEEE d"
let topDate = widget.addText(DATE_FORMATTER.string(NOW))
topDate.textColor = DATE_COLOR
topDate.font = Font.semiboldSystemFont(DATE_SIZE)
// Put all of the event items on the bottom
widget.addSpacer()
// If there is at least one item today
if (itemsToShow.length > 0) {
for (i = 0; i < itemsToShow.length; i++) {
// Add space between events
if (i != 0) {
widget.addSpacer(10)
}
let itemName
itemName = widget.addText(formatItemName(itemsToShow[i]))
itemName.lineLimit = 1
itemName.font = Font.semiboldSystemFont(ITEM_NAME_SIZE)
itemName.textColor = ITEM_NAME_COLOR
widget.addSpacer(5)
let itemDate = widget.addText(formatItemDate(itemsToShow[i]))
itemDate.font = Font.mediumSystemFont(ITEM_TIME_SIZE)
itemDate.textColor = getItemColor(itemsToShow[i])
}
} else {
// No Events found
let message = widget.addText(NO_ITEMS_MESSAGE)
message.textColor = ITEM_NAME_COLOR
message.font = Font.lightSystemFont(ITEM_NAME_SIZE)
}
// Finalize widget settings
widget.setPadding(12, 12, 12, 0)
widget.spacing = -3
Script.setWidget(widget)
widget.presentSmall()
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){
DATE_FORMATTER.dateFormat = "hh:mma"
let endDate = DATE_FORMATTER.string(item.endDate)
return "▐ Bis " + endDate
} 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 getItemColor(item) {
return CALENDAR_COLORS[item.calendarTitle]
}
function formatItemName(item) {
return item.name
}
function getItemUrl(item) {
return CALENDAR_URL + item.id
}